Python keras.applications.vgg16 模块,VGG16 实例源码

我们从Python开源项目中,提取了以下34个代码示例,用于说明如何使用keras.applications.vgg16.VGG16

项目:head-segmentation    作者:szywind    | 项目源码 | 文件源码
def fcn_32s(input_dim, nb_classes=2):
    inputs = Input(shape=(input_dim,input_dim,3))
    vgg16 = VGG16(weights=None, include_top=False, input_tensor=inputs)
    pretrain_model_path = "../weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
    if not os.path.exists(pretrain_model_path):
        raise RuntimeError("No pretrained model loaded.")
    vgg16.load_weights(pretrain_model_path)
    x = Conv2D(filters=nb_classes,
               kernel_size=(1, 1))(vgg16.output)
    x = Conv2DTranspose(filters=nb_classes,
                        kernel_size=(64, 64),
                        strides=(32, 32),
                        padding='same',
                        activation='sigmoid',
                        kernel_initializer=initializers.Constant(bilinear_upsample_weights(32, nb_classes)))(x)
    model = Model(inputs=inputs, outputs=x)
    for layer in model.layers[:15]:
        layer.trainable = False
    return model
项目:keras    作者:GeekLiB    | 项目源码 | 文件源码
def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    if K.image_dim_ordering() == 'th':
        x = x.transpose((1, 2, 0))
    x = np.clip(x, 0, 255).astype('uint8')
    return x

# build the VGG16 network with ImageNet weights
项目:pCVR    作者:xjtushilei    | 项目源码 | 文件源码
def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    if K.image_data_format() == 'channels_first':
        x = x.transpose((1, 2, 0))
    x = np.clip(x, 0, 255).astype('uint8')
    return x

# build the VGG16 network with ImageNet weights
项目:neural-style-keras    作者:robertomest    | 项目源码 | 文件源码
def get_loss_net(pastiche_net_output, input_tensor=None):
    '''
    Instantiates a VGG net and applies its layers on top of the pastiche net's
    output.
    '''
    loss_net = vgg16.VGG16(weights='imagenet', include_top=False,
                           input_tensor=input_tensor)
    targets_dict = dict([(layer.name, layer.output) for layer in loss_net.layers])
    i = pastiche_net_output
    # We need to apply all layers to the output of the style net
    outputs_dict = {}
    for l in loss_net.layers[1:]: # Ignore the input layer
        i = l(i)
        outputs_dict[l.name] = i

    return loss_net, outputs_dict, targets_dict
项目:KAGGLE_CERVICAL_CANCER_2017    作者:ZFTurbo    | 项目源码 | 文件源码
def get_learning_rate(cnn_type):
    if cnn_type == 'VGG16' or cnn_type == 'VGG16_DROPOUT':
        return 0.00004
    elif cnn_type == 'VGG16_KERAS':
        return 0.00005
    elif cnn_type == 'VGG19':
        return 0.00003
    elif cnn_type == 'VGG19_KERAS':
        return 0.00005
    elif cnn_type == 'RESNET50':
        return 0.00004
    elif cnn_type == 'INCEPTION_V3':
        return 0.00003
    elif cnn_type == 'SQUEEZE_NET':
        return 0.00003
    elif cnn_type == 'DENSENET_161':
        return 0.00003
    elif cnn_type == 'DENSENET_121':
        return 0.00001
    else:
        print('Error Unknown CNN type for learning rate!!')
        exit()
    return 0.00005
项目:KAGGLE_CERVICAL_CANCER_2017    作者:ZFTurbo    | 项目源码 | 文件源码
def get_random_state(cnn_type):
    if cnn_type == 'VGG16' or cnn_type == 'VGG16_DROPOUT':
        return 51
    elif cnn_type == 'VGG19_KERAS':
        return 52
    elif cnn_type == 'RESNET50':
        return 53
    elif cnn_type == 'INCEPTION_V3':
        return 54
    elif cnn_type == 'VGG16_KERAS':
        return 55
    elif cnn_type == 'VGG19':
        return 56
    elif cnn_type == 'SQUEEZE_NET':
        return 66
    elif cnn_type == 'DENSENET_161':
        return 71
    elif cnn_type == 'DENSENET_121':
        return 72
    else:
        print('Error Unknown CNN Type for random state!!')
        exit()
    return 0
