Beispiel #1
0
 def instantiate_network(self):
     if self.nn == 'VGG16':
         self.model = applications.VGG16(weights='imagenet',
                                         include_top=True,
                                         input_shape=(self.img_size[0],
                                                      self.img_size[0], 3))
    batch_size=batch_size)

# We now reuse a pretrained network.  Here we'll use the
# [VGG16](https://keras.io/applications/#vgg16) network architecture
# with weights learned using Imagenet.  We remove the top layers and
# freeze the pre-trained weights.
#
# ### Initialization

model = Sequential()

model.add(InputLayer(input_shape=input_image_size +
                     (3, )))  # possibly needed due to a bug in Keras

vgg_model = applications.VGG16(weights='imagenet',
                               include_top=False,
                               input_shape=input_image_size + (3, ))
for layer in vgg_model.layers:
    model.add(layer)

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

print(model.summary())

# We then stack our own, randomly initialized layers on top of the
# VGG16 network.

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(43, activation='softmax'))
Beispiel #3
0
# input_shape
in_s = 64
img_width, img_height = in_s, in_s

top_model_weights_path = 'bottleneck_fc_model_93.h5'
train_data_dir = 'dataset/training_set'
validation_data_dir = 'dataset/test_set'
# number of epochs to train top model
#epochs = 40
# batch size used by flow_from_directory and predict_generator
batch_size = 32
# ------------------------------------------------------------------------------
# create the VGG16 model -
# without the final fully-connected layers (by specifying include_top=False)
# - and load the ImageNet weights
model = applications.VGG16(include_top=False, weights='imagenet')
# ------------------------------------------------------------------------------
# create the data generator for training images,
# and run them on the VGG16 model to save the bottleneck features for training
#train_datagen = ImageDataGenerator(rescale= 1./255,
#                                   shear_range = 0.2,
#                                   zoom_range = 0.2,
#                                   #rotation_range=30,
#                                   #width_shift_range=0.2,
#                                   #height_shift_range=0.2,
#                                   horizontal_flip = False)

#generator = train_datagen.flow_from_directory(
#    train_data_dir,
#    target_size=(img_width, img_height),
#    batch_size=batch_size,
def TransferLearning(args):
    """
    Performs training.
    """
    train_dir = args.train_folder
    val_dir = args.validation_folder
    nb_train_samples = utils.get_nb_files(args.train_folder)
    nb_val_samples = utils.get_nb_files(args.validation_folder)
    nb_classes = utils.get_labels(args.train_folder, args.validation_folder)
    nb_epochs = int(args.nb_epoch)
    batch_size = int(args.batch_size)
    base_architecture = args.base_architecture
    model_load = args.model_load
    # Define base layer
    if base_architecture == 'VGG16':
        base_model = applications.VGG16(weights='imagenet', include_top=False)
        layers_to_freeze = 10
    elif base_architecture == 'VGG19':
        base_model = applications.VGG19(weights='imagenet', include_top=False)
        layers_to_freeze = 11
    elif base_architecture == 'InceptionV3':
        base_model = applications.InceptionV3(weights='imagenet', include_top=False)
        layers_to_freeze = 172 # TODO: understand how many levels!
    elif base_architecture == 'ResNet50':
        base_model = applications.ResNet50(weights='imagenet', include_top=False)
    elif base_architecture == 'Xception':
        base_model = applications.Xception(weights='imagenet', include_top=False)

    model = replace_classification_layer(base_model, nb_classes, 1024)
    
    train_datagen = ImageDataGenerator(
        rescale = 1./255,
        fill_mode='nearest')
    test_datagen = ImageDataGenerator(
        rescale = 1./255,
        fill_mode='nearest')

    train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')
    validation_generator = test_datagen.flow_from_directory(
        val_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical')
    

    transfer_learning(base_model, model, model_load)

    history_tl = model.fit_generator(
        train_generator,
        nb_epoch=nb_epochs,
        samples_per_epoch=nb_train_samples,
        validation_data=validation_generator,
        nb_val_samples=nb_val_samples,
        class_weight='auto',
        callbacks=args.callbacks)
    
    utils.plot_training(history_tl)

    setup_fine_tuning(model, layers_to_freeze, model_load)

    history_ft = model.fit_generator(
        train_generator,
        samples_per_epoch=nb_train_samples,
        nb_epoch=nb_epochs,
        validation_data=validation_generator,
        nb_val_samples=nb_val_samples,
        class_weight='auto',
        callbacks=args.callbacks)
    
    # NOTE
    model.save(os.path.join(os.getcwd(), 'models', args.output_model_file))

    utils.plot_training(history_ft)
TEST_DATA_PATH = '/Users/aboga/repos/car-damage-dataset/data1a/validation'

train_generator, validation_generator = create_data_generator(
    TRAIN_DATA_PATH, TEST_DATA_PATH)

# train_data = np.load('bottleneck_features_train.npy')
# # the features were saved in order, so recreating the labels is easy
# train_labels = np.array([0] * 920 + [1] * 920)

# validation_data = np.load('bottleneck_features_validation.npy')
# validation_labels = np.array([0] * 230 + [1] * 230)

BATCH_SIZE = 16

vgg = applications.VGG16(include_top=False,
                         weights='imagenet',
                         input_shape=(150, 150, 3))
vgg.trainable = False

model = Sequential()
model.add(vgg)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.summary()
Beispiel #6
0
def test_vgg16_notop():
    model = applications.VGG16(weights=None, include_top=False)
    assert model.output_shape == (None, None, None, 512)
    intra_op_parallelism_threads=NUM_PARALLEL_EXEC_UNITS,
    inter_op_parallelism_threads=1
)

session = tf.Session(config=config)
K.set_session(session)

#MKL and OpenMP
os.environ["OMP_NUM_THREADS"] = str(NUM_PARALLEL_EXEC_UNITS)
os.environ["KMP_BLOCKTIME"] = "1"
os.environ["KMP_SETTINGS"] = "1"
os.environ["KMP_AFFINITY"]= "granularity=fine,verbose,compact,1,0"

# Initialize VGG16 with transfer learning
base_model = applications.VGG16(weights='imagenet', 
                                include_top=False, 
                                input_shape=(WIDTH, HEIGHT,3))

# add a global spatial average pooling layer
x = base_model.output

