def get_frozen_pretrained_vgg19_model(input_shape, trainable_encoder, random_weights): if random_weights: model = VGG19(include_top=False, weights=None, input_shape=(input_shape[0], input_shape[1], 3)) else: model = VGG19(include_top=False, input_shape=(input_shape[0], input_shape[1], 3)) model.trainable = trainable_encoder return model
class KeyFrameExtractor: features = {} model = VGG19(weights='imagenet', include_top=False) def __init__(self, base_path, video): self.features = {} self.base_path = base_path self.video = video def get_feature(self, fig): path = os.path.join(self.base_path, self.video, 'fig{0:06d}.jpg'.format(fig)) img = image.load_img(path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return self.model.predict(x) def build_features(self): for filename in tqdm( sorted(os.listdir(os.path.join(self.base_path, self.video)))): digit = Util.get_integer(filename) if digit is not None: self.features[filename] = self.get_feature(digit).flatten() def get_features(self): return self.features
def load_pretrained_vgg19_embedding(pretrained_weights): # Construct VGG19 model without the classifer and weights trained on imagenet data # '''Takes (224, 224, 3) RGB and returns the embeddings(predicions) generated on the RGB image''' feature_extractor = VGG19(input_shape=(224, 224, 3), include_top = False) x = feature_extractor.output flat = Flatten()(x) fc_1 = Dense(1024, activation='relu')(flat) do_1 = Dropout(0.2)(fc_1) fc_2 = Dense(512, activation='relu')(do_1) do_2 = Dropout(0.3)(fc_2) output = Dense(9, activation= 'softmax')(do_2) embed_model = Model(feature_extractor.inputs, output) # Compile model embed_model.compile(loss="categorical_crossentropy", optimizer=tf.keras.optimizers.Adam(lr=0.0004), metrics=["accuracy"]) print("Model Compiled") embed_model.load_weights(pretrained_weights) print("Loaded Finetuned Weights") return embed_model
def consume_requests(): try: pretrained_model = VGG19(include_top=False, weights='imagenet') subscriber = pubsub_v1.SubscriberClient() subscription_path = 'projects/sylvan-terra-269023/subscriptions/new-painter-request-pull' def callback(message): try: data = message.data.decode('utf-8') message.ack() json_data = json.loads(data) print(json_data) # call endpoint on client to update request status to PROCESSING update_request_status(json_data['painter_request_id'], STATUS_CODE_PROCESSING) engine.paint(json_data['painter_request_id'], json_data['content_image_path'], pretrained_model) update_request_status(json_data['painter_request_id'], STATUS_CODE_COMPLETE) except ValueError: raise future = subscriber.subscribe(subscription_path, callback=callback) try: future.result() except KeyboardInterrupt: future.cancel() except KeyboardInterrupt: raise
def __init__(self, img_w: int, img_h: int, channels: int): """ Args: img_w: Pixel width for input images img_h: Pixel height for input images channels: number of channels for input images """ self.img_w = img_w self.img_h = img_h self.channels = channels self.vgg19 = VGG19(weights='imagenet', include_top=False, input_tensor=Input(shape=(self.img_h, self.img_w, 3), name='input_sentinel')) # Load part of the VGG without the top layers into 'pretrained' model self.pretrained_model = models.Model( inputs=self.vgg19.input, outputs=self.vgg19.get_layer('block5_pool').output) self.config = self.pretrained_model.get_config() if self.channels == 1: self.hs_inputs = Input(shape=(self.img_h, self.img_w, 3), name='input') else: self.hs_inputs = Input(shape=(self.img_h, self.img_w, self.channels), name='input')
def _build_model(self, content: np.ndarray, style: np.ndarray) -> tuple: """ Build a synthesis model with the given content and style. Args: content: the content to fuse the artwork into style: the artwork to get the style from Returns: a tuple of: - the constructed VGG19 model from the input images - the canvas tensor for the synthesized image """ # load the content image into Keras as a constant, it never changes content_tensor = K.constant(content, name='Content') # load the style image into Keras as a constant, it never changes style_tensor = K.constant(style, name='Style') # create a placeholder for the trained image, this variable changes canvas = K.placeholder(content.shape, name='Canvas') # combine the content, style, and canvas tensors along the frame # axis (0) into a 4D tensor of shape [3, height, width, channels] tensor = K.concatenate([content_tensor, style_tensor, canvas], axis=0) # build the model with the input tensor of content, style, and canvas model = VGG19(include_top=False, input_tensor=tensor, pooling='avg') return model, canvas
def get_model(): if args.weights == 'None': args.weights = None if args.model in ['vgg16']: base_model = VGG16(include_top=False, weights=args.weights, input_shape=(img_row_size, img_col_size, 3)) elif args.model in ['vgg19']: base_model = VGG19(include_top=False, weights=args.weights, input_shape=(img_row_size, img_col_size, 3)) elif args.model in ['resnet50']: base_model = ResNet50(include_top=False, weights=args.weights, input_shape=(img_row_size, img_col_size, 3)) else: print('# {} is not a valid value for "--model"'.format(args.model)) exit() out = Flatten()(base_model.output) out = Dense(fc_size, activation='relu')(out) out = Dropout(0.5)(out) out = Dense(fc_size, activation='relu')(out) out = Dropout(0.5)(out) output = Dense(10, activation='softmax')(out) model = Model(inputs=base_model.input, outputs=output) sgd = SGD(lr=1e-4, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) return model
def get_base_model(model_name, weights_path, weight_decay=1e-4): """ Define base model used in transfer learning. """ if not weights_path: weights_path = 'imagenet' if model_name == 'VGG16': base_model = VGG16(weights=weights_path, include_top=False) elif model_name == 'VGG19': base_model = VGG19(weights=weights_path, include_top=False) elif model_name == 'ResNet50V1': base_model = awsdet.models.backbones.ResNet50(weights=None, include_top=False, weight_decay=weight_decay) elif model_name == 'ResNet50V2': base_model = awsdet.models.backbones.ResNet50V2(weights=None, include_top=False, weight_decay=weight_decay) elif model_name == 'Xception': base_model = Xception(weights=weights_path, include_top=False) elif model_name == 'InceptionV3': base_model = InceptionV3(weights=weights_path, include_top=False) elif model_name == 'InceptionResNetV2': base_model = InceptionResNetV2(weights=weights_path, include_top=False) elif model_name == 'MobileNet': base_model = MobileNet(weights=weights_path, include_top=False) else: raise ValueError( 'Valid base model values are: "VGG16","VGG19","ResNet50V1", "ResNet50V2", "Xception", \ "InceptionV3","InceptionResNetV2","MobileNet".' ) return base_model
def get_model(input_shape=(256, 256, 3)): pre_trained_model = VGG19(input_shape=input_shape, include_top=False, weights='imagenet') for layer in pre_trained_model.layers: layer.trainable = False last_layer = pre_trained_model.get_layer('block5_pool') last_output = last_layer.output model = Flatten()(last_output) model = Dense(1024)(model) model = LeakyReLU(0.1)(model) model = Dropout(0.25)(model) model = BatchNormalization()(model) model = Dense(1024)(model) model = LeakyReLU(0.1)(model) model = Dropout(0.25)(model) model = BatchNormalization()(model) model = Dense(1, activation='sigmoid')(model) fmodel = Model(pre_trained_model.input, model) fmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return fmodel
def _vgg(output_layer): vgg = VGG19(weights='imagenet', input_shape=(HR_SIZE, HR_SIZE, 3), include_top=False) mdl = Model(vgg.input, vgg.layers[output_layer].output) mdl.trainable = False return mdl
def vgg_19(hp): base_model = VGG19(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False) for layer in base_model.layers: layer.trainable = False x = Flatten()(base_model.output) for i in range(hp.Int('n_connected_layers', 1, 10)): x = Dense(units=hp.Int(f'Dense_{i}', min_value=64, max_value=512, step=64), activation=LeakyReLU( hp.Choice('leaky_relu', values=[0.2, 0.3, 0.1])))(x) x = Dropout(0.2)(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=x) model.compile( loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=hp.Choice('learning_rate', values=LEARNING_RATE))) return model
def loss(self, y_true, y_pred): """ Computes content loss for Lower Resolution Images ... Parameters ---------- y_true : tensor Actual Higher Resolution Image y_pred : tensor Predicted Higher Resolution Image Returns ------- content loss using VGG19 model """ vgg = VGG19(include_top=False, weights='imagenet', input_shape=self.image_dimensions) vgg.trainable = False for layer in vgg.layers: layer.trainable = False model = Model(vgg.input, vgg.get_layer('block5_conv4').output) model.trainable = False return K.mean(K.square(model(y_true) - model(y_pred)))
def mildnet_vgg19(): vgg_model = VGG19(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) for layer in vgg_model.layers[:10]: layer.trainable = False intermediate_layer_outputs = get_layers_output_by_name( vgg_model, ["block1_pool", "block2_pool", "block3_pool", "block4_pool"]) convnet_output = GlobalAveragePooling2D()(vgg_model.output) for layer_name, output in intermediate_layer_outputs.items(): output = GlobalAveragePooling2D()(output) convnet_output = concatenate([convnet_output, output]) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))( convnet_output) first_input = Input(shape=(224, 224, 3)) second_input = Input(shape=(224, 224, 3)) final_model = tf.keras.models.Model( inputs=[first_input, second_input, vgg_model.input], outputs=convnet_output) return final_model
def create_model(self, image_shape): WEIGHTS_PATH = ('https://github.com/fchollet/deep-learning-models/' 'releases/download/v0.1/' 'vgg19_weights_tf_dim_ordering_tf_kernels.h5') WEIGHTS_PATH_NO_TOP = ( 'https://github.com/fchollet/deep-learning-models/' 'releases/download/v0.1/' 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5') vgg19 = VGG19(include_top=False, weights=None, input_shape=image_shape) # Block 5 without activation relu x = Conv2D( 512, (3, 3), # activation='relu', padding='same', name='block5_conv4')(vgg19.get_layer('block5_conv3').output) #x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) model = Model(inputs=vgg19.input, outputs=x) weights_path = tf.keras.utils.get_file( 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', file_hash='253f8cb515780f3b799900260a226db6') model.load_weights(weights_path) model.trainable = False return model
def _build_model(self, l2_lambda=0.004, **kwargs): """build the model modification of the model follows the example given in: https://github.com/geifmany/cifar-vgg/blob/master/cifar10vgg.py""" # include top false - the end of the model is modified in this implementation # - always start from imagenet weights model = VGG19(include_top=False, weights='imagenet', input_shape=self.input_shape) # allow weights in all layers to be adjusted during training # - this is the default behavior, we show to be transparent for layer in model.layers: layer.trainable = True # add dense layer of 512 nodes with L2 regularization and relu activation x = model.output x = Flatten()(x) x = Dense(512, kernel_regularizer=l2(l2_lambda), activation="relu")(x) # add batch normalization - VGG19 does not include batch normalization by default x = BatchNormalization()(x) # add 50% dropout x = Dropout(0.5)(x) # prediction layer is a sigmoid since it is assumed this is a binary problem # - future versions will allow handling of multiple classes predictions = Dense(1, activation='sigmoid')(x) self.model = Model(inputs=model.input, outputs=predictions)
def getVGG19Architecture(classes, dropoutRate): # create the base pre-trained model base_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) for layer in enumerate(base_model.layers): layer[1].trainable = False #flatten the results from conv block x = Flatten()(base_model.output) #add another fully connected layers with batch norm and dropout x = Dense(4096, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(dropoutRate)(x) #add another fully connected layers with batch norm and dropout x = Dense(4096, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(dropoutRate)(x) #add logistic layer with all car classes predictions = Dense(len(classes), activation='softmax', kernel_initializer='random_uniform', bias_initializer='random_uniform', bias_regularizer=regularizers.l2(0.01), name='predictions')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) return model
def load_initial_vgg19_weights(model): """ """ print("Loading vgg19 weights...") vgg_model = VGG19(include_top=False, weights='imagenet') for w in model.weights: idx = w.name.find("/kernel") if idx > -1: nn = w.name[:idx] vgg_layer_name = from_vgg.get(nn) if vgg_layer_name: weights = vgg_model.get_layer(vgg_layer_name).get_weights()[0] w.assign(weights) print("Loaded VGG19 : " + w.name) continue idx = w.name.find("/bias") if idx > -1: nn = w.name[:idx] vgg_layer_name = from_vgg.get(nn) if vgg_layer_name: weights = vgg_model.get_layer(vgg_layer_name).get_weights()[1] w.assign(weights) print("Loaded VGG19 : " + w.name)
def define_model(num_of_classes): """ Model Definition :param num_of_classes: number of classes to predict. type: int :return: compiled model """ # load pretrained VGG19 with imagenet weights and without last layer model = VGG19(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) # freeze all the layers for layer in model.layers: layer.trainable = False # add new layers to the model flatten = Flatten()(model.layers[-1].output) classification = Dense(128, activation="relu")(flatten) dropout = Dropout(0.5)(classification) output = Dense(num_of_classes, activation="softmax")(dropout) model = Model(inputs=model.inputs, outputs=output) opt = SGD(lr=0.0001, momentum=0.9) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) return model
def load_a_model(self,model_name="",model_file_name=""): """ This function loads a model architecture and weights. :param model_name: Name of the model to load. model_name should be one of the following: "VGG16", "VGG19" :param model_file_name: Name of the file to load the model (if both madel_name and model_file_name are specified, model_name takes precedence). :return: model """ # if model_name is not None: # if model_name=="VGG16": # self.model=Sequential(VGG16().layers) # elif model_name=="VGG19": # self.model=Sequential(VGG19().layers) # elif model_file_name is not None: # self.model = load_model(model_file_name) if(model_name=="VGG16"): self. model= Sequential(VGG16().layers) elif(model_name=="VGG19"): self. model= Sequential(VGG19().layers) else: self.model= load_model(model_file_name)
def _transfer_vgg19_weight(FLAGS, weight_dict): from_model = VGG19(include_top=False, weights=FLAGS.VGG19_weights, input_tensor=None, input_shape=(FLAGS.HR_image_size, FLAGS.HR_image_size, FLAGS.channel)) fetch_weight = [] for layer in from_model.layers: if 'conv' in layer.name: W, b = layer.get_weights() fetch_weight.append( tf.assign( weight_dict[ 'loss_generator/perceptual_vgg19/{}/kernel'.format( layer.name)], W)) fetch_weight.append( tf.assign( weight_dict[ 'loss_generator/perceptual_vgg19/{}/bias'.format( layer.name)], b)) return fetch_weight
def loadPretrainedWeights(): pretrained_weights={} pretrained_weights['vgg16']=VGG16(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['vgg19']=VGG19(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['resnet50']=ResNet50(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inceptionv3']=InceptionV3(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inception-resentv2']=InceptionResNetV2(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['xception']=Xception(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet121']=DenseNet121(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet169']=DenseNet169(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet201']=DenseNet201(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['mobilenet']=MobileNet(weights='imagenet', include_top=False,pooling='avg') #N retrained_weights['nasnetlarge']=NASNetLarge(weights='imagenet', include_top=False,pooling='avg',input_shape = (224, 224, 3)) #N pretrained_weights['nasnetmobile']=NASNetMobile(weights='imagenet', include_top=False,pooling='avg') #N pretrained_weights['mobilenetV2']=MobileNetV2(weights='imagenet', include_top=False,pooling='avg') return pretrained_weights
def pre_trained_softmax(d): # ''' # param: d - d['list1'] = ['ResNet50', 'VGG16', 'VGG19', 'InceptionV3', 'Xception'] # - d['num_classes'] = num_classes # - d['img_size'] = img_size # - d['channels'] = channels # returns: pre_trained designed model # ''' model = Sequential() img_size = d['img_size'] channels = d['channels'] num_classes = d['num_classes'] input_shape = (img_size, img_size, channels) for i, pre_trained in enumerate(d['list1']): if pre_trained == 'ResNet50': # Working base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'VGG16': base_model = VGG16(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'VGG19': base_model = VGG19(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'InceptionV3': base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'Xception': base_model = Xception(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) return model
def __init__(self): self.model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) self.names_of_knn = [] self.dic_of_knn = {} self.knn_csv = DeleteLowFreqHashtags( PreprocessingHashtags('data/knn_data.csv'))
def getWeights(): tf.enable_eager_execution() vgg19 = VGG19(include_top=False, weights='imagenet') weights = {} for weight in vgg19.weights: weights[weight.name] = weight.numpy() return weights
def vgg_loss( y_true, y_pred): #From deepak https://gist.github.com/deepak112/c76ed1dbbfa3eff1249493eadfd2b9b5 vgg19 = VGG19(include_top=False, weights='imagenet', input_shape=(32,32,3)) vgg19.trainable = False for l in vgg19.layers: l.trainable = False model = Model(inputs=vgg19.input, outputs=vgg19.get_layer('block5_conv4').output) model.trainable = False return tf.math.reduce_mean(tf.math.square(model(y_true) - model(y_pred)))
def build_vgg_model(self): img = Input(shape=[None, None, 3]) vgg = VGG19(include_top=False) vgg.outputs = [vgg.layers[20].output] vgg54 = Model(inputs=img, outputs=vgg(img)) vgg54.trainable = False return vgg54
def get_kernel(params): model = VGG19(weights=None) optimizer = SGD() model.compile( loss="sparse_categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"], ) return model
def vgg19(input_size): input_tensor = Input(shape=(input_size, input_size, 3)) base_model = VGG19(include_top=False, weights='imagenet', input_shape=(input_size, input_size, 3)) model = create_model(base_model, input_tensor) return model
def __init__(self): # TODO: need out own trained VGG for satelliate imagery self.vgg = VGG19(input_shape=(96, 96, 3), weights='imagenet', include_top=False) self.vgg.trainable = False for layer in self.vgg.layers: layer.trainable = False
def build_vgg(gen): vgg = VGG19(weights="imagenet") # 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=(96,96,3)) # Extract image features img_features = vgg(img) return Model(img, img_features)