def vgg_12(pretrained_weights='None', input_size=(256,256,1), initial_weights=True, lr=1e-4):
    
    new_input = Input(shape=input_size)
    
    if initial_weights:
        model = VGG16(include_top=False, input_tensor=new_input)
    else:
        model = VGG16(include_top=False, input_tensor=new_input, weights=None)
    
    # remove last 3 layers of the vgg16 model
    x = model.layers[-5].output
    pool1 = AveragePooling2D(pool_size=(4, 4))(x)
    flat1 = Flatten()(pool1)
    dense1 = Dense(1024, activation="relu")(flat1)
    drop1 = Dropout(0.5)(dense1)
    dense2 = Dense(1, activation="linear")(drop1)
    
    model = Model(inputs=model.inputs, outputs=dense2)
    
    # Compile Our Transfer Learning Model
    model.compile(loss='mean_squared_error', optimizer=Adam(lr=lr), metrics=['mse', 'mae', 'mape'])
    
    print(model.summary())

    if pretrained_weights!='None':
        print('Using pretrained weights:', pretrained_weights)
        model.load_weights(pretrained_weights)
        
    return model
Example #2
0
 def __init__(self):
     super(Nested_VGG, self).__init__()
     self.vgg1 = VGG16(weights=None)
     # print([x.name for x in self.vgg1.weights])
     self.vgg2 = VGG16(weights=None)
     self.dense = Conv2D(64, (3, 3),
                         activation='relu',
                         padding='same',
                         name='block1_conv1')
Example #3
0
    def build_vgg16(self):

        model = VGG16(include_top=False,
                      weights='imagenet',
                      input_shape=self.img_shape)

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

        for layer in model.layers[17:]:
            layer.trainable = True

        x = Flatten()(model.output)
        d1 = Dense(4096, activation='relu')(x)
        d2 = Dropout(0.5)(d1)
        d3 = Dense(4096, activation='relu')(d2)
        outputs = Dense(self.num_classes,
                        kernel_regularizer=regularizers.l2(0.01),
                        kernel_initializer="normal",
                        activation='linear')(d3)  #(d3)
        finetuning = Model(inputs=[model.input], outputs=[outputs])
        finetuning.compile(optimizer='adam',
                           loss='hinge',
                           metrics=['accuracy'])

        return finetuning
Example #4
0
def vgg_12(pretrained_weights=None, input_size=(256, 256, 1)):

    new_input = Input(shape=input_size)

    #model = VGG16(include_top=False, input_tensor=new_input, weights=None)
    model = VGG16(include_top=False, input_tensor=new_input)

    # Say not to train ResNet model layers as they are already trained
    #for layer in model.layers:
    #	layer.trainable = False

    # remove last 3 layers of the vgg16 model
    x = model.layers[-5].output
    pool1 = AveragePooling2D(pool_size=(4, 4))(x)
    flat1 = Flatten()(pool1)
    dense1 = Dense(1024, activation="relu")(flat1)
    drop1 = Dropout(0.5)(dense1)
    dense2 = Dense(1, activation="linear")(drop1)

    model = Model(inputs=model.inputs, outputs=dense2)

    # Compile Our Transfer Learning Model
    model.compile(loss='mean_squared_error',
                  optimizer=Adam(lr=1e-4),
                  metrics=['mse', 'mae', 'mape'])

    print(model.summary())

    if (pretrained_weights):
        print('Using pretrained weights:', pretrained_weights)
        model.load_weights(pretrained_weights)

    return model
Example #5
0
    def __init__(self,training,batch_size,optimizer,loss,epochs,step_per_epoch,directory_output):


        #decido se voglio il transfer learning o il fine tuning 2 opzioni 'transfer_learning' 'fine_tuning'
        self.training=training

        self.model = VGG16(include_top=True, weights='imagenet')


        self.class_weights=None

        self.batch_size=batch_size
        if(isinstance(self.batch_size,int)):
            #originale= self.batch_size=self.batch_size
            pass
        else:
            self.error="Batch Size non è un numero"

        if optimizer=="Adam":

            self.optimizer = Adam(lr=1e-5)


        self.loss = 'categorical_crossentropy'

        print self.loss

        self.save_to_dir=directory_output

        self.metrics = ['{}'.format('categorical_accuracy')]

        #numero di epoche *steps_per_epoca=batch_size*numero_training
        self.epochs=epochs
        #numero di training per epoca
        self.step_per_epoch=step_per_epoch