x = GlobalAveragePooling2D()(x)
# and a dense layer
x = Dense(1024, activation='relu')(x)
predictions = Dense(len(train_flow.class_indices), activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional VGG16 layers
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(image_train,
          labels_train,
          validation_data=(image_valid, labels_valid),
          batch_size=100,
          epochs=10)

# --------------------------transfer learning
from keras import applications
from keras.models import Model
from keras.callbacks import ModelCheckpoint, EarlyStopping

model = applications.VGG16(weights="imagenet",
                           include_top=False,
                           input_shape=(100, 100, 3))

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

x = model.output
x = Flatten()(x)
x = Dense(1024, activation='relu')(x)  # or 512
x = Dropout(0.2)(x)
x = Dense(22, activation='softmax')(x)

transfered_model = Model(inputs=model.input, output=x)
transfered_model.summary()
transfered_model.compile(optimizer='rmsprop',
                         loss='categorical_crossentropy',
Beispiel #9
0
def train_top_model(train_targets, valid_targets, test_targets, test_tensors):

    input_tensor = Input(shape=(150, 150, 3))
    base_model = applications.VGG16(weights='imagenet',
                                    include_top=False,
                                    input_tensor=input_tensor)

    print('Model loaded.')

    top_model = Sequential()
    top_model.add(BatchNormalization(input_shape=base_model.output_shape[1:]))
    top_model.add(GlobalAveragePooling2D())
    top_model.add(Dense(32, activation='relu'))
    top_model.add(Dropout(0.5))
    top_model.add(Dense(10, activation='softmax'))

    top_model.load_weights(top_model_weights_path)

    model = Model(input=base_model.input, output=top_model(base_model.output))

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

    #opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)

    model.compile(loss='categorical_crossentropy',
                  optimizer=keras.optimizers.SGD(lr=1e-4, momentum=0.9),
                  metrics=['accuracy'])

    model.summary()

    checkpointer = ModelCheckpoint(filepath=top_model_fine_weights_path,
                                   verbose=1,
                                   save_best_only=True)

    train_datagen = ImageDataGenerator(
        rotation_range=
        25,  # randomly rotate images in the range (degrees, 0 to 180)
        # randomly shift images horizontally (fraction of total width)
        width_shift_range=0.2,
        # randomly shift images vertically (fraction of total height)
        height_shift_range=0.2,
        shear_range=0.15,  # set range for random shear
        zoom_range=0.15,  # set range for random zoom
        channel_shift_range=0.0,  # set range for random channel shifts
        # set mode for filling points outside the input boundaries
        fill_mode='nearest',
        cval=0.,  # value used for fill_mode = "constant"
        horizontal_flip=True,
        rescale=1. / 255)

    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
    )

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
    )

    history = model.fit_generator(
        train_generator,
        steps_per_epoch=nb_train_samples // batch_size,
        epochs=5,
        validation_data=validation_generator,
        verbose=1,
        shuffle=True,
        callbacks=[checkpointer],
        validation_steps=nb_validation_samples // batch_size)

    #    history = model.fit(train_data, train_targets,
    #              #steps_per_epoch = 100,
    #              epochs=200, #300
    #              batch_size=32,
    #              shuffle = True,
    #             verbose=1,
    #              validation_data=(valid_data, valid_targets),
    #              callbacks=[checkpointer])

    #model.save_weights(top_model_fine_weights_path)
    model.load_weights(top_model_fine_weights_path)

    pred_values = [(model.predict(np.expand_dims(tensor, axis=0)))
                   for tensor in test_tensors]
    mushroom_predictions = [
        np.argmax(model.predict(np.expand_dims(tensor, axis=0)))
        for tensor in test_tensors
    ]
    true_predictions = np.argmax(test_targets, axis=1)

    test_accuracy = 100 * np.sum(
        np.array(mushroom_predictions) == np.argmax(
            test_targets, axis=1)) / len(mushroom_predictions)
    print('Test accuracy: %.4f%%' % test_accuracy)

    print(history.history.keys())
    # summarize history for accuracy
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'valid'], loc='upper left')
    plt.show()
    # summarize history for loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'valid'], loc='upper left')
    plt.show()

    cm = confusion_matrix(y_true=true_predictions, y_pred=mushroom_predictions)
    labels = [
        '001', '002', '003', '004', '005', '006', '007', '008', '009', '010'
    ]
    plt.figure()
    plot_confusion_matrix(cm,
                          classes=labels,
                          title='Confusion matrix, without normalization')
    plt.show()

    report = classification_report(true_predictions, mushroom_predictions)
    print(report)
Beispiel #10
0
from keras import applications
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import numpy as np
from keras.preprocessing.image import ImageDataGenerator

