def get_model():

    model = applications.VGG19(weights="imagenet",
                               include_top=False,
                               input_shape=(img_width, img_height, 3))

    # we set the first NON_TRAINABLE_LAYERS to non trainable, then the others to trainable
    for layer in model.layers[:NON_TRAINABLE_LAYERS]:
        layer.trainable = False
    for layer in model.layers[NON_TRAINABLE_LAYERS:]:
        layer.trainable = True

    x = model.output
    x = Flatten()(x)
    x = Dense(1024, activation="relu")(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation="relu")(x)
    predictions = Dense(4, activation="softmax")(x)

    # creating the final model
    model_final = tensorflow.keras.Model(inputs=model.input,
                                         outputs=predictions)

    # compile the model
    model_final.compile(loss="categorical_crossentropy",
                        optimizer=optimizers.Adam(lr=0.0001),
                        metrics=["accuracy"])

    return model_final
def compute_mean_and_std(model_name, X, input_shape):
    if model_name == 'Xception':
        model = applications.Xception(weights='imagenet',
                                      include_top=False,
                                      input_shape=input_shape)
    elif model_name == 'VGG16':
        model = applications.VGG16(weights='imagenet',
                                   include_top=False,
                                   input_shape=input_shape)
    elif model_name == 'VGG19':
        model = applications.VGG19(weights='imagenet',
                                   include_top=False,
                                   input_shape=input_shape)
    elif model_name == 'ResNet50':
        model = applications.ResNet50(weights='imagenet',
                                      include_top=False,
                                      input_shape=input_shape)
    elif model_name == 'InceptionResNetV2':
        model = applications.InceptionResNetV2(weights='imagenet',
                                               include_top=False,
                                               input_shape=input_shape)
    elif model_name == 'InceptionV3':
        model = applications.InceptionV3(weights='imagenet',
                                         include_top=False,
                                         input_shape=input_shape)
    elif model_name == 'MobileNet':
        model = applications.MobileNet(weights='imagenet',
                                       include_top=False,
                                       input_shape=input_shape)
    elif model_name == 'DenseNet121':
        model = applications.DenseNet121(weights='imagenet',
                                         include_top=False,
                                         input_shape=input_shape)
    elif model_name == 'DenseNet169':
        model = applications.DenseNet169(weights='imagenet',
                                         include_top=False,
                                         input_shape=input_shape)
    elif model_name == 'DenseNet201':
        model = applications.DenseNet201(weights='imagenet',
                                         include_top=False,
                                         input_shape=input_shape)
    elif model_name == 'NASNetMobile':
        model = applications.NASNetMobile(weights='imagenet',
                                          include_top=False,
                                          input_shape=input_shape)
    elif model_name == 'NASNetLarge':
        model = applications.NASNetLarge(weights='imagenet',
                                         include_top=False,
                                         input_shape=input_shape)
    else:
        assert (False), "Specified base model is not available !"

    features = model.predict(X)[:, 0, 0, :]

    return features.mean(axis=0), features.std(axis=0)
Beispiel #3
0
def build_features_extractor(model_name, input_shape):
    if model_name == 'VGG16':
        model = applications.VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
    elif model_name == 'VGG19':
        model = applications.VGG19(weights='imagenet', include_top=False, input_shape=input_shape)
    elif model_name == 'ResNet50':
        model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)
    else:
        assert (False), "Specified base model is not available !"

    x = model.output
    x = layers.Flatten()(x)
    return keras.Model(inputs=model.input, outputs=x)
Beispiel #4
0
def vgg_func():
    base_model = applications.VGG19(include_top=False, weights='imagenet', input_shape=(225, 225, 3))
    add_model = Sequential()
    add_model.add(Flatten(input_shape=base_model.output_shape[1:]))
    add_model.add(Dense(256, activation='relu'))
    add_model.add(Dense(1, activation='sigmoid'))

    model = Model(inputs=base_model.input, outputs=add_model(base_model.output))
    model.compile(loss='binary_crossentropy', optimizer = optimizers.SGD(lr=1e-4, momentum=0.9),
                  metrics=['accuracy'])
    model.summary()

    return model