Example #6
0
def FCN32(input_shape, dropout_rate=0.0, classes=2):
    """
    Implementation of the FCN32 network based on the VGG16 from
    keras.applications.

    :param input_shape: Model input shape.
    :type input_shape: (int, int)
    :param dropout_rate: dropout rate to be used in the model.
    :type dropout_rate: float
    :param classes: Number of classes for the segmantation.
    :type classes: int
    :return: FCN 32 Keras model.
    :rtype: `tensorflow.python.keras.Model`
    """
    net = VGG16(include_top=False, input_shape=input_shape, weights=None)
    inputs = net.input
    base_output = net.output

    net = Conv2D(4096, (7, 7), padding='same', activation='relu')(base_output)
    net = Dropout(dropout_rate)(net)
    net = Conv2D(4096, (1, 1), padding='same', activation='relu')(net)
    net = Dropout(dropout_rate)(net)
    net = Conv2D(classes, (1, 1))(net)
    net = Conv2DTranspose(classes, (64, 64),
                          strides=32,
                          use_bias=False,
                          padding='same',
                          activation='softmax')(net)
    model = Model(inputs, net, name='fcn32')
    # check the model using the summary
    model.summary()
    return model
def create_model(model_size):
    my_new_model = Sequential()
    if model_size == 'L':
        resnet_weights_path = 'input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
        resnet = ResNet50(include_top=False,
                          pooling='avg',
                          weights=resnet_weights_path)
        #resnet.summary()
        my_new_model.add(resnet)
        my_new_model.layers[0].trainable = False
    else:
        vgg_weights_path = 'input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
        vgg = VGG16(include_top=False, weights=vgg_weights_path)
        vgg.summary()
        my_new_model.add(vgg)
        my_new_model.add(GlobalAveragePooling2D())
        my_new_model.layers[0].trainable = False
        my_new_model.layers[1].trainable = False

    my_new_model.add(Dense(NUM_CLASSES, activation='softmax'))

    # Say no to train first layer (ResNet) model. It is already trained

    opt = optimizers.adam()
    my_new_model.compile(optimizer=opt,
                         loss='categorical_crossentropy',
                         metrics=['accuracy'])
    return my_new_model
Example #8
0
def build_vgg16_notop(image_dimensions,
                      pooling,
                      size_final_dense,
                      num_classes,
                      trainable=False,
                      weights='imagenet'):
    """
    Creates the VGG16 model without the final "top" dense layersself.
    Removing these top layers allows the VGG16 convolutional base to be used with a different set of image classes than the original VGG16 was trained on
    """

    vgg16_base = VGG16(
        weights=weights,
        include_top=False  # Ignore the final dense layers, we'll train our own
        ,
        input_shape=image_dimensions,
        pooling=pooling)
    vgg16_base.trainable = trainable

    image_input = Input(shape=image_dimensions)

    x = vgg16_base(image_input)
    x = Flatten()(x)
    x = Dense(size_final_dense, activation='relu')(x)
    out = Dense(num_classes, activation='softmax')(x)  # Task is classification

    model = Model(image_input, out)
    return (model)
Example #9
0
def get_transfer_model(num_classes, batch_size=20):
	model = VGG16(include_top=True, weights='imagenet')
	input_shape = model.layers[0].output_shape[1:3]
	transfer_layer = model.get_layer('block5_pool')
	conv_model = Model(inputs=model.input,
	                   outputs=transfer_layer.output)

	# Start a new Keras Sequential model.
	new_model = Sequential()
	# Add the convolutional part of the VGG16 model from above.
	new_model.add(conv_model)
	# Flatten the output of the VGG16 model because it is from a
	# convolutional layer.
	new_model.add(Flatten())
	# Add a dense (aka. fully-connected) layer.
	# This is for combining features that the VGG16 model has
	# recognized in the image.
	new_model.add(Dense(1024, activation='relu'))
	# Add a dropout-layer which may prevent overfitting and
	# improve generalization ability to unseen data e.g. the test-set.
	new_model.add(Dropout(0.5))
	# Add the final layer for the actual classification.
	new_model.add(Dense(num_classes, activation='softmax'))

	optimizer = Adam(lr=1e-5)
	loss = 'categorical_crossentropy'
	metrics = ['categorical_accuracy']

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

	new_model.compile(optimizer=optimizer, loss=loss, metrics=metrics)

	return new_model
