def VGG19(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the VGG19 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Arguments: include_top: whether to include the 3 fully-connected layers at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 input channels, and width and height should be no smaller than 48. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape( input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) # Block 1 x = Conv2D( 64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D( 64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D( 128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D( 128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D( 256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D( 256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D( 256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = Conv2D( 256, (3, 3), activation='relu', padding='same', name='block3_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block4_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = Conv2D( 512, (3, 3), activation='relu', padding='same', name='block5_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vgg19') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'vgg19_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') 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()
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')
dropout_rate = 0.25 opt = Adam(lr=1e-4) dopt = Adam(lr=1e-3) # Build Generative model ... nch = 200 g_input = Input(shape=[100]) H = Dense(nch * 14 * 14, kernel_initializer=init_ops.glorot_normal_initializer())(g_input) H = BatchNormalization()(H) H = Activation('relu')(H) H = Reshape([14, 14, nch])(H) H = UpSampling2D(size=(2, 2))(H) H = Conv2D(nch / 2, kernel_size=(3, 3), 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)
def create_AlexNet(num_fc_neurons, dropout_rate, num_classes=24, img_height=224, img_width=224, include_loc='all', activation='softmax'): weight_decay = 0.0005 kernel_regularizer = regularizers.l2(weight_decay) bias_regularizer = regularizers.l2(weight_decay) # build a convolutional model base_model = Sequential() base_model.add( Conv2D(96, (11, 11), strides=(4, 4), padding='valid', activation='relu', kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='conv1', input_shape=get_input_shape(img_height, img_width))) base_model.add(LRN2D(name='lrn1')) base_model.add(MaxPooling2D((3, 3), strides=(2, 2), name='pool1')) base_model.add( Conv2D(256, (5, 5), strides=(1, 1), padding='same', activation='relu', kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='conv2')) base_model.add(LRN2D(name='lrn2')) base_model.add(MaxPooling2D((3, 3), strides=(2, 2), name='pool2')) base_model.add( Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation='relu', kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='conv3')) base_model.add( Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation='relu', kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='conv4')) base_model.add( Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='conv5')) base_model.add(MaxPooling2D((3, 3), strides=(2, 2), name='pool3')) # build a classifier model to put on top of the convolutional model top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) for i in range(6, 8): top_model.add( Dense(num_fc_neurons, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='fc' + str(i))) #top_model.add(BatchNormalization(axis=1, name='fc'+str(i)+'_bn')) top_model.add(Activation('relu', name='fc' + str(i) + '_ac')) top_model.add(Dropout(dropout_rate)) top_model.add( Dense(num_classes, activation=activation, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, name='predictions')) if include_loc == 'base': model = base_model elif include_loc == 'top': model = top_model elif include_loc == 'all': # add the model on top of the convolutional base model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) else: raise ValueError('Only "base", "top" and "all" can be included.') return model
1. build a simple inception model: multiple inputs, multiple towers 2. Residual connection on a convolution layer 3. a shared vision model 4. Visual question answering model 5. Video question answering model ### Inception module For more information about the Inception architecture, see [Going Deeper with Convolutions](http://arxiv.org/abs/1409.4842). """ from tensorflow.contrib.keras.python.keras.layers import Conv2D, MaxPooling2D, Input, concatenate from tensorflow.contrib.keras.python.keras.models import Model input_img = Input(shape=(256, 256, 3)) tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1) tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2) tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img) tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3) output = concatenate([tower_1, tower_2, tower_3], axis=1) # add up on second dim axi=1 model = Model(input_img, output) model.summary()
shape_ord = (1, 28, 28) (x_train, _), (x_test, _) = mnist.load_data() x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = np.reshape(x_train, ((x_train.shape[0],) + shape_ord)) x_test = np.reshape(x_test, ((x_test.shape[0],) + shape_ord)) input_img = Input(shape=(28, 28, 1)) x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) encoded = MaxPooling2D((2, 2), padding='same')(x) # at this point the representation is (4, 4, 8) i.e. 128-dimensional x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded) x = UpSampling2D((2, 2))(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(16, (3, 3), activation='relu')(x) x = UpSampling2D((2, 2))(x) decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
def MobileNet( input_shape=None, # pylint: disable=invalid-name alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000): """Instantiates the MobileNet architecture. Note that only TensorFlow is supported for now, therefore it only works with the data format `image_data_format='channels_last'` in your Keras config at `~/.keras/keras.json`. To load a MobileNet model via `load_model`, import the custom objects `relu6` and `DepthwiseConv2D` and pass them to the `custom_objects` parameter. E.g. model = load_model('mobilenet.h5', custom_objects={ 'relu6': mobilenet.relu6, 'DepthwiseConv2D': mobilenet.DepthwiseConv2D}) Arguments: input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or (3, 224, 224) (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. alpha: controls the width of the network. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. depth_multiplier: depth multiplier for depthwise convolution (also called the resolution multiplier) dropout: dropout rate include_top: whether to include the fully-connected layer at the top of the network. weights: `None` (random initialization) or `imagenet` (ImageNet weights) input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. """ if K.backend() != 'tensorflow': raise RuntimeError('Only TensorFlow backend is currently supported, ' 'as other backends do not support ' 'depthwise convolution.') if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as ImageNet with `include_top` ' 'as true, `classes` should be 1000') # Determine proper input shape. input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=K.image_data_format(), include_top=include_top or weights) if K.image_data_format() == 'channels_last': row_axis, col_axis = (0, 1) else: row_axis, col_axis = (1, 2) rows = input_shape[row_axis] cols = input_shape[col_axis] if weights == 'imagenet': if depth_multiplier != 1: raise ValueError('If imagenet weights are being loaded, ' 'depth multiplier must be 1') if alpha not in [0.25, 0.50, 0.75, 1.0]: raise ValueError('If imagenet weights are being loaded, ' 'alpha can be one of' '`0.25`, `0.50`, `0.75` or `1.0` only.') if rows != cols or rows not in [128, 160, 192, 224]: raise ValueError('If imagenet weights are being loaded, ' 'input must have a static square shape (one of ' '(128,128), (160,160), (192,192), or (224, 224)).' ' Input shape provided = %s' % (input_shape, )) if K.image_data_format() != 'channels_last': warnings.warn('The MobileNet family of models is only available ' 'for the input data format "channels_last" ' '(width, height, channels). ' 'However your settings specify the default ' 'data format "channels_first" (channels, width, height).' ' You should set `image_data_format="channels_last"` ' 'in your Keras config located at ~/.keras/keras.json. ' 'The model being returned right now will expect inputs ' 'to follow the "channels_last" data format.') K.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = _conv_block(img_input, 32, alpha, strides=(2, 2)) x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3) x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4) x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11) x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12) x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13) if include_top: if K.image_data_format() == 'channels_first': shape = (int(1024 * alpha), 1, 1) else: shape = (1, 1, int(1024 * alpha)) x = GlobalAveragePooling2D()(x) x = Reshape(shape, name='reshape_1')(x) x = Dropout(dropout, name='dropout')(x) x = Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x) x = Activation('softmax', name='act_softmax')(x) x = Reshape((classes, ), name='reshape_2')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='mobilenet_%0.2f_%s' % (alpha, rows)) # load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': raise ValueError('Weights for "channels_last" format ' 'are not available.') if alpha == 1.0: alpha_text = '1_0' elif alpha == 0.75: alpha_text = '7_5' elif alpha == 0.50: alpha_text = '5_0' else: alpha_text = '2_5' if include_top: model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows) weigh_path = BASE_WEIGHT_PATH + model_name weights_path = get_file(model_name, weigh_path, cache_subdir='models') else: model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows) weigh_path = BASE_WEIGHT_PATH + model_name weights_path = get_file(model_name, weigh_path, cache_subdir='models') model.load_weights(weights_path) if old_data_format: K.set_image_data_format(old_data_format) return model
def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha, depth_multiplier=1, strides=(1, 1), block_id=1): """Adds a depthwise convolution block. A depthwise convolution block consists of a depthwise conv, batch normalization, relu6, pointwise convolution, batch normalization and relu6 activation. Arguments: inputs: Input tensor of shape `(rows, cols, channels)` (with `channels_last` data format) or (channels, rows, cols) (with `channels_first` data format). pointwise_conv_filters: Integer, the dimensionality of the output space (i.e. the number output of filters in the pointwise convolution). alpha: controls the width of the network. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. depth_multiplier: The number of depthwise convolution output channels for each input channel. The total number of depthwise convolution output channels will be equal to `filters_in * depth_multiplier`. strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the width and height. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with specifying any `dilation_rate` value != 1. block_id: Integer, a unique identification designating the block number. Input shape: 4D tensor with shape: `(batch, channels, rows, cols)` if data_format='channels_first' or 4D tensor with shape: `(batch, rows, cols, channels)` if data_format='channels_last'. Output shape: 4D tensor with shape: `(batch, filters, new_rows, new_cols)` if data_format='channels_first' or 4D tensor with shape: `(batch, new_rows, new_cols, filters)` if data_format='channels_last'. `rows` and `cols` values might have changed due to stride. Returns: Output tensor of block. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 pointwise_conv_filters = int(pointwise_conv_filters * alpha) x = DepthwiseConv2D( # pylint: disable=not-callable (3, 3), padding='same', depth_multiplier=depth_multiplier, strides=strides, use_bias=False, name='conv_dw_%d' % block_id)(inputs) x = BatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x) x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x) x = Conv2D(pointwise_conv_filters, (1, 1), padding='same', use_bias=False, strides=(1, 1), name='conv_pw_%d' % block_id)(x) x = BatchNormalization(axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x) return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)
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 VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the VGG16 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Arguments: include_top: whether to include the 3 fully-connected layers at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 48. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ ### how many weights option can we be allowed if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') ### if use imagenet weights and add last 3 dense layers, then class should be 1000 if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') ### set input shape : (224, 224, 3) # default input shape for VGG16 model, designed for imagenet dataset input_shape = _obtain_input_shape( input_shape, # if set must be a tuple of 3 integers (50, 50, 3) default_size=224, # if input_shape set, here must be None min_size=48, # 48, but freely change it to your need data_format=K.image_data_format( ), # 'channels_first' or 'channels_last' include_top=include_top ) # True, then must use 224 or False to be other number ### Create input tensor: real tensor or container? if input_tensor is None: # create input tensor placeholder img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) ## how to access weights of each layer block1_conv1 = x block1_conv1_bias = block1_conv1.graph._collections['trainable_variables'][ -1] # bias block1_conv1_kernel = block1_conv1.graph._collections[ 'trainable_variables'][-2] # kernel x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) block1_conv2 = x block1_conv2_bias = block1_conv2.graph._collections['trainable_variables'][ -1] # bias block1_conv2_kernel = block1_conv2.graph._collections[ 'trainable_variables'][-2] # kernel x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) block1_pool = x # access trainable_variables or weights with biases block1_pool.graph._collections['variables'][-1] # bias block1_pool.graph._collections['variables'][-2] # kernel # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) block2_conv1 = x x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) block2_conv2 = x x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) block2_pool = x # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) block3_conv1 = x x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) block3_conv2 = x x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) block3_conv3 = x x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) block3_pool = x # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) block4_conv1 = x x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) block4_conv2 = x x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) block4_conv3 = x x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) block4_pool = x # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) block5_conv1 = x x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) block5_conv2 = x x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) block5_conv3 = x x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) block5_pool = x if include_top: # Classification block x = Flatten(name='flatten')(x) flatten = x x = Dense(4096, activation='relu', name='fc1')(x) fc1 = x x = Dense(4096, activation='relu', name='fc2')(x) fc2 = x x = Dense(classes, activation='softmax', name='predictions')(x) predictions = x else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vgg16') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') return model
def Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Xception architecture. Optionally loads weights pre-trained on ImageNet. This model is available for TensorFlow only, and can only be used with inputs following the TensorFlow data format `(width, height, channels)`. You should set `image_data_format="channels_last"` in your Keras config located at ~/.keras/keras.json. Note that the default input image size for this model is 299x299. Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(299, 299, 3)`. It should have exactly 3 inputs channels, and width and height should be no smaller than 71. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') if K.backend() != 'tensorflow': raise RuntimeError('The Xception model is only available with ' 'the TensorFlow backend.') if K.image_data_format() != 'channels_last': logging.warning( 'The Xception model is only available for the ' 'input data format "channels_last" ' '(width, height, channels). ' 'However your settings specify the default ' 'data format "channels_first" (channels, width, height). ' 'You should set `image_data_format="channels_last"` in your Keras ' 'config located at ~/.keras/keras.json. ' 'The model being returned right now will expect inputs ' 'to follow the "channels_last" data format.') K.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=299, min_size=71, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(img_input) x = BatchNormalization(name='block1_conv1_bn')(x) x = Activation('relu', name='block1_conv1_act')(x) x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x) x = BatchNormalization(name='block1_conv2_bn')(x) x = Activation('relu', name='block1_conv2_act')(x) residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x) x = BatchNormalization(name='block2_sepconv1_bn')(x) x = Activation('relu', name='block2_sepconv2_act')(x) x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x) x = BatchNormalization(name='block2_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x) x = layers.add([x, residual]) residual = Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Activation('relu', name='block3_sepconv1_act')(x) x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x) x = BatchNormalization(name='block3_sepconv1_bn')(x) x = Activation('relu', name='block3_sepconv2_act')(x) x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x) x = BatchNormalization(name='block3_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x) x = layers.add([x, residual]) residual = Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Activation('relu', name='block4_sepconv1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x) x = BatchNormalization(name='block4_sepconv1_bn')(x) x = Activation('relu', name='block4_sepconv2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x) x = BatchNormalization(name='block4_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x) x = layers.add([x, residual]) for i in range(8): residual = x prefix = 'block' + str(i + 5) x = Activation('relu', name=prefix + '_sepconv1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = BatchNormalization(name=prefix + '_sepconv1_bn')(x) x = Activation('relu', name=prefix + '_sepconv2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = BatchNormalization(name=prefix + '_sepconv2_bn')(x) x = Activation('relu', name=prefix + '_sepconv3_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = BatchNormalization(name=prefix + '_sepconv3_bn')(x) x = layers.add([x, residual]) residual = Conv2D(1024, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Activation('relu', name='block13_sepconv1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block13_sepconv1')(x) x = BatchNormalization(name='block13_sepconv1_bn')(x) x = Activation('relu', name='block13_sepconv2_act')(x) x = SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='block13_sepconv2')(x) x = BatchNormalization(name='block13_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x) x = layers.add([x, residual]) x = SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x) x = BatchNormalization(name='block14_sepconv1_bn')(x) x = Activation('relu', name='block14_sepconv1_act')(x) x = SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x) x = BatchNormalization(name='block14_sepconv2_bn')(x) x = Activation('relu', name='block14_sepconv2_act')(x) if include_top: x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='xception') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'xception_weights_tf_dim_ordering_tf_kernels.h5', TF_WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'xception_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if old_data_format: K.set_image_data_format(old_data_format) return model
img_cols = x_train.shape[2] channels = x_train.shape[3] x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, channels) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, channels) input_shape = (img_rows, img_cols, channels) x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float32') / 255 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) inputs = Input(shape=input_shape) x = Conv2D(num_filters, kernel_size=7, padding='same', strides=2, kernel_initializer='he_normal', kernel_regularizer=l2(1e-4))(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) if use_max_pool: x = MaxPooling2D(pool_size=3, strides=2, padding='same')(x) num_blocks = 3 # convolutional base (stack of blocks). for i in range(num_blocks): for j in range(num_sub_blocks): strides = 1 is_first_layer_but_not_first_block = j == 0 and i > 0
### Visual question answering model This model can select the correct one-word answer when asked a natural-language question about a picture. It works by encoding the question into a vector, encoding the image into a vector, concatenating the two, and training on top a logistic regression over some vocabulary of potential answers. """ from tensorflow.contrib.keras.python.keras.layers import Conv2D, MaxPooling2D, Flatten, Input, LSTM, Embedding, Dense, concatenate from tensorflow.contrib.keras.python.keras.models import Model, Sequential # First, let's define a vision model using a Sequential model. # This model will encode an image into a vector. vision_model = Sequential() vision_model.add( Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3))) vision_model.add(Conv2D(64, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) vision_model.add(Conv2D(128, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) vision_model.add(Conv2D(256, (3, 3), activation='relu')) vision_model.add(Conv2D(256, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Flatten()) # Now let's get a tensor with the output of our vision model: image_input = Input(shape=(224, 224, 3)) encoded_image = vision_model(image_input)
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 input channels, and width and height should be no smaller than 197. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape( input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(img_input) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) return model
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 244)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = WEIGHTS_PATH else: weights_path = WEIGHTS_PATH_NO_TOP model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def build(self, alpha, img_input, temp_softmax): shape = (1, 1, int(1024 * alpha)) """ This looks dangerous. Not sure how the model would get affected with the laarning_phase variable set to True. """ K.set_learning_phase(True) with tf.name_scope('student') as scope: self.conv1 = Conv2D(int(32 * alpha), (3, 3), padding='same', use_bias=False, strides=(1, 1), name='student_conv1')(img_input) self.conv2 = BatchNormalization(axis=-1, name='student_conv1_bn')( self.conv1) self.conv3 = Activation(self.relu6, name='student_conv1_relu')(self.conv2) self.conv4 = self._depthwise_conv_block(self.conv3, 64, alpha, depth_multiplier, block_id=15) self.conv5 = self._depthwise_conv_block(self.conv4, 128, alpha, depth_multiplier, strides=(2, 2), block_id=16) self.conv6 = self._depthwise_conv_block(self.conv5, 128, alpha, depth_multiplier, block_id=17) self.conv7 = self._depthwise_conv_block(self.conv6, 256, alpha, depth_multiplier, strides=(2, 2), block_id=18) self.conv8 = self._depthwise_conv_block(self.conv7, 256, alpha, depth_multiplier, block_id=19) self.conv9 = self._depthwise_conv_block(self.conv8, 512, alpha, depth_multiplier, strides=(2, 2), block_id=20) self.conv10 = self._depthwise_conv_block(self.conv9, 512, alpha, depth_multiplier, block_id=21) self.conv11 = self._depthwise_conv_block(self.conv10, 512, alpha, depth_multiplier, block_id=22) self.conv12 = self._depthwise_conv_block(self.conv11, 512, alpha, depth_multiplier, block_id=23) self.conv13 = self._depthwise_conv_block(self.conv12, 512, alpha, depth_multiplier, block_id=24) self.conv14 = self._depthwise_conv_block(self.conv13, 512, alpha, depth_multiplier, block_id=25) self.conv15 = self._depthwise_conv_block(self.conv14, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=26) self.conv16 = self._depthwise_conv_block(self.conv15, 1024, alpha, depth_multiplier, block_id=27) self.conv17 = GlobalAveragePooling2D()(self.conv16) self.conv18 = Reshape(shape, name='student_reshape_1')(self.conv17) self.conv19 = Dropout(0.5, name='student_dropout')(self.conv18) self.conv20 = Conv2D(self.num_classes, (1, 1), padding='same', name='student_conv_preds')(self.conv18) self.conv21 = Activation('softmax', name='student_act_softmax')(tf.divide( self.conv20, temp_softmax)) self.conv22 = Reshape((self.num_classes, ), name='student_reshape_2')(self.conv21) return self
labels_onehot, test_size=0.2) x_train, x_test, y_train, y_test = train_test_split(x_train_full, y_train_full, test_size=0.25) np.save('x_test.npy', x_test) np.save('y_test.npy', y_test) print("Making model...") model = Sequential() model.add( Conv2D(filters=32, kernel_size=(3, 3), input_shape=(20, 20, 1), data_format='channels_last', padding='same', activation='relu')) model.add(MaxPooling2D(2, 2)) model.add(SpatialDropout2D(0.25, data_format='channels_last')) model.add( Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu')) model.add(MaxPooling2D(2, 2)) model.add(SpatialDropout2D(0.25, data_format='channels_last')) model.add( Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu')) model.add(MaxPooling2D(2, 2)) model.add(SpatialDropout2D(0.25, data_format='channels_last'))