def atari_qnet(input_shape, num_actions, net_name, net_size): net_name = net_name.lower() # input state state = Input(shape=input_shape) # convolutional layers conv1_32 = Conv2D(32, (8, 8), strides=(4, 4), activation='relu') conv2_64 = Conv2D(64, (4, 4), strides=(2, 2), activation='relu') conv3_64 = Conv2D(64, (3, 3), strides=(1, 1), activation='relu') # if recurrent net then change input shape if 'drqn' in net_name: # recurrent net (drqn) lambda_perm_state = lambda x: K.permute_dimensions(x, [0, 3, 1, 2]) perm_state = Lambda(lambda_perm_state)(state) dist_state = Lambda(lambda x: K.stack([x], axis=4))(perm_state) # extract features with `TimeDistributed` wrapped convolutional layers dist_conv1 = TimeDistributed(conv1_32)(dist_state) dist_conv2 = TimeDistributed(conv2_64)(dist_conv1) dist_convf = TimeDistributed(conv3_64)(dist_conv2) feature = TimeDistributed(Flatten())(dist_convf) elif 'dqn' in net_name: # fully connected net (dqn) # extract features with convolutional layers conv1 = conv1_32(state) conv2 = conv2_64(conv1) convf = conv3_64(conv2) feature = Flatten()(convf) # network type. Dense for dqn; LSTM or GRU for drqn if 'lstm' in net_name: net_type = LSTM elif 'gru' in net_name: net_type = GRU else: net_type = Dense # dueling or regular dqn/drqn if 'dueling' in net_name: value1 = net_type(net_size, activation='relu')(feature) adv1 = net_type(net_size, activation='relu')(feature) value2 = Dense(1)(value1) adv2 = Dense(num_actions)(adv1) mean_adv2 = Lambda(lambda x: K.mean(x, axis=1))(adv2) ones = K.ones([1, num_actions]) lambda_exp = lambda x: K.dot(K.expand_dims(x, axis=1), -ones) exp_mean_adv2 = Lambda(lambda_exp)(mean_adv2) sum_adv = add([exp_mean_adv2, adv2]) exp_value2 = Lambda(lambda x: K.dot(x, ones))(value2) q_value = add([exp_value2, sum_adv]) else: hid = net_type(net_size, activation='relu')(feature) q_value = Dense(num_actions)(hid) # build model return Model(inputs=state, outputs=q_value)
def classifier(base_layers, input_rois, num_rois, nb_classes=21, trainable=False): # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround pooling_regions = 14 input_shape = (num_rois, 14, 14, 1024) out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois]) out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True) out = TimeDistributed(Flatten())(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * (nb_classes - 1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def init_model(self): with tf.variable_scope('resnet'): x = self.input_img x = tf.cast(x, tf.float32) / 255 #split (1,2,3) x = self.split_block(x, 's1') #4 x = self.conv_block(x, 3, [32, 32, 128], stage=4, block='a') x = self.identity_block(x, 3, [32, 32, 128], stage=4, block='b') x = self.identity_block(x, 3, [32, 32, 128], stage=4, block='c') x = self.identity_block(x, 3, [32, 32, 128], stage=4, block='d') x = self.identity_block(x, 3, [32, 32, 128], stage=4, block='e') x = self.identity_block(x, 3, [32, 32, 128], stage=4, block='f') #5 x = self.conv_block(x, 3, [64, 64, 256], stage=5, block='a') x = self.identity_block(x, 3, [64, 64, 256], stage=5, block='b') x = self.identity_block(x, 3, [64, 64, 256], stage=5, block='c') #pool x = AveragePooling3D((7, 4, 2), name='avg_pool')(x) x = Flatten()(x) self.output_feature = x logger.info('feature shape:{}'.format(self.output_feature.shape)) #fc x = Dense(self.args.classes, activation='softmax', name='fc')(x) self.output = x logger.info('network inited!')
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 atari_acnet(input_shape, num_actions, net_name, net_size): net_name = net_name.lower() # input state state = Input(shape=input_shape) # convolutional layers conv1_32 = Conv2D(32, (8, 8), strides=(4, 4), activation='relu') conv2_64 = Conv2D(64, (4, 4), strides=(2, 2), activation='relu') conv3_64 = Conv2D(64, (3, 3), strides=(1, 1), activation='relu') # if recurrent net then change input shape if 'lstm' in net_name or 'gru' in net_name: # recurrent net lambda_perm_state = lambda x: K.permute_dimensions(x, [0, 3, 1, 2]) perm_state = Lambda(lambda_perm_state)(state) dist_state = Lambda(lambda x: K.stack([x], axis=4))(perm_state) # extract features with `TimeDistributed` wrapped convolutional layers dist_conv1 = TimeDistributed(conv1_32)(dist_state) dist_conv2 = TimeDistributed(conv2_64)(dist_conv1) dist_convf = TimeDistributed(conv3_64)(dist_conv2) feature = TimeDistributed(Flatten())(dist_convf) # specify net type for the following layer if 'lstm' in net_name: net_type = LSTM elif 'gru' in net_name: net_type = GRU elif 'fully connected' in net_name: # fully connected net # extract features with convolutional layers conv1 = conv1_32(state) conv2 = conv2_64(conv1) convf = conv3_64(conv2) feature = Flatten()(convf) # specify net type for the following layer net_type = Dense # actor (policy) and critic (value) stream hid = net_type(net_size, activation='relu')(feature) logits = Dense(num_actions, kernel_initializer='zeros')(hid) value = Dense(1)(hid) # build model return Model(inputs=state, outputs=[value, logits])
def build_read_tensor_2d_model(args): '''Build Read Tensor 2d CNN model for classifying variants. 2d Convolutions followed by dense connection. Dynamically sets input channels based on args via defines.total_input_channels_from_args(args) Uses the functional API. Supports theano or tensorflow channel ordering. Prints out model summary. Arguments args.window_size: Length in base-pairs of sequence centered at the variant to use as input. args.labels: The output labels (e.g. SNP, NOT_SNP, INDEL, NOT_INDEL) args.channels_last: Theano->False or Tensorflow->True channel ordering flag Returns The keras model ''' if args.channels_last: in_shape = (args.read_limit, args.window_size, args.channels_in) else: in_shape = (args.channels_in, args.read_limit, args.window_size) read_tensor = Input(shape=in_shape, name="read_tensor") read_conv_width = 16 x = Conv2D(128, (read_conv_width, 1), padding='valid', activation="relu", kernel_initializer="he_normal")(read_tensor) x = Conv2D(64, (1, read_conv_width), padding='valid', activation="relu", kernel_initializer="he_normal")(x) x = MaxPooling2D((3, 1))(x) x = Conv2D(64, (1, read_conv_width), padding='valid', activation="relu", kernel_initializer="he_normal")(x) x = MaxPooling2D((3, 3))(x) x = Flatten()(x) x = Dense(units=32, kernel_initializer='normal', activation='relu')(x) prob_output = Dense(units=len(args.labels), kernel_initializer='normal', activation='softmax')(x) model = Model(inputs=[read_tensor], outputs=[prob_output]) adamo = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=1.) my_metrics = [metrics.categorical_accuracy] model.compile(loss='categorical_crossentropy', optimizer=adamo, metrics=my_metrics) model.summary() if os.path.exists(args.weights_hd5): model.load_weights(args.weights_hd5, by_name=True) print('Loaded model weights from:', args.weights_hd5) return model
def 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 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 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 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 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 discriminator(X_dim, y_dim): df_dim = 64 x_in = Input(shape=(X_dim), name='X_input') y_in = Input(shape=(y_dim, ), name='Y_input') D_h = LeakyReLU(0.2)(Convolution2D(df_dim, kernel_size=(5, 5), strides=(2, 2), padding='same')(x_in)) D_h = LeakyReLU(0.2)(BatchNormalization()(Convolution2D( df_dim * 2, kernel_size=(5, 5), strides=(2, 2), padding='same')(D_h))) D_h = LeakyReLU(0.2)(BatchNormalization()(Convolution2D( df_dim * 2, kernel_size=(5, 5), strides=(2, 2), padding='same')(D_h))) D_h = LeakyReLU(0.2)(BatchNormalization()(Convolution2D( df_dim * 4, kernel_size=(5, 5), strides=(2, 2), padding='same')(D_h))) D_h = LeakyReLU(0.2)(BatchNormalization()(Convolution2D( df_dim * 8, kernel_size=(5, 5), strides=(2, 2), padding='same')(D_h))) D_h = Flatten()(D_h) D_h = concatenate([D_h, y_in]) D_logit = Dense(1, name='Discriminator_Output')(D_h) D = Model(inputs=[x_in, y_in], outputs=D_logit, name='Discriminator') print('=== Discriminator ===') D.summary() print('\n\n') return D
def create_VGG16(num_fc_neurons, dropout_rate, num_classes=24, top_model_weights_path=None, img_height=224, img_width=224, include_loc='all', activation='softmax'): # load pre-trained convolutional model base_model = VGG16(weights='imagenet', include_top=False, input_shape=get_input_shape(img_height, img_width)) # 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, 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, name='predictions')) if top_model_weights_path != None: top_model.load_weights(top_model_weights_path) 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
def discriminator(X_dim): # It must be Auto-Encoder style architecture # Architecture : (64)4c2s-FC32_BR-FC64*14*14_BR-(1)4dc2s_S x_in = Input(shape=X_dim, name='X_input') D_h = Activation('relu')(Convolution2D(64, kernel_size=(4, 4), strides=(2, 2), padding='same', name='d_conv1')(x_in)) D_h = Flatten()(D_h) code = Activation('relu')(BatchNormalization()(Dense(32)(D_h))) D_h = Activation('relu')(BatchNormalization()(Dense(64 * 14 * 14)(code))) D_h = Reshape(target_shape=[14, 14, 64])(D_h) out = Conv2DTranspose(1, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='sigmoid')(D_h) D_model = Model(inputs=x_in, outputs=[out, code]) # recon loss # recon_error = tf.sqrt(2 * tf.nn.l2_loss(out - x_in)) / self.batch_size # return out, recon_error, code D_model.summary() return D_model
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) # Next, let's define a language model to encode the question into a vector. # Each question will be at most 100 word long, # and we will index words as integers from 1 to 9999. question_input = Input(shape=(100, ), dtype='int32') embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)( question_input) # (?, 100, 256) encoded_question = LSTM(256)(embedded_question) # (?, 256)
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
np.save(os.path.join(SAVE_DIR, 'labels_' + str(i) + '_' + str(lendata) + '.npy'), labels[i:lendata]) np.save(os.path.join(SAVE_DIR, 'data_' + str(lendata-1) + '_' + str(lendata) + '.npy'), data[lendata-1:lendata]) np.save(os.path.join(SAVE_DIR, 'gens_' + str(lendata-1) + '_' + str(lendata) + '.npy'), data_gen[lendata-1:lendata]) np.save(os.path.join(SAVE_DIR, 'labels_' + str(lendata-1) + '_' + str(lendata) + '.npy'), labels[lendata-1:lendata]) tokenizer = None data= None labels=None embedding_layer_gen = Embedding(len(word_index_gen) + 1, 300, input_length=2) sequence_input_gen = Input(shape=(2,), dtype='int32') embedded_sequences_gen = embedding_layer_gen(sequence_input_gen) x_gen = Dropout (0.8)(embedded_sequences_gen) x_gen = Flatten()(x_gen) x_gen = Dense(512, activation='relu')(x_gen) embedding_layer = Embedding(len(word_index) + 1, EMBEDDING_DIM, weights=[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False) sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32') embedded_sequences = embedding_layer(sequence_input) x = Conv1D(128, 15, activation='relu')(embedded_sequences) x = MaxPooling1D(5)(x) x = Conv1D(128, 5, activation='relu')(x)
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 cnn_sentiment_model(inputs, nb_words, embedding_dim=300, static_embedding=True, embedding_weights=None, filter_hs=None, nb_filters=100, emb_size=100, hidden_dropout=0.2, is_training=True, augmentation_function=None, l2_weight=1e-4, img_shape=None, new_shape=None, image_summary=False, batch_norm_decay=0.99, seed=0, embedding_dropout=0.2): from tensorflow.contrib.keras.python.keras.layers import Embedding, Input, Convolution1D, MaxPooling1D, Flatten, \ Dense, Dropout, Activation from tensorflow.contrib.keras.python.keras.initializers import glorot_uniform from tensorflow.contrib.keras.python.keras.layers.merge import Concatenate from tensorflow.contrib.keras.python.keras import backend as K K.set_learning_phase(1 if is_training else 0) sequence_length = img_shape[0] if filter_hs is None: filter_hs = [3, 4, 5] model = inputs def ci(shape, dtype=None, partition_info=None): assert shape[0] == embedding_weights.shape[0] and shape[ 1] == embedding_weights.shape[ 1], 'Shapes are not equal required={} init value={}'.format( shape, embedding_weights.shape) return embedding_weights model = Embedding(nb_words, embedding_dim, input_length=sequence_length, trainable=(not static_embedding), embeddings_initializer='uniform' if embedding_weights is None else ci)(model) if embedding_dropout > 0.0: model = Dropout(embedding_dropout, seed=seed)(model, training=is_training) convs = list() for fsz in filter_hs: conv = Convolution1D( filters=nb_filters, kernel_size=fsz, padding='valid', activation='relu', kernel_initializer=glorot_uniform(seed=seed))(model) pool = MaxPooling1D(pool_size=sequence_length - fsz + 1)(conv) flatten = Flatten()(pool) convs.append(flatten) if len(filter_hs) > 0: graph_out = Concatenate()(convs) else: graph_out = convs[0] model = graph_out model = Dense(emb_size, kernel_initializer=glorot_uniform(seed=seed))(model) model = Dropout(hidden_dropout, seed=seed)(model, training=is_training) model = Activation('relu')(model) 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 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, 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: 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') 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) 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 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 out = Dense(1, activation='sigmoid')(concatenated)
2, padding='same', kernel_regularizer=l2(0.005), use_bias=False)) model.add(Activation("relu")) model.add(Dropout(0.3)) model.add( Convolution2D(1024, (2, 2), 2, padding='same', kernel_regularizer=l2(0.005), use_bias=False)) model.add(Activation("relu")) model.add(Flatten()) model.add(Dense(512, kernel_regularizer=l2(0.05))) model.add(Activation("relu")) model.add(Dropout(0.3)) model.add(Dense(128, kernel_regularizer=l2(0.05))) model.add(BatchNormalization(axis=-1)) model.add(Dropout(0.6)) model.add(Dense(2)) model.add(Activation("softmax")) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.summary() csv_logger = CSVLogger('training.log')
input_length=MAX_SEQUENCE_LENGTH, trainable=False) sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32') embedded_sequences = embedding_layer(sequence_input) x = Conv1D(256, 5, activation='relu')(embedded_sequences) x = MaxPooling1D(2)(x) x = Conv1D(128, 5, activation='relu')(x) x = MaxPooling1D(5)(x) x = Conv1D(128, 5, activation='relu')(x) x = MaxPooling1D(5)(x) x = Conv1D(128, 5, activation='relu')(x) x = MaxPooling1D(5)(x) x = Conv1D(128, 5, activation='relu')(x) x = MaxPooling1D(35)(x) # global max pooling x = Flatten()(x) x = Dense(512, activation='relu')(x) preds = Dense(len(labels_index), activation='softmax')(x) model = Model(sequence_input, preds) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) embedding_matrix = None embeddings_index = None for epoch in range(NUM_EPOCHS): i = 0 for j in range(NUM_ROWS_SAVE_TO_TRAIN - NUM_ROWS_SAVE_TO_VAL, lendata, NUM_ROWS_SAVE_TO_TRAIN - NUM_ROWS_SAVE_TO_VAL):
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
) # output tensor is (?, text_input_length, embedding_dim) == (?, 1000, 100) == each sample text has 1000 words to describe, each word has 100 dims to describe x = Conv1D(128, 5, activation='relu')( embedded_sequences ) # (?, 996, 128), 996== 1000-4; 1D convolution layer (e.g. temporal convolution). This layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs. # maybe, Conv1D is like a single vector or one line of 5 pixel screener, moving from left to right, and from row to row # conv1d_t1 = x x = MaxPooling1D(5)(x) # (?, 199, 128), 199=(996-1)/5 # maxp_t1 = x x = Conv1D(128, 5, activation='relu')(x) # (?, 195, 128), 195=199-4 # conv1d_t2 = x x = MaxPooling1D(5)(x) # (?, 39, 128), 39 == 195/5 # maxp_t2 = x x = Conv1D(128, 5, activation='relu')(x) # (?, 35, 128) # conv1d_t3 = x x = MaxPooling1D(35)(x) # (?, 1, 128) # maxp_t3 = x x = Flatten()(x) # (?, ?) not (?, 128) ?? # flatten_t = x x = Dense(128, activation='relu')(x) # (?, 128) # dense_t = x preds = Dense(len(labels_index), activation='softmax')(x) # (?, 20) model = Model(sequence_input, preds) # a number of samples as input, preds as output tensors model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) model.summary( ) # see layer output tensor shape and total params of each layer # model.layers[2].count_params() # calculate each layer's params # pp model.trainable_weights # display weights shape of each trainable layer """