Example #10
0
def bottleneck_features(input_shape, X_train, X_test):
    vgg_conv = VGG16(weights="imagenet",
                     include_top=False,
                     input_shape=input_shape)
    train_bottleneck = get_bottleneck(X_train, vgg_conv)
    test_bottleneck = get_bottleneck(X_test, vgg_conv)
    return train_bottleneck, test_bottleneck
Example #11
0
def callback(data):
    print(data.data)
    vgg16_net = VGG16(weights='imagenet',
                      include_top=False,
                      input_shape=(350, 350, 3))
    vgg16_net.trainable = False

    model = Sequential()
    model.add(vgg16_net)
    # Добавляем в модель новый классификатор
    model.add(Flatten())
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam(lr=1e-5),
                  metrics=['accuracy'])
    model.load_weights(path + "mnist_model.h5")

    img = image.load_img(path + 'IMG/' + data.data, target_size=(350, 350))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x) / 255
    preds = model.predict(x)
    classes = ['duck', 'none', 'nut', 'wheel']
    print(classes[np.argmax(preds)])
    pub = rospy.Publisher('class', String, queue_size=2)
    time.sleep(1)
    pub.publish(data.data[4] + ' ' + str(classes[np.argmax(preds)]))
def index(request):
    if request.method == 'POST':
        image = Images(image=request.FILES.get('image'))
        image.save()
        images = request.FILES.get('image')

        path = "./media/images/" + str(images)
        max_tokens = 30
        global language_model, tokenizer, graph, sess, image_model, VGG_last_layer, vgg_model
        with graph.as_default():

            path_checkpoint = 'model_weights.keras'
            language_model.load_weights(path_checkpoint)

            image_model = VGG16(include_top=True, weights='imagenet')
            VGG_last_layer = image_model.get_layer('fc2')
            vgg_model = Model(inputs=image_model.input,
                              outputs=VGG_last_layer.output)

            cap = generate_captions(vgg_model, language_model, tokenizer, path,
                                    graph, max_tokens)
            return render(request, "generatecaption/viewImage.html",
                          {"cap": cap})

    return render(request, "generatecaption/index.html")
 def load_model(self):
     """This function is used to load pretrained model
     """
     usage = """USAGE: %python run_experiment.py -m [model]
                      Model ResNet50:                  python run_experiment.py -o resnet50
                      Model VGG16:                     python run_experiment.py -o vgg16
                      Model LeNet-5:                   python run_experiment.py -o lenet5
                      Model AlexNet:                   python run_experiment.py -o alexnet
                      Model Xception:                  python run_experiment.py -o xception
                      Model InceptionV3:               python run_experiment.py -o inceptionv3
             """
     try:
         # resnet50
         if self.model_name == "resnet50":
             from tensorflow.python.keras.applications import ResNet50
             from tensorflow.python.keras.applications.resnet50 import preprocess_input
             self.preprocess = preprocess_input
             self.model = ResNet50()
         # vgg16
         elif self.model_name == "vgg16":
             from tensorflow.python.keras.applications import VGG16
             from tensorflow.python.keras.applications.vgg16 import preprocess_input
             self.preprocess = preprocess_input
             self.model = VGG16()
         # # vgg19
         # elif self.model_name == "lenet5":
         #     # compiling and training teacher network
         #     teacher = LeNet5Teacher()
         #     teacher.__init__()
         #     teacher.load(cfg.lenet_config + ".json", cfg.lenet_config + ".h5")
         #     self.model = teacher.getModel()
         # xception
         elif self.model_name == "xception":
             from tensorflow.python.keras.applications import Xception
             from tensorflow.python.keras.applications.xception import preprocess_input
             self.preprocess = preprocess_input
             self.model = Xception()
         # inceptionv3
         elif self.model_name == "inceptionv3":
             from tensorflow.python.keras.applications import InceptionV3
             from tensorflow.python.keras.applications.inception_v3 import preprocess_input
             self.preprocess = preprocess_input
             self.model = InceptionV3()
         # alexnet
         elif self.model_name == "alexnet":
             # TODO get a pre-trained alexnet model
             self.logger.info("[ERROR]: Not yet implemented")
         # custom teacher model
         # elif self.model_name == "custom_teacher":
         #     # compiling and training teacher network
         #     teacher = TeacherCNN()
         #     teacher.__init__()
         #     teacher.load(cfg.custom_teacher_config + ".json", cfg.custom_teacher_config + ".h5")
         #     self.model = teacher.getModel()
         else:
             self.logger.error("Invalid model name")
     except Exception as e:
         self.logger.error("Loading of model failed due to {0}".format(
             str(e)))