项目:KAGGLE_CERVICAL_CANCER_2017    作者:ZFTurbo    | 项目源码 | 文件源码
def get_batch_size(cnn_type):
    if cnn_type == 'VGG19' or cnn_type == 'VGG19_KERAS':
        return 18
    if cnn_type == 'VGG16_DROPOUT':
        return 15
    if cnn_type == 'VGG16' or cnn_type == 'VGG16_KERAS':
        return 20
    if cnn_type == 'RESNET50':
        return 20
    if cnn_type == 'INCEPTION_V3':
        return 22
    if cnn_type == 'SQUEEZE_NET':
        return 40
    if cnn_type == 'DENSENET_161':
        return 8
    if cnn_type == 'DENSENET_121':
        return 25
    return -1
项目:KAGGLE_CERVICAL_CANCER_2017    作者:ZFTurbo    | 项目源码 | 文件源码
def VGG_16_KERAS(classes_number, optim_name='Adam', learning_rate=-1):
    from keras.layers.core import Dense, Dropout, Flatten
    from keras.applications.vgg16 import VGG16
    from keras.models import Model

    base_model = VGG16(include_top=True, weights='imagenet')
    x = base_model.layers[-2].output
    del base_model.layers[-1:]
    x = Dense(classes_number, activation='softmax', name='predictions')(x)
    vgg16 = Model(input=base_model.input, output=x)

    optim = get_optim('VGG16_KERAS', optim_name, learning_rate)
    vgg16.compile(optimizer=optim, loss='categorical_crossentropy', metrics=['accuracy'])
    # print(vgg16.summary())
    return vgg16


# MIN: 1.00 Fast: 60 sec
项目:KAGGLE_CERVICAL_CANCER_2017    作者:ZFTurbo    | 项目源码 | 文件源码
def VGG_16_2_v2(classes_number, optim_name='Adam', learning_rate=-1):
    from keras.layers.core import Dense, Dropout, Flatten
    from keras.applications.vgg16 import VGG16
    from keras.models import Model
    from keras.layers import Input

    input_tensor = Input(shape=(3, 224, 224))
    base_model = VGG16(input_tensor=input_tensor, include_top=False, weights='imagenet')
    x = base_model.output
    x = Flatten()(x)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.2)(x)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.2)(x)
    x = Dense(classes_number, activation='softmax', name='predictions')(x)
    vgg16 = Model(input=base_model.input, output=x)

    optim = get_optim('VGG16_KERAS', optim_name, learning_rate)
    vgg16.compile(optimizer=optim, loss='categorical_crossentropy', metrics=['accuracy'])
    # print(vgg16.summary())
    return vgg16
项目:auto-triage    作者:zhijian-liu    | 项目源码 | 文件源码
def feature_extractor(FLAGS, suffix = ""):
  weights = FLAGS.weights if FLAGS.weights != "random" else None

  if FLAGS.model == "vgg16":
    from keras.applications.vgg16 import VGG16
    feature_extractor = VGG16(weights = weights)
    remove_last_layer(feature_extractor)
  elif FLAGS.model == "vgg19":
    from keras.applications.vgg19 import VGG19
    feature_extractor = VGG19(weights = weights)
    remove_last_layer(feature_extractor)
  elif FLAGS.model == "resnet50":
    from keras.applications.resnet50 import ResNet50
    feature_extractor = ResNet50(weights = weights)
    remove_last_layer(feature_extractor)
  else:
    raise NotImplementedError

  feature_extractor.name = FLAGS.model + suffix

  if FLAGS.regularizer == "l2":
    add_regularizer(feature_extractor)
  elif FLAGS.regularizer != "none":
    raise NotImplementedError
  return feature_extractor
项目:DEC-keras    作者:XifengGuo    | 项目源码 | 文件源码
def extract_vgg16_features(x):
    from keras.preprocessing.image import img_to_array, array_to_img
    from keras.applications.vgg16 import preprocess_input, VGG16
    from keras.models import Model

    # im_h = x.shape[1]
    im_h = 224
    model = VGG16(include_top=True, weights='imagenet', input_shape=(im_h, im_h, 3))
    # if flatten:
    #     add_layer = Flatten()
    # else:
    #     add_layer = GlobalMaxPool2D()
    # feature_model = Model(model.input, add_layer(model.output))
    feature_model = Model(model.input, model.get_layer('fc1').output)
    print('extracting features...')
    x = np.asarray([img_to_array(array_to_img(im, scale=False).resize((im_h,im_h))) for im in x])
    x = preprocess_input(x)  # data - 127. #data/255.#
    features = feature_model.predict(x)
    print('Features shape = ', features.shape)

    return features