def build_autoencoder(base_model_name, input_shape, imagenet_mean,
                      imagenet_std, hidden_layer_size, n_classes,
                      weight_decay):
    if base_model_name == 'Xception':
        base_model = applications.Xception(weights='imagenet',
                                           include_top=False,
                                           input_shape=input_shape)
    elif base_model_name == 'VGG16':
        base_model = applications.VGG16(weights='imagenet',
                                        include_top=False,
                                        input_shape=input_shape)
    elif base_model_name == 'VGG19':
        base_model = applications.VGG19(weights='imagenet',
                                        include_top=False,
                                        input_shape=input_shape)
    elif base_model_name == 'ResNet50':
        base_model = applications.ResNet50(weights='imagenet',
                                           include_top=False,
                                           input_shape=input_shape)
    elif base_model_name == 'InceptionResNetV2':
        base_model = applications.InceptionResNetV2(weights='imagenet',
                                                    include_top=False,
                                                    input_shape=input_shape)
    elif base_model_name == 'InceptionV3':
        base_model = applications.InceptionV3(weights='imagenet',
                                              include_top=False,
                                              input_shape=input_shape)
    elif base_model_name == 'MobileNet':
        base_model = applications.MobileNet(weights='imagenet',
                                            include_top=False,
                                            input_shape=input_shape)
    elif base_model_name == 'DenseNet121':
        base_model = applications.DenseNet121(weights='imagenet',
                                              include_top=False,
                                              input_shape=input_shape)
    elif base_model_name == 'DenseNet169':
        base_model = applications.DenseNet169(weights='imagenet',
                                              include_top=False,
                                              input_shape=input_shape)
    elif base_model_name == 'DenseNet201':
        base_model = applications.DenseNet201(weights='imagenet',
                                              include_top=False,
                                              input_shape=input_shape)
    elif base_model_name == 'NASNetMobile':
        base_model = applications.NASNetMobile(weights='imagenet',
                                               include_top=False,
                                               input_shape=input_shape)
    elif base_model_name == 'NASNetLarge':
        base_model = applications.NASNetLarge(weights='imagenet',
                                              include_top=False,
                                              input_shape=input_shape)
    else:
        assert (False), "Specified base model is not available !"

    n_features = base_model.output.shape[-1]

    x = base_model.output
    x = tf.keras.layers.Lambda(lambda x: (x - imagenet_mean) / imagenet_std)(
        x)  # normalization
    x = tf.keras.layers.Activation(activation='sigmoid',
                                   name='encoder')(x)  # sigmoid
    x = tf.keras.layers.Dense(units=hidden_layer_size,
                              activation=None)(x)  # encoding
    x = tf.keras.layers.Activation(activation='relu')(x)  # relu
    x = tf.keras.layers.Dense(units=n_features,
                              activation=None,
                              name='decoder')(x)  # decoding
    x = tf.keras.layers.Dense(units=n_classes, activation='sigmoid')(
        x)  # x = tf.keras.layers.Activation(activation='sigmoid')(x) # sigmoid

    model = tf.keras.Model(inputs=base_model.input, outputs=x[:, 0, 0, :])

    for layer in model.layers:
        if isinstance(layer, tf.keras.layers.Conv2D) or isinstance(
                layer, tf.keras.layers.Dense):
            layer.add_loss(
                tf.keras.regularizers.l2(weight_decay)(layer.kernel))
        if hasattr(layer, 'bias_regularizer') and layer.use_bias:
            layer.add_loss(tf.keras.regularizers.l2(weight_decay)(layer.bias))

    return model
Beispiel #6
0
                                    config.BATCH_SIZE,
                                    aug=aug,
                                    preprocessors=[sp, mp, iap],
                                    classes=8)
    valGen = HDF5DatasetGenerator(config.TEST_HDF5,
                                  config.BATCH_SIZE,
                                  preprocessors=[sp, mp, iap],
                                  classes=8)

# initialize the optimizer
print("[INFO] compiling model...")
opt = Adam(lr=1e-3)

#Use Imagenet or VGG-Face pre-weights
vgg = applications.VGG19(weights="imagenet",
                         include_top=False,
                         input_shape=(config.IMAGE_WIDTH, config.IMAGE_HEIGHT,
                                      3))

#Pick which layers to freeze/train
for layer in vgg.layers[:15]:
    layer.trainable = False
for layer in vgg.layers[15:]:
    layer.trainable = True