Example #14
0
def CNN():
    image_model = VGG16(include_top=True, weights='imagenet')
    transfer_layer = image_model.get_layer('fc2')
    image_model_transfer = Model(inputs=image_model.input,
                                 outputs=transfer_layer.output)
    img_size = K.int_shape(image_model.input)[1:3]
    transfer_values_size = K.int_shape(transfer_layer.output)[1]
    return transfer_values_size
Example #15
0
def get_model_vgg(input_shape=(128, 128, 16), classes=4, activation='sigmoid'):
    resnet = VGG16(weights=None, include_top=False, input_shape=input_shape)
    resnet.summary()
    model = Sequential()
    model.add(resnet)
    model.add(GlobalAveragePooling2D())
    model.add(
        Dense(units=classes,
              activation=activation,
              kernel_initializer="he_normal"))

    return model
def create_model():
    print("create model ...")
    inputs = Input(shape=(32, 32, 3))
    resize = Lambda(Resize, (256, 256, 3))(inputs)
    base_model = VGG16(include_top=False, pooling="avg")
    GAV = base_model(resize)
    #GAV=GlobalAveragePooling2D()(conv)
    outputs = Dense(10, activation='softmax')(GAV)
    model = Model(inputs, outputs)
    model.compile(optimizer='sgd',
                  loss="categorical_crossentropy",
                  metrics=['accuracy'])
    return model
def model_vgg(bayesian=False, dropout=[0.2, 0.2, 0]):
    inputs = Input(shape=(224, 224, 3))
    x = VGG16(include_top=False, pooling='avg', weights='imagenet')(inputs)
    x = Dense(1024, activation='relu')(x)
    x = Dropout(dropout[0])(x, training=bayesian)
    x = Dense(512, activation='relu')(x)
    x = Dropout(dropout[1])(x, training=bayesian)
    x = Dense(5, activation='linear')(x)
    x = Dropout(dropout[2])(x, training=bayesian)
    model = Model(inputs, x)
    # model.layers[1].trainable = False
    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss='mse')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='mse')
    return model
def lossnet(Restrainable=True):
    model = Sequential()
    if Restrainable:
        model.add(VGG16(include_top=False, pooling='avg', weights='imagenet'))
    model.add(Dense(2048, activation='relu'))
    model.add(Dropout(rate=0.2))
    model.add(Dense(1024, kernel_regularizer=l2(0.01), activation='relu'))
    model.add(Dropout(rate=0.2))
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(rate=0.2))  #, kernel_regularizer=l2(0.01)
    model.add(Dense(1, activation='linear'))
    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, decay=1e-6, momentum=0.9), loss='mse')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='mse')
    return model
    def __init__(self,
                 ROI_F_Fsize=[7, 7],
                 base_extractor_name=None,
                 base_opt_layer_name='conv5_3'):

        self._Fsize_h = ROI_F_Fsize[0]
        self._Fsize_w = ROI_F_Fsize[1]

        self._image_model_transfer = None
        if base_extractor_name is not None:
            if base_extractor_name is 'VGG16':
                image_model = VGG16(include_top=True, weights='imagenet')
                transfer_layer = image_model.get_layer(base_opt_layer_name)
                self._image_model_transfer = Model(
                    inputs=image_model.input, outputs=transfer_layer.output)
Example #20
0
def vgg16_enc_model(output_layers=None, freeze=True):
    """Returns a model that has all block pool layers of VGG16 as output.
  """
    if not output_layers:
        output_layers = ['block' + str(i) + '_pool' for i in range(1, 6)]
    from tensorflow.python.keras.applications import VGG16
    vgg16 = VGG16(include_top=False, weights='imagenet')
    if freeze:
        for layer in vgg16.layers:
            layer.trainable = False

    pool_layer_names = ['block' + str(i) + '_pool' for i in range(1, 6)]
    layers = [l for l in output_layers if l in pool_layer_names]
    return Model(inputs=vgg16.input,
                 outputs=[vgg16.get_layer(ln).output for ln in layers])