项目:keras-customized    作者:ambrite    | 项目源码 | 文件源码
def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    if K.image_dim_ordering() == 'th':
        x = x.transpose((1, 2, 0))
    x = np.clip(x, 0, 255).astype('uint8')
    return x

# build the VGG16 network with ImageNet weights
项目:alt-i2v    作者:GINK03    | 项目源码 | 文件源码
def build_model():
  input_tensor = Input(shape=(150, 150, 3))
  vgg16_model = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)
  dense  = Flatten()( \
             Dense(2048, activation='relu')( \
               BN()( \
             vgg16_model.layers[-1].output ) ) )
  result = Activation('sigmoid')(\
             Activation('linear')( \
           Dense(4096)(\
                 dense) ) )

  model = Model(input=vgg16_model.input, output=result)
  for i in range(len(model.layers)):
    print(i, model.layers[i])
  for layer in model.layers[:12]: # default 15
    layer.trainable = False
  model.compile(loss='binary_crossentropy', optimizer='adam')
  return model

#build_model()
项目:keras    作者:NVIDIA    | 项目源码 | 文件源码
def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    if K.image_dim_ordering() == 'th':
        x = x.transpose((1, 2, 0))
    x = np.clip(x, 0, 255).astype('uint8')
    return x

# build the VGG16 network with ImageNet weights
项目:visimil    作者:rene4jazz    | 项目源码 | 文件源码
def get_features(url):
    response = requests.get(url)
    img = Image.open(BytesIO(response.content)).convert('RGB')

    target_size = (224, 224)
    model = VGG16(weights='imagenet', include_top=False, pooling='avg')

    if img.size != target_size:
        img = img.resize(target_size)

    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    features = model.predict(x).flatten()
    return features.tolist()