##cc loss if using V/A/D and not discrete classes
def cc_coef(y_true, y_pred):
    mu_y_true = K.mean(y_true)
    mu_y_pred = K.mean(y_pred)
    return 1 - 2 * K.mean(
        (y_true - mu_y_true) *
Beispiel #7
0
def get_vgg_layers(layer_names):
    vgg = applications.VGG19(include_top=False, weights='imagenet')
    vgg.trainable = False
    output = [vgg.get_layer(name).output for name in layer_names]
    model = models.Model([vgg.input], output)
    return model
Beispiel #8
0
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(dataset_path_train,
                                                    target_size=(150, 150),
                                                    batch_size=32,
                                                    class_mode="binary")

validation_generator = test_datagen.flow_from_directory(dataset_path_validate,
                                                        target_size=(150, 150),
                                                        batch_size=32,
                                                        class_mode="binary")

from tensorflow.keras import applications, models, layers, optimizers

vgg19 = applications.VGG19(weights="imagenet",
                           include_top=False,
                           input_shape=(150, 150, 3))

vgg19.summary()

vgg19.trainable = False

model = models.Sequential()
model.add(vgg19)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation="relu"))
model.add(layers.Dense(1, activation="sigmoid"))

model.summary()

model.compile(optimizer=optimizers.RMSprop(lr=1e-4),
Beispiel #9
0
y_test = to_categorical(y_test, num_classes)

datagen = ImageDataGenerator(horizontal_flip=True,
                             fill_mode="nearest",
                             zoom_range=0.3,
                             width_shift_range=0.3,
                             height_shift_range=0.3,
                             rotation_range=30)

from tensorflow.keras import applications
from tensorflow.keras.models import Model
from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping

model = applications.VGG19(weights="imagenet",
                           include_top=False,
                           input_shape=(img_width, img_height, 3))
for layer in model.layers[:5]:
    layer.trainable = False

x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(2, activation="softmax")(x)

model_final = Model(inputs=model.input, outputs=predictions)

model_final.compile(loss="categorical_crossentropy",
                    optimizer=optimizers.SGD(lr=0.0001, momentum=0.9),
Beispiel #10
0
def get_model(num_classes, model_name=MODEL_NAME, size_of_image=SIZE_OF_IMAGE):
    """
    Returns a CNN model, or None if the parameter model is not recognised.

    Parameters:
        num_classes (int): Total number of different classes of Pokemon.
        model (string): One of the 3 models (custom, MobileNetV2, VGG19). Default: config.MODEL.
        size_of_image (int): Size of image after resizing. Default: config.SIZE_OF_IMAGE.
    
    Returns:
        model (tf.keras.Model): A CNN model.
    """
    if model_name == 'custom':
        """ Custom CNN Model """
        model = models.Sequential()
        model.add(layers.Conv2D(32, (3, 3), activation='relu',
                                input_shape=(size_of_image, size_of_image, CHANNEL)))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(64, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(128, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(256, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))

        model.add(layers.Flatten())
        model.add(layers.Dense(196, activation='relu'))
        # model.add(layers.Dropout(0.3))
        model.add(layers.Dense(num_classes))
    elif model_name == 'MobileNetV2':
        """ MobileNetV2 """
        base_model = applications.MobileNetV2(input_shape=(size_of_image, size_of_image, CHANNEL),
                                              include_top=False,
                                              weights='imagenet')
        base_model.trainable = False
        global_average_layer = layers.GlobalAveragePooling2D()
        output_layer = layers.Dense(num_classes)

        model = tf.keras.Sequential([
            base_model,
            global_average_layer,
            layers.Flatten(),
            layers.Dense(196, activation='relu'),
            layers.Dropout(0.3),
            output_layer
        ])
    elif model_name == 'VGG19':
        """ VGG19 """
        base_model = applications.VGG19(input_shape=(size_of_image, size_of_image, CHANNEL),
                                        include_top=False,
                                        weights='imagenet')
        base_model.trainable = False
        global_average_layer = layers.GlobalAveragePooling2D()
        output_layer = layers.Dense(num_classes)

        model = tf.keras.Sequential([
            base_model,
            global_average_layer,
            layers.Flatten(),
            layers.Dense(196, activation='relu'),
            layers.Dropout(0.3),
            output_layer
        ])
    else:
        print("ERROR: Cannot recognise model.")
        sys.exit(1)

    return model
Beispiel #11
0
def train_VGG19(xdim,
                ydim,
                classes,
                trainGen,
                valGen,
                steps,
                NUM_EPOCHS,
                bs,
                save=False,
                name="Default"):
    print("[" + name + "] training w/ generator...")

    # Seed
    SEED = 50
    np.random.seed(SEED)
    tf.random.set_seed(SEED)

    opt = optm.Adam(lr=0.001,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=10E-8,
                    decay=0.001,
                    amsgrad=False)
    model = applications.VGG19(weights="imagenet",
                               include_top=False,
                               input_shape=(xdim, ydim, 3))

    #Adding custom Layers
    x = model.output
    x = Flatten()(x)
    x = Dense(512,
              use_bias=False,
              kernel_initializer=initializers.he_normal(seed=SEED))(x)
    x = BatchNormalization()(x)
    x = relu(x)
    x = Dense(512,
              use_bias=False,
              kernel_initializer=initializers.he_normal(seed=SEED))(x)
    x = BatchNormalization()(x)
    x = relu(x)
    predictions = Dense(classes, activation="softmax")(x)

    # creating the final model
    model_final = Model(inputs=model.input, outputs=predictions)
    print(model_final.summary())

    checkpointer = ModelCheckpoint(
        filepath=name + '_best_weights.h5',
        verbose=1,
        monitor='val_loss',
        mode='auto',
        save_best_only=True)  #save at each epoch if the validation decreased
    tbr = TensorBoard(log_dir='./logs',
                      histogram_freq=0,
                      write_graph=True,
                      write_images=True,
                      embeddings_freq=0,
                      embeddings_layer_names=None,
                      embeddings_metadata=None,
                      embeddings_data=None,
                      update_freq='epoch')

    # compile the model
    model_final.compile(loss="categorical_crossentropy",
                        optimizer=opt,
                        metrics=["accuracy"])

    H = model_final.fit_generator(trainGen,
                                  steps_per_epoch=steps,
                                  validation_data=valGen,
                                  validation_steps=steps,
                                  epochs=NUM_EPOCHS,
                                  use_multiprocessing=MP,
                                  verbose=1)  #,
    #callbacks=[tbr])

    if (save == True):
        print("\nSaving model: " + name)
        model_final.save_weights(name + '_modelWeight.h5')

        model_final.save(name + '_fullModel.h5')

        with open(name + '_architecture.json', 'w') as f:
            f.write(model_final.to_json())

        with open(name + '_hist', 'wb') as file_pi:
            pickle.dump(H.history, file_pi)

        print('\nModel saved!\n')
    else:
        print('\nModel not saved!\n')

    return H, model_final
def main():

    vgg19_model = applications.VGG19(weights='imagenet',
                                     include_top=False,
                                     input_shape=(32, 32, 3))

    #print(vgg19_model.summary())

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

    last_layer = vgg19_model.get_layer('block5_pool')
    print("shape of last layer:", last_layer.output_shape)
    last_output = last_layer.output

    fmnist = tensorflow.keras.datasets.fashion_mnist
    callbacks = myCallback()
    (training_images, training_labels), (test_images,
                                         test_labels) = fmnist.load_data()

    training_images = transform_image(training_images)
    test_images = transform_image(test_images)

    training_images = training_images.reshape(60000, 32, 32, 3)
    test_images = test_images.reshape(10000, 32, 32, 3)
    images_val, images_test, labels_val, labels_test = train_test_split(
        test_images, test_labels, test_size=0.20)

    # Use the image generator

    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       rotation_range=40,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True,
                                       fill_mode='nearest')

    validation_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow(x=training_images,
                                         y=training_labels,
                                         batch_size=12,
                                         shuffle=True)
    validation_generator = validation_datagen.flow(x=images_val,
                                                   y=labels_val,
                                                   batch_size=12,
                                                   shuffle=True)

    x = tensorflow.keras.layers.GlobalAveragePooling2D()(last_output)
    #x = tensorflow.keras.layers.Dense(128, activation='relu')(x)

    #x = tensorflow.keras.layers.Dropout(0.2)(x)
    x = tensorflow.keras.layers.Dense(10, activation='softmax')(x)

    model = tensorflow.keras.Model(vgg19_model.input, x)
    #print(model.summary())
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    history = model.fit_generator(train_generator,
                                  steps_per_epoch=5000,
                                  epochs=20,
                                  validation_data=validation_generator,
                                  validation_steps=833,
                                  verbose=2,
                                  callbacks=[callbacks])
    #print(model.summary())
    #test_loss, test_acc = model.evaluate(test_images, test_labels)
    print("Accuracy for the model is {}".format(
        history.history.get('acc')[-1] * 100))
    print("Validation accuracy for the model is {}".format(
        history.history.get('val_acc')[-1] * 100))

    test_loss, test_acc = model.evaluate(images_test, labels_test)
    print("Test accuracy for the model is {}".format(test_acc * 100))