예제 #1
0
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 ranknet():
    vgg_model = VGG19(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
    convnet_output = GlobalAveragePooling2D()(vgg_model.output)
    convnet_output = Dense(4096, activation='relu')(convnet_output)
    convnet_output = Dropout(0.5)(convnet_output)
    convnet_output = Dense(4096, activation='relu')(convnet_output)
    convnet_output = Dropout(0.5)(convnet_output)
    convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output)

    s1 = MaxPool2D(pool_size=(4, 4), strides=(4, 4), padding='valid')(vgg_model.input)
    s1 = ZeroPadding2D(padding=(4, 4), data_format=None)(s1)
    s1 = Conv2D(96, kernel_size=(8, 8), strides=(4, 4), padding='valid')(s1)
    s1 = ZeroPadding2D(padding=(2, 2), data_format=None)(s1)
    s1 = MaxPool2D(pool_size=(7, 7), strides=(4, 4), padding='valid')(s1)
    s1 = Flatten()(s1)

    s2 = MaxPool2D(pool_size=(8, 8), strides=(8, 8), padding='valid')(vgg_model.input)
    s2 = ZeroPadding2D(padding=(4, 4), data_format=None)(s2)
    s2 = Conv2D(96, kernel_size=(8, 8), strides=(4, 4), padding='valid')(s2)
    s2 = ZeroPadding2D(padding=(1, 1), data_format=None)(s2)
    s2 = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='valid')(s2)
    s2 = Flatten()(s2)

    merge_one = concatenate([s1, s2])
    merge_one_norm = Lambda(lambda x: K.l2_normalize(x, axis=1))(merge_one)
    merge_two = concatenate([merge_one_norm, convnet_output], axis=1)
    emb = Dense(4096)(merge_two)
    l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb)

    final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=l2_norm_final)

    return final_model
    def __init__(self,
                 x,
                 t,
                 LR,
                 input_shape,
                 output_shape,
                 model_name='FCN_ResNet50'):
        # optimization setting
        self.LR = LR

        # naming setting
        self.model_name = model_name

        # pretrain model
        self.vgg19 = VGG19(include_top=False,
                           weights='imagenet',
                           input_tensor=None,
                           input_shape=input_shape[1:])
        self.vgg19.trainable = True
        #for layer in resnet50.layers[:164]:
        #    layer.trainable = False

        # model setting
        self.input_shape = input_shape
        self.output_shape = output_shape
        with tf.variable_scope(self.model_name):
            self.x = x
            self.t = t
            self.features = self.vgg19(inputs=self.x)  # (2, 13, 2048)
            self.y = self._forward_pass(self.features)