项目:geom_rcnn    作者:asbroad    | 项目源码 | 文件源码
def make_model(self):
        # create the base pre-trained model
        if self.model_architecture == 'vgg16':
            from keras.applications.vgg16 import VGG16
            self.base_model = VGG16(weights='imagenet', include_top=False)
        elif self.model_architecture == 'resnet':
            from keras.applications.resnet50 import ResNet50
            self.base_model = ResNet50(weights='imagenet', include_top=False)
        elif self.model_architecture == 'inception':
            from keras.applications.inception_v3 import InceptionV3
            self.base_model = InceptionV3(weights='imagenet', include_top=False)
        else:
            print 'Model architecture parameter unknown. Options are: vgg16, resnet, and inception'
            rospy.signal_shutdown("Model architecture unknown.")

        # now we add a new dense layer to the end of the network inplace of the old layers
        x = self.base_model.output
        x = GlobalAveragePooling2D()(x)
        x = Dense(1024, activation='relu')(x)
        # add the outplut layer
        predictions = Dense(len(self.categories.keys()), activation='softmax')(x)

        # create new model composed of pre-trained network and new final layers
        # if you want to change the input size, you can do this with the input parameter below
        self.model = Model(input=self.base_model.input, output=predictions)

        # now we go through and freeze all of the layers that were pretrained
        for layer in self.base_model.layers:
            layer.trainable = False

        if self.verbose:
            print 'compiling model ... '
            start_time = time.time()

        # in finetuning, these parameters can matter a lot, it is wise to observe 
        # how well your model is learning for this to work well
        self.model.compile(optimizer=self.optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

        if self.verbose:
            end_time = time.time()
            self.print_time(start_time,end_time,'compiling model')
项目:spark-deep-learning    作者:databricks    | 项目源码 | 文件源码
def model(self, preprocessed, featurize):
        # Model provided by Keras. All cotributions by Keras are provided subject to the
        # MIT license located at https://github.com/fchollet/keras/blob/master/LICENSE
        # and subject to the below additional copyrights and licenses.
        #
        # Copyright 2014 Oxford University
        #
        # Licensed under the Creative Commons Attribution License CC BY 4.0 ("License").
        # You may obtain a copy of the License at
        #
        #     https://creativecommons.org/licenses/by/4.0/
        #
        return vgg16.VGG16(input_tensor=preprocessed, weights="imagenet",
                           include_top=(not featurize))
项目:spark-deep-learning    作者:databricks    | 项目源码 | 文件源码
def _testKerasModel(self, include_top):
        return vgg16.VGG16(weights="imagenet", include_top=include_top)
项目:FCN_via_keras    作者:k3nt0w    | 项目源码 | 文件源码
def __init__(self, batchsize=1, img_height=224, img_width=224, FCN_CLASSES=21):
        self.batchsize = batchsize
        self.img_height = img_height
        self.img_width = img_width
        self.FCN_CLASSES = FCN_CLASSES
        self.vgg16 = VGG16(include_top=False,
                           weights='imagenet',
                           input_tensor=None,
                           input_shape=(3, self.img_height, self.img_width))
项目:flask-keras-cnn-image-retrieval    作者:willard-yuan    | 项目源码 | 文件源码
def extract_feat(img_path):
    # weights: 'imagenet'
    # pooling: 'max' or 'avg'
    # input_shape: (width, height, 3), width and height should >= 48

    input_shape = (224, 224, 3)
    model = VGG16(weights = 'imagenet', input_shape = (input_shape[0], input_shape[1], input_shape[2]), pooling = 'max', include_top = False)

    img = image.load_img(img_path, target_size=(input_shape[0], input_shape[1]))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    feat = model.predict(img)
    norm_feat = feat[0]/LA.norm(feat[0])
    return norm_feat
项目:planet-amazon-deforestation    作者:EKami    | 项目源码 | 文件源码
def create_model(img_dim=(128, 128, 3)):
    input_tensor = Input(shape=img_dim)
    base_model = VGG16(include_top=False,
                       weights='imagenet',
                       input_shape=img_dim)

    bn = BatchNormalization()(input_tensor)
    x = base_model(bn)
    x = Flatten()(x)
    output = Dense(17, activation='sigmoid')(x)
    model = Model(input_tensor, output)
    return model
项目:qtim_ROP    作者:QTIM-Lab    | 项目源码 | 文件源码
def _configure_network(self, build=True):

        network = self.config['network']
        type_, weights = network['type'].lower(), network.get('weights', None)
        fine_tuning = " with pre-trained weights '{}'".format(weights) if weights else " without pre-training"

        if 'vgg' in type_:

            from keras.applications.vgg16 import VGG16
            logging.info("Instantiating VGG model" + fine_tuning)
            self.model = VGG16(weights=weights, input_shape=(3, 227, 227), include_top=True)

        elif 'resnet' in type_:

            from keras.applications.resnet50 import ResNet50
            logging.info("Instantiating ResNet model" + fine_tuning)

            input_layer = Input(shape=(3, 224, 224))
            base_model = ResNet50(weights=weights, include_top=False, input_tensor=input_layer)

            x = base_model.output
            x = Flatten()(x)
            x = Dense(1024, activation='relu')(x)
            x = Dropout(0.5)(x)
            predictions = Dense(3, activation='softmax')(x)

            self.model = Model(input=base_model.input, output=predictions)
            # for layer in base_model.layers:
            #     layer.trainable = fine_tuning

        else:

            if 'googlenet' in type_:
                custom_objects = {"PoolHelper": PoolHelper, "LRN": LRN}
                mod_str = 'GoogLeNet'
            else:
                custom_objects = {}
                mod_str = 'custom'

            from googlenet import create_googlenet
            logging.info("Instantiating {} model".format(mod_str) + fine_tuning)
            arch = network.get('arch', None)

            if arch is None:
                self.model = create_googlenet(network.get('no_classes', 3), network.get('no_features', 1024))
            else:
                self.model = model_from_json(open(arch).read(), custom_objects=custom_objects)

            if weights:
                print "Loading weights '{}'".format(weights)
                self.model.load_weights(weights, by_name=True)

        # Configure optimizer
        if build:
            opt_options = self.config['optimizer']
            name, loss, params = opt_options['type'], opt_options['loss'], opt_options['params']
            optimizer = OPTIMIZERS[name](**params)
            self.model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
项目:Deconvnet-keras    作者:Jallet    | 项目源码 | 文件源码
def main():
    parser = argparser()
    args = parser.parse_args()
    image_path = args.image
    layer_name = args.layer_name
    feature_to_visualize = args.feature
    visualize_mode = args.mode

    model = vgg16.VGG16(weights = 'imagenet', include_top = True)
    layer_dict = dict([(layer.name, layer) for layer in model.layers])
    if not layer_dict.has_key(layer_name):
        print('Wrong layer name')
        sys.exit()

    # Load data and preprocess
    img = Image.open(image_path)
    img = img.resize(224, 224)
    img_array = np.array(img)
    img_array = np.transpose(img_array, (2, 0, 1))
    img_array = img_array[np.newaxis, :]
    img_array = img_array.astype(np.float)
    img_array = imagenet_utils.preprocess_input(img_array)

    deconv = visualize(model, img_array, 
            layer_name, feature_to_visualize, visualize_mode)

    # postprocess and save image
    deconv = np.transpose(deconv, (1, 2, 0))
    deconv = deconv - deconv.min()
    deconv *= 1.0 / (deconv.max() + 1e-8)
    deconv = deconv[:, :, ::-1]
    uint8_deconv = (deconv * 255).astype(np.uint8)
    img = Image.fromarray(uint8_deconv, 'RGB')
    img.save('results/{}_{}_{}.png'.format(layer_name, feature_to_visualize, visualize_mode))
项目:head-segmentation    作者:szywind    | 项目源码 | 文件源码
def vgg16():
    base_model = VGG16(weights=None, include_top=False, input_shape = (224,224,3))
    # Classification block
    x = Flatten(name='flatten', input_shape=base_model.output_shape[1:])(base_model.output)
    x = Dense(512, activation='relu', name='fc1')(x)
    x = Dense(512, activation='relu', name='fc2')(x)
    x = Dense(17, activation='softmax', name='predictions')(x)

    model = Model(inputs=base_model.input, outputs=x)
    return model
项目:Artistic-Style-Transfer-using-Keras-Tensorflow    作者:anujdutt9    | 项目源码 | 文件源码
def style_loss(style, combination):
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = ht * wd
    return K.sum(K.square(S - C))/(4. *(channels ** 2)*(size ** 2))

# Feature Layers of VGG16 Trained Neural Network
项目:unblackboxing_webinar    作者:deepsense-ai    | 项目源码 | 文件源码
def __init__(self, input_shape, classes, model_save_filepath):        
        self.model_save_filepath = model_save_filepath  

        self.neptune_organizer = None

        self.old_session = K.get_session()
        session = tf.Session('')
        K.set_session(session)
        K.set_learning_phase(1)

        face_input = Input(batch_shape=(None,) + (input_shape))

        pretrained_model = VGG16(input_tensor=face_input, 
                                 weights='imagenet', 
                                 include_top=False)
        x = pretrained_model.get_layer('block4_pool').output

        x = Flatten(name='flatten')(x)
        x = Dense(256, activation='relu', name='fc1')(x)
        x = Dense(256, activation='relu', name='fc2')(x)
        output = Dense(classes, activation='softmax', name='predictions')(x)

        self.facenet = Model(face_input, output)
        self.facenet.compile(optimizer='adam',
                             loss='categorical_crossentropy',
                             metrics=['accuracy'])
        self.facenet.summary()

        self.datagen = ImageDataGenerator(rotation_range=5,
                                          horizontal_flip=False, 
                                          vertical_flip=True)
项目:unblackboxing_webinar    作者:deepsense-ai    | 项目源码 | 文件源码
def load_model(img_size = (100,100,3),mode = "VGG"):
    '''softmax
        Helper for loading model. Only VGG available now
    '''

    if mode == "VGG":
        input_template = Input(batch_shape=(None,) + img_size)

        model = VGG16(input_tensor=input_template, 
                      weights='imagenet', 
                      include_top=False)
    else:
        raise NotImplemented

    return model
项目:VGG    作者:jackfan00    | 项目源码 | 文件源码
def VGGregionModel(inputshape):
    input_tensor = Input(shape=inputshape) #(448, 448, 3))
    vgg_model = VGG16(input_tensor=input_tensor, weights='imagenet', include_top=False )

    # add region detection layers
    x = vgg_model.output
    x = Flatten()(x)
    #x = Dense(256, activation='relu')(x)
    #x = Dense(2048, activation='relu')(x)
    #x = Dropout(0.5)(x)
    x = Dense((cfgconst.side**2)*(cfgconst.classes+5)*cfgconst.bnum)(x)

    model = Model(input=vgg_model.input, output=x)
    #
    print 'returned model:'
    index = 0
    for l in model.layers:
        if index <= (18-8):
            l.trainable = False
                #print l.name+' '+str(l.input_shape)+' -> '+str(l.output_shape)+', trainable:'+str(l.trainable)
        index = index + 1

    return model

#
# pretrained
#model = VGGregionModel((448, 448, 3) )
项目:VGG    作者:jackfan00    | 项目源码 | 文件源码
def VGGregionModel(inputshape):
    input_tensor = Input(shape=inputshape) #(448, 448, 3))
    vgg_model = VGG16(input_tensor=input_tensor, weights='imagenet', include_top=False )

    # add region detection layers
    x = vgg_model.output
    x = Flatten()(x)
    #x = Dense(256, activation='relu')(x)
    #x = Dense(2048, activation='relu')(x)
    #x = Dropout(0.5)(x)
    x = Dense((cfgconst.side**2)*(cfgconst.classes+5)*cfgconst.bnum)(x)

    model = Model(input=vgg_model.input, output=x)
    #
    print 'returned model:'
    index = 0
    for l in model.layers:
        if index <= (18-8):
            l.trainable = False
                #print l.name+' '+str(l.input_shape)+' -> '+str(l.output_shape)+', trainable:'+str(l.trainable)
        index = index + 1

    return model

#
# pretrained
#model = VGGregionModel((448, 448, 3) )
项目:keras-transfer-learning-for-oxford102    作者:Arsey    | 项目源码 | 文件源码
def _create(self):
        base_model = KerasVGG16(weights='imagenet', include_top=False, input_tensor=self.get_input_tensor())
        self.make_net_layers_non_trainable(base_model)

        x = base_model.output
        x = Flatten()(x)
        x = Dense(4096, activation='elu', name='fc1')(x)
        x = Dropout(0.6)(x)
        x = Dense(self.noveltyDetectionLayerSize, activation='elu', name=self.noveltyDetectionLayerName)(x)
        x = Dropout(0.6)(x)
        predictions = Dense(len(config.classes), activation='softmax', name='predictions')(x)

        self.model = Model(input=base_model.input, output=predictions)
项目:npeg    作者:ctmakro    | 项目源码 | 文件源码
def apply_vgg(tensor):
    print('importing VGG16...')
    from keras.applications.vgg16 import VGG16
    from keras import backend as K
    K.set_session(ct.get_session()) # make sure we are in the same universe

    vgginst = VGG16(include_top=False, weights='imagenet', input_tensor=tensor)
    return vgginst.get_layer('block1_conv2').output
项目:mcv-m5    作者:david-vazquez    | 项目源码 | 文件源码
def build_vgg(img_shape=(3, 224, 224), n_classes=1000, n_layers=16, l2_reg=0.,
                load_pretrained=False, freeze_layers_from='base_model'):
    # Decide if load pretrained weights from imagenet
    if load_pretrained:
        weights = 'imagenet'
    else:
        weights = None

    # Get base model
    if n_layers==16:
        base_model = VGG16(include_top=False, weights=weights,
                           input_tensor=None, input_shape=img_shape)
    elif n_layers==19:
        base_model = VGG19(include_top=False, weights=weights,
                           input_tensor=None, input_shape=img_shape)
    else:
        raise ValueError('Number of layers should be 16 or 19')

    # Add final layers
    x = base_model.output
    x = Flatten(name="flatten")(x)
    x = Dense(4096, activation='relu', name='dense_1')(x)
    x = Dropout(0.5)(x)
    x = Dense(4096, activation='relu', name='dense_2')(x)
    x = Dropout(0.5)(x)
    x = Dense(n_classes, name='dense_3_{}'.format(n_classes))(x)
    predictions = Activation("softmax", name="softmax")(x)

    # This is the model we will train
    model = Model(input=base_model.input, output=predictions)

    # Freeze some layers
    if freeze_layers_from is not None:
        if freeze_layers_from == 'base_model':
            print ('   Freezing base model layers')
            for layer in base_model.layers:
                layer.trainable = False
        else:
            for i, layer in enumerate(model.layers):
                print(i, layer.name)
            print ('   Freezing from layer 0 to ' + str(freeze_layers_from))
            for layer in model.layers[:freeze_layers_from]:
               layer.trainable = False
            for layer in model.layers[freeze_layers_from:]:
               layer.trainable = True

    return model
项目:keras_zoo    作者:david-vazquez    | 项目源码 | 文件源码
def build_vgg(img_shape=(3, 224, 224), n_classes=1000, n_layers=16, l2_reg=0.,
                load_pretrained=False, freeze_layers_from='base_model'):
    # Decide if load pretrained weights from imagenet
    if load_pretrained:
        weights = 'imagenet'
    else:
        weights = None

    # Get base model
    if n_layers==16:
        base_model = VGG16(include_top=False, weights=weights,
                           input_tensor=None, input_shape=img_shape)
    elif n_layers==19:
        base_model = VGG19(include_top=False, weights=weights,
                           input_tensor=None, input_shape=img_shape)
    else:
        raise ValueError('Number of layers should be 16 or 19')

    # Add final layers
    x = base_model.output
    x = Flatten(name="flatten")(x)
    x = Dense(4096, activation='relu', name='dense_1')(x)
    x = Dropout(0.5)(x)
    x = Dense(4096, activation='relu', name='dense_2')(x)
    x = Dropout(0.5)(x)
    x = Dense(n_classes, name='dense_3_{}'.format(n_classes))(x)
    predictions = Activation("softmax", name="softmax")(x)

    # This is the model we will train
    model = Model(input=base_model.input, output=predictions)

    # Freeze some layers
    if freeze_layers_from is not None:
        if freeze_layers_from == 'base_model':
            print ('   Freezing base model layers')
            for layer in base_model.layers:
                layer.trainable = False
        else:
            for i, layer in enumerate(model.layers):
                print(i, layer.name)
            print ('   Freezing from layer 0 to ' + str(freeze_layers_from))
            for layer in model.layers[:freeze_layers_from]:
               layer.trainable = False
            for layer in model.layers[freeze_layers_from:]:
               layer.trainable = True

    return model
项目:jamespy_py3    作者:jskDr    | 项目源码 | 文件源码
def run(epochs, PP=None, rgb_mean=None):
    # Load Data
    ig = image.ImageDataGenerator()  # rescale=1/255.0)
    it = ig.flow_from_directory(
        'tiny-imagenet-200/train/', target_size=(64, 64), batch_size=1000)
    ystr2y = it.class_indices
    Xt, Yt, yt = img.load_validation_data(
        it.class_indices, 'tiny-imagenet-200/val/images/', 'tiny-imagenet-200/val/val_annotations.txt')

    Num_classes = 200
    Input_shape = (64, 64, 3)

    # Build a Model
    vgg_model = VGG16(include_top=False)
    x = Input(shape=Input_shape)
    if PP:
        h = PP(8, 0.5, rgb_mean)(x)
        base_model = get_new_base_model_x_h(vgg_model, x, h)
    else:
        base_model = get_new_base_model_x(vgg_model, x)

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    predictions = Dense(Num_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)

    for layer in base_model.layers:
        layer.trainable = False
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy', metrics=['accuracy'])

    # Training
    model.fit_generator(it, 1000, epochs=epochs, validation_data=(Xt, Yt))

    # Class Activiation Map
    conv_model = base_model
    Ft = conv_model.predict(Xt[:20])

    Wb_all = model.get_weights()
    L_Wb = len(Wb_all)

    W_dense = Wb_all[L_Wb - 2]
    b_dense = Wb_all[L_Wb - 1]
    W_dense.shape, b_dense.shape

    CAM = CAM_process(Ft, yt, W_dense)

    from kakao import bbox
    for i in range(20):
        plt.figure(figsize=(12, 4))
        plt.subplot(1, 3, 1)
        plt.imshow(Xt[i])
        plt.subplot(1, 3, 2)
        CAM4 = img.cam_intp_reshape(CAM[i], Xt[i].shape[:2])
        plt.imshow(CAM4, interpolation='bicubic', cmap='jet_r')
        plt.subplot(1, 3, 3)
        plt.imshow(Xt[i])
        plt.imshow(CAM4, interpolation='bicubic', cmap='jet_r', alpha=0.5)
        plt.show()