def headless_model(input_shape): """ Return pre-trained mobilenet Keras Model with no top layers. """ model = MobileNet( input_shape=input_shape, alpha=1.0, depth_multiplier=1 #, dropout=1e-3 , include_top=False, weights='imagenet', input_tensor=None, pooling='max') model.preprocess_input = preprocess_input return model
def build(config): image_width = config['image_processing']['image_width'] image_height = config['image_processing']['image_height'] image_channels = config['image_processing']['image_channels'] number_of_classes = config['dataset']['number_of_classes'] model_file = config['model'].get('model_file', None) regularization = config['hyper_parameters'].get('activity_regularizer', None) weights = config['model'].get('weights', None) print("weights:", weights) if weights == 'imagenet': base_model = MobileNetV2(input_shape=(image_width, image_height, image_channels), weights='imagenet', include_top=False) else: base_model = MobileNetV2(input_shape=(image_width, image_height, image_channels), weights=None, include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) if regularization is not None: regularization = getattr( importlib.import_module(f'keras.regularizers'), regularization['name']) regularization = regularization( **config['hyper_parameters']['activity_regularizer']['params']) predictions = Dense(activity_regularizer=regularization, units=number_of_classes, activation='softmax', name='predictions')(x) model = Model(inputs=base_model.input, outputs=predictions) if weights != 'imagenet' and weights is not None: print(weights, weights is not 'imagenet') model.load_weights(weights) return model
def __init__(self): self.input_size = 96 # base = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights=os.path.dirname( # __file__)+'/weight/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_96_no_top.h5') # top_layer = GlobalAveragePooling2D()(base.output) base = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights='imagenet') top_layer = GlobalAveragePooling2D()(base.output) gender_FC = Dense(128, name='gender_FC', activation='relu')(top_layer) gender_layer = Dense(2, activation='softmax', name='gender_prediction')(gender_FC) age_FC = Dense(128, name='age_FC', activation='relu')(top_layer) age_layer = Dense(8, activation='softmax', name='age_prediction')(age_FC) emotion_FC = Dense(128, name='emotion_FC', activation='relu')(top_layer) emotion_layer = Dense(8, activation='softmax', name='emotion_prediction')(emotion_FC) super().__init__(inputs=base.input, outputs=[emotion_layer, gender_layer, age_layer], name='AgenderNetMobileNetV2')
def build_model(target_size): input_tensor = Input(shape=(target_size, target_size, 3)) base_model = MobileNetV2( include_top=True, weights='imagenet', input_tensor=input_tensor, input_shape=(target_size, target_size, 3), pooling='max') for layer in base_model.layers: layer.trainable = True # trainable has to be false in order to freeze the layers op = Dense(256, activation='relu')(base_model.output) op = Dropout(.5)(op) ## # softmax: calculates a probability for every possible class. # # activation='softmax': return the highest probability; # for example, if 'Coat' is the highest probability then the result would be # something like [0,0,0,0,1,0,0,0,0,0] with 1 in index 5 indicate 'Coat' in our case. ## output_tensor = Dense(10, activation='softmax')(op) model = Model(inputs=input_tensor, outputs=output_tensor) learning_rate = 0.0001 epoch = 10 decay_rate = learning_rate / epoch adam = keras.optimizers.Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=None, decay=decay_rate, amsgrad=False) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) return model
def create_model(self, num_of_category): #arcface インスタンスを作る arcfacelayer = arcface.Arcfacelayer(5, 30, 0.1) #trainモデルを作成 & 重みをロード base_model = MobileNetV2(input_shape=(224, 224, 3), weights='imagenet', include_top=False) # add new layers instead of FC networks x = base_model.output y_input = Input(shape=(num_of_category, )) # stock hidden model hidden = GlobalAveragePooling2D()(x) # stock Feature extraction #x = Dropout(0.5)(hidden) x = arcfacelayer([hidden, y_input]) # x = Dense(1024,activation='relu')(x) pred = Activation('softmax')(x) arcface_model = Model(inputs=[base_model.input, y_input], outputs=pred) arcface_model.load_weights(MODEL_WEIGHT_PATH) #Predictionを作成(arcfaceを切り離す) self.model = Model( arcface_model.get_layer(index=0).input, arcface_model.get_layer(index=-4).output) self.model.summary() return
def test(model_path: str, data_path, out_txt="confusion.csv"): savez = np.load(data_path) x_train = savez["x_train"] y_train = savez["y_train"] x_test = savez["x_test"] y_test = savez["y_test"] out_classes = savez["out_classes"] # reload model base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(96, 96, 3), pooling='avg') predictions = Dense(len(out_classes), activation='softmax')(base_model.outputs[0]) model = Model(inputs=base_model.input, outputs=predictions) model.load_weights(model_path) # predict on testing pred = model.predict(x=x_test) y_pred = np.argmax(pred, axis=1) y_test = np.argmax(y_test, axis=1) confusion = confusion_matrix(y_test, y_pred, [i for i in range(len(out_classes))]) df = pd.DataFrame(data=confusion, index=out_classes, columns=out_classes) df.to_csv(out_txt) plt.figure(figsize=confusion.shape) sn.heatmap(df, annot=True, fmt="d") plt.show()
def create_model(input_shape, num_class, k): fgc_base = MobileNetV2(input_shape=input_shape, include_top=False, weights=None, alpha=1.) fgc_base.trainable = True # fgc_base.summary() feature2 = fgc_base.get_layer("block_11_expand_relu").output fc_model = Model(fgc_base.inputs[0], [fgc_base.output, feature2]) fc_model.summary() input_tensor = Input(shape=input_shape) input_tensor_bn = BatchNormalization()(input_tensor) features = fc_model(input_tensor_bn) fc_obj = GlobalMaxPool2D()(features[0]) fc_obj = Dropout(0.7)(fc_obj) fc_obj = Dense(num_class, activation="softmax")(fc_obj) fc_part = Conv2D(filters=num_class * k, kernel_size=(1, 1), activation="relu")(features[1]) fc_part = GlobalMaxPool2D()(fc_part) fc_part = Dropout(0.5)(fc_part) fc_ccp = Lambda(lambda tmp: tf.expand_dims(tmp, axis=-1))(fc_part) fc_ccp = AvgPool1D(pool_size=k)(fc_ccp) fc_ccp = Lambda(lambda tmp: tf.squeeze(tmp, [-1]))(fc_ccp) fc_ccp = Activation(activation="softmax")(fc_ccp) fc_part = Dense(num_class, activation="softmax")(fc_part) output = Concatenate(axis=-1)([fc_obj, fc_part, fc_ccp]) return Model(input_tensor, output)
def mobilenet_v2(nb_classes): from keras.applications.mobilenetv2 import MobileNetV2, preprocess_input K.clear_session() input_tensor = Input(shape = (100,100,3)) model = MobileNetV2(input_tensor = input_tensor, weights = 'imagenet', include_top = False) for layer in model.layers: layer.trainable = False x = Flatten()(model.output) prediction = Dense(nb_classes, activation = 'softmax')(x) model_final = Model(inputs = model.input, outputs = prediction) model_final.compile(loss = 'categorical_crossentropy', optimizer = 'rmsprop', metrics = ['accuracy']) gen = ImageDataGenerator( featurewise_center=True, samplewise_center=True, rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.1, horizontal_flip=True, vertical_flip=True, featurewise_std_normalization=True, samplewise_std_normalization=True, preprocessing_function=preprocess_input, validation_split = 0.8 ) return model_final, gen
def getModelMobileNet0(size_row, size_column, labels): """重新训练mobileNet""" print("Model building!") inp = Input(shape=(size_row, size_column, 3)) mn = MobileNetV2( include_top=False, weights='imagenet', input_tensor=inp, input_shape=(size_row, size_column, 3), pooling='avg') for layer in mn.layers: layer.trainable = True # trainable has to be false in order to freeze the layers mn_out = mn.output x = Dense(256, activation='relu')(mn_out) x = Dropout(0.2)(x) outp = Dense(labels, activation='softmax')(x) model = Model(inputs=inp, outputs=outp) # model.summary() # 编译模型 model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy']) print("Model built!") return model
def Image_Classification_model(lr=0.005, decay=1e-6, momentum=0.9, nb_classes=2, img_rows=50, img_cols=50, RGB=True): if (RGB == True): color = 3 elif (RGB == False): color = 1 base_model = MobileNetV2(include_top=False, input_shape=(img_rows, img_cols, color), classes=nb_classes) x = base_model.output # let's add a fully-connected layer x = Flatten()(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(nb_classes, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) sgd = SGD(lr=lr, decay=decay, momentum=momentum, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd) return model
def yolo_model(input_shape): inp = Input(input_shape) model = MobileNetV2(input_tensor=inp, include_top=False, weights='imagenet') last_layer = model.output conv = Conv2D(512, (3, 3), activation='relu', padding='same')(last_layer) conv = Dropout(0.4)(conv) bn = BatchNormalization()(conv) lr = LeakyReLU(alpha=0.1)(bn) conv = Conv2D(128, (3, 3), activation='relu', padding='same')(lr) conv = Dropout(0.4)(conv) bn = BatchNormalization()(conv) lr = LeakyReLU(alpha=0.1)(bn) conv = Conv2D(5, (3, 3), activation='relu', padding='same')(lr) final = Reshape((grid_h, grid_w, classes, info))(conv) model = Model(inp, final) return model
def mobileNet_model(input_shape, classes): Input_shape = Input(shape=input_shape) mobilenet = MobileNetV2(include_top=False, alpha=1.0, weights='imagenet', pooling='avg') #mobilenet.trainable = False for layer in mobilenet.layers[:72]: layer.trainable = False if "bn" in layer.name: layer.trainable = True for layer in mobilenet.layers[72:]: layer.trainable = True mobilenet.summary() x_in = Input_shape x = mobilenet(x_in) x = Dense(128, activation='relu')(x) x = Dropout(0.3)(x) x = Dense(classes, activation='softmax')(x) model = Model(x_in, x) model.summary() return model
def __init__(self): self.input_size = 96 input_shape = (96, 96, 3) image1_batch = Input(shape=input_shape, name='in_t1') image2_batch = Input(shape=input_shape, name='in_t2') base = MobileNetV2(input_shape=input_shape, include_top=False, weights='imagenet') top_layer = GlobalAveragePooling2D()(base.output) inter_model = Model(inputs=base.input, output=top_layer) common1_feat = inter_model(image1_batch) common2_feat = inter_model(image2_batch) emotion_FC = Dense(128, name='emotion_FC', activation='relu')(common1_feat) emotion_out = Dense(8, name='emotion_prediction', activation='softmax')(emotion_FC) gender_FC = Dense(128, name='gender_FC', activation='relu')(common2_feat) gender_out = Dense(2, name='gender_prediction', activation='softmax')(gender_FC) age_FC = Dense(128, name='age_FC', activation='relu')(common2_feat) age_out = Dense(8, name='age_prediction', activation='softmax')(age_FC) super().__init__(inputs=[image1_batch, image2_batch], outputs=[emotion_out, gender_out, age_out], name='multitask_two_input_MobileNetV2')
def __init__(self): self.input_size = 224 base = MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet') top_layer = GlobalAveragePooling2D()(base.output) # landmark_FC = Dense(128,name='landmark_FC', activation="relu")(top_layer) # landmark_out = Dense(10,name='landmark_prediction',activation='relu')(landmark_FC) gender_FC = Dense(128, name='gender_FC', activation="relu")(top_layer) gender_out = Dense(2, activation='softmax', name='gender_prediction')(gender_FC) smile_FC = Dense(128, name='smile_FC', activation="relu")(top_layer) smile_out = Dense(2, name='smile_prediction', activation='softmax')(smile_FC) pose_FC = Dense(128, name='pose_FC', activation="relu")(top_layer) pose_out = Dense(5, name='pose_prediction', activation='softmax')(pose_FC) super().__init__(inputs=base.input, outputs=[pose_out, gender_out, smile_out], name='MTFLMobileNetV2')
def create_mobilenetv2(input_shape, alpha=1., depth_multiplier=1, l2_reg=0.001, seed=None): """ MobileNetv2 creation with two outputs :param input_shape: input image shape :param alpha: mobilenet width (channels) multiplier :param depth_multiplier: mobilenet depth (height and width of feature maps) multiplie :param l2_reg: l2 regularization :param seed: random state :return: keras model """ model_base = MobileNetV2(input_shape=input_shape, alpha=alpha, depth_multiplier=depth_multiplier, include_top=False, weights='imagenet', pooling='max') smile_branch = _create_branch(model_base.output, l2_reg, seed, name='smile_output') open_mouth_branch = _create_branch(model_base.output, l2_reg, seed, name='open_mouth_output') model = Model(model_base.input, [smile_branch, open_mouth_branch]) return model
def execute_learning_mobilenetv2(alpha, depth_multiplier): model = MobileNetV2(alpha=alpha, depth_multiplier=depth_multiplier, include_top=True, weights=None, classes=CLASSES) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) tensorboard = keras.callbacks.TensorBoard(log_dir=LOG_DIR+'/{}_{}'.format(alpha, depth_multiplier)) csv_logger = keras.callbacks.CSVLogger('./csv/log_{}_{}.csv'.format(alpha, depth_multiplier)) fit_result = model.fit( x=X_train, y=Y_train, epochs=EPOCHS, validation_split=VALIDATION_SPLIT, verbose=2, callbacks=[tensorboard, csv_logger] ) test_result = model.evaluate( x=X_test, y=Y_test ) print('-----------------------------------------------') print('Test Result:') print('alpha = {}, depth_multiplier = {}'.format(alpha, depth_multiplier)) print('Loss = {}, Accuracy = {}'.format(*test_result)) model.save('keyakizaka_member_detection_mobilenetv2_{}_{}.h5'.format(alpha, depth_multiplier))
def get_model(alpha=1, depth_multiplier=1, pooling='avg', lr=0.00001): base_mobilenetv2_model = MobileNetV2(alpha=alpha, depth_multiplier=depth_multiplier, input_shape=(224, 224, 1), include_top=False, weights=None, classes=1, pooling=pooling) top_model = Sequential() top_model.add(Dense(512)) top_model.add(Dropout(0.5)) top_model.add(Dense(1, activation='sigmoid')) # Create model. model = Model(inputs=base_mobilenetv2_model.input, outputs=top_model(base_mobilenetv2_model.output)) optimizer = Adam(lr=lr) model.compile( optimizer=optimizer, loss='binary_crossentropy', metrics=[keras.metrics.binary_accuracy, keras_metrics.precision()]) return model
def build_model(): input_tensor = Input(shape=(target_size, target_size, 3)) base_model = MobileNetV2(include_top=False, weights='imagenet', input_tensor=input_tensor, input_shape=(target_size, target_size, 3), pooling='avg') for layer in base_model.layers: layer.trainable = True # trainable has to be false in order to freeze the layers op = Dense(256, activation='relu')(base_model.output) op = Dropout(.25)(op) ## # softmax: calculates a probability for every possible class. # # activation='softmax': return the highest probability; # for example, if 'Coat' is the highest probability then the result would be # something like [0,0,0,0,1,0,0,0,0,0] with 1 in index 5 indicate 'Coat' in our case. ## output_tensor = Dense(20, activation='softmax')(op) model = Model(inputs=input_tensor, outputs=output_tensor) return model
def mobilenetv2_yolo_body(inputs, num_anchors, num_classes, alpha=0.75): mobilenetv2 = MobileNetV2(alpha=alpha, input_tensor=inputs, include_top=False, weights='imagenet') # 'imagenet' x = mobilenetv2.output x, y1 = make_mobilenet_last_layers(x, alpha, 512, num_anchors * (num_classes + 5)) x = compose(MobilenetConv2D_BN_Relu((1, 1), alpha, 256), UpSampling2D(2))(x) f2 = mobilenetv2.get_layer('block_12_project_BN').output x = Concatenate()([x, f2]) x, y2 = make_mobilenet_last_layers(x, alpha, 256, num_anchors * (num_classes + 5)) x = compose(MobilenetConv2D_BN_Relu((1, 1), alpha, 128), UpSampling2D(2))(x) f3 = mobilenetv2.get_layer("block_5_project_BN").output x = Concatenate()([x, f3]) x, y3 = make_mobilenet_last_layers(x, alpha, 128, num_anchors * (num_classes + 5)) # x = Concatenate()( # [x, MobilenetConv2D_BN_Relu((1, 1), alpha, 640)(mobilenetv2.get_layer('block_12_project_BN').output)]) # y2 = MobilenetConv2D_BN_Relu((1, 1), alpha, 640)(x) # y2 = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), padding='same')(y2) # x = compose( # MobilenetConv2D_BN_Relu((1, 1), alpha, 320), # tf.keras.layers.UpSampling2D(2))(x) # x = tf.keras.layers.Concatenate()( # [x, MobilenetConv2D_BN_Relu((1, 1), alpha, 320)(mobilenetv2.get_layer('block_5_project_BN').output)]) # y3 = MobilenetConv2D_BN_Relu((1, 1), alpha, 320)(x) # y3 = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), padding='same')(y3) return Model(inputs, [y1, y2, y3])
def create_mel_and_pca_model(): inp1 = Input(shape=(64, None, 1), name='mel') x = BatchNormalization()(inp1) x = Conv2D(10, kernel_size=(1, 1), padding='same', activation='relu')(x) x = Conv2D(3, kernel_size=(1, 1), padding='same', activation='relu')(x) mn = MobileNetV2(include_top=False) mn.layers.pop(0) mn_out = mn(x) x = GlobalAveragePooling2D()(mn_out) inp2 = Input(shape=(350,), name='pca') y = BatchNormalization()(inp2) x = concatenate([x, y], axis=-1) x = Dense(1536, activation='relu')(x) x = BatchNormalization()(x) x = Dense(384, activation='relu')(x) x = BatchNormalization()(x) x = Dense(41, activation='softmax')(x) model = Model(inputs=[inp1, inp2], outputs=x) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.0001), metrics=['accuracy']) return model
def build_model(self): weight_decay = self.config.model.weight_decay classes = self.config.model.classes alpha = self.config.model.alpha, dropout = self.config.model.dropout input_shape = (self.config.data_loader.image_size, self.config.data_loader.image_size, 3) base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=input_shape, alpha=alpha) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dropout(dropout)(x) logits = Dense( 10, kernel_regularizer=keras.regularizers.l2(weight_decay))(x) probabilities = Activation('softmax')(logits) self.model = Model(base_model.input, probabilities, name='mobilenet') self.model.compile(optimizer=optimizers.SGD(lr=1e-2, momentum=0.9, nesterov=True), loss='categorical_crossentropy', metrics=['accuracy', 'top_k_categorical_accuracy'])
def mobilenetv2_model_maker(): input_tensor = Input(shape=(img_rows, img_cols, 3)) mobile = MobileNetV2(include_top=False, weights='imagenet', input_tensor=input_tensor) x = Flatten()(mobile.output) x = BinaryDense(dim_mid_layer, H=1, use_bias=False)(x) x = Dropout(0.25)(x) if bn: x = BatchNormalization()(x) x = Activation(binary_tanh)(x) inputsub = Input(name='sub', shape=(dim_mid_layer, )) x = Add(name='mid2')([x, inputsub]) x = Dense(sec_dim, kernel_initializer=init_dence)(x) x = Dropout(0.25)(x) if bn: x = BatchNormalization()(x) x = Activation('relu')(x) x = Dense(num_classes, activation='softmax')(x) model = Model(inputs=[input_tensor, inputsub], outputs=x) return model
def load_mobilenet(shape): s = time.time() print("Loading model...") model = MobileNetV2(input_shape=shape, weights='imagenet') e = time.time() d = e - s print("Created MobileNet V2 model in %.1f seconds." % d) return model
def __init__(self, input_size=None): self.conv_base = MobileNetV2( weights='imagenet', include_top= True, # include the densely connected classifer, which sits on top of hte convolutional network #input_shape=(480, 640, 3) ) print(self.conv_base.summary())
def create_model(): mobilenet_model = MobileNetV2(weights='imagenet', include_top=False) input_tensor = Input((224, 224, 3)) tensor = mobilenet_model(input_tensor) tensor = GlobalAveragePooling2D()(tensor) tensor = Dense(512, activation="relu")(tensor) output_tensor = Dense(5, activation="sigmoid")(tensor) model = Model(input_tensor, output_tensor) return model
def __init__(self, input_size): input_image = Input(shape=(input_size, input_size, 3)) mobilenet = MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet') x = mobilenet(input_image) self.feature_extractor = Model(input_image, x)
def get_model_mobilenet(): from keras.models import Model from keras.applications.mobilenetv2 import MobileNetV2 from keras.layers.core import Dense model = MobileNetV2(input_shape=(128, 128, 3), weights=None, alpha=1.0, depth_multiplier=1) x = model.layers[-2].output x = Dense(7178, activation='sigmoid', name='predictions')(x) model = Model(inputs=model.input, outputs=x) return model
def _download_pretrained_model(self): input_tensor = Input(shape=(AppParams.img_size[0], AppParams.img_size[1], 3)) base_model = MobileNetV2( include_top=False, weights='imagenet', input_tensor=input_tensor, input_shape=(AppParams.img_size[0], AppParams.img_size[1], 3), pooling='avg') base_model.save(AppParams.base_model_path) return base_model
def load_mobilenet_v2(shape): print("Loading model...") if False: model = MobileNetV2(input_shape=shape, weights='imagenet') print("Created MobileNet V2 model.") else: model = MobileNet(include_top=True, weights='imagenet') print("Created MobileNet model.") return model
def create_encoder_model(self): input_tensor = Input(shape=self.input_shape) base_model = MobileNetV2( include_top=False, pooling='avg', input_tensor=input_tensor) for layer in base_model.layers: layer.trainable = False output_tensor = base_model.get_layer('out_relu').output model = Model(inputs=input_tensor, outputs=output_tensor) model.summary() return model