def test_vgg19_variable_input_channels(): input_shape = (1, None, None) if K.image_data_format() == 'channels_first' else (None, None, 1) model = applications.VGG19(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 512) input_shape = (4, None, None) if K.image_data_format() == 'channels_first' else (None, None, 4) model = applications.VGG19(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 512)
def build_model(self, X, X_noised, phase): ### 1.PVAE Generator ### model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(64, 64, 3)) for layer in model.layers[:]: layer.trainable = False mean, std = self.gaussian_encoder(X_noised, phase) s_latent = mean + std * tf.random_normal( tf.shape(mean, out_type=tf.int32), 0, 1, dtype=tf.float32) s_out = self.bernoulli_decoder(s_latent, phase) act_1 = model.layers[0](s_out) act_2 = model.layers[1](act_1) act_3 = model.layers[2](act_2) act_X_1 = model.layers[0](X) act_X_2 = model.layers[1](act_X_1) act_X_3 = model.layers[2](act_X_2) p_loss_1 = tf.reduce_mean(tf.squared_difference(act_1, act_X_1)) p_loss_2 = tf.reduce_mean(tf.squared_difference(act_2, act_X_2)) p_loss_3 = tf.reduce_mean(tf.squared_difference(act_3, act_X_3)) Recon_loss = p_loss_1 + p_loss_2 + p_loss_3 KL_Div = 0.5 * tf.reduce_mean(1 - tf.log(tf.square(std) + 1e-8) + tf.square(mean) + tf.square(std)) s_loss = Recon_loss + KL_Div return s_loss
def callModel(model_picked='vgg16'): '''function returns the model picked based on input Input choices: 'vgg16' - VGG16 'vgg19' - VGG19 'res50' - ResNet50 'xception' - Xception 'inception' - InceptionV3 'monet' - MobileNetV2 ''' if model_picked == 'vgg16': model = applications.VGG16(include_top=False, weights='imagenet') elif model_picked == 'vgg19': model = applications.VGG19(include_top=False, weights='imagenet') elif model_picked == 'res50': model = applications.ResNet50(include_top=False, weights='imagenet') elif model_picked == 'xception': model = applications.Xception(include_top=False, weights='imagenet') elif model_picked == 'inception': model = applications.InceptionV3(include_top=False, weights='imagenet') elif model_picked == 'monet': model = applications.MobileNetV2(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) return model
def create_network(): channels = 3 batch_size = 10 epochs = 15 model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(img_width, img_height, channels)) # model = applications.Xception(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, channels)) # model = applications.InceptionV3(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, channels)) # model = applications.ResNet50(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, channels)) # model = applications.VGG16(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, channels)) # Freeze the layers which you don't want to train. Here I am freezing the first 5 layers. for layer in model.layers[:-10]: layer.trainable = False # Adding custom Layers x = model.output x = Flatten() x = Dense(50, activation="relu")(x) # x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) # x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation="softmax") # creating the final model model_final = Model(input=model.input, output=predictions) # compile the model model_final.compile(loss="categorical_crossentropy", optimizer=optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"]) return model_final
def save_bottlebeck_features(): train_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator(rescale=1. / 255) # build the VGG19 network model = applications.VGG19(include_top=False, weights='imagenet') train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( train_generator, aug_factor * nb_classes * nb_train_samples // batch_size) train_labels = np.tile(np.repeat(np.arange(nb_classes), nb_train_samples), aug_factor) np.savez(bn_train_path, data=bottleneck_features_train, label=train_labels) test_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( test_generator, nb_classes * nb_validation_samples // batch_size) validation_labels = np.repeat(np.arange(nb_classes), nb_validation_samples) np.savez(bn_validation_path, data=bottleneck_features_validation, label=validation_labels)
def tf_model(img_size, num_class): model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) # Freeze the layers which you don't want to train. Here I am freezing all layers. for layer in model.layers: layer.trainable = False # Adding custom Layers x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(1024, activation="relu")(x) predictions = Dense(num_class, activation="softmax")(x) # creating the final model model_final = Model(input=model.input, output=predictions) adam = optimizers.adam(lr=0.001, beta_1=0.9, beta_2=0.999) # compile the model model_final.compile(loss="categorical_crossentropy", optimizer=adam, metrics=["accuracy"]) return model_final
def callModel(model_picked = 'vgg16'): '''function returns the model picked based on input Input choices: 'vgg16' - VGG16 'vgg19' - VGG19 'res50' - ResNet50 'xception' - Xception 'inception' - InceptionV3 'monet' - MobileNetV2 ''' #The models have a series of convolutional layers and then they have dense(deeply connected layers) #include_top = False only gets the convo layers and ignores the dense layer #imagenet is a huge image dataset on which the models are trained. if weights ='imagenet' means the weights are acquired from that. if model_picked == 'vgg16': model = applications.VGG16(include_top=False, weights='imagenet') elif model_picked =='vgg19': model = applications.VGG19(include_top=False, weights='imagenet') elif model_picked == 'res50': model = applications.ResNet50(include_top=False, weights='imagenet') elif model_picked == 'xception': model = applications.Xception(include_top=False, weights='imagenet') elif model_picked == 'inception': model = applications.InceptionV3(include_top=False, weights='imagenet') elif model_picked == 'monet': model = applications.MobileNetV2(include_top=False, weights='imagenet', input_shape=(224,224,3)) return model
def load_part_model(model_name, depth_level, weights='imagenet'): """ function to load the model according to the depth provided :param model_name: name of the cnn model to be loaded (currently only supports InceptionV3 and VGG19) :param depth_level: one of [shallow, intermediate, deep] :weights: one of 'imagenet' or None :return: the model to the desired depth level with imagenet or random weights """ #define a dictionary of dictionaries to help load the appropriate model: model_dict = { 'InceptionV3': { 'shallow': 'mixed2', 'intermediate': 'mixed7', 'deep': 'mixed10', }, 'VGG19': { 'shallow': 'block1_pool', 'intermediate': 'block4_pool', 'deep': 'block5_pool', }, } if model_name == 'InceptionV3': # load the entire model: base_model = applications.InceptionV3(weights=weights) if model_name == 'VGG19': base_model = applications.VGG19(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer( model_dict[model_name][depth_level]).output) return model
def getModel(): model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(128, 128, 3)) for layer in model.layers: layer.trainable = False x = model.output x = Flatten()(x) x = Dense(256, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(128, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(64, activation="relu")(x) x = Dropout(0.5)(x) predictions = Dense(3, activation="softmax")(x) combinedModel = Model(input=model.input, output=predictions) #combinedModel.summary() return combinedModel
def create_model(self): #load the model model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(self.img_width, self.img_height, 3)) #disable few layers for layer in model.layers[:5]: layer.trainable = False #Add layers x = model.output x = Flatten()(x) x = Dense(128, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(128, activation="relu")(x) x = Dropout(0.2)(x) x = Dense(128, activation="relu")(x) #Add output layer predictions = Dense(self.num_labels, activation="softmax")(x) #create the final model self.model_final = Model(inputs=model.input, outputs=predictions) #load the weights self.model_final.load_weights("vgg19_pictorecipe_model.h5") # compile the model self.model_final.compile(loss="categorical_crossentropy", optimizer=optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])
def get_pretrained_model(model, img_size): possible_names = ["vgg16", "vgg19", "mobilenet", "xception"] if model not in possible_names: raise ValueError(f"model needs to be either {possible_names}") logger.debug(f"backend for model is {model}") width, height = img_size if K.image_data_format() == 'channels_first': input_shape = (3, width, height) else: input_shape = (width, height, 3) logger.debug( f"backend suggests that we have the following input shape: {input_shape}" ) models = { "vgg16": applications.VGG16(input_shape=input_shape, include_top=False, weights='imagenet'), "vgg19": applications.VGG19(input_shape=input_shape, include_top=False, weights='imagenet'), "mobilenet": applications.MobileNet(input_shape=input_shape, include_top=False, weights='imagenet'), "xception": applications.Xception(input_shape=input_shape, include_top=False, weights='imagenet') } return models[model]
def save_bottlebeck_features(): datagen = ImageDataGenerator(rescale=1. / 255) # build the VGG16 network model = applications.VGG19(include_top=False, weights='imagenet') generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples // batch_size) np.save(open('vgg19_bottleneck_features_train.npy', 'wb'), bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples // batch_size) np.save(open('vgg19_bottleneck_features_validation.npy', 'wb'), bottleneck_features_validation) class_dictionary = generator.class_indices print(class_dictionary)
def save_bottlebeck_features(): datagen = ImageDataGenerator(featurewise_center=True) # build the VGG16 network model = applications.VGG19(include_top=False, weights='imagenet') generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples // batch_size) np.save(open(train_feature_dir, 'wb'), bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples // batch_size) np.save(open(validation_feature_dir, 'wb'), bottleneck_features_validation)
def save_bottleneck_features(): # build the VGG16 network model = applications.VGG19(include_top=False, weights='imagenet') datagen = ImageDataGenerator(rescale=1. / 255) generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) print(len(generator.filenames)) print(generator.class_indices) print(len(generator.class_indices)) bottleneck_features_train = model.predict_generator(generator, verbose=1) np.save(bft_file, bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator(generator, verbose=1) np.save(bfv_file, bottleneck_features_validation)
def build_model(): with tf.device('/device:GPU:2'): base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=train_tensors.shape[1:]) add_model = Sequential() add_model.add(Flatten(input_shape=base_model.output_shape[1:])) added0_model = Model(inputs=base_model.input, outputs=add_model(base_model.output)) stn_model = Sequential() stn_model.add( Lambda(lambda x: 2 * x - 1., input_shape=train_tensors.shape[1:], output_shape=train_tensors.shape[1:])) stn_model.add(BatchNormalization()) stn_model.add( SpatialTransformer(localization_net=locnet(), output_size=train_tensors.shape[1:3])) added_model = Model(inputs=stn_model.input, outputs=added0_model(stn_model.output)) inp = Input(batch_shape=(None, train_data.shape[1])) extra_model = Model(input=inp, output=inp) x = concatenate([added_model.output, extra_model.output]) x = Dropout(0.5)(x) x = Dense(256, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1, activation='sigmoid')(x) model = Model(input=[added_model.input, extra_model.input], output=x) model.summary() return model
def base_network(network='InceptionV3'): if network == 'InceptionV3': base_model = applications.InceptionV3(weights='imagenet', include_top=False) elif network == 'VGG16': base_model = applications.VGG16(weights='imagenet', include_top=False) elif network == 'VGG19': base_model = applications.VGG19(weights='imagenet', include_top=False) elif network == 'ResNet50': base_model = applications.ResNet50(weights='imagenet', include_top=False) elif network == 'InceptionResNetV2': base_model = applications.InceptionResNetV2(weights='imagenet', include_top=False) elif network == 'MobileNet': base_model = applications.MobileNet(weights='imagenet', include_top=False) elif network == 'DenseNet121': base_model = applications.DenseNet121(weights='imagenet', include_top=False) else: print('Wrong Model selected.') return None return base_model
def model_vgg_create(input_shape, num_classes): logging.debug('input_shape {}'.format(input_shape)) #model = applications.VGG19(weights = "imagenet", include_top=False, input_shape = (256, 256, 3)) model = applications.VGG19(weights = "imagenet", include_top=False, input_shape = (input_shape)) # input_shape (128, 128, 1) # input_shape (128, 128, 3) # Freeze the layers which you don't want to train. Freezing the first 5 layers. for layer in model.layers[:5]: layer.trainable = False # Adding custom Layers x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(1024, activation="relu")(x) predictions = Dense(num_classes, activation="softmax")(x) # Creating the final model model_final = Model(inputs = model.input, outputs = predictions) # Compile the model # opt = RMSprop(lr=0.0001, decay=1e-6) opt = SGD(lr=0.0001, momentum=0.9) model_final.compile(loss = "categorical_crossentropy", optimizer = opt, metrics=["accuracy"]) return model_final
def save_bottlebeck_features(self): print("Saving bottleneck features started") model = applications.VGG19(include_top=False, weights='imagenet', input_shape=(self.img_width, self.img_height, 3)) ImageFile.LOAD_TRUNCATED_IMAGES = True datagen = ImageDataGenerator( # shear_range=0.2, # zoom_range=0.2, # horizontal_flip=True, rescale=1. / 255) generator = datagen.flow_from_directory(self.train_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode=None, shuffle=False) print(generator.class_indices) nb_train_samples = len(generator.filenames) num_classes = len(generator.class_indices) predict_size_train = int(math.ceil(nb_train_samples / self.batch_size)) bottleneck_features_train = model.predict_generator( generator, predict_size_train) np.save(self.bottleneck_train_features, bottleneck_features_train) print("Bottleneck train features saved as ", self.bottleneck_train_features) datagen = ImageDataGenerator(rescale=1. / 255) generator = datagen.flow_from_directory(self.test_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode=None, shuffle=False) nb_validation_samples = len(generator.filenames) predict_size_validation = int( math.ceil(nb_validation_samples / self.batch_size)) bottleneck_features_validation = model.predict_generator( generator, predict_size_validation) np.save(self.bottleneck_test_features, bottleneck_features_validation) print("Bottleneck validation features saved as ", self.bottleneck_test_features) model.save(self.bottleneck_model) print("Bottleneck models saved")
def ADGAN(self, X, X_noised, phase): ### perceptual VAE, G of ADGAN ############################ mean, std = self.gaussian_encoder(X_noised, phase) z_r2f = mean + std * tf.random_normal(tf.shape(mean, out_type=tf.int32), 0, 1, dtype=tf.float32) f_logits, f_out = self.bernoulli_decoder(z_r2f, phase) if self.c == 1: f_out = tf.image.grayscale_to_rgb(f_out) X = tf.image.grayscale_to_rgb(X) model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(64, 64, 3)) for layer in model.layers[:]: layer.trainable = False act_1 = model.layers[0](f_out) act_2 = model.layers[1](act_1) act_3 = model.layers[2](act_2) act_X_1 = model.layers[0](X) act_X_2 = model.layers[1](act_X_1) act_X_3 = model.layers[2](act_X_2) p_loss_1 = tf.reduce_mean(tf.squared_difference(act_1, act_X_1)) p_loss_2 = tf.reduce_mean(tf.squared_difference(act_2, act_X_2)) p_loss_3 = tf.reduce_mean(tf.squared_difference(act_3, act_X_3)) if self.c == 1: f_out = tf.image.rgb_to_grayscale(f_out) X = tf.image.rgb_to_grayscale(X) KL_Div = 0.5 * tf.reduce_mean(1 - tf.log(tf.square(std) + 1e-8) + tf.square(mean) + tf.square(std)) Perceptual_loss = p_loss_1 + p_loss_2 + p_loss_3 ori2fake_loss = self.conf.PVAE_rate*(Perceptual_loss + self.conf.KL_rate*KL_Div) ############################################################## ''' z_r2f = self.Cycle_En(X_noised, phase) f_out = self.Cycle_De(z_r2f, phase) ori2fake_loss = tf.reduce_mean(tf.squared_difference(f_out, X)) ''' ### Plain AutoEncoder, F of ADGAN ############################ z_f2r = self.Cycle_En(f_out, phase) r_out = self.Cycle_De(z_f2r, phase) fake2ori_loss = self.conf.AE_rate*(tf.reduce_mean(tf.squared_difference(r_out,X))) ############################################################## ### EB Discriminator, Discriminator of ADGAN ################# X_patches = make_patch(X,[self.conf.batch_size,self.w,self.h,self.c], self.conf.patch_size, self.conf.patch_strides) f2r_patches = make_patch(r_out, [self.conf.batch_size,self.w,self.h,self.c], self.conf.patch_size, self.conf.patch_strides) D_fake_latent = self.EB_Discriminator_En(f2r_patches, phase) D_fake = self.EB_Discriminator_De(D_fake_latent, phase) D_real = self.EB_Discriminator_De(self.EB_Discriminator_En(X_patches, phase), phase) fake_loss = tf.reduce_mean(tf.squared_difference(D_fake, f2r_patches)) real_loss = self.conf.G_rate*(tf.reduce_mean(tf.squared_difference(D_real, X_patches))) D_loss = real_loss + relu(self.conf.margin - fake_loss) PT_loss = self.conf.PT_rate*self.EBGAN_PT(D_fake_latent) G_loss = ori2fake_loss + fake2ori_loss + PT_loss + fake_loss ############################################################## return z_r2f, f_out, ori2fake_loss, z_f2r, r_out, fake2ori_loss, PT_loss, G_loss, D_loss, X_patches
def save_bottlebeck_features(): # build the VGG16 network if topology == 'VGG16': model = applications.VGG16(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) elif topology == 'VGG19': model = applications.VGG19(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) elif topology == 'inceptionv3': model = applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) elif topology == 'resnet50': model = applications.resnet50.ResNet50(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) elif topology == 'inceptionresnetv2': model = applications.inception_resnet_v2.InceptionResNetV2( include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) bottle_train_datagen = ImageDataGenerator(rescale=1. / 255) bottle_valid_datagen = ImageDataGenerator(rescale=1. / 255) generator = bottle_train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) nb_train_samples = len(generator.filenames) num_classes = len(generator.class_indices) predict_size_train = int(math.ceil(nb_train_samples / batch_size)) bottleneck_features_train = model.predict_generator( generator, predict_size_train) np.save('bottleneck_features_train.npy', bottleneck_features_train) generator = bottle_valid_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) nb_validation_samples = len(generator.filenames) predict_size_validation = int(math.ceil(nb_validation_samples / batch_size)) bottleneck_features_validation = model.predict_generator( generator, predict_size_validation) np.save('bottleneck_features_validation.npy', bottleneck_features_validation)
def models_factory(model_type, image_size): if model_type == "vgg16": base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "vgg19": base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "resnet50": base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "inceptionv3": base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "xception": base_model = applications.Xception(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "mobilenet": base_model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "inceptionresnetv2": base_model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) elif model_type == "nasnet": base_model = applications.nasnet.NASNetLarge( weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3)) for layer in base_model.layers: layer.trainable = False top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) top_model.add( Dense(1024, kernel_initializer='glorot_uniform', activation='relu')) top_model.add( Dense(1024, kernel_initializer='glorot_uniform', activation='relu')) top_model.add(Dense(1, activation='sigmoid')) model = Model(input=base_model.input, output=top_model(base_model.output)) return model, base_model
def Revised_EBGAN(self, X, X_noised, phase): ### perceptual VAE, Generator of GANs ######################## mean, std = self.gaussian_encoder(X_noised, phase) z = mean + std * tf.random_normal( tf.shape(mean, out_type=tf.int32), 0, 1, dtype=tf.float32) z_c = tf.reduce_mean(z, axis=0) logits, X_out = self.bernoulli_decoder(z, phase) if self.c == 1: X_out = tf.image.grayscale_to_rgb(X_out) X = tf.image.grayscale_to_rgb(X) KL_Div = 0.5 * tf.reduce_mean(1 - tf.log(tf.square(std) + 1e-8) + tf.square(mean) + tf.square(std)) Potential_loss = tf.reduce_mean(tf.square(z_c - z)) model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(64, 64, 3)) for layer in model.layers[:]: layer.trainable = False act_1 = model.layers[0](X_out) act_2 = model.layers[1](act_1) act_3 = model.layers[2](act_2) act_X_1 = model.layers[0](X) act_X_2 = model.layers[1](act_X_1) act_X_3 = model.layers[2](act_X_2) p_loss_1 = tf.reduce_mean(tf.squared_difference(act_1, act_X_1)) p_loss_2 = tf.reduce_mean(tf.squared_difference(act_2, act_X_2)) p_loss_3 = tf.reduce_mean(tf.squared_difference(act_3, act_X_3)) Perceptual_loss = p_loss_1 + p_loss_2 + p_loss_3 ############################################################## if self.c == 1: X_out = tf.image.rgb_to_grayscale(X_out) X = tf.image.rgb_to_grayscale(X) D_latent_fake = self.EB_Discriminator_En(X_out, phase) D_fake = self.EB_Discriminator_De(D_latent_fake, phase) D_real = self.EB_Discriminator_De(self.EB_Discriminator_En(X, phase), phase) fake_loss = tf.reduce_mean(tf.squared_difference(D_fake, X_out)) real_loss = tf.reduce_mean(tf.squared_difference(D_real, X)) D_loss = real_loss + relu(self.conf.margin - fake_loss) G_loss_VAE = self.conf.KL_rate * KL_Div + self.conf.PL_rate * Perceptual_loss + self.conf.E_rate * Potential_loss G_loss_GAN = self.conf.pt_rate * self.EBGAN_PT( D_latent_fake) + self.conf.FL_rate * fake_loss return X_out, D_loss, G_loss_VAE, G_loss_GAN, D_real, D_fake
def save(self): self.top_model.load_weights(self.top_model_weights) base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(self.img_width, self.img_height, 3)) model = Model(input=base_model.input, output=self.top_model(base_model.output)) model.save(self.model_path) print("model saved")
def get_imagenet_architecture(architecture, variant, size, alpha, output_layer, include_top=False, weights='imagenet'): from keras import applications, Model if include_top: assert output_layer == 'last' if size == 'auto': size = get_image_size(architecture, variant, size) shape = (size, size, 3) if architecture == 'densenet': if variant == 'auto': variant = 'densenet-121' if variant == 'densenet-121': model = applications.DenseNet121(weights=weights, include_top=include_top, input_shape=shape) elif variant == 'densenet-169': model = applications.DenseNet169(weights=weights, include_top=include_top, input_shape=shape) elif variant == 'densenet-201': model = applications.DenseNet201(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'inception-resnet-v2': model = applications.InceptionResNetV2(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'mobilenet': model = applications.MobileNet(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha) elif architecture == 'mobilenet-v2': model = applications.MobileNetV2(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha) elif architecture == 'nasnet': if variant == 'auto': variant = 'large' if variant == 'large': model = applications.NASNetLarge(weights=weights, include_top=include_top, input_shape=shape) else: model = applications.NASNetMobile(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'resnet-50': model = applications.ResNet50(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'vgg-16': model = applications.VGG16(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'vgg-19': model = applications.VGG19(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'xception': model = applications.Xception(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'inception-v3': model = applications.InceptionV3(weights=weights, include_top=include_top, input_shape=shape) if output_layer != 'last': try: if isinstance(output_layer, int): layer = model.layers[output_layer] else: layer = model.get_layer(output_layer) except Exception: raise VergeMLError('layer not found: {}'.format(output_layer)) model = Model(inputs=model.input, outputs=layer.output) return model
def create_VGG19(image_size, num_class): resnet_conv = applications.VGG19(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) model = models.Sequential() model.add(resnet_conv) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(num_class, activation='softmax')) return model pass
def _create(self): base_model = applications.VGG19(weights=None, include_top=False, input_shape=(self.img_width, self.img_height, self.img_channels)) self.model = Sequential(base_model.layers) self.model.add(Flatten()) self.model.add(Dense(4096, activation='relu')) self.model.add(Dropout(0.5)) self.model.add(Dense(4096, activation='relu')) self.model.add(Dense(self.n_labels, activation='softmax'))
def VGG_finetune(input_shape = (256, 256, 3), num_classes = 5): model_VGG = applications.VGG19(include_top = False, weights='imagenet', input_shape=input_shape) for layer in model_VGG.layers: # or for layer in model_VGG.layers[:25]: layer.trainable = True ##True or False f = Flatten(input_shape = model_VGG.output_shape[1:])(model_VGG.output) d1 = Dense(4096, activation = 'relu')(f) dr1 = Dropout(0.5)(d1) d2 = Dense(4096, activation = 'relu')(dr1) dr2 = Dropout(0.5)(d2) o = Dense(num_classes, activation = 'softmax')(dr2) model = Model(inputs=model_VGG.input, outputs=[o]) return model
def train(): ### data preprocessing train_datagen = ImageDataGenerator(validation_split=0.2) train_generator = train_datagen.flow_from_directory( BASE_DIR, target_size=(IMG_HEIGHT, IMAGE_WIDTH), # shuffle=true, class_mode='categorical', subset='training') validation_generator = train_datagen.flow_from_directory( BASE_DIR, target_size=(IMG_HEIGHT, IMAGE_WIDTH), # shuffle=true, class_mode='categorical', subset='validation') base_model = applications.VGG19(input_shape=(299, 299, 3), weights='imagenet', include_top=False) model = add_new_layer(base_model) set_up_to_finetune(base_model, model) # parallel_model = multi_gpu_model(model, gpus=4) model.compile(optimizer=optimizers.SGD(lr=0.01, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) checkpoint = ModelCheckpoint('InceptionV3(trainale).h5', monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto') history_fit = model.fit_generator( train_generator, epochs=EPOCHS, verbose=1, steps_per_epoch=50, validation_data=validation_generator, validation_steps=10, # class_weight = 'auto', callbacks=[checkpoint, early]) model.save('InceptionV3(trainale).h5')
def test_vgg19_notop_specified_input_shape(): input_shape = (3, 300, 300) if K.image_data_format() == 'channels_first' else (300, 300, 3) model = applications.VGG19(weights=None, include_top=False, input_shape=input_shape) output_shape = (None, 512, 9, 9) if K.image_data_format() == 'channels_first' else (None, 9, 9, 512) assert model.output_shape == output_shape
def VGG16_convolutions(): model = applications.VGG19(include_top=None, weights='imagenet',input_shape=(224,224,3)) top_model = Sequential() top_model.add(GlobalMaxPooling2D(data_format='channels_last',input_shape=model.output_shape[1:])) top_model.add(Dense(1, activation = 'sigmoid', init='uniform')) new_model = Model(inputs = model.input, outputs = top_model(model.output)) adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08) new_model.compile(loss = 'binary_crossentropy', optimizer = adam, metrics=['accuracy']) return new_model