model = applications.VGG16(include_top=False,weights='imagenet',input_shape=(60,60,3))
inps = np.load('/home/wangxiaopeng/NUS_dataset/218838_conv_pics.npy')
trans = model.predict(inps)
print 'trans shape:',trans.shape
np.save(open('/home/wangxiaopeng/NUS_dataset/218838_vgg16_pics.npy', 'w'),trans)
Beispiel #11
0
def main(args):
    print("Loading Pretrained model VGG16")
    model = applications.VGG16(include_top=False, weights='imagenet')
    model_pre = Model(model.inputs, model.layers[-2].output)

    print("Got Weights")

    data_dir = args.data_dir
    #datagen_top = ImageDataGenerator(rescale=1. / 255,rotation_range=9,horizontal_flip=True)
    datagen_top = ImageDataGenerator()
    generator_top = datagen_top.flow_from_directory(
        data_dir,
        target_size=(args.image_size, args.image_size),
        batch_size=args.batch_size,
        class_mode='categorical',
        shuffle=False)

    nb_samples = len(generator_top.filenames)
    num_classes = len(generator_top.class_indices)
    labels = generator_top.classes
    print(labels.shape)

    predict_size_data = int(math.ceil(nb_samples / args.batch_size))
    print("Extracting features...")
    bottleneck_features = model_pre.predict_generator(generator_top,
                                                      predict_size_data,
                                                      verbose=1)

    #Covariance Calculation
    print("Getting covariance matrix ")
    covarianceDimension = bottleneck_features.shape[3]
    longVectorDimension = ((covarianceDimension * (covarianceDimension + 1)) /
                           2)
    longVector = np.zeros(
        (bottleneck_features.shape[0], int(longVectorDimension)))
    for i in tqdm(range(0, bottleneck_features.shape[0])):
        X = bottleneck_features[i]
        y = X.reshape(X.shape[0] * X.shape[1], X.shape[2])  #Shape (14*14, 512)
        yt = np.transpose(y)
        #print(y.shape)
        #covarianceMatrix = np.cov(y)
        covarianceMatrix = yt.dot(y)
        #Matrix Logarithm
        identityMatrix = np.identity(covarianceDimension)
        lambdaValue = 1.e-3
        covarianceMatrixNew = ((identityMatrix * lambdaValue) +
                               covarianceMatrix)
        matrixLogarithm = logm(covarianceMatrixNew)

        flattenVector = list(
            matrixLogarithm[np.triu_indices(bottleneck_features.shape[3])]
        )  #If matrix logarithm is applied change covariance matrix to matrixLogarithm
        longVector[i] = flattenVector
    print('Long Vector Dimension ', longVector.shape)
    print("Scaling Features")
    bottleneck_features = longVector
    #bottleneck_features = scale(longVector)
    classifier_filename_exp = os.path.expanduser(args.classifier_filename)
    ##After long Vector
    if (args.mode == 'TRAIN'):

        print('Training classifier')
        start = time.time()
        modelSVC = SVC(kernel='linear', probability=True, verbose=True)
        modelSVC.fit(bottleneck_features, labels)
        end = time.time()
        print("Time taken", (end - start))
        with open(classifier_filename_exp, 'wb') as outfile:
            joblib.dump(modelSVC, outfile)
        print('Saved classifier model to file "%s"' % classifier_filename_exp)

    elif (args.mode == 'CLASSIFY'):

        print('Testing classifier')
        with open(classifier_filename_exp, 'rb') as infile:
            modelSVC = joblib.load(infile)
        print('Loaded classifier model from file "%s"' %
              classifier_filename_exp)
        print("Predicting Images")
        predictions = modelSVC.predict_proba(bottleneck_features)
        best_class_indices = np.argmax(predictions, axis=1)
        best_class_probabilities = predictions[
            np.arange(len(best_class_indices)), best_class_indices]

        arr = []
        for i in range(len(best_class_indices)):
            arr.append((predictions[i], best_class_indices[i], labels[i]))
        print(arr)
        # pickle.dump(arr, open('classification_data.p','wb'))
        accuracy = 100 * np.mean(np.equal(best_class_indices, labels))
        print('Total Accuracy: %.3f' % accuracy)
    '''
ValIDs = readIDfile(ValIdFile)
ValIDs = ValIDs[0:ValSize]

TestIDs = readIDfile(TestIdFile)
numTestSam = len(TestIDs)
#====================================#

#============= Train and test data generators ========================#
#[X_F_batch_test, X_R_batch_test, X_L_batch_test], [y_Elev_test, y_Azim_test] = MydataGeneratorTest(TrainIDs, MyBatchSize, samples_per_epoch)
train_datagen = MydataGenerator(TrainIDs, MyBatchSize, samples_per_epoch)
Val_generator = MydataGenerator(ValIDs, MyBatchSize, ValSize)
#x, y = next(train_datagen)  ## for testing purposes

#============== Create the face Network ==============================#
model_F = applications.VGG16(weights="imagenet",
                             include_top=False,
                             input_shape=(FaceResize, FaceResize,
                                          3))  ##VGG network for face
model_L = applications.VGG16(weights="imagenet",
                             include_top=False,
                             input_shape=(EyeResize, EyeResize,
                                          3))  ##VGG network for left Eye
model_R = applications.VGG16(weights="imagenet",
                             include_top=False,
                             input_shape=(EyeResize, EyeResize,
                                          3))  ##VGG network for right Eye

# change the layers' names in left and right eyes network
for i, layer in enumerate(model_L.layers):
    layer.name = layer.name + '_l'
for i, layer in enumerate(model_R.layers):
    layer.name = layer.name + '_r'
X = np.array([i for i in np.array(df["imgData"])
              ]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
y = df["totPurchaseAmt"]

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=42)

# In[7]:

vggModel = Sequential()

vggModel.add(
    applications.VGG16(weights="imagenet",
                       input_shape=(IMG_SIZE, IMG_SIZE, 3),
                       include_top=False))

numLayers = len(vggModel.layers)

for layer in vggModel.layers[:numLayers - 2]:
    layer.trainable = False

vggModel.add(Flatten(input_shape=vggModel.output_shape[1:]))
vggModel.add(Dropout(.1))
vggModel.add(Dense(1, activation="linear"))

vggModel.summary()

# In[8]:
Beispiel #14
0
from PIL import Image, ImageOps
import uuid

# path to the model weights files.
top_model_weights_path = '../bottleneck_fc_model.h5'
# dimensions of our images. This is set according to the input dimension of the model
img_width, img_height = 150, 150
# this is set in accordance with model output
num_classes = 3

#initialize and load trained model
input_tensor = Input(shape=(img_height, img_width, 3))

# build the VGG16 network
base_model = applications.VGG16(weights='imagenet',
                                include_top=False,
                                input_tensor=input_tensor)
print('Base Model loaded.')

# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(num_classes, activation='softmax'))

# load the weights of top model
top_model.load_weights(top_model_weights_path)
print('Top Model loaded')

# Compile the full model by adding topmodel to base model
Beispiel #15
0
def test_vgg19_pooling():
    model = applications.VGG16(weights=None, include_top=False, pooling='avg')
    assert model.output_shape == (None, 512)
Beispiel #16
0
# path to the model weights files.
weights_path = '../keras/examples/vgg16_weights.h5'
top_model_weights_path = 'fc_model.h5'
# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = 'cats_and_dogs_small/train'
validation_data_dir = 'cats_and_dogs_small/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16

# build the VGG16 network
base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(200, 200, 3))
print("befor loading>>>")
base_model.summary()
print('Model loaded.')

# build a classifier model to put on top of the convolutional model
print("model.output_shape:\n", base_model.output_shape)
print("model.output_shape[1:]")
print(base_model.output_shape[1:])
'''
model.output_shape:
 (None, 6, 6, 512)