def confidnet(Restrainable=False):
    model = Sequential()
    if Restrainable:
        model.add(VGG16(include_top=False, pooling='avg', weights='imagenet'))
    model.add(Dense(2048, kernel_regularizer=l2(0.01), activation='relu'))
    model.add(Dropout(rate=0.2))
    model.add(Dense(1024, kernel_regularizer=l2(0.01), activation='relu'))
    model.add(Dropout(rate=0.3))
    model.add(Dense(512, kernel_regularizer=l2(0.01), activation='relu'))
    model.add(Dropout(rate=0.2))
    model.add(Dense(1, activation='sigmoid'))
    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss='mse')
    # model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.00001), loss=weighted_binary_crossentropy, metrics=['accuracy'])
    return model
def main():
    # Logging stuff
    now = datetime.now()
    logdir = "/tmp/image_captioning/" + now.strftime("%Y%m%d-%H%M%S") + "/"

    # Get Coco dataset
    coco.set_data_dir("../Datasets/coco/")
    coco.maybe_download_and_extract()

    # Get file names and captions
    _, filenames_train, captions_train = coco.load_records(train=True)

    num_images_train = len(filenames_train)
    print("Number of training images = {}".format(num_images_train))

    image_model = VGG16(include_top=True, weights='imagenet')
    image_model.summary()
Example #23
0
    def build_vgg16_pretrained_svm(self, lr=0.001):

        model = VGG16(include_top=True,
                      weights='imagenet',
                      input_shape=self.img_shape)

        x = model.get_layer('fc2').output

        outputs = Dense(self.num_classes,
                        kernel_regularizer=regularizers.l2(0.01),
                        kernel_initializer="normal",
                        activation='linear')(x)
        pretrain = Model(inputs=[model.input], outputs=[outputs])

        rmsprop = RMSprop(lr=lr)

        pretrain.compile(optimizer=rmsprop, loss='hinge', metrics=['accuracy'])

        return pretrain
def concrete_Dropout_model():
    l = 1e-4
    N = 2100              #Nb d'images dans X_train
    wd = l ** 2. / N
    dd = 2. / N
    inputs = Input(shape=(224, 224, 3))
    x = VGG16(include_top=False, pooling='avg', weights='imagenet')(inputs)
    x = ConcreteDropout(Dense(1024, activation='relu'), weight_regularizer=wd, dropout_regularizer=dd)(x)
    x = ConcreteDropout(Dense(512, activation='relu'), weight_regularizer=wd, dropout_regularizer=dd)(x)
    mean = ConcreteDropout(Dense(5), weight_regularizer=wd, dropout_regularizer=dd)(x)

    model = Model(inputs, mean)

    model.layers[1].trainable = False

    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss=min_mse)
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=min_mse)
    # model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=heteroscedastic_loss)
    return model
Example #25
0
def vgg_12_bn(pretrained_weights=None, input_size=(256, 256, 1)):

    new_input = Input(shape=input_size)

    model = VGG16(include_top=False, input_tensor=new_input, weights=None)

    # Say not to train ResNet model layers as they are already trained
    #for layer in model.layers:
    #	layer.trainable = False

    # extract first convolutional layer of vgg & apply BN to first two conv layers
    conv1 = model.layers[1].output
    bn1 = BatchNormalization()(conv1)
    conv2 = model.layers[2](bn1)
    bn2 = BatchNormalization()(conv2)
    x = model.layers[3](bn2)

    # append other layers of vgg network
    for layer in model.layers[4:15]:
        x = layer(x)

    # append final dense layers to model
    pool1 = AveragePooling2D(pool_size=(4, 4))(x)
    flat1 = Flatten()(pool1)
    dense1 = Dense(1024, activation="relu")(flat1)
    drop1 = Dropout(0.5)(dense1)
    dense2 = Dense(1, activation="linear")(drop1)

    model = Model(inputs=model.inputs, outputs=dense2)

    # Compile Our Transfer Learning Model
    model.compile(loss='mean_squared_error',
                  optimizer=Adam(lr=1e-4),
                  metrics=['mse', 'mae', 'mape'])

    print(model.summary())

    if (pretrained_weights):
        print('Using pretrained weights:', pretrained_weights)
        model.load_weights(pretrained_weights)

    return model
