def save_model10(new_model_path, conv_model_path): model = MobileNet( input_shape=(img_width, img_height, 3), include_top=False, weights=None ) if pretrained: model = MobileNet( input_shape=(img_width, img_height, 3), include_top=False, weights='imagenet' ) model.summary() transfer_layer = model.get_layer('conv_pw_13_relu') conv_model = Model(inputs=model.input, outputs=transfer_layer.output) new_model = Sequential() new_model.add(conv_model) new_model.add(GlobalAveragePooling2D()) if num_fc_layers>=1: new_model.add(Dense(num_fc_neurons, activation='relu')) if num_fc_layers>=2: new_model.add(Dropout(dropout)) new_model.add(Dense(num_fc_neurons, activation='relu')) if num_fc_layers>=3: new_model.add(Dropout(dropout)) new_model.add(Dense(num_fc_neurons, activation='relu')) new_model.add(Dense(num_classes, activation='softmax')) print(new_model.summary()) new_model.save(new_model_path) conv_model.save(conv_model_path) return
def get_model(optimizer, gpus): ''' Get compiled MobileNet models according to optimizer @param optimizer: optimizing function for training the network @return: compiled MobileNet models: for parallel launch and the network ''' model = MobileNet(input_shape=(IMG_ROWS, IMG_ROWS, IMG_CHANNELS), alpha=1.0, # control the width of the network include_top=True, # including FC-layers at the network weights=None, classes=CLASSES) ''' Architecture of MobileNet v1: [Conv 3x3] -> [BatchNorm] -> [ReLU] -> -> (x13) [Deepthwise Conv 3x3] -> [BatchNorm] -> [ReLU] -> [Conv 1x1] -> [BatchNorm] -> [ReLU] ''' model.summary() # print the architecture of model # TODO: optimize work on many GPUs if len(gpus) == 1: with K.tf.device('/gpu:{}'.format(gpus[0])): parallel_model = model else: with K.tf.device('/cpu:0'): kernel_model = model parallel_model = multi_gpu_model(kernel_model, gpus=gpus) parallel_model.compile(optimizer=optimizer, loss='categorical_crossentropy', # for multiclass classification metrics=['accuracy']) return parallel_model, model
def get_model(name='mobilenet', n_classes=1000, n_layers_to_remove=0, fine_tune=False, model_path="./"): if fine_tune: if name == 'mobilenet': with CustomObjectScope({ 'relu6': keras.applications.mobilenet.relu6, 'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D }): model = load_model(model_path) else: model = load_model(model_path) else: if name == 'mobilenet': o_model = MobileNet(alpha=0.25) else: o_model = Xception() o_model.summary() # replace last n layers with 'predictions' layer x = o_model.layers[-n_layers_to_remove - 1].output predictions = Dense(n_classes, activation='softmax')(x) model = Model(inputs=o_model.input, outputs=predictions) return model
def _mobilenetv1(): model = MobileNet(input_shape=(224, 224, 3), include_top=False, weights='imagenet') input = Input((224, 224, 3)) x = model(input) x = GlobalAveragePooling2D()(x) x = LeakyReLU(0.1)(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=input, outputs=x) model.summary() # plot_model(model,to_file='inceptionv3.png',show_shapes=True) return model
def main(): """ Script entrypoint """ train_datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, channel_shift_range=10, horizontal_flip=True, fill_mode='nearest') train_batches = train_datagen.flow_from_directory(DATASET_PATH + '/train', target_size=IMAGE_SIZE, interpolation='bicubic', class_mode='categorical', shuffle=True, batch_size=BATCH_SIZE) valid_datagen = ImageDataGenerator() valid_batches = valid_datagen.flow_from_directory(DATASET_PATH + '/valid', target_size=IMAGE_SIZE, interpolation='bicubic', class_mode='categorical', shuffle=False, batch_size=BATCH_SIZE) dnn = MobileNet(include_top=False, input_tensor=None, input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3)) final_dnn = prepare_model(dnn, NUM_CLASSES) # show class indices print('****************') for cls, idx in train_batches.class_indices.items(): print('Class #{} = {}'.format(idx, cls)) print('****************') print(dnn.summary()) train(network=final_dnn, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, training=train_batches, validation=valid_batches) # save trained weights # final_dnn.save(file_weights) final_dnn.save_weights(file_weights) with open(file_architecture, 'w') as f: f.write(final_dnn.to_json())
def yolo_body_mobilenet(inputs, num_anchors, num_classes): """Create YOLO_V3_mobilenet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs,weights='imagenet') # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x 1024 # conv_pw_11_relu :26 x 26 x 512 # conv_pw_5_relu : 52 x 52 x 256 mobilenet.summary() f1 = mobilenet.get_layer('conv_pw_13_relu').output # f1 :13 x 13 x 1024 # spp sp3 = MaxPooling2D(pool_size=(3,3),strides=1,padding='same')(f1) sp5 = MaxPooling2D(pool_size=(5,5),strides=1,padding='same')(f1) f1 = compose( Concatenate(), DarknetConv2D_BN_Leaky(256, (1,1)))([sp3,sp5,f1]) # end x, y1 = make_last_layers(f1, 512, num_anchors * (num_classes + 5)) x = compose( DarknetConv2D_BN_Leaky(256, (1,1)), UpSampling2D(2))(x) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f2: 26 x 26 x 512 x = Concatenate()([x,f2]) x, y2 = make_last_layers(x, 256, num_anchors*(num_classes+5)) x = compose( DarknetConv2D_BN_Leaky(128, (1,1)), UpSampling2D(2))(x) f3 = mobilenet.get_layer('conv_pw_5_relu').output # f3 : 52 x 52 x 256 x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, 128, num_anchors*(num_classes+5)) return Model(inputs = inputs, outputs=[y1,y2,y3])
def MobileNet_Yolo(input_shape=(240, 320, 3)): mobile = MobileNet(weights='imagenet', include_top=False, input_shape=input_shape) mobile.summary() img_input = Input(shape=input_shape) yolo = mobile(img_input) yolo = Conv2D(128, [1, 1])(yolo) yolo = BatchNormalization(name='norm_1')(yolo) yolo = LeakyReLU(alpha=0.1)(yolo) yolo = Flatten()(yolo) yolo = Dense(1024)(yolo) yolo = BatchNormalization()(yolo) yolo = LeakyReLU(alpha=0.1)(yolo) yolo = Dense(576)(yolo) yolo = Reshape((6, 8, 12))(yolo) model = Model(img_input, yolo) model.summary() return model
def make_classifier(optimizer): restnet = MobileNet(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) output = restnet.layers[-1].output output = keras.layers.Flatten()(output) restnet = Model(restnet.input, output=output) for layer in restnet.layers: layer.trainable = True restnet.summary() model = Sequential() model.add(restnet) model.add(Dense(512, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(256, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(13, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy']) return model
class MobileNetModel: #(ClassificationModel): def __init__(self): #super(MobileNetModel, self).__init___(model_name='MobileNet') self.num_classes = 2 self.build_model() return def build_model(self): # Initializing the model with random wights self.arch = MobileNet(weights=None, input_shape=(256, 256, 3), classes=self.num_classes) # Compiling model with optimization of RSM and cross entropy loss self.arch.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) return def __repr__(self): return str(self.arch.summary()) def fit_data(self, train_images, train_labels, val_images, val_labels, initial_epoch=None): train_history = self.arch.fit(train_images, train_labels, epochs=5, steps_per_epoch=train_images.shape[0], validation_steps=val_images.shape[0], validation_data=(val_images, val_labels), shuffle=True) return train_history def save_model(self, model_path): self.arch.save(model_path) return def load_model(self, model_path): self.arch = load_model(model_path) return
def mobilenet(input_tensor, input_shape, nb_classes): architectures_log.info("Architecture: MobileNet") model = MobileNet(include_top=True, weights=None, input_tensor=input_tensor, input_shape=input_shape, pooling="avg", classes=nb_classes) opt = Adam(lr=0.0001, decay=1e-6) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy", custom_acc(0.75)]) print(model.summary()) architectures_log.info("Architecture: MobileNet") return model
def main(): # Parameters if len(sys.argv) == 4: superclass = sys.argv[1] imgmove = sys.argv[2] if imgmove == 'False': imgmove = False else: imgmove = True lr = float(sys.argv[3]) else: print('Parameters error') exit() # The constants classNum = {'A': 40, 'F': 40, 'V': 40, 'E': 40, 'H': 24} testName = {'A': 'a', 'F': 'a', 'V': 'b', 'E': 'b', 'H': 'b'} date = '20180321' trainpath = 'trainval_' + superclass + '/train' valpath = 'trainval_' + superclass + '/val' if not os.path.exists('model'): os.mkdir('model') # Train/validation data preparation if imgmove: os.mkdir('trainval_' + superclass) os.mkdir(trainpath) os.mkdir(valpath) sourcepath = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_'+date\ +'/zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_images_'+date categories = os.listdir(sourcepath) for eachclass in categories: if eachclass[0] == superclass[0]: print(eachclass) os.mkdir(trainpath + '/' + eachclass) os.mkdir(valpath + '/' + eachclass) imgs = os.listdir(sourcepath + '/' + eachclass) idx = 0 for im in imgs: if idx % 8 == 0: shutil.copyfile( sourcepath + '/' + eachclass + '/' + im, valpath + '/' + eachclass + '/' + im) else: shutil.copyfile( sourcepath + '/' + eachclass + '/' + im, trainpath + '/' + eachclass + '/' + im) idx += 1 # Train and validation ImageDataGenerator batchsize = 32 train_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=15, width_shift_range=5, height_shift_range=5, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory(trainpath, target_size=(224, 224), batch_size=batchsize) valid_generator = test_datagen.flow_from_directory(valpath, target_size=(224, 224), batch_size=batchsize) # Train MobileNet model = MobileNet(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=classNum[superclass[0]]) model.summary() model.compile(optimizer=SGD(lr=lr, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) steps_per_epoch = int(train_generator.n / batchsize) validation_steps = int(valid_generator.n / batchsize) weightname = 'model/mobile_' + superclass + '_wgt.h5' checkpointer = ModelCheckpoint(weightname, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='auto', period=2) model.fit_generator(train_generator, steps_per_epoch=steps_per_epoch, epochs=100, validation_data=valid_generator, validation_steps=validation_steps, callbacks=[checkpointer])
# MobileNet - Seçilen Mimari """ #Model model = MobileNet(weights='imagenet', include_top=False) x = GlobalAveragePooling2D(input_shape=model.output_shape[1:])(model.output) x = Dense(6, activation='softmax', kernel_initializer='glorot_normal')(x) model = models.Model(inputs=model.input, outputs=x) # fine tune training for top layers for layer in model.layers[:-1]: #[n_frozen_layers:]: layer.trainable = True """# VGG16""" ''' model = VGG16(weights='imagenet', include_top=False) model.summary() x = GlobalAveragePooling2D(input_shape=model.output_shape[1:])(model.output) x = Dense(6, activation='softmax', kernel_initializer='glorot_normal')(x) model = Model(inputs=model.input, outputs=x) # fine tune training for top layers for layer in model.layers[:-1]:#[n_frozen_layers:]: layer.trainable=True model.compile(optimizer=SGD(lr=1e-4, momentum=0.9, nesterov=True),
from keras.applications.mobilenet import MobileNet import tensorflow as tf model = MobileNet(weights='imagenet', include_top=False, input_shape=(32, 32, 1)) model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy']) print(model.summary())
from keras.applications.vgg19 import VGG19 from keras.applications.inception_v3 import InceptionV3 from keras.applications.mobilenet import MobileNet from keras.preprocessing import image from keras.applications.vgg19 import preprocess_input from keras.models import Model # Params path = "/home/darragh/avito/data/" #path = '/Users/dhanley2/Documents/avito/data/' #base_model = VGG19(weights='imagenet') #base_model = InceptionV3(weights='imagenet') base_model = MobileNet(weights='imagenet') base_model.summary() #model.summary() # model = Model(inputs=base_model.input, outputs=base_model.get_layer('block5_pool').output) model = Model( inputs=base_model.input, outputs=base_model.get_layer('global_average_pooling2d_1').output) model.summary() def process_batch(img_ls): x = preprocess_input(np.array(img_ls)) pool_features = model.predict(x) pool_features = pool_features.reshape(len(img_ls), model.layers[-1].output_shape[-1]) pool_features = sparse.csr_matrix(pool_features, dtype=np.float32)
import keras from keras.applications.mobilenet import MobileNet from keras.layers import Activation, Input from keras.models import Model mn = MobileNet() mn.summary()
def main(): # Parameters if len(sys.argv) == 3: superclass = sys.argv[1] model_weight = sys.argv[2] else: print('Parameters error') exit() # The constants classNum = {'A': 40, 'F': 40, 'V': 40, 'E': 40, 'H': 24} testName = {'A': 'a', 'F': 'a', 'V': 'b', 'E': 'b', 'H': 'b'} date = '20180321' # Feature extraction model base_model = MobileNet(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=classNum[superclass[0]]) base_model.load_weights(model_weight) base_model.summary() model = Model(inputs=base_model.input, outputs=base_model.get_layer('global_average_pooling2d_1').output) imgdir_train = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_'+date\ +'/zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_images_'+date imgdir_test = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_test_'+date categories = os.listdir(imgdir_train) categories.append('test') num = 0 for eachclass in categories: if eachclass[0] == '.': continue if eachclass == 'test': classpath = imgdir_test else: classpath = imgdir_train+'/'+eachclass num += len(os.listdir(classpath)) print('Total image number = '+str(num)) features_all = np.ndarray((num, 1024)) labels_all = list() images_all = list() idx = 0 # Feature extraction for iter in tqdm(range(len(categories))): eachclass = categories[iter] if eachclass[0] == '.': continue if eachclass == 'test': classpath = imgdir_test else: classpath = imgdir_train+'/'+eachclass imgs = os.listdir(classpath) for eachimg in imgs: if eachimg[0] == '.': continue img_path = classpath+'/'+eachimg img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) feature = model.predict(x) features_all[idx, :] = feature labels_all.append(eachclass) images_all.append(eachimg) idx += 1 features_all = features_all[:idx, :] labels_all = labels_all[:idx] images_all = images_all[:idx] data_all = {'features_all':features_all, 'labels_all':labels_all, 'images_all':images_all} # Save features savename = 'features_' + superclass + '.pickle' fsave = open(savename, 'wb') pickle.dump(data_all, fsave) fsave.close()
class MobileNetModel: #(ClassificationModel): def __init__(self): #super(CardModel, self).__init___(model_name='CardModel') self.num_classes = 2 self.build_model() return def build_model(self): # Initializing the model with random wights self.arch = MobileNet(weights=None, include_top=True, classes=self.num_classes) # Compiling model with optimization of RSM and cross entropy loss self.arch.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) return def train(self, epochs, train_images, train_labels, val_data, batch_size, initial_epoch=None): history = self.arch.fit_generator( train_generator, epochs=epochs, steps_per_epoch=train_generator.samples // batch_size, validation_data=validation_generator, validation_steps=validation_generator.samples // batch_size) return history def fit_gen(self, train_dir, val_dir, num_train, num_val, batch_size, epochs): gen = ImageDataGenerator(preprocessing_function=preprocess_input) train_generator = gen.flow_from_directory(train_dir, target_size=(224, 224), batch_size=batch_size, class_mode='categorical') val_generator = gen.flow_from_directory(val_dir, target_size=(224, 224), batch_size=batch_size, class_mode='categorical') train_history = self.arch.fit_generator( train_generator, steps_per_epoch=(num_train // batch_size), epochs=epochs, validation_data=val_generator, validation_steps=(num_val // batch_size)) return train_history def save(self, output_dir): model_path = os.path.join(output_dir, 'model.h5') self.arch.save(model_path) return def load(self, input_path): model_path = os.path.join(output_dir, 'model.h5') self.arch = load_model(model_path) return def __repr__(self): return str(self.arch.summary())