def build_model(self, filters, filter_length, optimizer='rmsprop', loss='mse'): input_ = Input(shape=(self.time_step, self.input_size)) conv_h1 = Conv1D(filters, filter_length, padding='causal', activation='relu')(input_) conv_h2 = Conv1D(filters, filter_length, padding='causal', activation='relu')(conv_h1) conv_h3 = Conv1D(filters, filter_length, padding='causal', activation='relu')(conv_h2) conv_g3 = Conv1D(filters, filter_length, padding='causal', activation='relu')(conv_h3) merge_h2_g3 = add([conv_h2, conv_g3]) conv_g2 = Conv1D(filters, filter_length, padding='causal', activation='relu')(merge_h2_g3) merge_h1_g2 = add([conv_h1, conv_g2]) conv_g1 = Conv1D(filters, filter_length, padding='causal', activation='relu')(merge_h1_g2) conv_g0 = Conv1D(self.output_size, filter_length, padding='causal', activation='relu')(conv_g1) model = Model(input_, conv_g0) model.compile(optimizer, loss) model.summary() return model
def build_read_tensor_2d_model(args): '''Build Read Tensor 2d CNN model for classifying variants. 2d Convolutions followed by dense connection. Dynamically sets input channels based on args via defines.total_input_channels_from_args(args) Uses the functional API. Supports theano or tensorflow channel ordering. Prints out model summary. Arguments args.window_size: Length in base-pairs of sequence centered at the variant to use as input. args.labels: The output labels (e.g. SNP, NOT_SNP, INDEL, NOT_INDEL) args.channels_last: Theano->False or Tensorflow->True channel ordering flag Returns The keras model ''' if args.channels_last: in_shape = (args.read_limit, args.window_size, args.channels_in) else: in_shape = (args.channels_in, args.read_limit, args.window_size) read_tensor = Input(shape=in_shape, name="read_tensor") read_conv_width = 16 x = Conv2D(128, (read_conv_width, 1), padding='valid', activation="relu", kernel_initializer="he_normal")(read_tensor) x = Conv2D(64, (1, read_conv_width), padding='valid', activation="relu", kernel_initializer="he_normal")(x) x = MaxPooling2D((3, 1))(x) x = Conv2D(64, (1, read_conv_width), padding='valid', activation="relu", kernel_initializer="he_normal")(x) x = MaxPooling2D((3, 3))(x) x = Flatten()(x) x = Dense(units=32, kernel_initializer='normal', activation='relu')(x) prob_output = Dense(units=len(args.labels), kernel_initializer='normal', activation='softmax')(x) model = Model(inputs=[read_tensor], outputs=[prob_output]) adamo = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=1.) my_metrics = [metrics.categorical_accuracy] model.compile(loss='categorical_crossentropy', optimizer=adamo, metrics=my_metrics) model.summary() if os.path.exists(args.weights_hd5): model.load_weights(args.weights_hd5, by_name=True) print('Loaded model weights from:', args.weights_hd5) return model
def bidirectional_model(): inputs = Input(shape=(maxlen, ), dtype='int32') x = Embedding(max_features, 128, input_length=maxlen)(inputs) x = Bidirectional(LSTM(64))(x) x = Dropout(0.5)(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=inputs, outputs=x) # try using different optimizers and different optimizer configs model.compile('adam', 'binary_crossentropy', metrics=['accuracy']) return model
def finetune(base_model, n_class, lr = 1e-3): """Refine a given model by removing the last layer and adding a custom new one #Arguments base_model: model to start from n_class: number of classes #Returns Model where only the last layer is trained """ for layer in base_model.layers: layer.trainable = False x = base_model.layers[-2].output x = Dense(n_class, activation = 'softmax')(x) model = Model(inputs = base_model.input, outputs = x) model.compile(optimizer = Adam(lr = lr), loss = 'categorical_crossentropy', metrics = ['accuracy']) return model
def cnn_lstm_model(): ''' ''' print('Build model...') inputs = Input(shape=(maxlen, ), dtype='int32') x = Embedding(max_features, embedding_size, input_length=maxlen)(inputs) x = Dropout(0.25)(x) x = Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)(x) x = MaxPooling1D(pool_size=pool_size)(x) x = LSTM(lstm_output_size)(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=inputs, outputs=x) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def build_model(num_output_classes): conv1size = 32 conv2size = 64 convfiltsize = 4 densesize = 128 poolsize = (2, 2) imgdepth = 3 dropout = 0.3 if IMG_COLORMODE == 'grayscale': imgdepth = 1 inpshape = IMG_TGT_SIZE + (imgdepth, ) inputs = Input(shape=inpshape) conv1 = Convolution2D(conv1size, convfiltsize, strides=(1, 1), padding='valid', activation='relu', name='conv1', data_format='channels_last')(inputs) pool1 = MaxPooling2D(pool_size=poolsize, name='pool1')(conv1) drop1 = Dropout(dropout)(pool1) conv2 = Convolution2D(conv2size, convfiltsize, strides=(1, 1), padding='valid', activation='relu', name='conv2', data_format='channels_last')(drop1) pool2 = MaxPooling2D(pool_size=poolsize, name='pool2')(conv2) drop2 = Dropout(dropout)(pool2) flat2 = Flatten()(drop2) dense = Dense(densesize, name='dense')(flat2) denseact = Activation('relu')(dense) output = Dense(num_output_classes, name='output')(denseact) outputact = Activation('softmax')(output) model = Model(inputs=inputs, outputs=outputact) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def cnn_model_fn(): ''' define the model in function way ''' # input shape is (img_rows, img_cols, fea_channel) inputs = Input(shape=(img_rows, img_cols, 1)) x = Conv2D(32, kernel_size=(3, 3), activation='relu')(inputs) x = Conv2D(64, (3, 3), activation='relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = Dropout(0.25)(x) x = Flatten()(x) x = Dense(128, activation='relu')(x) x = Dropout(0.5)(x) pred = Dense(num_classes, activation='softmax')(x) # small change of Model parameters names, now is inputs, outputs model = Model(inputs=inputs, outputs=pred) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) return model
def cnn_model_fn(): ''' ''' print('Build model...') inputs = Input(shape=(maxlen, ), dtype='int32') # a index sequence with lenght = maxlen x = Embedding(max_features, embedding_dims, input_length=maxlen)(inputs) x = Dropout(0.2)(x) x = Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)(x) x = GlobalMaxPooling1D()(x) x = Dense(hidden_dims)(x) x = Dropout(0.2)(x) x = Activation('relu')(x) x = Dense(1)(x) x = Activation('sigmoid')(x) model = Model(inputs=inputs, outputs=x) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def main(): training_images, training_labels, test_images, test_labels = load_dataset() # plt.imshow(training_images[:,:,0], cmap='gray') # plt.show() perm_train = np.random.permutation(training_labels.size) training_labels = training_labels[perm_train] training_images = (training_images[perm_train, :, :] - 127.5) / 127.5 training_images = np.expand_dims(training_images, -1) print(training_images.shape) test_images = test_images / 255.0 test_images = np.expand_dims(test_images, -1) # pdb.set_trace() # training_labels = to_categorical(training_labels, NUM_CLASSES) # test_labels = to_categorical(test_labels, NUM_CLASSES) BATCH_SIZE = 32 * 8 WIDTH, HEIGHT = 28, 28 adam_lr = 0.0002 adam_beta_1 = 0.5 ##################################### ### Defiining the Discriminator: ##################################### input_D = Input(shape=(HEIGHT, WIDTH, 1), name='input_D') x = Conv2D(filters=32, kernel_size=3, strides=(2, 2), padding='same', name='conv1_D')(input_D) #x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters=32, kernel_size=3, strides=(2, 2), padding='same', name='conv2_D')(x) #x = BatchNormalization()(x) x = Activation('relu')(x) x = Flatten()(x) x = Dense(128, activation='relu', name='dense1_D')(x) output_D = Dense(1, activation='sigmoid', name='output_D')(x) model_D = Model(inputs=input_D, outputs=output_D) model_D.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr, beta1=adam_beta_1), metrics=['accuracy']) ##################################### ### Defiining the Generator: ##################################### LATENT_SIZE = 100 input_G = Input(shape=(LATENT_SIZE, ), name='input_gen') x = Dense(7 * 7 * 32, activation='linear', name='Dense1_G')(input_G) x = BatchNormalization()(x) x = Activation('relu')(x) x = Reshape((7, 7, 32))(x) x = UpSampling2D((2, 2))(x) x = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='same', name='conv1_gen')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='same', name='conv2_gen')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters=1, kernel_size=1, strides=(1, 1), padding='same', name='conv3_gen')(x) img_G = Activation('tanh')(x) model_G = Model(inputs=input_G, outputs=img_G) model_G.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr, beta1=adam_beta_1)) ##################################### ### Defiining the Combined GAN: ##################################### model_D.trainable = False # Since model_D is already compiled, thediscriminator model remains trainble, # but here in the combined model it becomes non-trainable input_main = Input( shape=(LATENT_SIZE, ), name='input_main' ) # Note that this input should be different from the input to Generator combined = Model(inputs=input_main, outputs=model_D(model_G(input_main))) combined.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr, beta1=adam_beta_1), metrics=['accuracy']) print(combined.summary()) ##################################### ### Training: ##################################### bar = InitBar() N = training_images.shape[0] for iter in range(100): fake_input = np.random.randn(1, LATENT_SIZE) fake_image = model_G.predict(fake_input) loss_G, acc_G, loss_D, acc_D = 0, 0, 0, 0 steps = (int)(np.ceil(float(N) / float(BATCH_SIZE))) for batch_iter in range(steps): bar(100.0 * batch_iter / float(steps)) real_image, _ = get_batch(batch_iter, BATCH_SIZE / 2, training_images, training_labels) #################### ## Discriminator Training #################### # Note that if using BN layer in Discriminator, minibatch should contain only real images or fake images. fake_input = np.random.randn(BATCH_SIZE / 2, LATENT_SIZE) fake_image = model_G.predict(fake_input) #real_image = get_real_mbatch(batch_sz=BATCH_SIZE/2, data=training_images) agg_input = np.concatenate((fake_image, real_image), axis=0) agg_output = np.zeros((BATCH_SIZE, )) agg_output[BATCH_SIZE / 2:] = 1 perm = np.random.permutation(BATCH_SIZE) agg_input = agg_input[perm] agg_output = agg_output[perm] #pdb.set_trace() tr = model_D.train_on_batch(x=agg_input, y=agg_output) loss_D += tr[0] acc_D += tr[1] ##################### ## Generator Training ##################### fake_input = np.random.randn(BATCH_SIZE, LATENT_SIZE) fake_label = np.ones(BATCH_SIZE, ) tr = combined.train_on_batch(x=fake_input, y=fake_label) loss_G += tr[0] acc_G += tr[1] print('\nG_loss = {}, G_acc = {}\nD_loss = {}, D_acc = {}'.format( loss_G / float(steps), acc_G / float(steps), loss_D / float(steps), acc_D / float(steps))) for iter in range(10): fake_input = np.random.randn(1, LATENT_SIZE) fake_image = model_G.predict(fake_input) plt.imshow(fake_image[0, :, :, 0]) plt.show()
w_1_3_2.append(init_weights[0][1, 2]) w_1_4_1.append(init_weights[0][0, 3]) w_1_4_2.append(init_weights[0][1, 3]) w_2_1.append(init_weights[2][0, 0]) w_2_2.append(init_weights[2][1, 0]) w_2_3.append(init_weights[2][2, 0]) w_2_4.append(init_weights[2][3, 0]) losses = [] accuracies = [] # use SGD optimizer with learning rate 0.1 sgd = SGD(lr=0.1) # set loss function to be mse, print out accuracy model.compile(loss='mse', optimizer=sgd, metrics=['accuracy']) # set loss function to be binary_crossentropy, print accuracy model1.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) ####################################################### # batch_size = 1, update weights every sample; # batch_size = 100, update weights every 100 sample; # without validation_split, all dataset are trained # with validation_split=0.25, 25% dataset is saved for validation,rest for training; during training, there will be loss and val_loss, accu, and val_accu # shuffle = True, all samples of training set will be shuffled for each epoch training # epochs = 10, train with the entire dataset for 10 times # hist = fit() will record a loss for each epoch ####################################################### # hist1 = model.fit(X, y, batch_size=1, validation_split=0.25, epochs=10) # accuracy 0.75 # hist2 = model.fit(X, y, batch_size=1, epochs=1000) # accuracy 100%, loss keep dropping down to 0.0016 #######################################################
def resnet50_deeplab(): """ Building a resnet50 model fr semantic segmentation :return : returns thekeras implementation of deeplab architecture """ ################################################ ######## Building the model #################### ################################################ input_layer = Input(shape=(None, None, 3), name='input_layer') conv1_1 = Conv2D(filters=64, kernel_size=7, strides=(2, 2), use_bias=False, padding='same', name='conv1')(input_layer) bn1_1 = BatchNormalization(name='bn_conv1')(conv1_1) relu1_1 = Activation('relu')(bn1_1) mxp1_1 = MaxPooling2D(pool_size=3, strides=(2, 2))(relu1_1) conv1_2 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2a_branch1')(mxp1_1) bn1_2 = BatchNormalization(name='bn2a_branch1')(conv1_2) conv2_1 = Conv2D(filters=64, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2a_branch2a')(mxp1_1) bn2_1 = BatchNormalization(name='bn2a_branch2a')(conv2_1) relu2_1 = Activation('relu')(bn2_1) conv2_2 = Conv2D(filters=64, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res2a_branch2b')(relu2_1) bn2_2 = BatchNormalization(name='bn2a_branch2b')(conv2_2) relu2_2 = Activation('relu')(bn2_2) conv2_3 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2a_branch2c')(relu2_2) bn2_3 = BatchNormalization(name='bn2a_branch2c')(conv2_3) merge3 = Add()([bn1_2, bn2_3]) relu3_1 = Activation('relu')(merge3) conv3_1 = Conv2D(filters=64, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2b_branch2a')(relu3_1) bn3_1 = BatchNormalization(name='bn2b_branch2a')(conv3_1) relu3_2 = Activation('relu')(bn3_1) conv3_2 = Conv2D(filters=64, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res2b_branch2b')(relu3_2) bn3_2 = BatchNormalization(name='bn2b_branch2b')(conv3_2) relu3_3 = Activation('relu')(bn3_2) conv3_3 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2b_branch2c')(relu3_3) bn3_3 = BatchNormalization(name='bn2b_branch2c')(conv3_3) merge4 = Add()([relu3_1, bn3_3]) relu4_1 = Activation('relu')(merge4) conv4_1 = Conv2D(filters=64, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2c_branch2a')(relu4_1) bn4_1 = BatchNormalization(name='bn2c_branch2a')(conv4_1) relu4_2 = Activation('relu')(bn4_1) conv4_2 = Conv2D(filters=64, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res2c_branch2b')(relu4_2) bn4_2 = BatchNormalization(name='bn2c_branch2b')(conv4_2) relu4_3 = Activation('relu')(bn4_2) conv4_3 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res2c_branch2c')(relu4_3) bn4_3 = BatchNormalization(name='bn2c_branch2c')(conv4_3) merge5 = Add()([relu4_1, bn4_3]) relu5_1 = Activation('relu')(merge5) conv5_1 = Conv2D(filters=512, kernel_size=1, strides=(2, 2), use_bias=False, padding='same', name='res3a_branch1')(relu5_1) bn5_1 = BatchNormalization(name='bn3a_branch1')(conv5_1) conv6_1 = Conv2D(filters=128, kernel_size=1, strides=(2, 2), use_bias=False, padding='same', name='res3a_branch2a')(relu5_1) bn6_1 = BatchNormalization(name='bn3a_branch2a')(conv6_1) relu6_1 = Activation('relu')(bn6_1) conv6_2 = Conv2D(filters=128, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res3a_branch2b')(relu6_1) bn6_2 = BatchNormalization(name='bn3a_branch2b')(conv6_2) relu6_2 = Activation('relu')(bn6_2) conv6_3 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3a_branch2c')(relu6_2) bn6_3 = BatchNormalization(name='bn3a_branch2c')(conv6_3) merge7 = Add()([bn5_1, bn6_3]) relu7_1 = Activation('relu', name='res3a_relu')(merge7) conv7_1 = Conv2D(filters=128, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3b1_branch2a')(relu7_1) bn7_1 = BatchNormalization(name='bn3b1_branch2a')(conv7_1) relu7_2 = Activation('relu')(bn7_1) conv7_2 = Conv2D(filters=128, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res3b1_branch2b')(relu7_2) bn7_2 = BatchNormalization(name='bn3b1_branch2b')(conv7_2) relu7_3 = Activation('relu')(bn7_2) conv7_3 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3b1_branch2c')(relu7_3) bn7_3 = BatchNormalization(name='bn3b1_branch2c')(conv7_3) merge8 = Add()([relu7_1, bn7_3]) relu8_1 = Activation('relu', name='res3b1_relu')(merge8) conv8_1 = Conv2D(filters=128, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3b2_branch2a')(relu8_1) bn8_1 = BatchNormalization(name='bn3b2_branch2a')(conv8_1) relu8_2 = Activation('relu')(bn8_1) conv8_2 = Conv2D(filters=128, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res3b2_branch2b')(relu8_2) bn8_2 = BatchNormalization(name='bn3b2_branch2b')(conv8_2) relu8_3 = Activation('relu')(bn8_2) conv8_3 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3b2_branch2c')(relu8_3) bn8_3 = BatchNormalization(name='bn3b2_branch2c')(conv8_3) merge9 = Add()([relu8_1, bn8_3]) relu9_1 = Activation('relu', name='res3b2_relu')(merge9) conv9_1 = Conv2D(filters=128, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3b3_branch2a')(relu9_1) bn9_1 = BatchNormalization(name='bn3b3_branch2a')(conv9_1) relu9_2 = Activation('relu')(bn9_1) conv9_2 = Conv2D(filters=128, kernel_size=3, strides=(1, 1), use_bias=False, padding='same', name='res3b3_branch2b')(relu9_2) bn9_2 = BatchNormalization(name='bn3b3_branch2b')(conv9_2) relu9_3 = Activation('relu')(bn9_2) conv9_3 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res3b3_branch2c')(relu9_3) bn9_3 = BatchNormalization(name='bn3b3_branch2c')(conv9_3) merge10 = Add()([relu9_1, bn9_3]) relu10_1 = Activation('relu', name='res3b3_relu')(merge10) conv10_1 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4a_branch1')(relu10_1) bn10_1 = BatchNormalization(name='bn4a_branch1')(conv10_1) conv11_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4a_branch2a')(relu10_1) bn11_1 = BatchNormalization(name='bn4a_branch2a')(conv11_1) relu11_1 = Activation('relu')(bn11_1) at_conv11_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4a_branch2b')(relu11_1) bn11_2 = BatchNormalization(name='bn4a_branch2b')(at_conv11_2) relu11_2 = Activation('relu')(bn11_2) conv11_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4a_branch2c')(relu11_2) bn11_3 = BatchNormalization(name='bn4a_branch2c')(conv11_3) merge12 = Add()([bn10_1, bn11_3]) relu12_1 = Activation('relu', name='res4a_relu')(merge12) conv12_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b1_branch2a')(relu12_1) bn12_1 = BatchNormalization(name='bn4b1_branch2a')(conv12_1) relu12_2 = Activation('relu')(bn12_1) conv12_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b1_branch2b')(relu12_2) bn12_2 = BatchNormalization(name='bn4b1_branch2b')(conv12_2) relu12_3 = Activation('relu')(bn12_2) conv12_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b1_branch2c')(relu12_3) bn12_3 = BatchNormalization(name='bn4b1_branch2c')(conv12_3) merge13 = Add()([relu12_1, bn12_3]) relu13_1 = Activation('relu', name='res4b1_relu')(merge13) conv13_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b2_branch2a')(relu13_1) bn13_1 = BatchNormalization(name='bn4b2_branch2a')(conv13_1) relu13_2 = Activation('relu')(bn13_1) conv13_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b2_branch2b')(relu13_2) bn13_2 = BatchNormalization(name='bn4b2_branch2b')(conv13_2) relu13_3 = Activation('relu')(bn13_2) conv13_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b2_branch2c')(relu13_3) bn13_3 = BatchNormalization(name='bn4b2_branch2c')(conv13_3) merge14 = Add()([relu13_1, bn13_3]) relu14_1 = Activation('relu', name='res4b2_relu')(merge14) conv14_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b3_branch2a')(relu14_1) bn14_1 = BatchNormalization(name='bn4b3_branch2a')(conv14_1) relu14_2 = Activation('relu')(bn14_1) conv14_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b3_branch2b')(relu14_2) bn14_2 = BatchNormalization(name='bn4b3_branch2b')(conv14_2) relu14_3 = Activation('relu')(bn14_2) conv14_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b3_branch2c')(relu14_3) bn14_3 = BatchNormalization(name='bn4b3_branch2c')(conv14_3) merge15 = Add()([relu14_1, bn14_3]) relu15_1 = Activation('relu', name='res4b3_relu')(merge15) conv15_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b4_branch2a')(relu15_1) bn15_1 = BatchNormalization(name='bn4b4_branch2a')(conv15_1) relu15_2 = Activation('relu')(bn15_1) conv15_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b4_branch2b')(relu15_2) bn15_2 = BatchNormalization(name='bn4b4_branch2b')(conv15_2) relu15_3 = Activation('relu')(bn15_2) conv15_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b4_branch2c')(relu15_3) bn15_3 = BatchNormalization(name='bn4b4_branch2c')(conv15_3) merge16 = Add()([relu15_1, bn15_3]) relu16_1 = Activation('relu', name='res4b4_relu')(merge16) conv16_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b5_branch2a')(relu16_1) bn16_1 = BatchNormalization(name='bn4b5_branch2a')(conv16_1) relu16_2 = Activation('relu')(bn16_1) conv16_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b5_branch2b')(relu16_2) bn16_2 = BatchNormalization(name='bn4b5_branch2b')(conv16_2) relu16_3 = Activation('relu')(bn16_2) conv16_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b5_branch2c')(relu16_3) bn16_3 = BatchNormalization(name='bn4b5_branch2c')(conv16_3) merge17 = Add()([relu16_1, bn16_3]) relu17_1 = Activation('relu', name='res4b5_relu')(merge17) conv17_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b6_branch2a')(relu17_1) bn17_1 = BatchNormalization(name='bn4b6_branch2a')(conv17_1) relu17_2 = Activation('relu')(bn17_1) conv17_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b6_branch2b')(relu17_2) bn17_2 = BatchNormalization(name='bn4b6_branch2b')(conv17_2) relu17_3 = Activation('relu')(bn17_2) conv17_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b6_branch2c')(relu17_3) bn17_3 = BatchNormalization(name='bn4b6_branch2c')(conv17_3) merge18 = Add()([relu17_1, bn17_3]) relu18_1 = Activation('relu', name='res4b6_relu')(merge18) conv18_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b7_branch2a')(relu18_1) bn18_1 = BatchNormalization(name='bn4b7_branch2a')(conv18_1) relu18_2 = Activation('relu')(bn18_1) conv18_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b7_branch2b')(relu18_2) bn18_2 = BatchNormalization(name='bn4b7_branch2b')(conv18_2) relu18_3 = Activation('relu')(bn18_2) conv18_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b7_branch2c')(relu18_3) bn18_3 = BatchNormalization(name='bn4b7_branch2c')(conv18_3) merge19 = Add()([relu18_1, bn18_3]) relu19_1 = Activation('relu', name='res4b7_relu')(merge19) conv19_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b8_branch2a')(relu19_1) bn19_1 = BatchNormalization(name='bn4b8_branch2a')(conv19_1) relu19_2 = Activation('relu')(bn19_1) conv19_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b8_branch2b')(relu19_2) bn19_2 = BatchNormalization(name='bn4b8_branch2b')(conv19_2) relu19_3 = Activation('relu')(bn19_2) conv19_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b8_branch2c')(relu19_3) bn19_3 = BatchNormalization(name='bn4b8_branch2c')(conv19_3) merge20 = Add()([relu19_1, bn19_3]) relu20_1 = Activation('relu', name='res4b8_relu')(merge20) conv20_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b9_branch2a')(relu20_1) bn20_1 = BatchNormalization(name='bn4b9_branch2a')(conv20_1) relu20_2 = Activation('relu')(bn20_1) conv20_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b9_branch2b')(relu20_2) bn20_2 = BatchNormalization(name='bn4b9_branch2b')(conv20_2) relu20_3 = Activation('relu')(bn20_2) conv20_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b9_branch2c')(relu20_3) bn20_3 = BatchNormalization(name='bn4b9_branch2c')(conv20_3) merge21 = Add()([relu20_1, bn20_3]) relu21_1 = Activation('relu', name='res4b9_relu')(merge21) conv21_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b10_branch2a')(relu21_1) bn21_1 = BatchNormalization(name='bn4b10_branch2a')(conv21_1) relu21_2 = Activation('relu')(bn21_1) conv21_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b10_branch2b')(relu21_2) bn21_2 = BatchNormalization(name='bn4b10_branch2b')(conv21_2) relu21_3 = Activation('relu')(bn21_2) conv21_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b10_branch2c')(relu21_3) bn21_3 = BatchNormalization(name='bn4b10_branch2c')(conv21_3) merge22 = Add()([relu21_1, bn21_3]) relu22_1 = Activation('relu', name='res4b10_relu')(merge22) conv22_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b11_branch2a')(relu22_1) bn22_1 = BatchNormalization(name='bn4b11_branch2a')(conv22_1) relu22_2 = Activation('relu')(bn22_1) conv22_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b11_branch2b')(relu22_2) bn22_2 = BatchNormalization(name='bn4b11_branch2b')(conv22_2) relu22_3 = Activation('relu')(bn22_2) conv22_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b11_branch2c')(relu22_3) bn22_3 = BatchNormalization(name='bn4b11_branch2c')(conv22_3) merge23 = Add()([relu22_1, bn22_3]) relu23_1 = Activation('relu', name='res4b11_relu')(merge23) conv23_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b12_branch2a')(relu23_1) bn23_1 = BatchNormalization(name='bn4b12_branch2a')(conv23_1) relu23_2 = Activation('relu')(bn23_1) conv23_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b12_branch2b')(relu23_2) bn23_2 = BatchNormalization(name='bn4b12_branch2b')(conv23_2) relu23_3 = Activation('relu')(bn23_2) conv23_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b12_branch2c')(relu23_3) bn23_3 = BatchNormalization(name='bn4b12_branch2c')(conv23_3) merge24 = Add()([relu23_1, bn23_3]) relu24_1 = Activation('relu', name='res4b12_relu')(merge24) conv24_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b13_branch2a')(relu24_1) bn24_1 = BatchNormalization(name='bn4b13_branch2a')(conv24_1) relu24_2 = Activation('relu')(bn24_1) conv24_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b13_branch2b')(relu24_2) bn24_2 = BatchNormalization(name='bn4b13_branch2b')(conv24_2) relu24_3 = Activation('relu')(bn24_2) conv24_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b13_branch2c')(relu24_3) bn24_3 = BatchNormalization(name='bn4b13_branch2c')(conv24_3) merge25 = Add()([relu24_1, bn24_3]) relu25_1 = Activation('relu', name='res4b13_relu')(merge25) conv25_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b14_branch2a')(relu25_1) bn25_1 = BatchNormalization(name='bn4b14_branch2a')(conv25_1) relu25_2 = Activation('relu')(bn25_1) conv25_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b14_branch2b')(relu25_2) bn25_2 = BatchNormalization(name='bn4b14_branch2b')(conv25_2) relu25_3 = Activation('relu')(bn25_2) conv25_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b14_branch2c')(relu25_3) bn25_3 = BatchNormalization(name='bn4b14_branch2c')(conv25_3) merge26 = Add()([relu25_1, bn25_3]) relu26_1 = Activation('relu', name='res4b14_relu')(merge26) conv26_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b15_branch2a')(relu26_1) bn26_1 = BatchNormalization(name='bn4b15_branch2a')(conv26_1) relu26_2 = Activation('relu')(bn26_1) conv26_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b15_branch2b')(relu26_2) bn26_2 = BatchNormalization(name='bn4b15_branch2b')(conv26_2) relu26_3 = Activation('relu')(bn26_2) conv26_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b15_branch2c')(relu26_3) bn26_3 = BatchNormalization(name='bn4b15_branch2c')(conv26_3) merge27 = Add()([relu26_1, bn26_3]) relu27_1 = Activation('relu', name='res4b15_relu')(merge27) conv27_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b16_branch2a')(relu27_1) bn27_1 = BatchNormalization(name='bn4b16_branch2a')(conv27_1) relu27_2 = Activation('relu')(bn27_1) conv27_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b16_branch2b')(relu27_2) bn27_2 = BatchNormalization(name='bn4b16_branch2b')(conv27_2) relu27_3 = Activation('relu')(bn27_2) conv27_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b16_branch2c')(relu27_3) bn27_3 = BatchNormalization(name='bn4b16_branch2c')(conv27_3) merge28 = Add()([relu27_1, bn27_3]) relu28_1 = Activation('relu', name='res4b16_relu')(merge28) conv28_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b17_branch2a')(relu28_1) bn28_1 = BatchNormalization(name='bn4b17_branch2a')(conv28_1) relu28_2 = Activation('relu')(bn28_1) conv28_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b17_branch2b')(relu28_2) bn28_2 = BatchNormalization(name='bn4b17_branch2b')(conv28_2) relu28_3 = Activation('relu')(bn28_2) conv28_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b17_branch2c')(relu28_3) bn28_3 = BatchNormalization(name='bn4b17_branch2c')(conv28_3) merge29 = Add()([relu28_1, bn28_3]) relu29_1 = Activation('relu', name='res4b17_relu')(merge29) conv29_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b18_branch2a')(relu29_1) bn29_1 = BatchNormalization(name='bn4b18_branch2a')(conv29_1) relu29_2 = Activation('relu')(bn29_1) conv29_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b18_branch2b')(relu29_2) bn29_2 = BatchNormalization(name='bn4b18_branch2b')(conv29_2) relu29_3 = Activation('relu')(bn29_2) conv29_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b18_branch2c')(relu29_3) bn29_3 = BatchNormalization(name='bn4b18_branch2c')(conv29_3) merge30 = Add()([relu29_1, bn29_3]) relu30_1 = Activation('relu', name='res4b18_relu')(merge30) conv30_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b19_branch2a')(relu30_1) bn30_1 = BatchNormalization(name='bn4b19_branch2a')(conv30_1) relu30_2 = Activation('relu')(bn30_1) conv30_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b19_branch2b')(relu30_2) bn30_2 = BatchNormalization(name='bn4b19_branch2b')(conv30_2) relu30_3 = Activation('relu')(bn30_2) conv30_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b19_branch2c')(relu30_3) bn30_3 = BatchNormalization(name='bn4b19_branch2c')(conv30_3) merge31 = Add()([relu30_1, bn30_3]) relu31_1 = Activation('relu', name='res4b19_relu')(merge31) conv31_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b20_branch2a')(relu31_1) bn31_1 = BatchNormalization(name='bn4b20_branch2a')(conv31_1) relu31_2 = Activation('relu')(bn31_1) conv31_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b20_branch2b')(relu31_2) bn31_2 = BatchNormalization(name='bn4b20_branch2b')(conv31_2) relu31_3 = Activation('relu')(bn31_2) conv31_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b20_branch2c')(relu31_3) bn31_3 = BatchNormalization(name='bn4b20_branch2c')(conv31_3) merge32 = Add()([relu31_1, bn31_3]) relu32_1 = Activation('relu', name='res4b20_relu')(merge32) conv32_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b21_branch2a')(relu32_1) bn32_1 = BatchNormalization(name='bn4b21_branch2a')(conv32_1) relu32_2 = Activation('relu')(bn32_1) conv32_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b21_branch2b')(relu32_2) bn32_2 = BatchNormalization(name='bn4b21_branch2b')(conv32_2) relu32_3 = Activation('relu')(bn32_2) conv32_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b21_branch2c')(relu32_3) bn32_3 = BatchNormalization(name='bn4b21_branch2c')(conv32_3) merge33 = Add()([relu32_1, bn32_3]) relu33_1 = Activation('relu', name='res4b21_relu')(merge33) conv33_1 = Conv2D(filters=256, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b22_branch2a')(relu33_1) bn33_1 = BatchNormalization(name='bn4b22_branch2a')(conv33_1) relu33_2 = Activation('relu')(bn33_1) conv33_2 = Conv2D(filters=256, kernel_size=3, dilation_rate=(2, 2), use_bias=False, padding='same', name='res4b22_branch2b')(relu33_2) bn33_2 = BatchNormalization(name='bn4b22_branch2b')(conv33_2) relu33_3 = Activation('relu')(bn33_2) conv33_3 = Conv2D(filters=1024, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res4b22_branch2c')(relu33_3) bn33_3 = BatchNormalization(name='bn4b22_branch2c')(conv33_3) merge34 = Add()([relu33_1, bn33_3]) relu34_1 = Activation('relu', name='res4b22_relu')(merge34) conv34_1 = Conv2D(filters=2048, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5a_branch1')(relu34_1) bn34_1 = BatchNormalization(name='bn5a_branch1')(conv34_1) conv35_1 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5a_branch2a')(relu34_1) bn35_1 = BatchNormalization(name='bn5a_branch2a')(conv35_1) relu35_2 = Activation('relu')(bn35_1) conv35_2 = Conv2D(filters=512, kernel_size=3, dilation_rate=(4, 4), use_bias=False, padding='same', name='res5a_branch2b')(relu35_2) bn35_2 = BatchNormalization(name='bn5a_branch2b')(conv35_2) relu35_3 = Activation('relu')(bn35_2) conv35_3 = Conv2D(filters=2048, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5a_branch2c')(relu35_3) bn35_3 = BatchNormalization(name='bn5a_branch2c')(conv35_3) merge36 = Add()([bn34_1, bn35_3]) relu36_1 = Activation('relu', name='res5a_relu')(merge36) conv36_1 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5b_branch2a')(relu36_1) bn36_1 = BatchNormalization(name='bn5b_branch2a')(conv36_1) relu36_2 = Activation('relu')(bn36_1) conv36_2 = Conv2D(filters=512, kernel_size=3, dilation_rate=(4, 4), use_bias=False, padding='same', name='res5b_branch2b')(relu36_2) bn36_2 = BatchNormalization(name='bn5b_branch2b')(conv36_2) relu36_3 = Activation('relu')(bn36_2) conv36_3 = Conv2D(filters=2048, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5b_branch2c')(relu36_3) bn36_3 = BatchNormalization(name='bn5b_branch2c')(conv36_3) merge37 = Add()([relu36_1, bn36_3]) relu37_1 = Activation('relu', name='res5b_relu')(merge37) conv37_1 = Conv2D(filters=512, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5c_branch2a')(relu37_1) bn37_1 = BatchNormalization(name='bn5c_branch2a')(conv37_1) relu37_2 = Activation('relu')(bn37_1) conv37_2 = Conv2D(filters=512, kernel_size=3, dilation_rate=(4, 4), use_bias=False, padding='same', name='res5c_branch2b')(relu37_2) bn37_2 = BatchNormalization(name='bn5c_branch2b')(conv37_2) relu37_3 = Activation('relu')(bn37_2) conv37_3 = Conv2D(filters=2048, kernel_size=1, strides=(1, 1), use_bias=False, padding='same', name='res5c_branch2c')(relu37_3) bn37_3 = BatchNormalization(name='bn5c_branch2c')(conv37_3) merge38 = Add()([relu37_1, bn37_3]) relu38_1 = Activation('relu', name='res5c_relu')(merge38) conv38_1 = Conv2D(filters=NUM_CLASSES, kernel_size=3, dilation_rate=(6, 6), padding='same', name='fc1_voc12_c0')(relu38_1) conv38_2 = Conv2D(filters=NUM_CLASSES, kernel_size=3, dilation_rate=(12, 12), padding='same', name='fc1_voc12_c1')(relu38_1) conv38_3 = Conv2D(filters=NUM_CLASSES, kernel_size=3, dilation_rate=(18, 18), padding='same', name='fc1_voc12_c2')(relu38_1) conv38_4 = Conv2D(filters=NUM_CLASSES, kernel_size=3, dilation_rate=(24, 24), padding='same', name='fc1_voc12_c3')(relu38_1) output = Add(name='fc1_voc12')([conv38_1, conv38_2, conv38_3, conv38_4]) output = Lambda(lambda image: tf.image.resize_images(image, (H, W)))( output) #output = UpSampling2D((3,3))(output) #output = MaxPooling2D(pool_size=(2,2), strides=(2,2))(output) #output = Activation('softmax')(output) model = Model(inputs=input_layer, outputs=output) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) return model
def create_model(input_shape, k=1, lr=0.001): inputs = Input(shape=input_shape) i = 0 nb_filter = [16, 32, 64, 128, 256, 512, 256, 128, 64, 32, 16] #0 x = Conv2D(nb_filter[i] * k, (3, 3), padding='same', use_bias=False)(inputs) x = BN(x) x = Activation('relu')(x) x = Conv2D(nb_filter[i] * k, (3, 3), padding='same', use_bias=False)(x) x0 = BN(x) x = Activation('relu')(x0) i += 1 #1 x = dw_conv(x0, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x1 = res_block(x, k, nb_filter[i]) i += 1 #2 x = dw_conv(x1, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x2 = res_block(x, k, nb_filter[i]) i += 1 #3 x = dw_conv(x2, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x3 = res_block(x, k, nb_filter[i]) i += 1 #4 x = dw_conv(x3, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x4 = res_block(x, k, nb_filter[i]) i += 1 #--------------- center ------------ x = dw_conv(x4, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x = res_block(x, k, nb_filter[i]) #--------------- center ------------ i += 1 #4 x = up_conv(x, x4, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x = res_block(x, k, nb_filter[i]) i += 1 #3 x = up_conv(x, x3, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x = res_block(x, k, nb_filter[i]) i += 1 #2 x = up_conv(x, x2, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x = res_block(x, k, nb_filter[i]) i += 1 #1 x = up_conv(x, x1, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x = res_block(x, k, nb_filter[i]) i += 1 #0 x = up_conv(x, x0, nb_filter[i], k) x = res_block(x, k, nb_filter[i]) x = res_block(x, k, nb_filter[i]) x = Activation('relu')(x) classify = Conv2D(1, (1, 1), activation='sigmoid')(x) model = Model(inputs=inputs, outputs=classify) model.compile(optimizer=RMSprop(lr=lr), loss=bce_dice_loss, metrics=[dice_loss]) return model
x = MaxPooling1D(5)(x) # (?, 39, 128), 39 == 195/5 # maxp_t2 = x x = Conv1D(128, 5, activation='relu')(x) # (?, 35, 128) # conv1d_t3 = x x = MaxPooling1D(35)(x) # (?, 1, 128) # maxp_t3 = x x = Flatten()(x) # (?, ?) not (?, 128) ?? # flatten_t = x x = Dense(128, activation='relu')(x) # (?, 128) # dense_t = x preds = Dense(len(labels_index), activation='softmax')(x) # (?, 20) model = Model(sequence_input, preds) # a number of samples as input, preds as output tensors model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) model.summary( ) # see layer output tensor shape and total params of each layer # model.layers[2].count_params() # calculate each layer's params # pp model.trainable_weights # display weights shape of each trainable layer """ Important Tips: Contemplating the following two tips deeply and patiently !!!!!!!!!! Compare model.summary() and pp model.trainable_weights, we can see that how Conv1D weights or filters are used to screen embedding_1 tensor In fact, for all layers, either weights of Conv1D, Dense, or that of Conv2D, consider them to be filters, to screen previous layer's tensor How embedding weights transform input_1 (?, 1000) to embedding_1 (?, 1000, 100)? deserve research later
x = Dropout(0.3)(x) x = Conv2D(64, (8, 1), padding='same', use_bias=False)(inputs) x = Conv2D(64, (8, 1), padding='same', use_bias=False, activation='relu')(x) x = BatchNormalization(axis=1)(x) x = Dropout(0.3)(x) x = Conv2D(64, (1, 5), padding='valid', use_bias=False, activation='relu')(x) x = Reshape((128, 64))(x) x = Bidirectional(LSTM(20, dropout=0.2, return_sequences=False))(x) #x = Flatten()(x) x = Dense(256, use_bias=False, activation='relu')(x) x = BatchNormalization(axis=-1)(x) x = Dropout(0.5)(x) logits = Dense(2, use_bias=False)(x) model = Model(inputs=inputs, outputs=logits) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.summary() history = model.fit(train, train_label, validation_split=2 / 7, batch_size=batch_size, epochs=epochs, shuffle=True)
try: dprint('loading weights from {}'.format(C.base_net_weights)) model_rpn.load_weights(C.base_net_weights, by_name=True) model_classifier.load_weights(C.base_net_weights, by_name=True) except: dprint( 'Could not load pretrained model weights. Weights can be found at {} and {}' .format( 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels_notop.h5', 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' )) optimizer = Adam(lr=1e-5) optimizer_classifier = Adam(lr=1e-5) model_rpn.compile( optimizer=optimizer, loss=[losses.rpn_loss_cls(num_anchors), losses.rpn_loss_regr(num_anchors)]) model_classifier.compile( optimizer=optimizer_classifier, loss=[ losses.class_loss_cls, losses.class_loss_regr(len(classes_count) - 1) ], metrics={'dense_class_{}'.format(len(classes_count)): 'accuracy'}) model_all.compile(optimizer='sgd', loss='mae') epoch_length = 1000 num_epochs = int(options.num_epochs) iter_num = 0 epoch_num = 0
from tensorflow.contrib.keras.python.keras.layers import Dropout, BatchNormalization, Input, Dense from tensorflow.contrib.keras.python.keras.models import Model import numpy as np from tensorflow.contrib.keras.python.keras import backend as K from tensorflow.contrib.keras.python.keras import losses x = np.random.random((10, 10)) * 2 y = np.random.randint(2, size=(10, 1)) input_tensor = Input(shape=(10, )) bn_tensor = BatchNormalization()(input_tensor) dp_tensor = Dropout(0.7)(bn_tensor) final_tensor = Dense(1)(dp_tensor) model = Model(input_tensor, final_tensor) model.compile(optimizer='SGD', loss='mse') # x, y won't get into batches for training here loss_on_batch = model.train_on_batch(x, y) # dive in for details """ ('Runs a single gradient update on a single batch of data.\n' '\n' 'Arguments:\n' ' x: Numpy array of training data,\n' ' or list of Numpy arrays if the model has multiple inputs.\n' ' If all inputs in the model are named,\n' ' you can also pass a dictionary\n' ' mapping input names to Numpy arrays.\n' ' y: Numpy array of target data,\n' ' or list of Numpy arrays if the model has multiple outputs.\n' ' If all outputs in the model are named,\n'
from tensorflow.contrib.keras.python.keras.layers import Dropout, BatchNormalization, Input, Dense from tensorflow.contrib.keras.python.keras.models import Model import numpy as np from tensorflow.contrib.keras.python.keras import backend as K from tensorflow.contrib.keras.python.keras import losses x = np.random.random((100,10))*2 y = np.random.randint(2, size=(100,1)) input_tensor = Input(shape=(10,)) bn_tensor = BatchNormalization()(input_tensor) dp_tensor = Dropout(0.7)(bn_tensor) final_tensor = Dense(1)(dp_tensor) model = Model(input_tensor, final_tensor) model.compile(optimizer='SGD', loss='mse', metrics=['accuracy']) from tensorflow.contrib.keras.python.keras import callbacks from tensorflow.contrib.keras.python.keras.callbacks import EarlyStopping early_stopping = EarlyStopping(monitor='val_loss', patience=5) # patience=2 hist1=model.fit(x, y, validation_split=0.2, callbacks=[early_stopping], epochs=100) print(hist1.history) hist2=model.fit(x, y, validation_split=0.3, epochs=10) # checkout all the losses and metrics print(hist2.history)
x = Dense(64, activation='relu')(x) # And finally we add the main logistic regression layer main_output = Dense(1, activation='sigmoid', name='main_output')(x) # create main_output_array for training later main_output_array = np.random.random((1000, 1)) # This defines a model with two inputs and two outputs: model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output]) # We compile the model and assign a weight of 0.2 to the auxiliary loss. # To specify different `loss_weights` or `loss` for each different output, you can use a list or a dictionary. # Here we pass a single loss as the `loss` argument, so the same loss will be used on all outputs. model.compile(optimizer='rmsprop', loss='binary_crossentropy', loss_weights=[1., 0.2]) # We can train the model by passing it lists of input arrays and target arrays: model.fit([main_input_array, auxiliary_input_array], [main_output_array, auxiliary_output_array], validation_split=0.2, epochs=2, batch_size=32) # Since our inputs and outputs are named (we passed them a "name" argument), # We could also have compiled the model via: model.compile(optimizer='rmsprop', loss={ 'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'
classifier = nn.classifier(feature_map_input, roi_input, C.num_rois, nb_classes=len(class_mapping), trainable=True) model_rpn = Model(img_input, rpn_layers) model_classifier_only = Model([feature_map_input, roi_input], classifier) model_classifier = Model([feature_map_input, roi_input], classifier) model_rpn.load_weights(C.model_path, by_name=True) model_classifier.load_weights(C.model_path, by_name=True) model_rpn.compile(optimizer='sgd', loss='mse') model_classifier.compile(optimizer='sgd', loss='mse') all_imgs = [] classes = {} bbox_threshold = 0.8 visualise = True for idx, img_name in enumerate(sorted(os.listdir(img_path))): if not img_name.lower().endswith( ('.bmp', '.jpeg', '.jpg', '.png', '.tif', '.tiff')): continue print(img_name)
# flowers_dataset = create_image_dataset(FLAGS.image_dir, training_percentage=FLAGS.training_percentage, # testing_percentage=FLAGS.testing_percentage) image_data_generator = ImageDataGenerator() train_data = image_data_generator.flow_from_directory( '/home/meizu/WORK/code/YF_baidu_ML/dataset/flowers/flower_photos/train', target_size=(FLAGS.input_image_size, FLAGS.input_image_size), batch_size=FLAGS.batch_size, class_mode='categorical') test_data = image_data_generator.flow_from_directory( '/home/meizu/WORK/code/YF_baidu_ML/dataset/flowers/flower_photos/test', target_size=(FLAGS.input_image_size, FLAGS.input_image_size), batch_size=FLAGS.batch_size, class_mode='categorical') # compile the model (should be done *after* setting layers to non-trainable) model.compile(optimizer='rmsprop', loss='categorical_crossentropy') train_data_count = len(train_data.filenames) test_data_count = len(test_data.filenames) model.fit_generator(train_data, steps_per_epoch=(train_data_count // FLAGS.batch_size + 1), epochs=1, verbose=1, validation_data=test_data, validation_steps=(test_data_count // FLAGS.batch_size + 1)) # a = model_inception_v3.predict_generator(g, len(g.filenames)) # print '' # a = model_inception_v3.predict_generator(g, steps=len(g.filenames)) # # # extract features
# create a flattened placeholder with conv2d tensor is a must, if to link to a dense layer # no additional args are needed for Flatten layer flattened = layers.Flatten(name='flatten')(conv2d_tensor) # create a dense placeholder with flatten tensor dense_2_cls = layers.Dense(2, activation='softmax', name='dense_2_cls')(flattened) # create a simple model start from input layer, a conv2d layer, flatten layer, to dense layer model = Model(input_tensor, dense_2_cls, name='con2d_simdl') # see the model model.summary() # compile the model lr = 0.001 model.compile(optimizer=Adam(lr=lr), loss='categorical_crossentropy', metrics=['accuracy']) """ def compile(self,\n', ' optimizer,\n', ' loss,\n', ' metrics=None,\n', ' loss_weights=None,\n', ' sample_weight_mode=None,\n', ' **kwargs):\n', ' Configures the model for training.\n', '\n', ' Arguments:\n', ' optimizer: str (name of optimizer) or optimizer object.\n', ' See [optimizers](/optimizers).\n', ' loss: str (name of objective function) or objective function.\n',
padding='same', kernel_initializer=init_ops.glorot_normal_initializer(), name='Convolution_1')(H) H = BatchNormalization()(H) H = Activation('relu')(H) H = Conv2D(nch / 4, kernel_size=(3, 3), padding='same', kernel_initializer=init_ops.glorot_normal_initializer(), name='Convolution_2')(H) H = BatchNormalization()(H) H = Activation('relu')(H) H = Conv2D(1, 1, 1, padding='same', kernel_initializer='random_normal')(H) g_V = Activation('sigmoid')(H) generator = Model(inputs=g_input, outputs=g_V) generator.compile(loss='binary_crossentropy', optimizer=opt) generator.summary() # Build Discriminative model ... d_input = Input(shape=shp) H = Convolution2D(256, kernel_size=(5, 5), strides=(2, 2), padding='same', activation='relu')(d_input) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Convolution2D(512, kernel_size=(5, 5), strides=(2, 2), padding='same',
``` Additionally, you can set the `trainable` property of a layer to `True` or `False` after instantiation. For this to take effect, you will need to call `compile()` on your model after modifying the `trainable` property. Here's an example: """ from tensorflow.contrib.keras.python.keras.layers import Input, Dense from tensorflow.contrib.keras.python.keras.models import Model import numpy as np input_tensor = Input(shape=(32, )) layer = Dense(5) layer.trainable = False tensor_no_train = layer(input_tensor) frozen_model = Model(input_tensor, tensor_no_train) # in the model below, the weights of `layer` will not be updated during training frozen_model.compile(optimizer='rmsprop', loss='mse') layer.trainable = True tensor_do_train = layer(input_tensor) trainable_model = Model(input_tensor, tensor_do_train) # with this model the weights of the layer will be updated during training # (which will also affect the above model since it uses the same layer instance) trainable_model.compile(optimizer='rmsprop', loss='mse') # create dataset data = np.random.random((100, 32)) # check on source labels = np.random.randint(5, size=(100, 5)) # check source frozen_model.fit(data, labels, epochs=5) # no validation_set, then no val_loss frozen_model.fit( data, labels, validation_split=0.3, epochs=5
def main(): training_images, training_labels, test_images, test_labels = load_dataset() # plt.imshow(training_images[:,:,0], cmap='gray') # plt.show() perm_train = np.random.permutation(training_labels.size) training_labels = training_labels[perm_train] training_images = training_images[perm_train, :, :] / 255.0 training_images = np.expand_dims(training_images, -1) print(training_images.shape) test_images = test_images / 255.0 test_images = np.expand_dims(test_images, -1) # pdb.set_trace() # training_labels = to_categorical(training_labels, NUM_CLASSES) # test_labels = to_categorical(test_labels, NUM_CLASSES) BATCH_SIZE = 32 * 8 WIDTH, HEIGHT = 28, 28 # Defiining the network input_layer = Input(shape=(HEIGHT, WIDTH, 1), name='input_layer') cnn1 = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='same', activation='relu')(input_layer) maxpool = MaxPooling2D(pool_size=2)(cnn1) cnn2 = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='valid', activation='relu')(maxpool) maxpool = MaxPooling2D(pool_size=2)(cnn2) flat = Flatten()(maxpool) dense1 = Dense(units=128, activation='relu')(flat) dropout = Dropout(.5)(dense1) output_layer = Dense(units=NUM_CLASSES, activation='softmax')(dropout) model = Model(inputs=input_layer, outputs=output_layer) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) # pdb.set_trace() print(model.summary()) model.fit(x=training_images, y=training_labels, batch_size=BATCH_SIZE, epochs=30, verbose=1, validation_data=(test_images, test_labels)) accuracy = model.evaluate(x=test_images, y=test_labels, batch_size=BATCH_SIZE) print('test score = {}'.format(accuracy))
def main(): training_images, training_labels, test_images, test_labels = load_dataset() # plt.imshow(training_images[:,:,0], cmap='gray') # plt.show() N = training_labels.size Nt = test_labels.size perm_train = np.random.permutation(N) training_labels = training_labels[perm_train] training_images = training_images[perm_train, :, :] / 255.0 training_images = np.expand_dims(training_images, -1) print(training_images.shape) test_images = test_images / 255.0 test_images = np.expand_dims(test_images, -1) # pdb.set_trace() training_labels = to_categorical(training_labels, NUM_CLASSES) test_labels = to_categorical(test_labels, NUM_CLASSES) BATCH_SIZE = 32 * 8 WIDTH, HEIGHT = 28, 28 epochs = 30 # Defiining the placeholders input_data = tf.placeholder(dtype=tf.float32, shape=[None, HEIGHT, WIDTH, 1], name='data') input_labels = tf.placeholder(dtype=tf.float32, shape=[None, NUM_CLASSES], name='labels') do_rate = tf.placeholder(dtype=tf.float32, name='dropout_rate') # pdb.set_trace() with tf.name_scope('conv1'): with tf.variable_scope('conv1'): W_conv1 = tf.get_variable('w', [3, 3, 1, 32]) b_conv1 = tf.get_variable('b', [32]) conv1 = tf.nn.conv2d(input=input_data, filter=W_conv1, strides=[1, 1, 1, 1], padding='SAME') relu1 = tf.nn.relu(conv1 + b_conv1) with tf.name_scope('pool1'): pool1 = tf.nn.max_pool(value=relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') with tf.name_scope('conv2'): with tf.variable_scope('conv2'): W_conv2 = tf.get_variable('w', [3, 3, 32, 32]) b_conv2 = tf.get_variable('b', [32]) conv2 = tf.nn.conv2d(input=pool1, filter=W_conv2, strides=[1, 1, 1, 1], padding='VALID') relu2 = tf.nn.relu(conv2 + b_conv2) with tf.name_scope('pool2'): pool2 = tf.nn.max_pool(value=relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') with tf.name_scope('dense1'): with tf.variable_scope('dense1'): W_dense1 = tf.get_variable('w', [6 * 6 * 32, 128]) b_dense1 = tf.get_variable('b', 128) flat = tf.reshape(pool2, [-1, 6 * 6 * 32], name='reshape') dense1 = tf.matmul(flat, W_dense1) relu3 = tf.nn.relu(dense1 + b_dense1) with tf.name_scope('dropout'): dropout = tf.nn.dropout(relu3, do_rate) with tf.name_scope('output'): with tf.variable_scope('output'): W_out = tf.get_variable('w', [128, NUM_CLASSES]) b_out = tf.get_variable('b', [NUM_CLASSES]) output = tf.matmul(dropout, W_out) + b_out ''' ################################################################ """ Using Keras layers instead """ #input_layer = Input(shape=(HEIGHT, WIDTH, 1), name='input_layer') Kcnn1 = Conv2D(filters=32, kernel_size=3, strides=(1,1), padding='same', activation='relu')(input_data) Kmaxpool1 = MaxPooling2D(pool_size=2)(Kcnn1) Kcnn2 = Conv2D(filters=32, kernel_size=3, strides=(1,1), padding='valid', activation='relu')(Kmaxpool1) Kmaxpool2 = MaxPooling2D(pool_size=2)(Kcnn2) Kflat = Flatten()(Kmaxpool2) Kdense1 = Dense(units=128, activation='relu')(Kflat) Kdropout = Dropout(.5)(Kdense1) output = Dense(units=NUM_CLASSES, activation='softmax')(Kdropout) """ The rest of the code is almost the same as in pure_tf_mnist.py, except for the feed_dict, where instead of do_rate in tensorflow, we need to provide keras specific dropout tensor 'learning_phase' in the backend of Keras. """ ################################################################ ''' print('\n\n') print('-------------------------------------------------------') print('--------------- Trainable parameters ------------------') print('-------------------------------------------------------') total_parameters = 0 for v in tf.trainable_variables(): shape = v.get_shape() print(shape) #pdb.set_trace() params = 1 for dim in shape: params *= dim.value total_parameters += params print('total_parameters = {}'.format(total_parameters)) print('-------------------------------------------------------\n\n') loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=input_labels, logits=output, name='loss')) train_op = tf.train.AdamOptimizer(1e-4).minimize(loss) accuracy = tf.cast( tf.equal(tf.argmax(input_labels, 1), tf.argmax(output, 1)), tf.float32) print('') print('-------------------------------------------------------') print('---------- Starting a TF session ----------------------') print('-------------------------------------------------------') print('') tf_weights = [] tf.set_random_seed(1234) # Training: with tf.Session() as sess: sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('graph', sess.graph) print('-------------------------------------------------------') print('--------------- Training phase ------------------------') print('-------------------------------------------------------') for i in range(epochs): steps = (int)(np.ceil(float(N) / float(BATCH_SIZE))) total_l = 0 total_acc = 0 for step in range(steps): x_in, y_in = get_batch(step, BATCH_SIZE, training_images, training_labels) l, acc, _ = sess.run([loss, accuracy, train_op], { input_data: x_in, input_labels: y_in, do_rate: 0.5 }) total_l += l total_acc += np.sum(acc) #pdb.set_trace() total_acc /= np.float32(N) print( "Epoch {}: Training loss = {}, Training accuracy = {}".format( i, total_l, total_acc)) # Test: total_acc = 0 steps = (int)(np.ceil(float(Nt) / float(BATCH_SIZE))) for step in range(steps): x_in, y_in = get_batch(step, BATCH_SIZE, test_images, test_labels) acc = sess.run([accuracy], { input_data: x_in, input_labels: y_in, do_rate: 1 }) total_acc += np.sum(acc) total_acc /= np.float32(Nt) print('\n-----------------------') print("Test accuracy = {}".format(total_acc)) print('-------------------------------------------------------') ################################################################# ### Exporting the trained weights into a list of numpy vectors for v in tf.trainable_variables(): tf_weights.append(sess.run(v)) writer.close() print('') print('-------------------------------------------------------') print('---------- Starting a Keras session -------------------') print('-------------------------------------------------------') print('') ################################################################# """ Building a Keras Model """ input_layer = Input(shape=(HEIGHT, WIDTH, 1), name='input_layer') Kkcnn1 = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='same', activation='relu')(input_layer) Kkmaxpool1 = MaxPooling2D(pool_size=2)(Kkcnn1) Kkcnn2 = Conv2D(filters=32, kernel_size=3, strides=(1, 1), padding='valid', activation='relu')(Kkmaxpool1) Kkmaxpool2 = MaxPooling2D(pool_size=2)(Kkcnn2) Kkflat = Flatten()(Kkmaxpool2) Kkdense1 = Dense(units=128, activation='relu')(Kkflat) Kkdropout = Dropout(.5)(Kkdense1) output_layer = Dense(units=NUM_CLASSES, activation='softmax')(Kkdropout) model = Model(inputs=input_layer, outputs=output_layer) model.compile(optimizer=tf.train.AdamOptimizer(), loss='categorical_crossentropy', metrics=['accuracy']) ################################################################# ################################################################# ### Loarding the already trained weights, onto the keras layers c = 0 # counter for iterating over tensorflow trainable variables #pdb.set_trace() for l in model.layers: trainable_weights = l.trainable_weights if not trainable_weights: # empty trainable weight list in this keras layer; so move on to the next layer. continue len_w = len( trainable_weights ) # e.g. for a normal conv layer, it is two: weight and bias. l.set_weights(tf_weights[c:c + len_w]) c += len_w accuracy = model.evaluate(x=test_images, y=test_labels, batch_size=BATCH_SIZE) print('\n') print('Keras test score = {}'.format(accuracy)) print('\n')