model.output_shape[1:]
(6, 6, 512)
'''
top_model = Sequential()
Beispiel #17
0
def test_vgg16():
    model = applications.VGG16(weights=None)
    assert model.output_shape == (None, 1000)
def main():
    image_size = Configure.vgg_image_size
    # all train data
    train_x_image_path, train_y = data_util.load_train_data(
        image_size=image_size)
    # split train/validate
    train_X, validate_X, train_y, validate_y = train_test_split(
        train_x_image_path, train_y, test_size=0.1, random_state=0)
    train_data_wapper = data_util.DataWapper(x=train_X,
                                             y=train_y,
                                             target_size_x=image_size,
                                             target_size_y=image_size,
                                             istrain=True)
    validate_data_wapper = data_util.DataWapper(x=validate_X,
                                                y=validate_y,
                                                target_size_x=image_size,
                                                target_size_y=image_size,
                                                istrain=True)

    if K.image_data_format() == 'channels_first':  # theano
        input_shape = (3, image_size, image_size)
    else:  # tensorflow
        input_shape = (image_size, image_size, 3)

    print 'built vgg16 model'
    # build the VGG16 network
    image_input = Input(shape=input_shape)
    model = applications.VGG16(weights='imagenet',
                               include_top=False,
                               input_tensor=image_input)
    print('Model loaded.')
    for layer in model.layers[0:3]:
        print 'frozen layer', layer
        layer.trainable = False

    # build a classifier model to put on top of the convolutional model
    top_model = Flatten(name='flatten')(model.output)
    top_model = Dense(256,
                      activation='relu',
                      name='fc1',
                      kernel_regularizer=regularizers.l2(5e-4))(top_model)
    top_model = Dropout(0.5)(top_model)
    top_model = Dense(256,
                      activation='relu',
                      name='fc2',
                      kernel_regularizer=regularizers.l2(5e-4))(top_model)
    top_model = Dropout(0.5)(top_model)
    top_model = Dense(1, activation='sigmoid', name='predictions')(top_model)

    model = Model(input=image_input, output=top_model, name='vgg16')

    # compile the model with a SGD/momentum optimizer
    # and a very slow learning rate.
    model.compile(loss='binary_crossentropy',
                  optimizer=optimizers.Adam(lr=0.0001,
                                            beta_1=0.9,
                                            beta_2=0.999,
                                            epsilon=1e-08,
                                            decay=0.0),
                  metrics=['accuracy'])
    print(model.summary())
    plot_model(model, to_file='vgg16_model.png')

    print '========== start training =========='
    print 'training data size: ', train_X.shape[0]
    print 'validate data size: ', validate_X.shape[0]

    epochs = 100
    batch_size = 50
    validate_X, validate_y = validate_data_wapper.load_all_data()

    def data_generator(gen_batch_size):
        while 1:
            batch_x, batch_y = train_data_wapper.next_batch(gen_batch_size)
            yield batch_x, batch_y

    weights_file = Configure.vgg16_best_model_weights
    earlystop = keras.callbacks.EarlyStopping(monitor='val_loss',
                                              patience=5,
                                              verbose=0,
                                              mode='auto')
    checkpoint_lr_decay = ModelCheckpointAndLearningRateDecay(
        weights_file,
        lr_decay=0.9,
        monitor='val_loss',
        verbose=0,
        save_best_only=True,
        mode='min',
        patience=3)

    if os.path.exists(weights_file):
        model.load_weights(weights_file)
        print("Model loaded.")

    model.fit_generator(data_generator(gen_batch_size=batch_size),
                        steps_per_epoch=train_X.shape[0] // batch_size,
                        epochs=epochs,
                        verbose=1,
                        validation_data=(validate_X, validate_y),
                        callbacks=[earlystop, checkpoint_lr_decay])

    print '============ load weights ============'
    model.load_weights(weights_file)
    print '========== start validating =========='
    predict = model.predict(validate_X, batch_size=100, verbose=1)
    val_roc = roc_auc_score(validate_y, predict)
    print 'validate roc_auc_score =', val_roc

    print '========== start predicting =========='
    # load all all test data
    print 'load all all test data...'
    test_image_name, test_x = data_util.load_test_data(image_size)
    test_data_wapper = data_util.DataWapper(test_x,
                                            target_size_x=image_size,
                                            target_size_y=image_size,
                                            istrain=False)
    test_x, _ = test_data_wapper.load_all_data()
    print 'predict...'
    predict = model.predict(test_x, batch_size=100, verbose=1)
    predict = predict[:, 0]
    predict_df = pd.DataFrame({'name': test_image_name, 'invasive': predict})
    print 'result:', predict_df.shape[0]
    print 'average...'
    predict_df['name'] = predict_df['name'].map(lambda n: n.split('_')[-1])
    predict_df = predict_df.groupby('name').agg('mean').reset_index()
    predict_df = predict_df[['name', 'invasive']]
    print 'submit test size:', predict_df.shape[0]
    predict_df.to_csv(Configure.submission_path.format('vgg16'), index=False)
    print 'done.'
Beispiel #19
0
def model_app(arch, weights):
    """Loads the appropriate convolutional neural network (CNN) model
      Args:
        arch: String key for model to be loaded.
        weights: one of 'imagenet' or None
      Returns:
        model: The specified Keras Model instance with ImageNet weights loaded and without the top classification layer.
      """
    # function that loads the appropriate model
    if arch == 'Xception':
        model = applications.Xception(include_top=False,
                                      weights=weights,
                                      input_shape=(299, 299, 3))
        print('Xception loaded')
    elif arch == 'VGG16':
        model = applications.VGG16(include_top=False,
                                   weights=weights,
                                   input_shape=(299, 299, 3))
        print('VGG16 loaded')
    elif arch == 'VGG19':
        model = applications.VGG19(include_top=False,
                                   weights=weights,
                                   input_shape=(299, 299, 3))
        print('VGG19 loaded')
    elif arch == 'ResNet50':
        model = applications.ResNet50(include_top=False,
                                      weights=weights,
                                      input_shape=(299, 299, 3))
        print('ResNet50 loaded')
    elif arch == 'InceptionV3':
        model = applications.InceptionV3(include_top=False,
                                         weights=weights,
                                         input_shape=(299, 299, 3))
        print('InceptionV3 loaded')
    elif arch == 'InceptionResNetV2':
        model = applications.InceptionResNetV2(include_top=False,
                                               weights=weights,
                                               input_shape=(299, 299, 3))
        print('InceptionResNetV2 loaded')
    elif arch == 'MobileNet':
        model = applications.MobileNet(input_shape=(224, 224, 3),
                                       include_top=False,
                                       weights=weights)
        print('MobileNet loaded')
    elif arch == 'DenseNet121':
        model = applications.DenseNet121(include_top=False,
                                         weights=weights,
                                         input_shape=(299, 299, 3))
        print('DenseNet121 loaded')
    elif arch == 'NASNetLarge':
        model = applications.NASNetLarge(include_top=False,
                                         weights=weights,
                                         input_shape=(299, 299, 3))
        print('NASNetLarge loaded')
    elif arch == 'MobileNetV2':
        model = applications.MobileNetV2(input_shape=(224, 224, 3),
                                         include_top=False,
                                         weights=weights)
        print('MobileNetV2 loaded')
    else:
        print('Invalid model selected')
        model = False

    return model
Beispiel #20
0
def get_model(num, print_model=False, reg=False, reg_type=None, num_classes=2):
    """
    reg_type is only in accesssed if reg is True
    reg_type is a dictionary with keys
        dropout: [<rate>, ...]
        l2: [<rate>, ...]
    """
    model = Sequential()

    if num == 1:
        if reg:
            model.add(
                Conv2D(
                    16,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][0]),
                ))
            model.add(Dropout(reg_type['dropout'][0]))
            model.add(
                Conv2D(
                    4,
                    (5, 5),
                    activation='relu',
                    activity_regularizer=regularizers.l2(reg_type['l2'][1]),
                ))
            model.add(Dropout(reg_type['dropout'][1]))
        else:
            model.add(
                Conv2D(16,
                       kernel_size=(3, 3),
                       activation='relu',
                       input_shape=input_shape))
            model.add(Conv2D(4, (5, 5), activation='relu'), )

        model.add(MaxPooling2D(pool_size=(7, 7)))
        model.add(Flatten())
        model.add(Dense(num_classes, activation='softmax'))

    elif num == 2:
        if reg:
            model.add(
                Conv2D(
                    4,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][0]),
                ))
            model.add(MaxPooling2D(pool_size=(5, 5)))
            model.add(Dropout(reg_type['dropout'][0]))

            model.add(
                Conv2D(
                    16,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][1]),
                ))
            model.add(MaxPooling2D(pool_size=(5, 5)))
            model.add(Dropout(reg_type['dropout'][1]))

            model.add(
                Conv2D(
                    1,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][2]),
                ))
            model.add(Dropout(reg_type['dropout'][2]))

        else:
            model.add(
                Conv2D(4,
                       kernel_size=(3, 3),
                       activation='relu',
                       input_shape=input_shape))
            model.add(MaxPooling2D(pool_size=(5, 5)))

            model.add(
                Conv2D(16,
                       kernel_size=(3, 3),
                       activation='relu',
                       input_shape=input_shape))
            model.add(MaxPooling2D(pool_size=(5, 5)))
            model.add(
                Conv2D(1,
                       kernel_size=(3, 3),
                       activation='relu',
                       input_shape=input_shape))
        model.add(Flatten())
        model.add(Dense(num_classes, activation='softmax'))

    elif num == 3:
        if reg:
            model.add(
                Conv2D(
                    4,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][0]),
                ))
            model.add(MaxPooling2D(pool_size=(3, 3)))
            model.add(Dropout(reg_type['dropout'][0]))

            model.add(
                Conv2D(
                    16,
                    kernel_size=(3, 3),
                    strides=(1, 1),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][1]),
                ))
            model.add(MaxPooling2D(pool_size=(3, 3)))
            model.add(Dropout(reg_type['dropout'][1]))

            model.add(
                Conv2D(
                    1,
                    kernel_size=(3, 3),
                    strides=(1, 1),
                    activation='relu',
                    input_shape=input_shape,
                    activity_regularizer=regularizers.l2(reg_type['l2'][2]),
                ))
            model.add(Dropout(reg_type['dropout'][2]))
        else:
            model.add(
                Conv2D(4,
                       kernel_size=(3, 3),
                       activation='relu',
                       input_shape=input_shape))
            model.add(MaxPooling2D(pool_size=(3, 3)))

            model.add(
                Conv2D(16,
                       kernel_size=(3, 3),
                       strides=(1, 1),
                       activation='relu',
                       input_shape=input_shape))
            model.add(MaxPooling2D(pool_size=(3, 3)))
            model.add(
                Conv2D(1,
                       kernel_size=(3, 3),
                       strides=(1, 1),
                       activation='relu',
                       input_shape=input_shape))
        model.add(Flatten())
        model.add(Dense(num_classes, activation='softmax'))

    elif num == 4:  # VGG Model
        base_model = applications.VGG16(weights='imagenet',
                                        include_top=False,
                                        input_tensor=input_tensor)

        layer_dict = dict([(layer.name, layer) for layer in base_model.layers])
        num = 6
        x = layer_dict['block2_pool'].output
        x = Conv2D(filters=4, kernel_size=(1, 1), activation='relu')(x)
        x = MaxPooling2D(pool_size=(5, 5))(x)
        x = Flatten()(x)
        x = Dense(32, activation='relu')(x)
        if reg:
            num += 1
            x = Dropout(reg_type['dropout'][0])(x)
        x = Dense(num_classes, activation='softmax')(x)
        model = Model(base_model.input, x)
        for layer in model.layers[:-num]:
            layer.trainable = False

    elif num == 5:  # Resnet 50 v2
        base_model = applications.ResNet50V2(weights='imagenet',
                                             include_top=False,
                                             input_tensor=input_tensor)

        layer_dict = dict([(layer.name, layer) for layer in base_model.layers])
        num = 5
        x = layer_dict['post_relu'].output
        x = Conv2D(filters=8, kernel_size=(1, 1), activation='relu')(x)
        x = MaxPooling2D(pool_size=(5, 5))(x)
        if reg:
            num += 1
            x = Dropout(reg_type['dropout'][0])(x)
        x = Flatten()(x)
        x = Dense(num_classes, activation='softmax')(x)
        model = Model(base_model.input, x)
        for layer in model.layers[:-num]:
            layer.trainable = False

    elif num == 6:  # MobileNet v2
        base_model = applications.MobileNetV2(weights='imagenet',
                                              include_top=False,
                                              input_tensor=input_tensor)

        layer_dict = dict([(layer.name, layer) for layer in base_model.layers])
        num = 5
        x = layer_dict['out_relu'].output
        x = Conv2D(filters=16, kernel_size=(1, 1), activation='relu')(x)
        x = MaxPooling2D(pool_size=(5, 5))(x)
        if reg:
            num += 1
            x = Dropout(reg_type['dropout'][0])(x)
        x = Flatten()(x)
        x = Dense(num_classes, activation='softmax')(x)
        model = Model(base_model.input, x)
        for layer in model.layers[:-num]:
            layer.trainable = False

    model.compile(
        loss=keras.losses.categorical_crossentropy,
        optimizer=keras.optimizers.Adam(),
        metrics=['accuracy'],
    )
    if print_model:
        print(model.summary())
    return model
Beispiel #21
0
# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16

# build the VGG16 network

# build a classifier model to put on top of the convolutional model
model = Sequential(
    applications.VGG16(weights='imagenet',
                       input_shape=(150, 150, 3),
                       include_top=False).layers)
# print(model.get_layer('block5_pool').get_output_at(0).shape)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:25]:
    layer.trainable = False

# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='binary_crossentropy',
Beispiel #22
0
    def post(self):
        postedData = request.get_json()
        image_path = postedData["image_path"]
        K.clear_session()

        postedData3 = request.get_json(
            'http://182.18.157.124/ImgVerification/ProjectService.asmx')
        print("posted  data", postedData3)
        postedData2 = requests.get(
            'http://182.18.157.124/ImgVerification/ProjectService.asmx')
        print(postedData2.json)

        # load the class_indices saved in the earlier step
        class_dictionary = np.load('./data1/model/class_indices.npy',
                                   allow_pickle=True).item()

        num_classes = len(class_dictionary)

        # add the path to your test image below
        #image_path = './test_images/bin1.jpg'

        orig = cv2.imread(image_path)

        print("[INFO] loading and preprocessing image...")
        image = load_img(image_path, target_size=(224, 224))
        image = img_to_array(image)

        # important! otherwise the predictions will be '0'
        image = image / 255

        image = np.expand_dims(image, axis=0)

        # build the VGG16 network
        model = applications.VGG16(include_top=False, weights='imagenet')

        # get the bottleneck prediction from the pre-trained VGG16 model
        bottleneck_prediction = model.predict(image)

        # build top model
        model = Sequential()
        model.add(Flatten(input_shape=bottleneck_prediction.shape[1:]))
        model.add(Dense(256, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(num_classes, activation='softmax'))

        model.load_weights(top_model_weights_path)

        # use the bottleneck prediction on the top model to get the final
        # classification
        class_predicted = model.predict_classes(bottleneck_prediction)

        probabilities = model.predict_proba(bottleneck_prediction)

        inID = class_predicted[0]

        inv_map = {v: k for k, v in class_dictionary.items()}

        label = inv_map[inID]

        # get the prediction label
        print("Image ID: {}, Label: {}".format(inID, label))
        response = requests.post(
            "http://182.18.157.124/ImgVerification/ProjectService.asmx",
            json={
                "status": "200",
                "image id": inID,
                "msg": label
            })
        response.json()
        """retJson = {
Beispiel #23
0
def learnModelMulti():
    
    if(MODEL_NAME == 'RESNET'):
        K.set_learning_phase(0) # set model to inference / test mode manually (required for BN layers)
        base_model = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3))
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        #x = layers.Dropout(0.5)(x)
    elif(MODEL_NAME == 'Xception'):
        K.set_learning_phase(0) # set model to inference / test mode manually (required for BN layers)
        base_model = applications.Xception(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        #ox = layers.Dropout(0.7)(x)
    else:
        base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))
        x = base_model.output
        x = layers.Flatten()(x)     # flatten 3D output to 1D
    
    print('***** Base model {} loaded:'.format(MODEL_NAME))

    #n_layers_base = len(base_model.layers)
    
    if(MODEL_NAME == 'Xception' or MODEL_NAME == 'RESNET'):
        K.set_learning_phase(1) # set model to training mode manually (required for BN layers)
        
    MLT_shared_repr = layers.Dense(NUM_HIDDEN_UNITS, activation='relu', name='shared_repr')(x)        
    drop_out_layer = layers.Dropout(DROPOUT)(MLT_shared_repr)
    artist_prediction = layers.Dense(nClassesArtist, kernel_regularizer=regularizers.l2(0.001), activation='softmax', name='artist')(drop_out_layer)
    year_prediction = layers.Dense(1, name='year')(drop_out_layer) # regression hence no activation function
    type_prediction = layers.Dense(nClassesType, activation='sigmoid', name='type')(drop_out_layer)
    mat_prediction = layers.Dense(nClassesMat, activation='sigmoid', name='mat')(drop_out_layer)

#    MLT_shared_repr = layers.Dense(NUM_HIDDEN_UNITS, activation='relu', name='shared_repr')(x)        
#    MLT_shared_repr = layers.Dropout(0.5)(MLT_shared_repr)
#
#    artist_prediction = layers.Dense(300, activation='relu', name='artist_h')(MLT_shared_repr)
#    artist_prediction = layers.Dropout(0.5)(artist_prediction)
#    artist_prediction = layers.Dense(nClassesArtist, activation='softmax', name='artist')(artist_prediction)
#    
#    year_prediction = layers.Dense(300, activation='relu', name='year_h')(MLT_shared_repr) # regression hence no activation function
#    year_prediction = layers.Dropout(0.5)(year_prediction)
#    year_prediction = layers.Dense(1, name='year')(year_prediction) # regression hence no activation function
#
#    type_prediction = layers.Dense(300, activation='relu', name='type_h')(MLT_shared_repr)
#    type_prediction = layers.Dropout(0.5)(type_prediction)
#    type_prediction = layers.Dense(nClassesType, activation='sigmoid', name='type')(type_prediction)
#
#    mat_prediction = layers.Dense(300, activation='relu', name='mat_h')(MLT_shared_repr)
#    mat_prediction = layers.Dropout(0.5)(mat_prediction)
#    mat_prediction = layers.Dense(nClassesMat, activation='sigmoid', name='mat')(mat_prediction)

    global custom_model
    custom_model = Model(base_model.input,[artist_prediction, year_prediction, type_prediction, mat_prediction])
    
    #print("***** Full model")
    #custom_model.summary()
    
    print('# trainable weights '
          'before freezing the conv base:', len(custom_model.trainable_weights))
    
    for layer in base_model.layers:
        layer.trainable = False

    print('# trainable weights '
          'after freezing the conv base:', len(custom_model.trainable_weights))
    
    artist_loss_weight = 2
    year_loss_weight = 0 #0.1 #0.05
    type_loss_weight = 1
    mat_loss_weight = 1
    
    f = open(LOGS_FOLDER + MODEL_NAME + '_info.txt', 'a')
    msg = '\nartist_loss_weight = {:.2f}\n'.format(artist_loss_weight)
    msg += 'year_loss_weight = {:.2f}\n'.format(year_loss_weight)
    msg += 'type_loss_weight = {:.2f}\n'.format(type_loss_weight)
    msg += 'mat_loss_weight = {:.2f}\n'.format(mat_loss_weight)
    print(msg)    
    f.write(msg)
    f.close()
    
    #optimizer = tf.train.RMSPropOptimizer(learning_rate=2e-3, decay=0.9)
    #custom_model.compile(optimizer='rmsprop',
    #sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True) 
    loss_mae_tol = cm.mae_tol_param(50)

    custom_model.compile(optimizer='adam',
                  loss = {'artist': 'categorical_crossentropy', 'year': 'mae', 'type': 'binary_crossentropy', 'mat': 'binary_crossentropy'},
                  loss_weights = {'artist': artist_loss_weight, 'year': year_loss_weight, 'type': type_loss_weight, 'mat': mat_loss_weight},
                  metrics = {'artist': 'accuracy' , 'year': ['mae', loss_mae_tol] , 'type': cm.precision, 'mat': cm.precision},
                  weighted_metrics = {'artist': 'categorical_accuracy', 'year': 'mae'},
                  sample_weight_mode ={'artist': None,  'year': None , 'type': None, 'mat': None})
    
    train_gen = data_generator(train_set, Train=True)
    valid_gen = data_generator(valid_set, Train=False)
    
    #callbacks = [keras.callbacks.TensorBoard(log_dir='../logs',histogram_freq=1]    
    
    callbacks_list = [callbacks.CSVLogger(LOGS_FOLDER + MODEL_NAME +'.log'),
                         callbacks.EarlyStopping(monitor='val_artist_weighted_categorical_accuracy',patience=4),
                         callbacks.ModelCheckpoint(filepath='artistsRD_W.h5',monitor='val_loss',save_best_only=True)]
    
    #csv_logger = callbacks.CSVLogger(LOGS_FOLDER + MODEL_NAME +'.log')

    history = custom_model.fit_generator(train_gen, 
                        validation_data = valid_gen,  
                        validation_steps = ceil(valid_set.shape[0] / BATCH_SIZE),
                        steps_per_epoch = ceil(2 * train_set.shape[0] / BATCH_SIZE),
                        epochs = NUM_EPOCHS, verbose = 1,
                        #class_weight = {'artist': dic_artist_weights},
                        callbacks=callbacks_list)

    print('***** Training logs saved as ' + LOGS_FOLDER + MODEL_NAME +'.log')

    custom_model.save(MODEL_FOLDER + 'artistsRD_W.h5')
    print('***** Model saved as artistsRD.h5')
class WeightsSaver(Callback):
    def __init__(self, model, N=1):
        self.model = model
        self.N = N
        self.epoch = 1

    def on_epoch_end(self, epoch, logs={}):
        if self.epoch % self.N == 0:
            name = 'script1model6_50try02%05d.h5' % self.epoch
            self.model.save_weights(src + name)
        self.epoch += 1


#We remove the last 8 layers of the convolutional network
oldModel = applications.VGG16(include_top=False,
                              weights='imagenet',
                              input_shape=(img_size, img_size, 3))
for i in range(8):
    oldModel.layers.pop()

#we add the fully connected layer
model = Sequential()
for layer in oldModel.layers:
    model.add(layer)
model.add(Flatten())
model.add(
    Dense(300,
          kernel_initializer=initializers.RandomNormal(mean=0, stddev=0.01),
          bias_initializer='zeros'))
model.add(
    Dense(300,
Beispiel #25
0
	def __init__(self,w_path,model):
		self.model = model
		self.model.load_weights(w_path)
		self.b_model = applications.VGG16(include_top=False, weights='imagenet')
		self.graph = tf.get_default_graph()
Beispiel #26
0
def finetune_vgg16(train_folder_path, weights_dir, image_shape, num_epochs):
    ##
    #from glob import glob
    class_names = glob.glob(train_folder_path+"/*") # Reads all the folders in which images are present
    print(class_names)
    class_names = sorted(class_names) # Sorting them
    class_labels_dict = dict(zip(class_names, range(len(class_names))))
    num_classes = len(class_names)
    print(num_classes)
    ##
    ## define input of model
    #image_shape = (None, None, 3)
    ##inp=keras.layers.Input(shape =image_shape)
    ##
    ## build the VGG16 network for variable image sizes
    base_model = applications.VGG16(include_top=False, weights='imagenet', input_shape=image_shape)
    ## add layers specific to our task
    x=base_model.output
    print(x.shape)
    x = keras.layers.GlobalAveragePooling2D()(x)
    print(x.shape)
    x = keras.layers.Dense(4096, activation='relu', name='fc1')(x)
    print(x.shape)
    x = keras.layers.Dense(4096, activation='relu', name='fc2')(x)
    print(x.shape)
    predictions = keras.layers.Dense(num_classes, activation='softmax', name='predictions')(x)
    print(predictions.shape)
    ##
    custom_model = keras.Model(inputs=base_model.input,outputs=predictions)
    # Make sure that the pre-trained bottom layers are not trainable
    for layer in custom_model.layers[:7]:
        layer.trainable = False
    adam_opt = keras.optimizers.Adam(lr=1e-5)
    custom_model.compile(optimizer=adam_opt,loss='categorical_crossentropy',metrics=['accuracy'])

    ## get list of all training data
    #all_data = glob.glob('{}/*/*'.format(train_folder_path))
    all_data = glob.glob('{}/*'.format(train_folder_path))
    #with open('/data_file.txt','w') as f:
    #    f.write('\n'.join(all_data))
    #
    #train_data = all_data[10:]
    #test_data = all_data[:10]
    #num_iters = len(train_data)//batch_size
    #iter_counter = 0
    #
    ## set up dataset generatpr
    train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies
    train_generator=train_datagen.flow_from_directory(train_folder_path,
                                                     target_size=(image_shape[0], image_shape[1]),
                                                     color_mode='rgb',
                                                     batch_size=3,
                                                     class_mode='categorical',
                                                     shuffle=True)
    # generate_arrays_from_file(all_data, batch_size, class_labels_dict)
    #
    ##  train the network
    STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
    custom_model.fit_generator(train_generator, 
                                steps_per_epoch=STEP_SIZE_TRAIN, 
                                epochs=num_epochs, 
                                verbose=1)
    ## save as tf model
    #tf.keras.models.save_model(custom_model, os.path.join(weights_dir,'vgg16_custom_model_tf'), save_format='tf')
    #custom_model.save(os.path.join(weights_dir,'vgg16_custom_model_tf'), save_format='tf')
    ## save the network weights
    custom_model.save_weights(os.path.join(weights_dir,'vgg16_custom_model_weights.h5'))
    ## Save the model architecture
    with open(os.path.join(weights_dir, 'model_architecture.json'), 'w') as f:
        f.write(custom_model.to_json())
    ## save the dataset class dictionary
    label_map = train_generator.class_indices
    #
    with open(os.path.join(weights_dir, 'classes.txt'), 'w') as f:
        f.write(str(label_map))
#     train_data_dir,
#     target_size=(img_height, img_width),
#     batch_size=batch_size,
#     class_mode='binary')

# validation_generator = test_datagen.flow_from_directory(
#     validation_data_dir,
#     target_size=(img_height, img_width),
#     batch_size=batch_size,
#     class_mode='binary')

# In[3]:

# build the VGG16 network
base_model = applications.VGG16(weights=None,
                                include_top=False,
                                input_shape=(150, 150, 3))
print('Model loaded.')

# In[4]:

# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))

# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
Beispiel #28
0
    elif path[0].split('/')[-2] == 'mucca':
        name_animal.append('cow')
    elif path[0].split('/')[-2] == 'gatto':
        name_animal.append('cat')
    elif path[0].split('/')[-2] == 'pecora':
        name_animal.append('sheep')
    elif path[0].split('/')[-2] == 'gallina':
        name_animal.append('chicken')
    elif path[0].split('/')[-2] == 'elefante':
        name_animal.append('elephant')
    elif path[0].split('/')[-2] == 'ragno':
          name_animal.append('spider')
    elif path[0].split('/')[-2] == 'cane':
        name_animal.append('dog')

    base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(img_rows, img_cols, img_channel))

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(10, activation='softmax'))

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()

batch_size = 32
epochs = 15
Beispiel #29
0
def save_bottlebeck_features():
    np.random.seed(2929)

    vgg_model = applications.VGG16(weights='imagenet',
                                   include_top=False,
                                   input_shape=(150, 150, 3))
    print('Model loaded.')

    #initialise top model
    top_model = Sequential()
    top_model.add(Flatten(input_shape=vgg_model.output_shape[1:]))
    top_model.add(Dense(256, activation='relu'))
    top_model.add(Dropout(0.5))
    top_model.add(Dense(1, activation='sigmoid'))

    model = Model(inputs=vgg_model.input, outputs=top_model(vgg_model.output))

    model.trainable = True

    model.summary()

    #Total of 20 layers. The classification is considered as one layer
    #Therefore, intermediate is 19 layers
    #0, 4[:4], 3[:7], 4[:11], 4[:15], 4[:19] (Group 0, 1, 2, 3, 4, 5)
    #0 -> All trainable
    #5 -> All non-trainable except classification layer
    #Always keep layer 20 trainable because it is classification layer
    #layer_count = 1
    for layer in model.layers[:15]:
        layer.trainable = False
    #print("NO-Top: Layer is %d trainable" %layer_count)
    #layer_count = layer_count + 1

    model.summary()

    train_datagen = ImageDataGenerator(rescale=1. / 255)
    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='binary')

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='binary')

    sgd = optimizers.Adam(
        lr=1e-6
    )  #optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

    model.compile(loss="binary_crossentropy",
                  optimizer=sgd,
                  metrics=['accuracy'])

    #        model.compile(optimizer='rmsprop',
    #            loss='binary_crossentropy', metrics=['accuracy'])

    history = model.fit_generator(
        train_generator,
        steps_per_epoch=nb_train_samples // batch_size,
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps=nb_validation_samples // batch_size,
        verbose=1)

    history_dict = history.history

    #Plotting the training and validation loss
    history_dict = history.history
    loss_values = history_dict['loss']
    val_loss_values = history_dict['val_loss']
    epochs_0 = range(1, len(history_dict['acc']) + 1)
    plt.plot(epochs_0, loss_values, 'bo', label='Training loss')
    plt.plot(epochs_0, val_loss_values, 'b', label='Validation loss')
    plt.title(
        'MCvsNM_64_VGG16_Freeze_data2_group4 - Training and validation loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    #plt.show()
    plt.savefig('MCvsNM_64_VGG16_Freeze_data2_group4_loss.png')
    plt.close()

    #Plotting the training and validation accuracy
    acc_values = history_dict['acc']
    val_acc_values = history_dict['val_acc']
    plt.plot(epochs_0, acc_values, 'bo', label='Training acc')
    plt.plot(epochs_0, val_acc_values, 'b', label='Validation acc')
    plt.title(
        'MCvsNM_64_VGG16_Freeze_data2_group4 - Training and validation accuracy'
    )
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    #plt.show()
    plt.savefig('MCvsNM_64_VGG16_Freeze_data2_group4_acc.png')
    plt.close()
Beispiel #30
0
# expected image size
img_width, img_height = 256, 256
# how many images to be considered for training
train_samples = 1840
# how many images to be used for validation
validation_samples = 460

num_epochs = 20

top_model_weights_path = location + '/model_weights.h5'
fine_tuned_model_path = location + '/trained_model.h5'

#Transfer learning with VGG16
base_model = applications.VGG16(weights='imagenet',
                                include_top=False,
                                input_shape=(img_width, img_height, 3))
## set model architecture
add_model = Sequential()
add_model.add(Flatten(input_shape=base_model.output_shape[1:]))
add_model.add(Dense(256, activation='relu', W_regularizer=l2(0.01)))
add_model.add(Dropout(0.5))
#use activation softmax for multiple categories
add_model.add(Dense(1, activation='sigmoid'))

model = Model(inputs=base_model.input, outputs=add_model(base_model.output))
#can use categorical_crossentropy depending on the output classes
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])