예제 #4
0
def vgg_19_model(img_width, img_height, color_type=3):
    # create the base pre-trained model
    base_model = VGG19(weights='imagenet', include_top=False, input_shape=(img_width, img_height, color_type))
    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(2048, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.8)(x)

    #add another fully connected layers with batch norm and dropout
    x = Dense(2048, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.8)(x)

    #add another fully connected layers with batch norm and dropout
    x = Dense(4096, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.8)(x)
   

    #add logistic layer with all car classes
    predictions = Dense(10, 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
예제 #5
0
파일: vgg.py 프로젝트: johSchm/faceDeep
 def get_base_model(self):
     """ Returns the corresponding Keras VGG model.
     :return: model
     """
     if self.version == VGGVersion.VGG_16:
         return VGG16(input_shape=self.input_shape, include_top=True, weights='imagenet')
     if self.version == VGGVersion.VGG_19:
         return VGG19(input_shape=self.input_shape, include_top=True, weights='imagenet')
     else:
         raise ValueError("VGG Version not found!")
예제 #6
0
 def initialize_models_and_layers(self):
     self.model = VGG19(include_top=False, weights='imagenet')
     self.model.trainable = False
     self.style_layers = ['block1_conv1', 'block2_conv1',
                          'block3_conv1', 'block4_conv1', 'block5_conv1']
     self.content_layer = 'block5_conv2'
     self.content_model = Model(inputs=self.model.input,
                                outputs=self.model.get_layer(self.content_layer).output)
     self.style_models = [Model(inputs=self.model.input,
                                outputs=self.model.get_layer(layer).output) for layer in self.style_layers]
예제 #7
0
    def vgg_loss_model(self):
        vgg_in = Input(shape=self.shape)
        vgg_model = VGG19(include_top=False, input_tensor=vgg_in)
        vgg_out = vgg_model.get_layer('block3_conv1').output

        vgg = Model(vgg_in, vgg_out)
        for layer in vgg.layers:
            layer.trainable = False

        return vgg
예제 #8
0
파일: Train.py 프로젝트: Tubbz-alt/eyeSRGAN
def build_vgg(target_shape_vgg):
    vgg19 = VGG19(include_top=False, input_shape=target_shape_vgg, weights='imagenet')

    vgg19.trainable = False
    for layer in vgg19.layers:
        layer.trainable = False

    # vgg_model = Model(inputs=vgg19.input, outputs=vgg19.layers[20].output, name="VGG")
    vgg_model = Model(inputs=vgg19.input, outputs=vgg19.get_layer("block5_conv4").output, name="VGG")

    return vgg_model
예제 #9
0
    def _build_model(self, num_classes: int):
        self.__base_model = VGG19(weights='imagenet',
                                  include_top=False,
                                  input_shape=(self.__width, self.__height, 3))

        x = self.__base_model.output
        x = GlobalAveragePooling2D()(x)
        x = Dense(512, activation='relu')(x)
        predictions = Dense(num_classes, activation='softmax')(x)

        self.__model = Model(inputs=self.__base_model.input,
                             outputs=predictions)
예제 #10
0
def classification(
    shape: Tuple[int, int, int] = (224, 224, 3),
    n_class: int = 3,
    model_net: str = "Resnet50V2",
    resnet_train: bool = True,
) -> Model:
    """
    Modelo de classificação entre covid, normal e pneumonia

    Args:
    -----
        input_size (tuple, optional): Tamanho da imagem de entrada.
                                      Defaults to (224, 224, 3).
        n_class (int, optional): Número de classes de saída.
                                 Defaults to 3.

    Returns:
    --------
        (keras.Model) : Modelo do keras
    """
    inputs = Input(shape, name="entrada_modelo")
    model = Conv2D(
        filters=3,
        kernel_size=(3, 3),
        padding="same",
        activation="relu",
        name="conv_gray_rgb",
    )(inputs)
    params = {
        "include_top": False,
        "weights": "imagenet",
        "input_shape": (shape[0], shape[1], 3),
        "pooling": "avg",
    }
    if model_net == "VGG19":
        base_model = VGG19(**params)
    elif model_net == "InceptionResNetV2":
        base_model = InceptionV3(**params)
    elif model_net == "MobileNetV2":
        base_model = MobileNetV3Small(**params)
    elif model_net == "DenseNet201":
        base_model = DenseNet201(**params)
    else:
        base_model = ResNet50V2(**params)
    base_model.trainable = resnet_train
    model = base_model(model)
    model = Dropout(0.5, name="drop_0")(model)
    model = Dense(units=256, name="dense_0")(model)
    model = Dropout(0.5, name="drop_1")(model)
    model = Dense(units=n_class, name="classifier")(model)
    predictions = Activation(activation="softmax", name="output")(model)
    return Model(inputs=inputs, outputs=predictions)
예제 #11
0
파일: losses.py 프로젝트: OrangeBai/C3DLab
    def vgg_loss_fixed_img(y_true, y_pred):
        vgg19 = VGG19(include_top=False,
                      weights='imagenet',
                      input_shape=image_shape)
        vgg19.trainable = False
        # Make trainable as False
        for layer in vgg19.layers:
            layer.trainable = False
        model = Model(inputs=vgg19.input,
                      outputs=vgg19.get_layer('block5_conv4').output)
        model.trainable = False

        return bk.mean(bk.square(model(y_true) - model(y_pred)))
def init_model(model_name):
    if (model_name == "VGG19"):  # Initialisierung des VGG19
        return VGG19(include_top=True, weights='imagenet')
    if (model_name == "VGG16"):
        return tf.keras.applications.VGG16(include_top=True,
                                           weights='imagenet')
    if (model_name == "ResNet50"):
        return ResNet50(include_top=True, weights="imagenet")
    if (model_name == "DenseNet201"):
        return DenseNet201(include_top=True, weights="imagenet")
    if (model_name == "DenseNet121"):
        return DenseNet121(include_top=True, weights="imagenet")
    if (model_name == "InceptionResNetV2"):
        return InceptionResNetV2(include_top=True, weights="imagenet")
def create_model(num_classes):
    # base_model = InceptionV3(include_top=False, weights='imagenet')
    base_model = VGG19(include_top=False, weights='imagenet')

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(256, activation='relu')(x)
    predictions = Dense(num_classes, activation='softmax')(x)

    for layer in base_model.layers:
        layer.trainable = False

    model = Model(base_model.input, predictions)

    return model
예제 #14
0
 def _load_model(self, model='VGG'):
     if model=='VGG':
         print("Loading VGG19 pre-trained model...")
         base_model = VGG19(weights='imagenet')
         base_model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
         x = base_model.output
         x = GlobalAveragePooling2D()(x)
         self.VGG_model = Model(inputs=base_model.input, outputs=x)
     
     if model=='Inception_Resnet':
         #load Inception_Resnet model
         print("Loading Inception_Resnet_V2 pre-trained model...")
         base_model = InceptionResNetV2(weights='imagenet', include_top=False)
         x = base_model.output
         x = GlobalAveragePooling2D()(x)
         self.IR_model = Model(inputs=base_model.input, outputs=x)
예제 #15
0
def vgg_19():
    input_image = Input(shape=(224,224,3), name='input_image')
    model = VGG19(include_top=False,weights=None,input_tensor=input_image)
    model.load_weights("model/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5")
    # input_image = model.input
    #model.layers.pop()
    # model.layers[-1].outbound_nodes = []
    #conv5_layer = model.layers[-1]
    #outputs = conv5_layer.output #<----看!是output,输出,输出的是一个tensor

    conv5_layer = model.get_layer("block5_pool")
    print(conv5_layer)
    outputs = conv5_layer.output  # <----看!是output,输出,输出的是一个tensor

    print("vgg_conv5_layer",outputs)
    return input_image,outputs # 去掉VGG16的2个1x1卷积,返回的是一个张量,VGG16是一个Model(也就是Functional)的模型,不是Sequential的
예제 #16
0
def apply_PreTrained_model(model, data):
    # Se crea el modelo pre entrenado seleccionado
    # include_top=False no incluye las ultimas capas Softmax
    if model == "VGG16":
        CNN_model = VGG16(include_top=False,
                          input_shape=(48, 48, 3),
                          pooling='avg',
                          weights='imagenet')

    if model == "VGG19":
        CNN_model = VGG19(include_top=False,
                          input_shape=(48, 48, 3),
                          pooling='avg',
                          weights='imagenet')

    result = CNN_model.predict(data)
    # Se aplica a los datos y se retorna el modelo para contruir el modelo final completo
    return result, CNN_model
예제 #17
0
    def similarity(self, n_neighbors=6):
        base_model = VGG19(weights=self.get_weights())  # w_tm: 5.669065000000001
        print(f'Base model loaded: {self.get_timing()}')
        # with graph.as_default():
        vecs = self.load_sparse_matrix()  # w_tm: 11.862487999999999

        # Read about fc1 here http://cs231n.github.io/convolutional-networks/
        model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
        print(f'model without layers loaded: {self.get_timing()}')  # w_tm: 11.863167999999998
        knn = NearestNeighbors(metric='cosine', algorithm='brute')
        knn.fit(vecs)
        print(f'Training without control: {self.get_timing()}')
        list_data = self.config.filename_list

        if self.list_content == 'article':
            list_data = self.config.article_list
        print(f'Got list data: {self.get_timing()}')
        vec = self._vectorize(model)
        print(f'Vector search img got: {self.get_timing()}')  # w_tm: 20.432828
        return self._similar(vec, knn, list_data, n_neighbors)
예제 #18
0
 def get_model(self, model_name, weights):
     if model_name == 'InceptionV3':
         from tensorflow.python.keras.applications.inception_v3 import InceptionV3
         base_model = InceptionV3(weights=weights, include_top=False)
     elif model_name == 'NASNetLarge':
         from tensorflow.python.keras.applications.nasnet import NASNetLarge
         base_model = NASNetLarge(weights=weights, include_top=False)
     elif model_name == 'DenseNet201':
         from tensorflow.python.keras.applications.densenet import DenseNet201
         base_model = DenseNet201(weights=weights, include_top=False)
     elif model_name == 'Xception':
         from tensorflow.python.keras.applications.xception import Xception
         base_model = Xception(weights=weights, include_top=False)
     elif model_name == 'VGG16':
         from tensorflow.python.keras.applications.vgg16 import VGG16
         base_model = VGG16(weights=weights, include_top=False)
     elif model_name == 'VGG19':
         from tensorflow.python.keras.applications.vgg19 import VGG19
         base_model = VGG19(weights=weights, include_top=False)
     elif model_name == 'NASNetMobile':
         from tensorflow.python.keras.applications.nasnet import NASNetMobile
         base_model = NASNetMobile(weights=weights, include_top=False)
     elif model_name == 'MobileNetV2':
         from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2
         base_model = MobileNetV2(weights=weights, include_top=False)
     elif model_name == 'ResNet50':
         from tensorflow.python.keras.applications.resnet50 import ResNet50
         base_model = ResNet50(weights=weights, include_top=False)
     elif model_name == 'InceptionResNetV2':
         from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2
         base_model = InceptionResNetV2(weights=weights, include_top=False, )
     else:
         raise KeyError('Unknown network.')
     x = base_model.output
     x = GlobalAveragePooling2D()(x)
     x = Dense(512, activation='relu')(x)
     predictions = Dense(1, activation='sigmoid')(x)
     model = Model(inputs=base_model.input, outputs=predictions)
     return model
예제 #19
0
파일: siamese_model.py 프로젝트: mx0551/IQA
 def get_model(self, model_name, weights='imagenet'):
     if model_name == 'InceptionV3':
         from tensorflow.python.keras.applications.inception_v3 import InceptionV3
         base_model = InceptionV3(weights=weights, include_top=False)
     elif model_name == 'NASNetLarge':
         from tensorflow.python.keras.applications.nasnet import NASNetLarge
         base_model = NASNetLarge(weights=weights, include_top=False)
     elif model_name == 'DenseNet201':
         from tensorflow.python.keras.applications.densenet import DenseNet201
         base_model = DenseNet201(weights=weights, include_top=False)
     elif model_name == 'Xception':
         from tensorflow.python.keras.applications.xception import Xception
         base_model = Xception(weights=weights, include_top=False)
     elif model_name == 'VGG19':
         from tensorflow.python.keras.applications.vgg19 import VGG19
         base_model = VGG19(weights=weights, include_top=False)
     elif model_name == 'NASNetMobile':
         from tensorflow.python.keras.applications.nasnet import NASNetMobile
         base_model = NASNetMobile(weights=weights, include_top=False)
     elif model_name == 'MobileNetV2':
         from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2
         base_model = MobileNetV2(weights=weights, include_top=False)
     elif model_name == 'ResNet50':
         from tensorflow.python.keras.applications.resnet50 import ResNet50
         base_model = ResNet50(weights=weights, include_top=False)
     elif model_name == 'InceptionResNetV2':
         from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2
         base_model = InceptionResNetV2(weights=weights, include_top=False)
     else:
         raise KeyError('Unknown network.')
     x = base_model.output
     x = GlobalAveragePooling2D()(x)
     # x = Dense(1024, activation='relu')(x)
     output = Dense(1, activation='sigmoid')(x)
     # output = SiameseLossLayer(self.batch_size, self.num_distort, self.num_level)(x)
     model = Model(inputs=base_model.input, outputs=output)
     self.name = model_name
     return model
예제 #20
0
    def get(self, request):
        id = request.query_params['id']
        image_path = 'media/' + id
        print(image_path)
        # load VGG19 model
        print("Loading VGG19 pre-trained model...")
        base_model = VGG19(weights='imagenet')
        base_model = Model(inputs=base_model.input,
                           outputs=base_model.get_layer('block4_pool').output)
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        VGG_model = Model(inputs=base_model.input, outputs=x)
        img_VGG = image.load_img(image_path, target_size=(224, 224))

        img = image.img_to_array(img_VGG)  # convert to array

        img = np.expand_dims(img, axis=0)
        img = ppVGG19(img)

        features = VGG_model.predict(img).flatten()
        point_a = features.reshape(1, 512)
        filename = []
        score = []
        for i in os.listdir('data/features'):
            point_b = np.load('data/features/' + i).reshape(1, 512)
            filename.append(i)
            distance = np.linalg.norm(point_a - point_b)

            cos_lib = cosine_similarity(point_a, point_b)
            score.append(cos_lib[0][0])

        ind = score.index((max(score)))
        print(ind)
        print(filename[ind])
        product = products.objects.get(obj_id=filename[ind])

        serialier = productsSerializer(product)
        return Response(serialier.data)
예제 #21
0
    def __init__(self,
                 input_tar_path,
                 ranking_matrix,
                 index2name,
                 m0=0.1,
                 learning_rate=0.01,
                 epochs=None,
                 batchSz=None):

        # images placeholder
        self.Ia_place = tf.placeholder(shape=[None, None, None, 3],
                                       dtype=tf.float32)
        self.Ic_place = tf.placeholder(shape=[None, None, None, 3],
                                       dtype=tf.float32)
        self.If_place = tf.placeholder(shape=[None, None, None, 3],
                                       dtype=tf.float32)

        # ranking placeholder
        self.rankings = ranking_matrix
        self.rank_c_f = tf.placeholder(shape=[None], dtype=tf.float32)

        self.pretrained_vgg = VGG19(weights='imagenet',
                                    pooling='avg',
                                    include_top=False)

        # index2name MAP
        self.index2name = index2name
        # imageset
        self.tar_set = tarfile.open(input_tar_path)

        # construct graph
        self.m0 = m0
        self.loss = self.construct_loss()
        self.train = self.optimize()
        print(np.shape(self.rankings)[1])
        pass
예제 #22
0
def get_base_model():
    model = VGG19(input_shape=(224, 224, 3),
                  weights='imagenet',
                  include_top=False)
    model.layers.pop()
    model.layers.pop()
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-2]._outbound_nodes = []
    x = Conv2D(256, kernel_size=(2, 2), strides=2)(model.output)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(128, kernel_size=(2, 2), strides=1)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.3)(x)
    x = Activation('relu')(x)
    x = Flatten()(x)
    x = Dense(len(class_names), activation='softmax')(x)
    model = Model(model.input, x)

    for layer in model.layers[:22]:
        layer.trainable = False

    return model