Example #26
0
    def _create_model_VGG16(self):
        # Create a stock tf VGG16, prepended with an image processor
        vgg = VGG16(input_shape=(224, 224, 3),
                    include_top=True,
                    weights='imagenet')
        vgg.trainable = False
        layer_block5_conv3 = vgg.get_layer(
            name='block5_conv3')  # Last 14x14 block

        vgg2 = Model(inputs=vgg.input, outputs=layer_block5_conv3.output)

        top_model = Sequential()
        lbd = Lambda(self._vgg_preprocess,
                     input_shape=(224, 224, 3),
                     name="image_preprocess")
        lbd.trainable = False
        top_model.add(lbd)
        top_model.add(vgg2)
        top_model.layers[1].trainable = False
        return top_model
def model_proper_scoring():
    model = Sequential()
    # 1st layer as the lumpsum weights from resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
    # NOTE that this layer will be set below as NOT TRAINABLE, i.e., use it as is
    model.add(VGG16(include_top=False, pooling='avg', weights='imagenet'))
    model.add(Dense(1024, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dropout(rate=0.2))

    model.add(Dense(512, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dropout(rate=0.2))
    model.add(Dense(10, activation='linear'))
    # Say not to train first layer (ResNet) model as it is already trained
    model.layers[0].trainable = False

    # model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9), loss='mse')
    # model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss='mse')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=neg_log_likelihood, metrics=[numerateur, denominateur, value_max, value_min])
    return model
Example #28
0
    def launch_neural(self):
        vgg16_net = VGG16(weights='imagenet',
                          include_top=False,
                          input_shape=(161, 161, 3))
        vgg16_net.trainable = False

        model = Sequential()
        model.add(vgg16_net)
        # Добавляем в модель новый классификатор
        model.add(Flatten())
        model.add(Dense(256))
        model.add(Activation('relu'))
        model.add(Dropout(0.5))
        model.add(Dense(2))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy',
                      optimizer=Adam(lr=1e-5),
                      metrics=['accuracy'])
        model.load_weights("mnist_model.h5")
        return model
Example #29
0
    def build_vgg16_pretrained(self):
        '''
            img_shape = (224,224,3)
        '''
        model = VGG16(include_top=True,
                      weights='imagenet',
                      input_shape=self.img_shape)

        x = model.get_layer('fc2').output

        outputs = Dense(self.num_classes,
                        kernel_regularizer=regularizers.l2(0.01),
                        kernel_initializer="random_uniform",
                        activation='softmax')(x)
        pretrain = Model(inputs=[model.input], outputs=[outputs])

        pretrain.compile(optimizer='adam',
                         loss='binary_crossentropy',
                         metrics=['accuracy'])

        return pretrain
Example #30
0
def vgg16(train_data, train_labels, val_data, val_labels):
    # 使用keras内置的vgg16
    # weights:指定模型初始化的权重检查点、
    # include_top: 指定模型最后是否包含密集连接分类器。
    # 默认情况下,这个密集连接分类器对应于ImageNet的100个类别。
    # 如果打算使用自己的密集连接分类器,可以不适用它,置为False。
    sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
    model_vgg16 = VGG16(weights='imagenet',
                        include_top=False,
                        input_shape=(224, 224, 3))
    # 查看model_vgg16的架构
    # model_vgg16.summary()
    # 权值冻结
    for layer in model_vgg16.layers:
        layer.trainable = False
    # 用于将输入层的数据压成一维的数据
    model = layers.Flatten(name='flatten')(model_vgg16.output)
    # 全连接层,输入的维度z
    model = layers.Dense(64, activation='relu')(model)
    # 每一批的前一层的激活值重新规范化,输出均值接近0,标准差接近1
    model = layers.BatchNormalization()(model)
    model = layers.Dropout(0.5)(model)
    model = layers.Dense(32, activation='relu')(model)
    model = layers.BatchNormalization()(model)
    model = layers.Dropout(0.5)(model)
    model = layers.Dense(16, activation='relu')(model)
    model = layers.BatchNormalization()(model)
    model = layers.Dropout(0.5)(model)
    model = layers.Dense(5, activation='softmax')(model)
    model = Model(inputs=model_vgg16.input, outputs=model, name='vgg16')

    model.compile(loss='categorical_crossentropy',
                  optimizer=sgd,
                  metrics=['accuracy'])
    model.fit(train_data,
              train_labels,
              batch_size=32,
              epochs=50,
              validation_data=(val_data, val_labels))