def save_model2(new_model_path, conv_model_path): model = VGG19( input_shape=(img_width, img_height, 3), include_top=False, weights=None ) if pretrained: model = VGG19( input_shape=(img_width, img_height, 3), include_top=False, weights='imagenet' ) transfer_layer = model.get_layer('block5_pool') 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)
def choose_network_model(network_model='VGG16', input_layer_shape=(197, 197, 3)): """ Choose the network base model. Params: - network_model: a string which represents the network model. - input_layer_shape: the shape of the input layer. Returns: - the network model """ network_model_dictionary = { 'Xception': Xception(weights='imagenet', include_top=False, input_shape=input_layer_shape), 'VGG16': VGG16(weights='imagenet', include_top=False, input_shape=input_layer_shape), 'VGG19': VGG19(weights='imagenet', include_top=False, input_shape=input_layer_shape), 'ResNet50': ResNet50(weights='imagenet', include_top=False, input_shape=input_layer_shape), 'InceptionV3': InceptionV3(weights='imagenet', include_top=False, input_shape=input_layer_shape) } return network_model_dictionary[network_model]
def transfer(self): print('Start transfer.') input_tensor = backend.concatenate([ self.target_image, self.style_reference_image, self.combination_image ], axis=0) model = VGG19(input_tensor=input_tensor, weights='imagenet', include_top=False) evaluator = Evaluator(model, self.combination_image, self.img_height, self.img_width) for i in range(self.iterations): start_time = time.time() self.x, min_val, info = fmin_l_bfgs_b(evaluator.loss, self.x, fprime=evaluator.grads, maxfun=self.iterations) print(f'Current loss value: {min_val}.') img = self.x.copy().reshape((self.img_height, self.img_width, 3)) img = ProcessImage.deprocess_image(img) fpath = os.path.join(self.save_path, self.prefix + f'_at_iteration_{i}.png') imsave(fpath, img) print(f'Image saved as {fpath}.') end_time = time.time() print(f'Iteration {i} completed in {end_time - start_time}.') backend.clear_session()
def build(self) -> Model: model = VGG19(include_top=True, weights=None, input_shape=(self.width, self.height, self.channels), classes=2) return model
def load_vgg19(width, height, classes_num): with tf.device('/cpu:0'): model = VGG19(weights=None, input_shape=(width, height, 3), classes=classes_num) return model
def create_ranking_network(img_size): """ Create ranking network which give a score to an image. :param img_size: size of input images during training :type img_size: tuple(int) :return: ranking network model :rtype: keras.Model """ # Create feature extractor from VGG19 feature_extractor = VGG19(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) # for layer in feature_extractor.layers[:-4]: # layer.trainable = False # Add dense layers on top of the feature extractor inp = Input(shape=(img_size, img_size, 3), name='input_image') base = feature_extractor(inp) base = Flatten(name='Flatten')(base) # Block 1 base = Dense(32, activation='relu', name='Dense_1')(base) base = BatchNormalization(name='BN1')(base) base = Dropout(0.490, name='Drop_1')(base) # # Block 2 # base = Dense(128, activation='relu', name='Dense_2')(base) # base = BatchNormalization(name='BN2')(base) # base = Dropout(0.368, name='Drop_2')(base) # Final dense base = Dense(1, name="Dense_Output")(base) base_network = Model(inp, base, name='Scoring_model') return base_network
def load(self, weights_path, opt): #if not using imagenet weights, switch off mean setting and do rescale=1/255 datagen = ImageDataGenerator(rescale=1. / 255) model = VGG19(include_top=False, weights=None) #datagen = ImageDataGenerator(rescale=1., featurewise_center=True) #(rescale=1./255) #datagen.mean=np.array([103.939, 116.779, 123.68],dtype=np.float32).reshape(1,1,3) #model = VGG19(include_top=False, weights='imagenet') generator = datagen.flow_from_directory(self.train_data_dir, target_size=(self.img_width, self.img_height), color_mode='rgb', batch_size=self.batch_size, class_mode=None, shuffle=False) train_data = model.predict_generator( generator, self.nb_train_samples // self.batch_size) model = Sequential() model.add(Flatten(input_shape=train_data.shape[1:])) model.add(Dense(256, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4, activation='softmax')) model = to_multi_gpu(model, n_gpus=4) model.load_weights(weights_path) model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy']) self.model = model
def new_model_construct(vocabulary_len,max_text_length,lr,cnn): #kostil img_shape = (224,224,3) img_input = Input(img_shape, name='image_input') if cnn == 'resnet': cnn_base = ResNet50(weights='imagenet', input_tensor=img_input, include_top=False) # 175 layers cnn_flatten = Flatten(name='cnn_flatten')(cnn_base.get_layer('activation_49').output) elif cnn == 'vgg': cnn_base = VGG19(weights='imagenet', input_tensor=img_input, include_top=False) cnn_flatten = Flatten(name='cnn_flatten')(cnn_base.get_layer('block5_pool').output) for l in cnn_base.layers: l.trainable = False EMBEDDING_DIM = 20 text_input = Input((max_text_length,)) embed = Embedding(input_dim=vocabulary_len, output_dim=EMBEDDING_DIM, input_length=max_text_length)(text_input) drop_1 = Dropout(0.5)(embed) lstm_1 = GRU(1024, input_shape=(max_text_length, EMBEDDING_DIM),return_sequences=True)(drop_1) drop_2 = Dropout(0.5)(lstm_1) lstm_2 = GRU(1024,return_sequences=True)(drop_2) drop_3 = Dropout(0.5)(lstm_2) dense_1 = Dense(1024,activation='tanh')(drop_3) lstm_flatten = Flatten(name='gru_flatten')(dense_1) data_concatenate = Concatenate(name='all_data_concat')([cnn_flatten,lstm_flatten]) dense_2 = Dense(1024,activation='tanh')(data_concatenate) drop_4 = Dropout(0.5)(dense_2) dense_2 = Dense(1,activation='softmax')(drop_4) model = Model([img_input,text_input], dense_2) model.compile(optimizer=Adam(lr=lr), loss='binary_crossentropy',metrics=[f1,'accuracy']) print('model construced!') return model
def func9(shape): from keras.applications import VGG19 BS = 16 conv_base = VGG19(weights = 'imagenet', include_top = False, input_shape = (shape[0],shape[1],3)) return BS,conv_base
def main(params): # take the output features from the last pooling layer model = VGG19(include_top=False, weights='imagenet', input_tensor=None) json_file = json.load(open(params['input_json'], 'r')) img_list_train = json_file['unique_img_train'] img_list_test = json_file['uniuqe_img_test'] batch_size = params['batch_size'] print "process %d training images..." % len(img_list_train) features_train = [] for img_names in get_batches(img_list_train, batch_size): ims = [] for img_name in img_names: im = load_img(os.path.join(params['image_root'], img_name)) ims.append(im) features_train.extend( model.predict(np.vstack(ims), batch_size=batch_size)) train_h5_file = h5py.File(params['out_name_train'], 'w') train_h5_file.create_dataset('images_train', data=features_train) print "process %d testing images..." % len(img_list_test) features_test = [] for img_names in get_batches(img_list_test, batch_size): ims = [] for img_name in img_names: im = load_img(os.path.join(params['image_root'], img_name)) ims.append(im) features_test.extend( model.predict(np.vstack(ims), batch_size=batch_size)) test_h5_file = h5py.File(params['out_name_test'], 'w') test_h5_file.create_dataset('images_test', data=features_test)
def vgg19_net(input_shape, nb_class): # Initialize VGG16 using pre-trained weights on imagenet model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape) # use transfer learning for re-training the last layers # Freeze first 25 layers, so that we can retrain 26th and so on using our classes #for layer in model.layers: # layer.trainable = False # Adding our new layers top_layers = model.output top_layers = Flatten()(top_layers) top_layers = Dense(256, activation="relu")(top_layers) top_layers = Dropout(0.5)(top_layers) top_layers = Dense(128, activation="relu")(top_layers) top_layers = Dropout(0.5)(top_layers) out = Dense(nb_class, activation="softmax")(top_layers) model_final = Model(input=model.input, output=out) model_final.compile( loss="categorical_crossentropy", optimizer=optimizers.Adam( ), #optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"]) model_final.summary() return model_final
def VGG19_model(self): print("创建VGG19模型") model_vgg = VGG19(include_top=False, weights="imagenet", input_shape=self.INPUT_SHAPE) if self.FREEZE_LAYER > 0: for layer in model_vgg.layers[:-self.FREEZE_LAYER]: layer.trainable = False for layer in model_vgg.layers[-self.FREEZE_LAYER:]: layer.trainable = True elif self.FREEZE_LAYER == -1: for layer in model_vgg.layers: layer.trainable = False else: for layer in model_vgg.layers: layer.trainable = True # model_self = GlobalAveragePooling2D()(model_vgg.output) model_self = Flatten(name='flatten')(model_vgg.output) model_self = Dense(self.FC_SIZE, activation='relu', name='fc1')(model_self) # model_self = Dense(self.FC_SIZE, activation='relu', name='fc1', kernel_regularizer=regularizers.l2(0.01), # activity_regularizer=regularizers.l1(0.001))(model_self) model_self = Dropout(self.DROPOUT)(model_self) # model_self = Dense(self.FC_SIZE, activation='relu', name='fc2')(model_self) # model_self = Dropout(self.DROPOUT)(model_self) model_self = Dense(self.CLASSIFY, activation='softmax')(model_self) model_vgg_102 = Model(inputs=model_vgg.input, outputs=model_self, name='VGG19') model_vgg_102.summary() return model_vgg_102
def perceptual_loss(y_true, y_pred): vgg = VGG19(include_top=False, weights="imagenet", input_shape=(None, None, 3)) loss_model = Model(inputs=vgg.inputs, outputs=vgg.get_layer("block5_conv4").output) loss_model.trainable = False preprocessed_y_pred = preprocess_vgg_input(y_pred) preprocessed_y_true = preprocess_vgg_input(y_true) return losses.mse(loss_model(preprocessed_y_true), loss_model(preprocessed_y_pred))
def get_models(): """Get all five pretrained models.""" models = [] xception_base = Xception(weights='imagenet', include_top=False, input_shape=(290, 290, 3)) xception = fine_tune_model(xception_base) models.append(xception) vgg16_base = VGG16(weights='imagenet', include_top=False, input_shape=(290, 290, 3)) vgg16 = fine_tune_model(vgg16_base) models.append(vgg16) vgg19_base = VGG19(weights='imagenet', include_top=False, input_shape=(290, 290, 3)) vgg19 = fine_tune_model(vgg19_base) models.append(vgg19) resnet_base = ResNet50(weights='imagenet', include_top=False, input_shape=(290, 290, 3)) resnet = fine_tune_model(resnet_base) models.append(resnet) inception_base = InceptionV3(weights='imagenet', include_top=False, input_shape=(290, 290, 3)) inception = fine_tune_model(inception_base) models.append(inception) return models
def build_vgg(self): """ Builds a pre-trained VGG19 model that outputs image features extracted at the third block of the model """ # # vgg = VGG16() # Set outputs to outputs of last conv. layer in block 3 # See architecture at: https://github.com/keras-team/keras/blob/master/keras/applications/vgg19.py # vgg.outputs = [vgg.layers[9].output] # img = Input(shape=self.hr_shape) # Extract image features # vgg = VGG19(weights="imagenet") vgg.outputs = [vgg.layers[9].output] img = Input(shape=self.hr_shape) img_features = vgg(img) # res = ResNet50() # img = Input(shape=self.hr_shape) # img_features = res(img) return Model(img, img_features)
def buildModel(self, dense_list): ''' For dense layers, the dropouts are always 0.5 den_list: key arg list for den layers ''' in_layer = Input(self.input_shape) # build vgg_block model_vgg19_conv = VGG19(weights='imagenet', include_top=False) vgg19_block = model_vgg19_conv(in_layer) model_vgg19_conv.summary() vgg19_block = Conv2D(512, kernel_size=2) (vgg19_block) vgg19_block = Activation('elu') (BatchNormalization(axis=-1) (vgg19_block)) vgg19_block = Dropout(0.32) (vgg19_block) print vgg19_block.shape denlayer = GlobalAveragePooling2D() (vgg19_block) # denlayer = GlobalMaxPooling2D() (vgg19_block) # denlayer = Flatten() (vgg19_block) # adding dense layers for kargs in dense_list: denlayer = Dropout(0.68) (Dense(**kargs) (denlayer)) out_layer = Dense(self.output_dim, activation='softmax') (denlayer) self.model = Model(inputs=[in_layer], outputs=[out_layer])
def cust_vgg19(): # Use VGG19 pretrain model. I freeze the weights of VGG19 up to the last 3 layers # Train the last 3 layers of VGG19 as well as a few more Dense layers with batch normalization and dropouts input_image = Input(shape=(224, 224, 3)) base_model = VGG19(input_tensor=input_image, include_top=False) for layer in base_model.layers[:-4]: layer.trainable = False x = base_model.get_layer("block5_conv4").output x = AveragePooling2D((2, 2))(x) x = Dropout(0.5)(x) x = BatchNormalization()(x) x = Dropout(0.5)(x) x = Flatten()(x) x = Dense(4096, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(2048, activation="relu")(x) x = Dense(2048, activation="relu")(x) x = Dense(1, activation="linear")(x) model = Model(input=input_image, output=x) return model
def con_model(): vision_model = VGG19(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3)) # In order to prevent overfitting, as advised in the keras documentation, # we freeze the 18 first convolutional layers (corresponding to the first 2 blocks) for layer in vision_model.layers[:18]: layer.trainable = False # Definition of the 2 inputs img_a = Input(shape=(IMG_SIZE, IMG_SIZE, 3)) img_b = Input(shape=(IMG_SIZE, IMG_SIZE, 3)) # Outputs of the vision model corresponding to the inputs # Note that this method applies the 'tied' weights between the branches out_a = vision_model(img_a) out_b = vision_model(img_b) # Concatenation of these ouputs concat = concatenate([out_a, out_b]) # The classification model is the full model: it takes 2 images as input and # returns a number between 0 and 1. The closest the number is to 1, the more confident classification_model = Model([img_a, img_b], concat) return classification_model
def get_model(): conv_base = VGG19(weights='imagenet', include_top=False, input_shape=(width, height, 3)) conv_base.trainable = True # set_trainable = False # for layer in conv_base.layers: # if layer.name == 'block3_conv1': # set_trainable = True # if set_trainable: # layer.trainable = True # else: # layer.trainable = False model = Sequential() model.add(conv_base) model.add(Flatten()) model.add(Dropout(0.4)) model.add(Dense(2048, activation='relu')) model.add(Dense(2048, activation='relu')) model.add(Dropout(0.4)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='binary_crossentropy', metrics=['accuracy']) return model
def build_conv_base(self, model_name, input_shape): """ Function that intializes the pretrained model from keras. Args: --- model_name: String identifier for pretrained model from Keras. A value from ["vgg16", "vgg19"] input_shape: A tuple with the input shape for the pretrained model Returns: --- conv_base: An Keras model object of the corresponding pretrained model. """ if model_name == "vgg19": conv_base = VGG19(weights="imagenet", include_top=False, input_shape=input_shape) elif model_name == "vgg16": conv_base = VGG16(weights="imagenet", include_top=False, input_shape=input_shape) else: print("Not implemented yet:", model_name) conv_base.trainable = False return conv_base
def generate_model(self) -> Model: # This is a bastardised version of VGG16, VGG19 is probably a bit too # big to run on our hardware, which is likely to limit our experiment # going forward. img_input = Input(shape=(self.height, self.width, 3)) cnn = VGG19(include_top=False, input_tensor=img_input) for layer in cnn.layers: # HACK: Ensuring that the top of the model (VGG16 classifier) # is set to train. layer.trainable = True x = Conv2D(filters=2, kernel_size=(1, 1))(cnn.output) x = Conv2DTranspose(filters=self.n_classes, kernel_size=(64, 64), strides=(32, 32), padding="same", activation="sigmoid")(x) model = Model(inputs=img_input, outputs=x) if self.n_classes == 1: loss_func = "binary_crossentropy" elif self.n_classes > 1: loss_func = "categorical_crossentropy" sgd = SGD() model.compile(loss=loss_func, optimizer=sgd) return model
def get_vgg_19() -> VGG19: global vgg_19 if vgg_19 is not None: return vgg_19 print("Please wait. Initializing VGG19 model..") vgg_19 = VGG19(weights="imagenet") return vgg_19
def build_vgg(self): # features of pre-trained VGG19 model at the third block vgg = VGG19(weights="imagenet") vgg.outputs = [vgg.layers[9].output] img = Input(shape=self.hr_shape) img_features = vgg(img) return Model(img, img_features)
def build_VGG19(): base_model = VGG19(include_top=False, weights='imagenet', classes=label_num, input_shape=IMG_SHAPE) model = Sequential() model.add(base_model) model.add(Flatten()) model.add(Dense(512)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(256)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(128)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(13, activation='softmax')) model.layers[0].trainable = False model.summary() # base_model.trainable = True # set_trainable = False # model.save('VGG19_pretrain_all.model') return model
def vgg19_model(shape, weights=None, deeper=False, start=11): from keras.applications import VGG19 model = VGG19(include_top=False, weights=weights, input_tensor=Input(shape=shape + (3, ))) if deeper: # removing from block3_conv4 ... onwards for idx in range(start, len(model.layers)): model.layers.pop() # output = model.output # output = Flatten(name='flatten')(output) # output = Dense(4096, activation='relu', name='fc1')(output) # output = Dropout(0.5)(output) # output = Dense(4096, activation='relu', name='fc2')(output) # output = Dropout(0.5)(output) # output = Dense(nb_classes, activation='softmax', # name='predictions')(output) # final_model = Model(model.input, output) # sgd = SGD(lr=1e-4, decay=1e-6, momentum=0.9, nesterov=True) # final_model.compile(optimizer=sgd, # loss='categorical_crossentropy', # metrics=['accuracy']) print("Model => vgg19") return model
def change_model(model0): # 选择模型 if model0 == "ResNet50": tr_model = ResNet50(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') elif model0 == "VGG19": tr_model = VGG19(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') elif model0 == "InceptionV3": tr_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') # 不能用input_shape=(220, 220, 3) #elif model0 == "MobileNet": # tr_model = MobileNet(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') #只能在weights=None时使用 #elif model0 == "NASNetMobile": # tr_model = NASNetMobile(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') elif model0 == "Xception": tr_model = Xception(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') elif model0 == "DenseNet121": tr_model = DenseNet121(include_top=False, weights='imagenet', input_shape=(220, 220, 3), pooling='avg') return tr_model
def vgg19_plus_vgg16_model(shape, weights=None): from keras.applications import VGG19 model_vgg19 = VGG19(include_top=False, weights=weights, input_tensor=Input(shape=shape + (3, ))) from keras.applications import VGG16 model_vgg16 = VGG16(include_top=False, weights=weights, input_tensor=Input(shape=shape + (3, ))) left = Sequential() right = Sequential() final = Sequential() left.add(model_vgg19) left.add(Flatten(name='vgg19_flatten')) right.add(model_vgg16) right.add(Flatten(name='vgg16_flatten')) final.add(Merge([left, right], mode='concat')) final.add(Dense(4096, activation='relu', name='fc1')) final.add(Dropout(0.35, name='dropout1')) final.add(Dense(4096, activation='relu', name='fc2')) final.add(Dropout(0.5, name='dropout2')) final.add(Dense(nb_classes, activation='softmax', name='predictions')) final.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print("Model => vgg19 + vgg16") return final
def __init__(self): self.bm = VGG19(weights='imagenet') self.path_to_model = '/home/flomko/.keras/models/vgg19_weights_tf_dim_ordering_tf_kernels.h5' self.model = Model(inputs=self.bm.input, outputs=self.bm.get_layer('fc1').output) logging.debug('Model has been initialized')
def make_vgg(in_shape, name, output_layer=-1): vgg = VGG19(include_top=False, input_shape=in_shape, weights="imagenet") cnn_out = GlobalAveragePooling2D()(vgg.layers[16].output) model = Model(vgg.input, cnn_out, name=f"VGG19_{name}") #model.trainable = False #model.summary() return model
def loadvgg19(): global modelvgg19 print "Loading VGG19 ... " if modelvgg19 is None: modelvgg19 = VGG19(weights='imagenet') print "Model loaded" switchvgg19()