def run_model(args):
    # Configure the memory optimizer
    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    config = tf.ConfigProto()
    config.graph_options.rewrite_options.memory_optimization = rewriter_config_pb2.RewriterConfig.SCHEDULING_HEURISTICS
    #config.gpu_options.allow_growth=True
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    K.set_session(tf.Session(config=config))

    num_classes = args.num_classes
    batch_size = args.batch_size

    model_name = args.model
    if model_name == 'ResNet50':
        model = ResNet50(weights=None,
                         include_top=True,
                         input_shape=input_shape,
                         classes=num_classes)
    elif model_name == 'ResNet101':
        model = keras.applications.resnet.ResNet101(weights=None,
                                                    include_top=True,
                                                    input_shape=input_shape,
                                                    classes=num_classes)
    elif model_name == 'ResNet152':
        model = ResNet152(weights=None,
                          include_top=True,
                          input_shape=input_shape,
                          classes=num_classes)
    elif model_name == 'VGG16':
        model = VGG16(weights=None,
                      include_top=True,
                      input_shape=input_shape,
                      classes=num_classes)
    elif model_name == 'VGG19':
        model = VGG19(weights=None,
                      include_top=True,
                      input_shape=input_shape,
                      classes=num_classes)
    elif model_name == 'Xception':
        model = Xception(weights=None,
                         include_top=True,
                         input_shape=input_shape,
                         classes=num_classes)
    elif model_name == 'MobileNet':
        model = MobileNet(weights=None,
                          include_top=True,
                          input_shape=input_shape,
                          classes=num_classes)
    elif model_name == 'MobileNetV2':
        model = MobilenetV2(weights=None,
                            include_top=True,
                            input_shape=input_shape,
                            classes=num_classes)
    elif model_name == 'InceptionV3':
        model = InceptionV3(weights=None,
                            include_top=True,
                            input_shape=input_shape,
                            classes=num_classes)
    else:
        print('Running with ResNet50 -- the default model')
        model = ResNet50(weights=None,
                         include_top=True,
                         input_shape=input_shape,
                         classes=num_classes)
    execute_model(model, input_shape)
