def mk_model_with_bn(): model = Sequential() model.add( Convolution2D(filters=64, kernel_size=(3, 3), strides=(1, 1), input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1), kernel_initializer=kernel_initializer())) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(filters=64, kernel_size=(3, 3), strides=(1, 1), kernel_initializer=kernel_initializer())) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add( Convolution2D(filters=128, kernel_size=(3, 3), kernel_initializer=kernel_initializer())) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(filters=128, kernel_size=(3, 3), kernel_initializer=kernel_initializer())) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add( Convolution2D(filters=256, kernel_size=(3, 3), kernel_initializer=kernel_initializer())) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Convolution2D(filters=256, kernel_size=(3, 3), kernel_initializer=kernel_initializer())) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(256, kernel_initializer='he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(LABEL_NUM, kernel_initializer='he_normal')) model.add(Activation('softmax')) return model
def inference(input_shape, N_CLASSES): '''build the model args: image: image batch, 4D tensor [batch_size, width, height, channels=3], dtype=tf.float32 return: output tensor with the computed logits, float, [batch_size, n_classes] ''' inputs = Input(shape=input_shape) K.set_learning_phase( False) # all new operations will be in test mode from now on ## Conv layer 1 x = dn_layer(inputs=inputs, name='layer1') x = MaxPooling2D((2, 2), strides=(2, 2), name='layer1_maxpool')(x) ## Conv layer 2 x = dn_layer(inputs=x, num_filters=32, name='layer2') x = MaxPooling2D((2, 2), strides=(2, 2), name='layer2_maxpool')(x) ## Conv layer 3 x = dn_layer(inputs=x, num_filters=64, name='layer3') x = MaxPooling2D((2, 2), strides=(2, 2), name='layer3_maxpool')(x) x = GlobalAveragePooling2D(name='GlobalAveragePooling2D')(x) #x = Flatten(name='flatten')(x) x = Dense(N_CLASSES, activation='softmax', name='predictions')(x) model = Model(inputs=inputs, outputs=x) return model
def main(): x_train, y_train, x_test, y_test = load_data() model = Sequential() model.add( Conv2D(32, kernel_size=(11, 11), strides=4, padding="same", activation='relu', input_shape=(48, 48, 1))) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding="valid")) model.add( Conv2D(32, kernel_size=(5, 5), strides=1, padding="same", activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding="valid")) model.add( Conv2D(32, kernel_size=(3, 3), strides=1, padding="same", activation='relu')) model.add( Conv2D(32, kernel_size=(3, 3), strides=1, padding="same", activation='relu')) model.add( Conv2D(32, kernel_size=(3, 3), strides=1, padding="same", activation='relu')) model.add(Dense(1024, activation='relu')) model.add(Dense(512, activation='relu')) model.add(Dense(7, activation='softmax')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=1, validation_data=(x_test, y_test)) model.save(expanduser("~/emotion/alex_net.h5")) accuracy, fbeta = test_model(model, x_test, y_test) print("Accuracy: %s" % accuracy) print("F-Beta: %s" % fbeta)
def model_fn(features, targets, mode, params): """Model function for Estimator.""" # 1. Configure the model via TensorFlow operations # First, build all the model, a good idea is using Keras or tf.layers # since these are high-level API's conv1 = Conv2D(32, (5, 5), activation='relu', input_shape=(28, 28, 1))(features) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(64, (5, 5), activation='relu')(pool1) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) flat = Flatten()(pool2) dense = Dense(1024, activation='relu')(flat) preds = Dense(10, activation='softmax')(dense) # 2. Define the loss function for training/evaluation loss = None train_op = None # Calculate Loss (for both TRAIN and EVAL modes) if mode != learn.ModeKeys.INFER: loss = tf.losses.softmax_cross_entropy(onehot_labels=targets, logits=preds) # 3. Define the training operation/optimizer # Configure the Training Op (for TRAIN mode) if mode == learn.ModeKeys.TRAIN: train_op = tf.contrib.layers.optimize_loss( loss=loss, global_step=tf.contrib.framework.get_global_step(), learning_rate=params["learning_rate"], optimizer="Adam", ) # 4. Generate predictions predictions_dict = { "classes": tf.argmax(input=preds, axis=1), "probabilities": tf.nn.softmax(preds, name="softmax_tensor") } # 5. Define how you want to evaluate the model metrics = { "accuracy": tf.metrics.accuracy(tf.argmax(input=preds, axis=1), tf.argmax(input=targets, axis=1)) } # 6. Return predictions/loss/train_op/eval_metric_ops in ModelFnOps object return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions_dict, loss=loss, train_op=train_op, eval_metric_ops=metrics)
def discriminator_model(): model = Sequential() model.add(Conv2D(64, (5, 5),padding='same',input_shape=(28, 28, 1)) ) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(128, (5, 5))) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(1024)) model.add(Activation('tanh')) model.add(Dense(1)) model.add(Activation('sigmoid')) return model
def model_fn(features, labels, mode, params): conv1 = Conv2D(32, (5, 5), activation='relu', input_shape=(28, 28, 1))(features["x"]) conv2 = Conv2D(64, (5, 5), activation='relu')(conv1) conv3 = Conv2D(128, (2, 2), activation='relu')(conv2) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(256, (5, 5), activation='relu')(pool3) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) flat = Flatten()(pool4) dense1 = Dense(1024, activation='relu')(flat) dense2 = Dense(1024, activation='relu')(dense1) dense3 = Dense(1024, activation='relu')(dense2) dense4 = Dense(1024, activation='relu')(dense3) logits = Dense(10, activation='softmax')(dense4) loss = tf.losses.softmax_cross_entropy( onehot_labels=labels, logits=logits) train_op = tf.contrib.layers.optimize_loss( loss=loss, global_step=tf.contrib.framework.get_global_step(), learning_rate=params["learning_rate"], optimizer="SGD") predictions = { "classes": tf.argmax(input=logits, axis=1), "probabilities": tf.nn.softmax(logits) } eval_metric_ops = { "accuracy": tf.metrics.accuracy( tf.argmax(input=logits, axis=1), tf.argmax(input=labels, axis=1)) } return model_fn_lib.EstimatorSpec( mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=eval_metric_ops)
def build(self, input_shape): self.input_spec = [InputSpec(shape=input_shape)] self.conv_layers = {c: [] for c in ['i', 'f', 'c', 'o', 'a', 'ahat']} for l in range(self.nb_layers): for c in ['i', 'f', 'c', 'o']: act = self.LSTM_activation if c == 'c' else self.LSTM_inner_activation self.conv_layers[c].append( Convolution2D(self.R_stack_sizes[l], self.R_filt_sizes[l], padding='same', data_format="channels_last", activation=act)) act = 'relu' if l == 0 else self.A_activation self.conv_layers['ahat'].append( Convolution2D(self.stack_sizes[l], self.Ahat_filt_sizes[l], padding='same', data_format="channels_last", activation=act)) if l < self.nb_layers - 1: self.conv_layers['a'].append( Convolution2D(self.stack_sizes[l + 1], self.A_filt_sizes[l], padding='same', data_format="channels_last", activation=self.A_activation)) self.upsample = UpSampling2D(data_format="channels_last") # upsampling self.pool = MaxPooling2D(data_format="channels_last") # downsampling self._trainable_weights = [] nb_row, nb_col = (input_shape[-3], input_shape[-2]) # Super model for c in sorted(self.conv_layers.keys()): for l in range(len(self.conv_layers[c])): ds_factor = 2**l if c == 'ahat': nb_channels = self.R_stack_sizes[l] elif c == 'a': nb_channels = 2 * self.stack_sizes[l] else: # i, c, o, f nb_channels = self.stack_sizes[l] * 2 + self.R_stack_sizes[ l] if l < self.nb_layers - 1: nb_channels += self.R_stack_sizes[l + 1] in_shape = (input_shape[0], nb_row // ds_factor, nb_col // ds_factor, nb_channels ) # up -> downsampling self.conv_layers[c][l].build(in_shape) self._trainable_weights += self.conv_layers[c][ l].trainable_weights self.states = [None] * self.nb_layers * 3 # ['r', 'c', 'e'] if self.extrap_start_time is not None: self.t_extrap = K.variable(np.array(self.extrap_start_time), 'int32') self.states += [None] * 2
def mk_model(arch, data_type, classes): if data_type == 'mnist': channels = 1 elif data_type == 'aerial': channels = 3 if arch == 'lenet-5': model = Sequential() #モデルの初期化 #畳み込み第1層 model.add(Conv2D( 32, 5, padding='same', input_shape=(28, 28, channels))) #output_shape=(None,28,28,32) #filters=32, kernel_size=(5,5), strides(1,1), use_bias=True #dilidation_rate(膨張率)=(1,1), kernel_initializer='glorot_uniform', bias_initializer='zeros' #padding='sane'は出力のshapeが入力と同じになるように調整 #output_shape=(None(60000),28,28,32) model.add(Activation('relu')) model.add(MaxPooling2D(padding='same')) #output_shape=(None,14,14,32) #pool_size=(2,2), strides(2,2) #畳み込み第2層 model.add(Conv2D(64, 5, padding='same')) #output_shape=(None,14,14,64) model.add(Activation('relu')) model.add(MaxPooling2D(padding='same')) #output_shape=(None,7,7,64) #平坦化 model.add(Flatten()) #output_shape=(None,3136(7*7*64)) #全結合第1層 model.add(Dense(1024)) #output_shape=(None,1024) model.add(Activation('relu')) model.add(Dropout(0.5)) #無視する割合を記述(例えば、0.2と記述した場合、80%の結合が残る) #全結合第2層 model.add(Dense(classes)) #output_shape=(None,classes) model.add(Activation('softmax')) return model elif arch == 'alexnet': model = Sequential() #モデルの初期化
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 simple_model(): model = Sequential() model.add( Convolution2D(filters=32, kernel_size=(3, 3), strides=(1, 1), input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(filters=64, kernel_size=(3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(LABEL_NUM)) model.add(Activation('softmax')) return model
def mk_model(): model = Sequential() model.add( Convolution2D(filters=32, kernel_size=(3, 3), strides=(1, 1), input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1), kernel_initializer=kernel_initializer())) model.add(LeakyReLU(alpha=.1)) model.add( Convolution2D(filters=32, kernel_size=(3, 3), strides=(1, 1), kernel_initializer=kernel_initializer())) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add( Convolution2D(filters=64, kernel_size=(3, 3), kernel_initializer=kernel_initializer())) model.add(LeakyReLU(alpha=0.1)) model.add( Convolution2D(filters=64, kernel_size=(3, 3), kernel_initializer=kernel_initializer())) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(MaxoutDense(output_dim=256, nb_feature=4)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(LABEL_NUM, kernel_initializer='he_uniform')) model.add(Activation('softmax')) return model
def dw_conv(init, nb_filter, k): residual = Conv2D(nb_filter * k, (1, 1), strides=(2, 2), padding='same', use_bias=False)(init) residual = BN(residual) x = Conv2D(nb_filter * k, (3, 3), padding='same', use_bias=False)(init) x = BN(x) x = Activation('relu')(x) x = Conv2D(nb_filter * k, (3, 3), padding='same', use_bias=False)(x) x = BN(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = layers.add([x, residual]) return x
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_tf(inputs): ''' defin the model in tf way ''' img, labels, keep_rate = inputs[0], inputs[1], inputs[2] x = Conv2D(32, kernel_size=(3, 3), activation='relu')(img) x = Conv2D(64, (3, 3), activation='relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) #x = Dropout(0.25)(x) # mix with tf operator. ok. x = tf.nn.dropout(x, keep_rate) x = Flatten()(x) x = Dense(128, activation='relu')(x) #x = Dropout(0.5)(x) x = tf.nn.dropout(x, keep_rate) pred = Dense(num_classes, activation='softmax')(x) loss = tf.reduce_mean(categorical_crossentropy(labels, pred)) # train train_op = tf.train.AdadeltaOptimizer().minimize(loss) correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) return [train_op, 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 = 5 # 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 ''' print('-------------------------------------------------------') """ 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) Kmaxpool = MaxPooling2D(pool_size=2)(Kcnn1) """ 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=Kmaxpool, filter=W_conv2, strides=[1,1,1,1], padding='VALID') Kcnn2 = tf.nn.relu(conv2 + b_conv2) """ Kcnn2 = Conv2D(filters=32, kernel_size=3, strides=(1,1), padding='valid', activation='relu')(Kmaxpool) Kmaxpool = MaxPooling2D(pool_size=2)(Kcnn2) Kflat = Flatten()(Kmaxpool) Kdense1 = Dense(units=128, activation='relu')(Kflat) Kdropout = Dropout(.5)(Kdense1) output = Dense(units=NUM_CLASSES, activation='linear')(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('-------------------------------------------------------') 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) # Training: sess = tf.Session() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('graph', sess.graph) 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, learning_phase():1})#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, learning_phase():0})#do_rate:1}) total_acc += np.sum(acc) total_acc /= np.float32(Nt) print('\n--------------------------\n') print("Test accuracy = {}".format(total_acc)) sess.close() writer.close()
Y_train = np_utils.to_categorical(y_train, 10) Y_test = np_utils.to_categorical(y_test, 10) # Model model = Sequential() #defaultformat: (samples, rows, col model.add( Convolution2D(32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=(28, 28, 1))) model.add( Convolution2D(32, kernel_size=(3, 3), strides=(1, 1), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax')) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1) score = model.evaluate(X_test, Y_test, verbose=0)
def SSD300(input_shape, num_classes=21): """SSD300 architecture. # Arguments input_shape: Shape of the input image, expected to be either (300, 300, 3) or (3, 300, 300)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ net = {} # Block 1 input_tensor = input_tensor = Input(shape=input_shape) img_size = (input_shape[1], input_shape[0]) net['input'] = input_tensor net['conv1_1'] = Convolution2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')(net['input']) net['conv1_2'] = Convolution2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(net['conv1_1']) net['pool1'] = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool1')(net['conv1_2']) # Block 2 net['conv2_1'] = Convolution2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(net['pool1']) net['conv2_2'] = Convolution2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(net['conv2_1']) net['pool2'] = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool2')(net['conv2_2']) # Block 3 net['conv3_1'] = Convolution2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(net['pool2']) net['conv3_2'] = Convolution2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(net['conv3_1']) net['conv3_3'] = Convolution2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(net['conv3_2']) net['pool3'] = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool3')(net['conv3_3']) # Block 4 net['conv4_1'] = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(net['pool3']) net['conv4_2'] = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(net['conv4_1']) net['conv4_3'] = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(net['conv4_2']) net['pool4'] = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool4')(net['conv4_3']) # Block 5 net['conv5_1'] = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(net['pool4']) net['conv5_2'] = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(net['conv5_1']) net['conv5_3'] = Convolution2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(net['conv5_2']) net['pool5'] = MaxPooling2D((3, 3), strides=(1, 1), padding='same', name='pool5')(net['conv5_3']) # FC6 net['fc6'] = Convolution2D(1024, (3, 3), dilation_rate=(6, 6), activation='relu', padding='same', name='fc6')(net['pool5']) # x = Dropout(0.5, name='drop6')(x) # FC7 net['fc7'] = Convolution2D(1024, (1, 1), activation='relu', padding='same', name='fc7')(net['fc6']) # x = Dropout(0.5, name='drop7')(x) # Block 6 net['conv6_1'] = Convolution2D(256, (1, 1), activation='relu', padding='same', name='conv6_1')(net['fc7']) net['conv6_2'] = Convolution2D(512, (3, 3), strides=(2, 2), activation='relu', padding='same', name='conv6_2')(net['conv6_1']) # Block 7 net['conv7_1'] = Convolution2D(128, (1, 1), activation='relu', padding='same', name='conv7_1')(net['conv6_2']) net['conv7_2'] = ZeroPadding2D()(net['conv7_1']) net['conv7_2'] = Convolution2D(256, (3, 3), strides=(2, 2), activation='relu', padding='valid', name='conv7_2')(net['conv7_2']) # Block 8 net['conv8_1'] = Convolution2D(128, (1, 1), activation='relu', padding='same', name='conv8_1')(net['conv7_2']) net['conv8_2'] = Convolution2D(256, (3, 3), strides=(2, 2), activation='relu', padding='same', name='conv8_2')(net['conv8_1']) # Last Pool net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2']) # Prediction from conv4_3 net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['conv4_3']) num_priors = 3 x = Convolution2D(num_priors * 4, (3, 3), padding='same', name='conv4_3_norm_mbox_loc')(net['conv4_3_norm']) net['conv4_3_norm_mbox_loc'] = x flatten = Flatten(name='conv4_3_norm_mbox_loc_flat') net['conv4_3_norm_mbox_loc_flat'] = flatten(net['conv4_3_norm_mbox_loc']) name = 'conv4_3_norm_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, (3, 3), padding='same', name=name)(net['conv4_3_norm']) net['conv4_3_norm_mbox_conf'] = x flatten = Flatten(name='conv4_3_norm_mbox_conf_flat') net['conv4_3_norm_mbox_conf_flat'] = flatten(net['conv4_3_norm_mbox_conf']) priorbox = PriorBox(img_size, 30.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_norm_mbox_priorbox') net['conv4_3_norm_mbox_priorbox'] = priorbox(net['conv4_3_norm']) # Prediction from fc7 num_priors = 6 net['fc7_mbox_loc'] = Convolution2D(num_priors * 4, (3, 3), padding='same', name='fc7_mbox_loc')(net['fc7']) flatten = Flatten(name='fc7_mbox_loc_flat') net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc']) name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes, (3, 3), padding='same', name=name)(net['fc7']) flatten = Flatten(name='fc7_mbox_conf_flat') net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf']) priorbox = PriorBox(img_size, 60.0, max_size=114.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # Prediction from conv6_2 num_priors = 6 x = Convolution2D(num_priors * 4, (3, 3), padding='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x flatten = Flatten(name='conv6_2_mbox_loc_flat') net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc']) name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, (3, 3), padding='same', name=name)(net['conv6_2']) net['conv6_2_mbox_conf'] = x flatten = Flatten(name='conv6_2_mbox_conf_flat') net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf']) priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # Prediction from conv7_2 num_priors = 6 x = Convolution2D(num_priors * 4, (3, 3), padding='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x flatten = Flatten(name='conv7_2_mbox_loc_flat') net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc']) name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, (3, 3), padding='same', name=name)(net['conv7_2']) net['conv7_2_mbox_conf'] = x flatten = Flatten(name='conv7_2_mbox_conf_flat') net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf']) priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # Prediction from conv8_2 num_priors = 6 x = Convolution2D(num_priors * 4, (3, 3), padding='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x flatten = Flatten(name='conv8_2_mbox_loc_flat') net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc']) name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, (3, 3), padding='same', name=name)(net['conv8_2']) net['conv8_2_mbox_conf'] = x flatten = Flatten(name='conv8_2_mbox_conf_flat') net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf']) priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # Prediction from pool6 num_priors = 6 x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6']) net['pool6_mbox_loc_flat'] = x name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) x = Dense(num_priors * num_classes, name=name)(net['pool6']) net['pool6_mbox_conf_flat'] = x priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox') target_shape = (1, 1, 256) net['pool6_reshaped'] = Reshape(target_shape, name='pool6_reshaped')(net['pool6']) net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped']) # Gather all predictions net['mbox_loc'] = concatenate([ net['conv4_3_norm_mbox_loc_flat'], net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat'] ], axis=1, name='mbox_loc') net['mbox_conf'] = concatenate([ net['conv4_3_norm_mbox_conf_flat'], net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat'] ], axis=1, name='mbox_conf') net['mbox_priorbox'] = concatenate([ net['conv4_3_norm_mbox_priorbox'], net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox'] ], axis=1, name='mbox_priorbox') num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4 net['mbox_loc'] = Reshape((num_boxes, 4), name='mbox_loc_final')(net['mbox_loc']) net['mbox_conf'] = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(net['mbox_conf']) net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) net['predictions'] = concatenate( [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], axis=2, #axis = 0, name='predictions') model = Model(net['input'], net['predictions']) 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 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 input 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(), require_flatten=False, weights=weights) 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
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 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 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: img_input = Input(tensor=input_tensor, shape=input_shape) 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') x0 = x 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') # 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, x0], 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 = "./saved_model/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5" model.load_weights(weights_path) return model
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception v3 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. 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)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 input channels, and width and height should be no smaller than 139. 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. """ 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=299, min_size=139, data_format=K.image_data_format(), require_flatten=False, 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_first': channel_axis = 1 else: channel_axis = 3 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid') x = conv2d_bn(x, 32, 3, 3, padding='valid') x = conv2d_bn(x, 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn(x, 80, 1, 1, padding='valid') x = conv2d_bn(x, 192, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0, 1, 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 32, 1, 1) x = layers.concatenate( [branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # mixed 1: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate( [branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed1') # mixed 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate( [branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # mixed 3: 17 x 17 x 768 branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn( branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate( [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3') # mixed 4: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 128, 1, 1) branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 128, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed4') # mixed 5, 6: 17 x 17 x 768 for i in range(2): branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 160, 1, 1) branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 160, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) # mixed 7: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 192, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed7') # mixed 8: 8 x 8 x 1280 branch3x3 = conv2d_bn(x, 192, 1, 1) branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = conv2d_bn(x, 192, 1, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) branch7x7x3 = conv2d_bn( branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate( [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8') # mixed 9: 8 x 8 x 2048 for i in range(2): branch1x1 = conv2d_bn(x, 320, 1, 1) branch3x3 = conv2d_bn(x, 384, 1, 1) branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) branch3x3 = layers.concatenate( [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i)) branch3x3dbl = conv2d_bn(x, 448, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) branch3x3dbl = layers.concatenate( [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed' + str(9 + i)) if include_top: # Classification block 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='inception_v3') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='9a0d58056eeedaa3f26cb7ebd46da564') else: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='bcbd6486424b2319ff4ef7d526e38f63') model.load_weights(weights_path) return model
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception-ResNet v2 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 TensorFlow, Theano and CNTK backends. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299, instead of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing function is different (i.e., do not use `imagenet_utils.preprocess_input()` with this model. Use `preprocess_input()` defined in this module instead). # 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)` (with `'channels_last'` data format) or `(3, 299, 299)` (with `'channels_first'` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. 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. """ 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=299, min_size=139, data_format=K.image_data_format(), require_flatten=False, weights=weights) 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 # Stem block: 35 x 35 x 192 x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid') x = conv2d_bn(x, 32, 3, padding='valid') x = conv2d_bn(x, 64, 3) x = MaxPooling2D(3, strides=2)(x) x = conv2d_bn(x, 80, 1, padding='valid') x = conv2d_bn(x, 192, 3, padding='valid') x = MaxPooling2D(3, strides=2)(x) # Mixed 5b (Inception-A block): 35 x 35 x 320 branch_0 = conv2d_bn(x, 96, 1) branch_1 = conv2d_bn(x, 48, 1) branch_1 = conv2d_bn(branch_1, 64, 5) branch_2 = conv2d_bn(x, 64, 1) branch_2 = conv2d_bn(branch_2, 96, 3) branch_2 = conv2d_bn(branch_2, 96, 3) branch_pool = AveragePooling2D(3, strides=1, padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1) branches = [branch_0, branch_1, branch_2, branch_pool] channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 x = Concatenate(axis=channel_axis, name='mixed_5b')(branches) # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320 for block_idx in range(1, 11): x = inception_resnet_block(x, scale=0.17, block_type='block35', block_idx=block_idx) # Mixed 6a (Reduction-A block): 17 x 17 x 1088 branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid') branch_1 = conv2d_bn(x, 256, 1) branch_1 = conv2d_bn(branch_1, 256, 3) branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid') branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x) branches = [branch_0, branch_1, branch_pool] x = Concatenate(axis=channel_axis, name='mixed_6a')(branches) # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088 for block_idx in range(1, 21): x = inception_resnet_block(x, scale=0.1, block_type='block17', block_idx=block_idx) # Mixed 7a (Reduction-B block): 8 x 8 x 2080 branch_0 = conv2d_bn(x, 256, 1) branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid') branch_1 = conv2d_bn(x, 256, 1) branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid') branch_2 = conv2d_bn(x, 256, 1) branch_2 = conv2d_bn(branch_2, 288, 3) branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid') branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='mixed_7a')(branches) # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080 for block_idx in range(1, 10): x = inception_resnet_block(x, scale=0.2, block_type='block8', block_idx=block_idx) x = inception_resnet_block(x, scale=1., activation=None, block_type='block8', block_idx=10) # Final convolution block: 8 x 8 x 1536 x = conv2d_bn(x, 1536, 1, name='conv_7b') if include_top: # Classification block 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='inception_resnet_v2') # Load weights if weights == 'imagenet': if K.image_data_format() == '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.') if include_top: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', file_hash='e693bd0210a403b3192acc6073ad2e96') else: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', file_hash='d19885ff4a710c122648d3b5c3b684e4') model.load_weights(weights_path) 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
(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 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
""" ### Shared vision model This model re-uses the same image-processing module on two inputs, to classify whether two MNIST digits are the same digit or different digits. """ from tensorflow.contrib.keras.python.keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten, concatenate from tensorflow.contrib.keras.python.keras.models import Model # First, define the vision modules # digit_input = Input(shape=(1, 27, 27)) digit_input = Input(shape=(27, 27, 1)) x = Conv2D(64, (3, 3))(digit_input) # padding 'valid' or 'same' affect shape x = Conv2D(64, (3, 3))(x) # padding='same', can keep image size the same x = MaxPooling2D((2, 2))(x) # downscale image size out = Flatten()(x) vision_model = Model(digit_input, out) # Then define the tell-digits-apart model # digit_a = Input(shape=(1, 27, 27)) # digit_b = Input(shape=(1, 27, 27)) digit_a = Input(shape=(27, 27, 1)) digit_b = Input(shape=(27, 27, 1)) # The vision model will be shared, weights and all out_a = vision_model(digit_a) out_b = vision_model(digit_b) concatenated = concatenate([out_a, out_b]) # cbind tensors
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')
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. """ 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(), 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) # 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 = 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 = 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 = 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='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') 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 nn_base(input_tensor=None, trainable=False): # Determine proper input shape input_shape = (None, None, 3) if input_tensor is None: img_input = Input(shape=input_shape) else: if not is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 x = ZeroPadding2D((3, 3))(img_input) x = Convolution2D(64, (7, 7), strides=(2, 2), name='conv1', trainable=trainable)(x) x = FixedBatchNormalization(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), trainable=trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', trainable=trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', trainable=trainable) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', trainable=trainable) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', trainable=trainable) return x