예제 #24
0
# Here we import our model
import tensorflow as tf
from tensorflow.python.keras.applications.vgg19 import VGG19
from tensorflow.python.keras.preprocessing.image import load_img,img_to_array
from tensorflow.python.keras.applications.vgg19 import preprocess_input
from tensorflow.python.keras.models import Model
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


model = VGG19(
    include_top = False,
    weights = 'imagenet'
)
model.trainable = False
# model.summary()

# here we are going to do image preprocessing and displaying
def load_and_process_image(image_path):
    img = load_img(image_path)
    img = img_to_array(img)
    img = preprocess_input(img)
    img = np.expand_dims(img,axis=0)
    return img

def deprocess(image):
    image[:,:,0] +=103.939
    image[:,:,1] +=116.779
    image[:,:,2] +=123.68
    image = image[:,:,::-1]
예제 #25
0
def _vgg(output_layer):
    vgg = VGG19(input_shape=(None, None, 3), include_top=False)
    return Model(vgg.input, vgg.layers[output_layer].output)
예제 #26
0
    content_image[:, :, 1] -= 116.779
    content_image[:, :, 2] -= 103.939

    # Reshape to 1 batch size.
    image_shape = (1, ) + content_image.shape  # shape = (1,224,224,3)
    content_image = tf.reshape(tf.constant(content_image, dtype=tf.float32),
                               shape=image_shape)
    generated_image = tf.reshape(generated_image, shape=image_shape)

    # Concatenate with two image(Tensors).
    input_tensor = tf.concat([generated_image, content_image], axis=0)

    # Load pretrained model using Keras API
    with tf.variable_scope('pretrained_model'):
        model = VGG19(weights='imagenet',
                      input_tensor=input_tensor,
                      include_top=False)
        keras_variables = [
            var.name for var in tf.global_variables()
            if 'pretrained_model' in var.name
        ]

    # Output Tensor of Keras model into Dictionary
    output_dict = {layer.name: layer.output for layer in model.layers}

    # Loss
    feature_vectors = output_dict['block4_conv1']
    generated_feature = feature_vectors[0, :, :, :]
    content_feature = feature_vectors[1, :, :, :]
    """
    (l2_normalize)||Φ(σx) − Φ0||/||Φ0||   +    RV(x)
예제 #27
0
def vgg():
    _ = VGG19(input_shape=(None, None, 3), include_top=False)
    return Model(_.input, _.layers[20].output)
예제 #28
0
    elif category == 2:
        file_name.append(i)
        categories.append(2)
    else:
        file_name.append(i)
        categories.append(1)

# Closing file
f.close()

#create a dataframe including file_name and category together
df = pd.DataFrame({'filename': file_name, 'category': categories})

# VGG19 initialization
vgg = VGG19(weights="imagenet",
            include_top=False,
            input_tensor=Input(shape=(IMAGE_WIDTH, IMAGE_WIDTH,
                                      IMAGE_CHANNELS)))
for layer in vgg.layers:
    layer.trainable = False
model = Sequential()
model.add(vgg)
model.add(Flatten())
model.add(Dense(units=512, activation='relu', kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Dense(units=512, activation='relu', kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(
    units=3,
    activation='softmax'))  # 3 because we have mask, no mask and some mask
    # start time
    print("start time - {}".format(
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))
    start = time.time()

    # create the pretrained models
    if model_name == "vgg16":
        base_model = VGG16(weights=weights)
        try:
            model = Model(base_model.input, base_model.get_layer('fc1').output)
        except:
            model = Model(input=base_model.input,
                          output=base_model.get_layer('fc1').output)
        image_size = (224, 224)
    elif model_name == "vgg19":
        base_model = VGG19(weights=weights)
        try:
            model = Model(base_model.input, base_model.get_layer('fc1').output)
        except:
            model = Model(input=base_model.input,
                          output=base_model.get_layer('fc1').output)
        image_size = (224, 224)
    elif model_name == "resnet50":
        base_model = ResNet50(weights=weights)
        try:
            model = Model(base_model.input,
                          base_model.get_layer('avg_pool').output)
        except:
            model = Model(input=base_model.input,
                          output=base_model.get_layer('avg_pool').output)
        image_size = (224, 224)
예제 #30
0
def get_vgg_layers(layer_names):
    vgg = VGG19(include_top=False, weights='imagenet')
    vgg.trainable = False
    outputs = [vgg.get_layer(name).output for name in layer_names]
    model = models.Model([vgg.input], outputs)
    return model