Exemplo n.º 1
0
def format_img_inceptionresnet(file):
  pic=load_img(file,target_size=(299,299))
  pic_array=img_to_array(pic)
# pic_array.shape
  expanded=np.expand_dims(pic_array,axis=0)
  # expanded.shape
  print(expanded)
  return preprocess_input(expanded)

data=format_img_inceptionresnet(FILE_2)
prediction=inception_model.predict(data)
decode_predictions(prediction)

"""#Testing the VGG model"""

def format_img_VGG19(file):
  pic=load_img(file,target_size=(224,224))
  display(pic)
  pic_array=img_to_array(pic)
# pic_array.shape
  expanded=np.expand_dims(pic_array,axis=0)
  # expanded.shape
  return preprocess_input_vgg19(expanded)
VGG19model=VGG19('imagenet')
VGG19model.graph=tf.compat.v1.get_default_graph()
print(data.shape)
data=format_img_VGG19(FILE_3)
prediction=VGG19model.predict(data)
decode_predictions(prediction)

Exemplo n.º 2
0
def vgg_norm():
    img_input = Input(shape=(256, 256, 3))
    x1 = Conv2D(64, (3, 3),
                activation='relu',
                padding='same',
                name='block1_conv1')(img_input)
    x2 = Conv2D(64, (3, 3),
                activation='relu',
                padding='same',
                name='block1_conv2')(x1)
    x3 = AveragePooling2D((2, 2), strides=(2, 2), name='block1_pool')(x2)

    x4 = Conv2D(128, (3, 3),
                activation='relu',
                padding='same',
                name='block2_conv1')(x3)
    x5 = Conv2D(128, (3, 3),
                activation='relu',
                padding='same',
                name='block2_conv2')(x4)
    x6 = AveragePooling2D((2, 2), strides=(2, 2), name='block2_pool')(x5)

    x7 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                name='block3_conv1')(x6)
    x8 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                name='block3_conv2')(x7)
    x9 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                name='block3_conv3')(x8)
    x10 = Conv2D(256, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block3_conv4')(x9)
    x11 = AveragePooling2D((2, 2), strides=(2, 2), name='block3_pool')(x10)

    x12 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block4_conv1')(x11)
    x13 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block4_conv2')(x12)
    x14 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block4_conv3')(x13)
    x15 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block4_conv4')(x14)
    x16 = AveragePooling2D((2, 2), strides=(2, 2), name='block4_pool')(x15)

    x17 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block5_conv1')(x16)
    x18 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block5_conv2')(x17)
    x19 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block5_conv3')(x18)
    x20 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block5_conv4')(x19)
    x21 = AveragePooling2D((2, 2), strides=(2, 2), name='block5_pool')(x20)

    model = Model(
        inputs=[img_input],
        outputs=[x1, x2, x4, x5, x7, x8, x9, x10, x12, x13, x14, x15])
    model_orig = VGG19(weights='imagenet',
                       input_shape=(256, 256, 3),
                       include_top=False)

    for i in range(len(model.layers)):
        weights = model_orig.layers[i].get_weights()
        model.layers[i].set_weights(weights)

    return model
Exemplo n.º 3
0
if args.resume:
    print('resume from checkpoint')
    model = keras.models.load_model(save_file)
else:
    print('train from start')
    model = models.Sequential()

    if '16' in args_model:
        base_model = VGG16(weights=None,
                           include_top=False,
                           input_shape=(32, 32, 3),
                           pooling=None)
    elif '19' in args_model:
        base_model = VGG19(weights=None,
                           include_top=False,
                           input_shape=(32, 32, 3),
                           pooling=None)

    #base_model.summary()

    #pdb.set_trace()

    model.add(base_model)
    model.add(layers.Flatten())
    model.add(layers.BatchNormalization())
    model.add(layers.Dense(
        128, activation='relu'))  #, kernel_initializer='he_uniform'))
    #model.add(layers.Dropout(0.2))
    model.add(layers.BatchNormalization())
    model.add(layers.Dense(
        64, activation='relu'))  #, kernel_initializer='he_uniform'))
Exemplo n.º 4
0
        len(IMAGE_TYPES) * len(IMAGE_IDX))[[0, 5, 10]]
    print(avg_original_pred)


if __name__ == "__main__":

    trials = []

    if len(sys.argv) < 2 or '-s' not in sys.argv:

        K.set_learning_phase(0)

        if MODEL == 'VGG19':
            model = VGG19(include_top=True,
                          weights='imagenet',
                          input_tensor=None,
                          input_shape=None,
                          pooling=None,
                          classes=1000)
        elif MODEL == 'Xception':
            model = Xception(include_top=True,
                             weights='imagenet',
                             input_tensor=None,
                             input_shape=None,
                             pooling=None,
                             classes=1000)
        else:
            print('Error unrecognised model!')
            exit(-1)

        # for network_file_path in glob.iglob('test_networks/' + FILENAME_PATTERN + '*.h5'):
        network_file_path = 'test_networks/imagenet.pdf'
Exemplo n.º 5
0
def transferLearning(image_dir, imgs_rows, imgs_cols, batch_size, epochs,
                     modelT):
    channel = 3

    classes = next(os.walk(image_dir + '/entrenamiento'))[1]

    if modelT == 'vgg16':
        datagen = ImageDataGenerator(preprocessing_function=preprocess_input16)
        datagen_test = ImageDataGenerator(
            preprocessing_function=preprocess_input16)
    else:
        datagen = ImageDataGenerator(preprocessing_function=preprocess_input19)
        datagen_test = ImageDataGenerator(
            preprocessing_function=preprocess_input19)

    train_generator = datagen.flow_from_directory(
        image_dir + '/entrenamiento',  #train
        target_size=(imgs_cols, imgs_rows),
        batch_size=batch_size,
        class_mode='categorical',
        shuffle=False)

    test_generator = datagen_test.flow_from_directory(
        image_dir + '/validacion',
        target_size=(imgs_cols, imgs_rows),
        batch_size=batch_size,
        class_mode='categorical',
        shuffle=False)

    # Load our model
    if modelT == 'vgg16':
        model = VGG16(include_top=False, weights='imagenet', pooling='max')
    else:
        model = VGG19(weights='imagenet', include_top=False, pooling='max')

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

    features = model.predict_generator(train_generator, steps=10)
    train_labels = np_utils.to_categorical(train_generator.classes,
                                           len(classes))

    test_features = model.predict_generator(test_generator, steps=10)
    test_labels = np_utils.to_categorical(test_generator.classes, len(classes))

    # Creamos un nuevo modelo, de forma que entrenemos sólo la última capa
    new_model = Sequential()
    #new_model.add(Flatten()) # O bien no se usa Flatten y se usa el arguemnto 'pooling = 'max'' al llamar a VGG o bien no se usa y se usa Flatten aquí
    new_model.add(
        Dense(512, activation='relu', input_shape=features.shape[1:])
    )  # Si se usa Flatten hay que quitar el argumento input_shape aquí
    new_model.add(Dense(len(classes), activation='softmax'))

    opt = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    new_model.compile(optimizer=opt,
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])
    new_model.fit(features, train_labels, epochs=epochs, batch_size=batch_size)

    # Make predictions
    predictions = new_model.predict(test_features,
                                    batch_size=batch_size,
                                    verbose=1)

    # Calculate accuracy
    test_labels = np.argmax(test_labels, axis=1)
    predictions = np.argmax(predictions, axis=1)
    acc = sum(test_labels == predictions) / len(test_labels)

    return acc
import time
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)

os.chdir("/Users/ashutosh/Documents/analytics/DeepLearning/VGG19")
from keras.applications.vgg19 import VGG19
from keras.applications.vgg19 import preprocess_input, decode_predictions
from keras.preprocessing import image

from keras.models import Model
import cv2


# load pre-trained model
model = VGG19(weights='imagenet', include_top=True)
# display model layers
model.summary()

# display the image
img_disp = plt.imread('./peacock.jpg')
#img_disp = cv2.cvtColor(img_disp, cv2.COLOR_BGR2RGB)
plt.imshow(img_disp)
plt.axis("off")  
plt.show()

# pre-process the image
img = image.load_img('./peacock.jpg', target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
import os
import nltk
import scipy
import json
import cPickle
from sklearn.feature_extraction.text import CountVectorizer
from nltk.tokenize import TreebankWordTokenizer
import pdb

from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import numpy as np

base_model = VGG19(weights='imagenet')
model = Model(input=base_model.input,
              output=base_model.get_layer('block5_conv4').output)

TRAIN_SIZE = 16
TEST_SIZE = 2

annotation_path = '/local/wangxin/Data/Flickr8k_Dataset/Flickr8k_text/Flickr8k.token.txt'
flickr_image_path = '/local/wangxin/Data/Flickr8k_Dataset/Flicker8k_Dataset'

feat_path = 'feat/flickr8k'


def my_tokenizer(s):
    return s.split()
def generate_base_model(model_name, lam, dropout_rate, import_weights):
    if model_name in ['VGG16', 'VGG19']:
        if model_name == 'VGG16':
            from keras.applications.vgg16 import VGG16
            base_model = VGG16(include_top=False,
                               weights=import_weights,
                               input_shape=(224, 224, 3))
        elif model_name == 'VGG19':
            from keras.applications.vgg19 import VGG19
            base_model = VGG19(include_top=False,
                               weights=import_weights,
                               input_shape=(224, 224, 3))
        x = base_model.output
        x = Flatten()(x)
        x = Dense(4096,
                  activation='relu',
                  kernel_regularizer=regularizers.l2(lam))(x)
        x = Dropout(dropout_rate)(x)
        x = Dense(4096,
                  activation='relu',
                  kernel_regularizer=regularizers.l2(lam))(x)
        x = Dropout(dropout_rate)(x)
    elif model_name in ['MobileNet', 'MobileNetV2']:
        if model_name == 'MobileNet':
            from keras.applications.mobilenet import MobileNet
            base_model = MobileNet(include_top=False,
                                   weights=import_weights,
                                   input_shape=(224, 224, 3))
        elif model_name == 'MobileNetV2':
            from keras.applications.mobilenet_v2 import MobileNetV2
            base_model = MobileNetV2(include_top=False,
                                     weights=import_weights,
                                     input_shape=(224, 224, 3))
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
    elif model_name in ['DenseNet121', 'DenseNet169', 'DenseNet201']:
        if model_name == 'DenseNet121':
            from keras.applications.densenet import DenseNet121
            base_model = DenseNet121(include_top=True,
                                     weights=import_weights,
                                     input_shape=(224, 224, 3))
        elif model_name == 'DenseNet169':
            from keras.applications.densenet import DenseNet169
            base_model = DenseNet169(include_top=True,
                                     weights=import_weights,
                                     input_shape=(224, 224, 3))
        elif model_name == 'DenseNet201':
            from keras.applications.densenet import DenseNet201
            base_model = DenseNet201(include_top=True,
                                     weights=import_weights,
                                     input_shape=(224, 224, 3))
        base_model = Model(base_model.inputs, base_model.layers[-2].output)
        x = base_model.output
    elif model_name in ['NASNetMobile', 'NASNetLarge']:
        if model_name == 'NASNetMobile':
            from keras.applications.nasnet import NASNetMobile
            base_model = NASNetMobile(include_top=True,
                                      weights=import_weights,
                                      input_shape=(224, 224, 3))
        elif model_name == 'NASNetLarge':
            from keras.applications.nasnet import NASNetLarge
            base_model = NASNetLarge(include_top=True,
                                     weights=import_weights,
                                     input_shape=(331, 331, 3))
        base_model = Model(base_model.inputs, base_model.layers[-2].output)
        x = base_model.output
    elif model_name == 'Xception':
        from keras.applications.xception import Xception
        base_model = Xception(include_top=False,
                              weights=import_weights,
                              input_shape=(299, 299, 3))
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
    elif model_name == 'InceptionV3':
        from keras.applications.inception_v3 import InceptionV3
        base_model = InceptionV3(include_top=False,
                                 weights=import_weights,
                                 input_shape=(299, 299, 3))
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
    elif model_name == 'InceptionResNetV2':
        from keras.applications.inception_resnet_v2 import InceptionResNetV2
        base_model = InceptionResNetV2(include_top=False,
                                       weights=import_weights,
                                       input_shape=(299, 299, 3))
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
    return x, base_model.input
Exemplo n.º 9
0
    exp = args.exp # name of the experiment
    bbox = args.bbox # whether use the heatmap loss
    per = args.per # use percentage of the training data
    year = args.year # the year of the coco dataset, can be 2017 or 2014

    batch_size = 16
    target_size = (224, 224)
    COI = ['cat']
    epochs = 20
    r_blk = 1 # number of blocks to learn
    model_name = "vgg19" # only support vgg19 and resnet50
    
    if model_name == "vgg19":
        blk_layers = [4,3,5,5,6] # number of layers at each block
        base_model = VGG19(weights="imagenet", input_shape=target_size + (3,), include_top=False,pooling="avg")
        last_conv = "block5_pool" # for vgg19 
    if model_name == "resnet50"::
        blk_layers =  [5, 12, 10, 10, 12, 10, 10, 10, 12,  10, 10, 10, 10, 10, 12, 10, 12] # number of layers at each block
        base_model = ResNet50(weights="imagenet", input_shape=target_size + (3,), include_top=False,pooling="avg")
        last_conv = "activation_49" # for resnet50
    num_classes = 1
    conv_features = base_model.get_layer(name=last_conv).output # get the output of last conv, shape (batch_size, 7, 7, 2048)

    x = base_model.output # the results after global avg, shape(batch_size, 2048)
    # x = keras.layers.Dense(512, activation='relu')(x)
    # x = keras.layers.Dense(256, activation='relu')(x)
    fc_layer = FC_layer(num_classes, name="last_fc") # custom fully connected layer, it is shared between "x" and "conv_feature"
    x = fc_layer(x)
    labels = layers.Activation("sigmoid", name="label")(x)
Exemplo n.º 10
0
def train(args, arch):
    """Use transfer learning and fine-tuning to train a network on a new dataset"""
    nb_train_samples = get_nb_files(args.train_dir)
    nb_classes = len(glob.glob(args.train_dir + "/*"))
    #nb_val_samples = get_nb_files(args.val_dir)
    nb_epoch = int(args.nb_epoch)
    #batch_size = int(args.batch_size)

    if arch == 'vgg19':
        from keras.applications.vgg19 import VGG19
        # setup model
        base_model = VGG19(
            weights='imagenet',
            include_top=False)  #include_top=False excludes final FC layer
        model = add_new_last_layer(base_model, nb_classes)

        filepath = "_{acc:.4f}_{loss:.4f}_{epoch:02d}_"
        checkpoint = ModelCheckpoint("D:/CODE/New folder/{}vgg19.model".format(
            filepath,
            monitor=['acc', 'loss'],
            verbose=1,
            save_best_only=True,
            mode='max'))

    elif arch == 'vgg16':
        from keras.applications.vgg16 import VGG16
        # setup model
        base_model = VGG16(
            weights='imagenet',
            include_top=False)  #include_top=False excludes final FC layer
        model = add_new_last_layer(base_model, nb_classes)

        filepath = "_{acc:.4f}_{loss:.4f}_{epoch:02d}_"
        checkpoint = ModelCheckpoint("D:/CODE/New folder/{}vgg16.model".format(
            filepath,
            monitor=['acc', 'loss'],
            verbose=1,
            save_best_only=True,
            mode='max'))

    elif arch == 'xception':
        from keras.applications.xception import Xception
        # setup model
        base_model = Xception(
            weights='imagenet',
            include_top=False)  #include_top=False excludes final FC layer
        model = add_new_last_layer(base_model, nb_classes)

        filepath = "_{acc:.4f}_{loss:.4f}_{epoch:02d}_"
        checkpoint = ModelCheckpoint(
            "D:/CODE/New folder/{}xception.model".format(
                filepath,
                monitor=['acc', 'loss'],
                verbose=1,
                save_best_only=True,
                mode='max'))

    elif arch == 'res50':
        from keras.applications.resnet50 import ResNet50
        # setup model
        base_model = ResNet50(
            weights='imagenet',
            include_top=False)  #include_top=False excludes final FC layer
        model = add_new_last_layer(base_model, nb_classes)

        filepath = "_{acc:.4f}_{loss:.4f}_{epoch:02d}_"
        checkpoint = ModelCheckpoint("D:/CODE/New folder/{}res50.model".format(
            filepath,
            monitor=['acc', 'loss'],
            verbose=1,
            save_best_only=True,
            mode='max'))

    elif arch == 'inv3':
        from keras.applications.inception_v3 import InceptionV3
        # setup model
        base_model = InceptionV3(
            weights='imagenet',
            include_top=False)  #include_top=False excludes final FC layer
        model = add_new_last_layer(base_model, nb_classes)

        filepath = "_{acc:.4f}_{loss:.4f}_{epoch:02d}_"
        checkpoint = ModelCheckpoint("models/{}inv3.model".format(
            filepath,
            monitor=['acc', 'loss'],
            verbose=1,
            save_best_only=True,
            mode='max'))


#  elif arch == 'dense201':
#      from keras.applications.densenet import DenseNet201
#      # setup model
#      base_model = DenseNet201(weights='imagenet', include_top=False) #include_top=False excludes final FC layer
#      model = add_new_last_layer(base_model, nb_classes)
#
#      filepath = "{acc:.4f}_{loss:.4f}_{epoch:02d}"
#      checkpoint = ModelCheckpoint("D:/CODE/New folder/{}dense.model".format(filepath, monitor=['acc', 'loss'], verbose=1, save_best_only=True, mode='max'))

# data prep
    train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                       rotation_range=40,
                                       width_shift_range=0.3,
                                       height_shift_range=0.3,
                                       shear_range=0.3,
                                       zoom_range=0.3,
                                       horizontal_flip=True)
    #  test_datagen = ImageDataGenerator(
    #      preprocessing_function=preprocess_input,
    #      rotation_range=45,
    #      width_shift_range=0.4,
    #      height_shift_range=0.4,
    #      shear_range=0.4,
    #      zoom_range=0.4,
    #      horizontal_flip=True
    #  )

    train_generator = train_datagen.flow_from_directory(
        args.train_dir,
        target_size=(IM_WIDTH, IM_HEIGHT),
        batch_size=BAT_SIZE,
    )

    #  validation_generator = test_datagen.flow_from_directory(
    #    args.val_dir,
    #    target_size=(IM_WIDTH, IM_HEIGHT),
    #    batch_size=batch_size,
    #  )

    # transfer learning
    setup_to_transfer_learn(model, base_model)

    history_tl = model.fit_generator(
        train_generator,
        nb_epoch=nb_epoch,
        samples_per_epoch=nb_train_samples,
        #validation_data=validation_generator,
        #nb_val_samples=nb_val_samples,
        callbacks=[checkpoint],
        class_weight='auto')

    # fine-tuning
    setup_to_finetune(model)

    history_ft = model.fit_generator(
        train_generator,
        samples_per_epoch=nb_train_samples,
        nb_epoch=nb_epoch,
        #validation_data=validation_generator,
        #nb_val_samples=nb_val_samples,
        callbacks=[checkpoint],
        class_weight='auto')

    model.save(args.output_model_file)

    if args.plot:
        plot_training(history_ft)
Exemplo n.º 11
0
from_vgg['conv3_4'] = 'block3_conv4'
from_vgg['conv4_1'] = 'block4_conv1'
from_vgg['conv4_2'] = 'block4_conv2'

# load previous weights or vgg19 if this is the first run
last_epoch, wfile = get_last_epoch_and_weights_file()
if wfile is not None:
    print("Loading %s ..." % wfile)

    model.load_weights(wfile)
    last_epoch = last_epoch + 1

else:
    print("Loading vgg19 weights...")

    vgg_model = VGG19(include_top=False, weights='imagenet')

    for layer in model.layers:
        if layer.name in from_vgg:
            vgg_layer_name = from_vgg[layer.name]
            layer.set_weights(
                vgg_model.get_layer(vgg_layer_name).get_weights())
            print("Loaded VGG19 layer: " + vgg_layer_name)

    last_epoch = 0

# setup lr multipliers for conv layers
lr_mult = dict()
for layer in model.layers:

    if isinstance(layer, Conv2D):
Exemplo n.º 12
0
def get_model():
    # input = Input(shape=(256,256,3),name = 'image_input')
    
    model_vgg19_conv = VGG19(weights='imagenet', include_top=False, input_shape=(256,256,3))
 
    # output_vgg19_conv = model_vgg19_conv(input)

    # concat = Input(shape=(256,256,3), name = 'concat')


    # conv2_3_16_zeropadding = ZeroPadding2D(padding=(1,1), name= "conv2_3_16_zeropadding", data_format="channels_last")(model_vgg19_conv.layers[-17].output)

    # conv3_3_16_zeropadding = ZeroPadding2D(padding=(1,1), name= "conv3_3_16_zeropadding", data_format="channels_last")(model_vgg19_conv.layers[-12].output)

    # conv4_3_16_zeropadding = ZeroPadding2D(padding=(1,1), name= "conv4_3_16_zeropadding", data_format="channels_last")(model_vgg19_conv.layers[-7].output)

    # conv5_3_16_zeropadding = ZeroPadding2D(padding=(1,1), name= "conv5_3_16_zeropadding", data_format="channels_last")(model_vgg19_conv.layers[-2].output)



    # conv2_3_16 = Conv2D(filters=16, kernel_size=(3, 3), strides=(1, 1), dilation_rate=(1,1), activation='linear', name= "conv2_3_16", data_format="channels_last")(conv2_3_16_zeropadding)

    # conv3_3_16 = Conv2D(filters=16, kernel_size=(3, 3), strides=(1, 1), dilation_rate=(1,1), activation='linear', name= "conv3_3_16", data_format="channels_last")(conv3_3_16_zeropadding)

    # conv4_3_16 = Conv2D(filters=16, kernel_size=(3, 3), strides=(1, 1), dilation_rate=(1,1), activation='linear', name= "conv4_3_16", data_format="channels_last")(conv4_3_16_zeropadding)

    # conv5_3_16 = Conv2D(filters=16, kernel_size=(3, 3), strides=(1, 1), dilation_rate=(1,1), activation='linear', name= "conv5_3_16", data_format="channels_last")(conv5_3_16_zeropadding)


    conv2_3_16 = Conv2D(filters=16, kernel_size=(3, 3), padding = "same", activation='linear', name= "conv2_3_16", data_format="channels_last")(model_vgg19_conv.layers[-17].output)

    conv3_3_16 = Conv2D(filters=16, kernel_size=(3, 3), padding = "same", activation='linear', name= "conv3_3_16", data_format="channels_last")(model_vgg19_conv.layers[-12].output)

    conv4_3_16 = Conv2D(filters=16, kernel_size=(3, 3), padding = "same", activation='linear', name= "conv4_3_16", data_format="channels_last")(model_vgg19_conv.layers[-7].output)

    conv5_3_16 = Conv2D(filters=16, kernel_size=(3, 3), padding = "same", activation='linear', name= "conv5_3_16", data_format="channels_last")(model_vgg19_conv.layers[-2].output)


    # upsample2_zeropadding = ZeroPadding2D(padding=(1,1), name= "upsample2_zeropadding", data_format="channels_last")(conv2_3_16)


    # new_score_weighting = Conv2D(filters=1, kernel_size=(1, 1), strides=(1, 1), dilation_rate=(1,1), activation='linear', name= "new_score_weighting", data_format="channels_last")(concat)


    # upsample2_ = Conv2DTranspose(filters=16, kernel_size=(4, 4), strides=(2, 2), activation='linear', name= "upsample2_", data_format="channels_last"dat)(upsample2_zeropadding)

    upsample2_ = Conv2DTranspose(filters=16, kernel_size=(4, 4), strides=(2, 2), padding = "same", activation='linear', name= "upsample2_", data_format="channels_last")(conv2_3_16)
    
    upsample4_ = Conv2DTranspose(filters=16, kernel_size=(8, 8), strides=(4, 4), padding = "same", activation='linear', name= "upsample4_", data_format="channels_last")(conv3_3_16)

    upsample8_ = Conv2DTranspose(filters=16, kernel_size=(16, 16), strides=(8, 8), padding = "same", activation='linear', name= "upsample8_", data_format="channels_last")(conv4_3_16)

    upsample16_ = Conv2DTranspose(filters=16, kernel_size=(32, 32), strides=(16, 16), padding = "same", activation='linear', name= "upsample16_", data_format="channels_last")(conv5_3_16)


    #sigmoid_fuse = Activation(activation="sigmoid")(new_score_weighting)

    
    concat_upscore = concatenate([upsample2_, upsample4_, upsample8_, upsample16_], axis=-1)
    
    # upscore_fuse = Activation(activation="sigmoid")(concat_upscore)
    
    new_score_weighting = Conv2D(filters=1, kernel_size=(1,1), activation='sigmoid', name= "new_score_weighting", data_format="channels_last")(concat_upscore)
    
    my_model = Model(input=model_vgg19_conv.input , output=new_score_weighting)

    return my_model
def getFeatureData(fileNm, dataFold):
    featData = np.load(os.path.join(dataFold, fileNm))
    outVec = np.zeros((1, numConcatFeats))
    outVec[0, 0:numGivenFeat] = np.mean(featData, axis=0)
    outVec[0, numGivenFeat:numGivenFeat * 2] = np.max(
        featData, axis=0)  # this causes potential overfit. should remove
    outVec[0, numGivenFeat * 2:numGivenFeat * 3] = np.min(
        featData, axis=0)  # this causes potential overfit. should remove
    return outVec


matFiles = os.listdir(curDir)

origNet = VGG19(include_top=True,
                weights='imagenet',
                input_tensor=None,
                input_shape=None)

#net2 = Model(input=origNet.input,output=origNet.get_layer('flatten').output)
net3 = Model(input=origNet.input, output=origNet.get_layer('fc2').output)


def getResNetData(curData):
    curImg = curData
    #curImg[curImg==-2000]=0
    batch = []
    #for i in range(0, curData.shape[0] - 3, 3):
    for i in range(curData.shape[2]):
        tmp = []
        for j in range(3):
            img = curImg[i]
Exemplo n.º 14
0
def main():
    parser = argparse.ArgumentParser(description=DESCRIPTION)
    parser.add_argument('--dataset_dir',
                        type=str,
                        default=config.DEFAULT_DATASET_DIR)
    parser.add_argument('--batch_size', type=int, default=20)
    parser.add_argument('--inv_model_file',
                        type=str,
                        help='a saved model (.h5) file')
    args = parser.parse_args()
    config_keras_backend()
    if not args.inv_model_file.endswith('.h5'):
        sys.exit('model_file is not a .h5')
    inv_model = tf.keras.models.load_model(args.inv_model_file,
                                           compile=False,
                                           custom_objects={'AdamW': AdamW})
    inv_model.compile(optimizer='sgd',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])

    ds_validation = get_dataset(args.dataset_dir, 'validation',
                                args.batch_size)

    ## VGG
    vgg_model = VGG19(include_top=True, weights='imagenet', classes=1000)
    vgg_model.compile(optimizer='sgd',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])
    # InceptionV3
    inception_model = InceptionV3(include_top=True,
                                  weights='imagenet',
                                  classes=1000)
    inception_model.compile(optimizer='sgd',
                            loss='sparse_categorical_crossentropy',
                            metrics=['accuracy'])
    ## ResNet
    resnet_model = ResNet50(include_top=True, weights='imagenet', classes=1000)
    resnet_model.compile(optimizer='sgd',
                         loss='sparse_categorical_crossentropy',
                         metrics=['accuracy'])

    # Process batches
    iteration = 0
    sum1 = 0
    sum2 = 0
    for images, labels in tfds.as_numpy(ds_validation):

        if iteration < 199:
            print('continuing')
            iteration += 1
            continue
        if iteration == 500:
            exit()

        labels = np.argmax(labels, axis=1)

        #adv_imgs = run_attack(True, 'CarliniL2Method', inception_model, images, labels, batch_size=args.batch_size, dataset='cifar', fgsm_epsilon=0.3, cwl2_confidence=0)
        #adv_imgs = run_attack(False, 'DeepFool', inception_model, images, labels, batch_size=args.batch_size, dataset='cifar', fgsm_epsilon=0.3, cwl2_confidence=0)
        adv_imgs = run_attack(False,
                              'FastGradientMethod',
                              inception_model,
                              images,
                              labels,
                              batch_size=args.batch_size,
                              dataset='cifar',
                              fgsm_epsilon=0.3,
                              cwl2_confidence=0)
        #adv_imgs = run_attack(False, 'ProjectedGradientDescent', inception_model, images, labels, batch_size=10, dataset='cifar', fgsm_epsilon=0.1, cwl2_confidence=0)
        ## VGG ################################################

        #img *= (2.0/255)  # normalize to: 0.0~2.0
        #img -= 1.0        # subtract mean to make it: -1.0~1.0
        #img = np.expand_dims(img, axis=0)

        vgg_imgs = []
        resnet_imgs = []
        inc_imgs = []
        flip_imgs = []
        inv_imgs = []
        adv_vgg_imgs = []
        adv_resnet_imgs = []
        adv_inc_imgs = []
        adv_flip_imgs = []
        adv_inv_imgs = []
        for ii in range(images.shape[0]):
            img = copy.deepcopy(images[ii, :, :, :])
            img += 1.0
            #img /= (2.0/255)
            img *= (255.0 / 2.0)

            ## VGG
            vgg_img = copy.deepcopy(img)
            vgg_img = cv2.resize(vgg_img, (224, 224))
            vgg_img = vgg_preprocess_input(vgg_img)
            vgg_imgs.append(vgg_img)

            ## Resnet
            resnet_img = copy.deepcopy(img)
            resnet_img = cv2.resize(resnet_img, (224, 224))
            resnet_img = resnet_preprocess_input(resnet_img)
            resnet_imgs.append(resnet_img)

            ## InceptionV3
            inc_img = copy.deepcopy(img)
            inc_img = cv2.resize(inc_img, (299, 299))
            inc_img = inception_preprocess_input(inc_img)
            inc_imgs.append(inc_img)

            ## Flipped
            #flip_img = copy.deepcopy(img)
            #flip_img = cv2.resize(flip_img, (299, 299))
            #flip_img = cv2.flip(flip_img, 1)
            #flip_img = inception_preprocess_input(flip_img)
            #flip_imgs.append(flip_img)
            flip_img = copy.deepcopy(images[ii, :, :, :])
            flip_img = cv2.flip(flip_img, 1)
            flip_imgs.append(flip_img)

            ## Inverse
            inv_img = copy.deepcopy(images[ii, :, :, :])  #########
            inv_img += 1.0
            inv_img /= 2.0
            inv_img = 1 - inv_img
            inv_img *= 255.0
            inv_img = cv2.resize(inv_img, (299, 299))
            inv_img = inception_preprocess_input(inv_img)
            inv_imgs.append(inv_img)

            #==========================================
            # ADVERSARIAL ---------------
            adv_img = copy.deepcopy(adv_imgs[ii, :, :, :])
            adv_img += 1.0
            #adv_img /= (2.0/255)
            adv_img *= (255.0 / 2.0)

            # VGG
            adv_vgg_img = copy.deepcopy(adv_img)
            adv_vgg_img = cv2.resize(adv_vgg_img, (224, 224))
            adv_vgg_img = vgg_preprocess_input(adv_vgg_img)
            adv_vgg_imgs.append(adv_vgg_img)

            # Resnet
            adv_resnet_img = copy.deepcopy(adv_img)
            adv_resnet_img = cv2.resize(adv_resnet_img, (224, 224))
            adv_resnet_img = resnet_preprocess_input(adv_resnet_img)
            adv_resnet_imgs.append(adv_resnet_img)

            # InceptionV3
            adv_inc_img = copy.deepcopy(adv_img)
            adv_inc_img = cv2.resize(adv_inc_img, (299, 299))
            adv_inc_img = inception_preprocess_input(adv_inc_img)
            adv_inc_imgs.append(adv_inc_img)

            ## Flipped
            #adv_flip_img = copy.deepcopy(img)
            #adv_flip_img = cv2.resize(adv_flip_img, (299, 299))
            #adv_flip_img = cv2.flip(adv_flip_img, 1)
            #adv_flip_img = inception_preprocess_input(adv_flip_img)
            #adv_flip_imgs.append(adv_flip_img)
            adv_flip_img = copy.deepcopy(adv_imgs[ii, :, :, :])
            adv_flip_img = cv2.flip(adv_flip_img, 1)
            adv_flip_imgs.append(adv_flip_img)

            ## Inverse
            ##test on inverse Inceptionv3
            adv_inv_img = copy.deepcopy(adv_imgs[ii, :, :, :])  #########
            adv_inv_img += 1.0
            adv_inv_img /= 2.0
            adv_inv_img = 1 - adv_inv_img
            adv_inv_img *= 255.0
            adv_inv_img = cv2.resize(adv_inv_img, (299, 299))
            adv_inv_img = inception_preprocess_input(adv_inv_img)
            adv_inv_imgs.append(adv_inv_img)

            # Horizontal Flipping
            # test on Resnet

        vgg_imgs = np.asarray(vgg_imgs)
        resnet_imgs = np.asarray(resnet_imgs)
        inc_imgs = np.asarray(inc_imgs)
        flip_imgs = np.asarray(flip_imgs)
        inv_imgs = np.asarray(inv_imgs)

        adv_vgg_imgs = np.asarray(adv_vgg_imgs)
        adv_resnet_imgs = np.asarray(adv_resnet_imgs)
        adv_inc_imgs = np.asarray(adv_inc_imgs)
        adv_flip_imgs = np.asarray(adv_flip_imgs)
        adv_inv_imgs = np.asarray(adv_inv_imgs)

        # Default ResNet accuracy
        _, results1 = resnet_model.evaluate(x=resnet_imgs, y=labels, verbose=0)
        _, results2 = vgg_model.evaluate(x=vgg_imgs, y=labels, verbose=0)
        _, results3 = inception_model.evaluate(x=inc_imgs, y=labels, verbose=0)
        _, results4 = inception_model.evaluate(x=flip_imgs,
                                               y=labels,
                                               verbose=0)
        _, results5 = inv_model.evaluate(x=inv_imgs, y=labels, verbose=0)
        #        print('-----------------------------------------------------')
        _, results6 = resnet_model.evaluate(x=adv_resnet_imgs,
                                            y=labels,
                                            verbose=0)
        _, results7 = vgg_model.evaluate(x=adv_vgg_imgs, y=labels, verbose=0)
        _, results8 = inception_model.evaluate(x=adv_inc_imgs,
                                               y=labels,
                                               verbose=0)
        _, results9 = inception_model.evaluate(x=adv_flip_imgs,
                                               y=labels,
                                               verbose=0)
        _, results10 = inv_model.evaluate(x=adv_inv_imgs, y=labels, verbose=0)

        print(iteration)
        print(results1, results6)
        print(results2, results7)
        print(results3, results8)
        print(results4, results9)
        print(results5, results10)

        with open("kot_fgsm_untarg.txt", "a") as myfile:
            myfile.write(
                str(results1) + ' ' + str(results2) + ' ' + str(results3) +
                ' ' + str(results4) + ' ' + str(results5) + ' ' +
                str(results6) + ' ' + str(results7) + ' ' + str(results8) +
                ' ' + str(results9) + ' ' + str(results10) + '\n')

        iteration += 1

        #exit()

        #results = resnet_model.evaluate(x=adv_imgs, y=to_categorical(labels, 1000))
        #print('RESNET test loss, test acc:', results)
        #results = vgg_model.evaluate(x=adv_imgs, y=to_categorical(labels, 1000))
        #print('VGG    test loss, test acc:', results)

#        labels = np.argmax(labels, axis=1)
#
#        #results = model.evaluate(
#        #               x=images, y=to_categorical(labels, 1000))
#        #print('test loss, test acc:', results)
#        total = total + images.shape[0]
#    print(total)
    exit()

    results = resnet_model.evaluate(x=ds_validation,
                                    steps=50000 // args.batch_size)
    print('test loss, test acc:', results)
    clear_keras_session()
Exemplo n.º 15
0
def compute_perceptual_loss(x_ref, decoded_ref2ref, decoded_art2ref, patchSize,
                            pl_network):
    if K.ndim(x_ref) == 5 and K.ndim(decoded_ref2ref) == 5 and K.ndim(
            decoded_art2ref) == 5:
        x_ref = reshape(x_ref, patchSize)
        decoded_ref2ref = reshape(decoded_ref2ref, patchSize)
        decoded_art2ref = reshape(decoded_art2ref, patchSize)

    if pl_network == 'vgg19':
        x_ref = concatenate([x_ref, x_ref, x_ref], axis=1)
        decoded_ref2ref = concatenate(
            [decoded_ref2ref, decoded_ref2ref, decoded_ref2ref], axis=1)
        decoded_art2ref = concatenate(
            [decoded_art2ref, decoded_art2ref, decoded_art2ref], axis=1)

        x_ref = Lambda(preprocessing,
                       output_shape=(3, patchSize[0], patchSize[1]))(x_ref)
        decoded_ref2ref = Lambda(preprocessing,
                                 output_shape=(3, patchSize[0],
                                               patchSize[1]))(decoded_ref2ref)
        decoded_art2ref = Lambda(preprocessing,
                                 output_shape=(3, patchSize[0],
                                               patchSize[1]))(decoded_art2ref)

        input = Input(shape=(3, patchSize[0], patchSize[1]))

        model = VGG19(include_top=False,
                      weights='imagenet',
                      input_tensor=input)

    else:
        sys.exit("loss network is not supported.")

    l1 = model.layers[1].output
    l2 = model.layers[4].output
    l3 = model.layers[7].output

    l1_model = Model(input, l1)
    l2_model = Model(input, l2)
    l3_model = Model(input, l3)

    f_l1_ref = l1_model(x_ref)
    f_l2_ref = l2_model(x_ref)
    f_l3_ref = l3_model(x_ref)

    f_l1_decoded_ref = l1_model(decoded_ref2ref)
    f_l2_decoded_ref = l2_model(decoded_ref2ref)
    f_l3_decoded_ref = l3_model(decoded_ref2ref)
    f_l1_decoded_art = l1_model(decoded_art2ref)
    f_l2_decoded_art = l2_model(decoded_art2ref)
    f_l3_decoded_art = l3_model(decoded_art2ref)

    p1_loss_ref = Lambda(
        lambda x: K.mean(K.sum(K.square(x[0] - x[1]), [1, 2, 3])),
        output_shape=(None, ))([f_l1_ref, f_l1_decoded_ref])
    p2_loss_ref = Lambda(
        lambda x: K.mean(K.sum(K.square(x[0] - x[1]), [1, 2, 3])),
        output_shape=(None, ))([f_l2_ref, f_l2_decoded_ref])
    p3_loss_ref = Lambda(
        lambda x: K.mean(K.sum(K.square(x[0] - x[1]), [1, 2, 3])),
        output_shape=(None, ))([f_l3_ref, f_l3_decoded_ref])

    p1_loss_art = Lambda(
        lambda x: K.mean(K.sum(K.square(x[0] - x[1]), [1, 2, 3])),
        output_shape=(None, ))([f_l1_ref, f_l1_decoded_art])
    p2_loss_art = Lambda(
        lambda x: K.mean(K.sum(K.square(x[0] - x[1]), [1, 2, 3])),
        output_shape=(None, ))([f_l2_ref, f_l2_decoded_art])
    p3_loss_art = Lambda(
        lambda x: K.mean(K.sum(K.square(x[0] - x[1]), [1, 2, 3])),
        output_shape=(None, ))([f_l3_ref, f_l3_decoded_art])

    perceptual_loss_ref2ref = p1_loss_ref + p2_loss_ref + p3_loss_ref
    perceptual_loss_art2ref = p1_loss_art + p2_loss_art + p3_loss_art

    return perceptual_loss_ref2ref, perceptual_loss_art2ref
Exemplo n.º 16
0
VALIDATION_STEPS = TEST_COUNT // BATCH_SIZE
WIDTH = 299
HEIGHT = 299
LEARNING_RATE = 0.0001

# Define directories
TRAIN_DIR = 'E:\Attempt 6\Original'
TEST_DIR = 'E:\Attempt 6\Test'
Attempt = "Attempt-1"
filepath_epoch = R"E:\Attempt 6/InceptionFineTuneProper-" + Attempt + "-NoBatchNormalization-" + str(
    BATCH_SIZE) + "-{epoch:02d}-.model"

# Model used is vgg16 with imagenet weights by default
# Remove output layer as we are replacing it with out own
base_model = VGG19(weights='imagenet',
                   include_top=False,
                   input_shape=(HEIGHT, WIDTH, 3))
print(len(base_model.layers))

# Declare checkpoints
now = datetime.now()
date_time = now.strftime("%m-%d-%Y")

model_checkpoint = k.callbacks.callbacks.ModelCheckpoint(filepath_epoch,
                                                         monitor='val_acc',
                                                         verbose=1,
                                                         save_best_only=False,
                                                         mode='max')

csv_logger = CSVLogger('finetuning_model_history - ' + date_time + '.csv',
                       append=True)
Exemplo n.º 17
0
    member_dir = member_base_dir + '/' + member + '/'
    for filename in os.listdir(member_dir):
        if not re.match('.+.jpg', filename): continue
        filepath = member_dir + filename
        img = img_to_array(load_img(filepath, target_size=(64,64)))
        X_train.append(img)
        y_train.append(i)

X_train = np.asarray(X_train).astype('float32') / 255.0
y_train = to_categorical(y_train, n_category)
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2, random_state=1)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

# モデルの定義: VGG19モデル
input_tensor = Input(shape=(64,64,3))
vgg19_model = VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor)

top_model = Sequential()
top_model.add(Flatten(input_shape=vgg19_model.output_shape[1:]))
top_model.add(Dense(256))
top_model.add(Activation("sigmoid"))
top_model.add(Dropout(0.5))
top_model.add(Dense(128))
top_model.add(Activation('sigmoid'))
top_model.add(Dense(n_category))
top_model.add(Activation("softmax"))

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

for layer in model.layers[:17]:
    layer.trainable = False
def feature_extraction(pre_trained_model='VGG16',
                       pooling_mode='avg',
                       classes=9,
                       data_augm_enabled=False):
    """ConvNet as fixed feature extractor, consist of taking the convolutional base of a previously-trained network,
    running the new data through it, and training a new classifier on top of the output.
    (i.e. train only the randomly initialized top layers while freezing all convolutional layers of the original model).

    # Arguments
        pre_trained_model: one of `VGG16`, `VGG19`, `ResNet50`, `VGG16_Places365`
        pooling_mode: Optional pooling_mode mode for feature extraction
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling_mode
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling_mode will
                be applied.
        classes: optional number of classes to classify images into,
                            only to be specified if `weights` argument is `None`.
        data_augm_enabled: whether to augment the samples during training

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `pre_trained_model`, `pooling_mode` or invalid input shape.
    """

    if not (pre_trained_model
            in {'VGG16', 'VGG19', 'ResNet50', 'VGG16_Places365'}):
        raise ValueError(
            'The `pre_trained_model` argument should be either '
            '`VGG16`, `VGG19`, `ResNet50`, '
            'or `VGG16_Places365`. Other models will be supported in future releases. '
        )

    if not (pooling_mode in {'avg', 'max', 'flatten'}):
        raise ValueError('The `pooling_mode` argument should be either '
                         '`avg` (GlobalAveragePooling2D), `max` '
                         '(GlobalMaxPooling2D), '
                         'or `flatten` (Flatten).')

    # Define the name of the model and its weights
    weights_name = 'cost_sensitive_feature_extraction_' + pre_trained_model + '_' + pooling_mode + '_pool_weights_tf_dim_ordering_tf_kernels.h5'

    augm_samples_weights_name = 'cost_sensitive_augm_feature_extraction_' + pre_trained_model + '_' + pooling_mode + '_pool_weights_tf_dim_ordering_tf_kernels.h5'

    model_log = logs_dir + 'cost_sensitive_feature_extraction_' + pre_trained_model + '_' + pooling_mode + '_pool_log.csv'
    csv_logger = CSVLogger(model_log, append=True, separator=',')

    input_tensor = Input(shape=(224, 224, 3))

    # create the base pre-trained model for warm-up
    if pre_trained_model == 'VGG16':
        base_model = VGG16(weights='imagenet',
                           include_top=False,
                           input_tensor=input_tensor)

    elif pre_trained_model == 'VGG19':
        base_model = VGG19(weights='imagenet',
                           include_top=False,
                           input_tensor=input_tensor)

    elif pre_trained_model == 'ResNet50':
        base_model = ResNet50(weights='imagenet',
                              include_top=False,
                              input_tensor=input_tensor)

    elif pre_trained_model == 'VGG16_Places365':
        base_model = VGG16_Places365(weights='places',
                                     include_top=False,
                                     input_tensor=input_tensor)

    print('\n \n')
    print('The plain `' + pre_trained_model +
          '` pre-trained convnet was successfully initialised.\n')

    x = base_model.output

    # Now we set-up transfer learning process - freeze all but the penultimate layer
    # and re-train the last Dense layer with 9 final outputs representing probabilities for HRA classes.
    # Build a  randomly initialised classifier model to put on top of the convolutional model

    # both `avg`and `max`result in the same size of the Dense layer afterwards
    # Both Flatten and GlobalAveragePooling2D are valid options. So is GlobalMaxPooling2D.
    # Flatten will result in a larger Dense layer afterwards, which is more expensive
    # and may result in worse overfitting. But if you have lots of data, it might also perform better.
    # https://github.com/keras-team/keras/issues/8470
    if pooling_mode == 'avg':
        x = GlobalAveragePooling2D(name='GAP')(x)
    elif pooling_mode == 'max':
        x = GlobalMaxPooling2D(name='GMP')(x)
    elif pooling_mode == 'flatten':
        x = Flatten(name='FLATTEN')(x)

    x = Dense(256, activation='relu',
              name='FC1')(x)  # let's add a fully-connected layer

    # When random init is enabled, we want to include Dropout,
    # otherwise when loading a pre-trained HRA model we want to omit
    # Dropout layer so the visualisations are done properly (there is an issue if it is included)
    x = Dropout(0.5, name='DROPOUT')(x)
    # and a logistic layer with the number of classes defined by the `classes` argument
    predictions = Dense(classes, activation='softmax',
                        name='PREDICTIONS')(x)  # new softmax layer

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

    print(
        'Randomly initialised classifier was successfully added on top of the original pre-trained conv. base. \n'
    )

    print(
        'Number of trainable weights before freezing the conv. base of the original pre-trained convnet: '
        '' + str(len(model.trainable_weights)))

    # first: train only the top layers (which were randomly initialized)
    # i.e. freeze all convolutional layers of the preliminary base model
    for layer in base_model.layers:
        layer.trainable = False

    print(
        'Number of trainable weights after freezing the conv. base of the pre-trained convnet: '
        '' + str(len(model.trainable_weights)))

    print('\n')

    # compile the warm_up_model (should be done *after* setting layers to non-trainable)

    model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.summary()

    # # The attribute model.metrics_names will give you the display labels for the scalar outputs.
    # print warm_up_model.metrics_names

    if data_augm_enabled:
        print(
            'Using augmented samples for training. This may take a while ! \n')

        t = now()

        history = model.fit_generator(augmented_train_generator,
                                      steps_per_epoch=nb_train_samples //
                                      batch_size,
                                      epochs=feature_extraction_epochs,
                                      callbacks=[csv_logger],
                                      class_weight=class_weight)

        print(
            'Training time for re-training the last Dense layer using augmented samples: %s'
            % (now() - t))

        model.save_weights(feature_extraction_dir + augm_samples_weights_name)
        print('Model weights using augmented samples were saved as `' +
              augm_samples_weights_name + '`')
        print('\n')

    else:
        t = now()
        history = model.fit_generator(train_generator,
                                      steps_per_epoch=nb_train_samples //
                                      batch_size,
                                      epochs=feature_extraction_epochs,
                                      callbacks=[csv_logger],
                                      class_weight=class_weight)

        print('Training time for re-training the last Dense layer: %s' %
              (now() - t))

        model.save_weights(feature_extraction_dir + weights_name)
        print('Model weights were saved as `' + weights_name + '`')
        print('\n')

    return model
Exemplo n.º 19
0
def init(args):
    """Init dataset and model."""
    # initialize dataset
    if args.estimated_3DBB is None:
        dataset = BoxCarsDataset(load_split="hard", load_atlas=True)
    else:
        dataset = BoxCarsDataset(load_split="hard", load_atlas=True,
                                 use_estimated_3DBB=True,
                                 estimated_3DBB_path=args.estimated_3DBB)

    # get optional path to load model
    model = None
    for path in [args.eval, args.resume]:
        if path is not None:
            print("Loading model from %s" % path)
            model = load_model(path)
            break

    # construct the model as it was not passed as an argument
    if model is None:
        print("Initializing new %s model ..." % args.train_net)
        if args.train_net in ("ResNet50", ):
            base_model = ResNet50(weights='imagenet',
                                  include_top=False,
                                  input_shape=(224, 224, 3))
            x = Flatten()(base_model.output)

        if args.train_net in ("VGG16", "VGG19"):
            if args.train_net == "VGG16":
                base_model = VGG16(weights='imagenet',
                                   include_top=False,
                                   input_shape=(224, 224, 3))
            elif args.train_net == "VGG19":
                base_model = VGG19(weights='imagenet',
                                   include_top=False,
                                   input_shape=(224, 224, 3))
            x = Flatten()(base_model.output)
            x = Dense(4096, activation='relu', name='fc1')(x)
            x = Dropout(0.5)(x)
            x = Dense(4096, activation='relu', name='fc2')(x)
            x = Dropout(0.5)(x)

        if args.train_net in ("InceptionV3", ):
            base_model = InceptionV3(weights='imagenet',
                                     include_top=False,
                                     input_shape=(224, 224, 3))
            output_dim = int(base_model.outputs[0].get_shape()[1])
            x = AveragePooling2D((output_dim, output_dim), strides=(
                output_dim, output_dim), name='avg_pool')(base_model.output)
            x = Flatten()(x)

        predictions = Dense(dataset.get_number_of_classes(),
                            activation='softmax')(x)
        model = Model(input=base_model.input,
                      output=predictions,
                      name="%s%s" % (
                          args.train_net,
                          {True: "_estimated3DBB",
                           False: ""}[args.estimated_3DBB is not None]))
        optimizer = SGD(lr=args.lr, decay=1e-4, nesterov=True)
        model.compile(optimizer=optimizer,
                      loss='categorical_crossentropy', metrics=["accuracy"])

    print("Model name: %s" % (model.name))
    if args.estimated_3DBB is not None and "estimated3DBB" not in model.name:
        print("ERROR: using estimated 3DBBs "
              "with model trained on original 3DBBs")
        sys.exit(1)
    if args.estimated_3DBB is None and "estimated3DBB" in model.name:
        print("ERROR: using model trained on estimated 3DBBs "
              "and running on original 3DBBs")
        sys.exit(1)

    args.output_final_model_path = os.path.join(
        args.cache, model.name, "final_model.h5")
    args.snapshots_dir = os.path.join(args.cache, model.name, "snapshots")
    args.tensorboard_dir = os.path.join(args.cache, model.name, "tensorboard")

    dataset.initialize_data("test")

    return model, dataset
Exemplo n.º 20
0
def TrainedVGG19():
    return VGG19(include_top=True, weights='imagenet', input_tensor=None)
results 		= config["results"]
model_path 		= config["model_path"]

# start time
print ("[STATUS] start time - {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))
start = time.time()

# create the pretrained models
# check for pretrained weight usage or not
# check for top layers to be included or not
if model_name == "vgg16":
	base_model = VGG16(weights=weights)
	model = Model(input=base_model.input, output=base_model.get_layer('fc1').output)
	image_size = (224, 224)
elif model_name == "vgg19":
	base_model = VGG19(weights=weights)
	model = Model(input=base_model.input, output=base_model.get_layer('fc1').output)
	image_size = (224, 224)
elif model_name == "resnet50":
	base_model = ResNet50(weights=weights)
	model = Model(input=base_model.input, output=base_model.get_layer('flatten').output)
	image_size = (224, 224)
elif model_name == "inception_v3":
	base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3)))
	model = Model(input=base_model.input, output=base_model.get_layer('custom').output)
	image_size = (299, 299)
elif model_name == "inceptionresnetv2":
	base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3)))
	model = Model(input=base_model.input, output=base_model.get_layer('custom').output)
	image_size = (299, 299)
elif model_name == "mobilenet":
Exemplo n.º 22
0
validation_dir = os.path.join(base_dir, 'validation')

# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir, 'cats')

# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')

# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')

# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')

conv_base = VGG19(weights='imagenet',
                  include_top=False,
                  input_shape=(150, 150, 3))

conv_base.summary()
conv_base.trainable = False

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
# no dropout
model.add(layers.Dense(1, activation='sigmoid'))

model.summary()

# data augmentation
Exemplo n.º 23
0
                            delete_old_model=True)

# for monitoring the training curves
lh_cb = LearningHistoryCallback(prefix=prefix,
                                style='ggplot',
                                save_logs=args.save_logs,
                                plot_steps=args.plot_steps)
callbacks = [mc_cb, lh_cb]

# -------------------------------------------------------
#    Build model based on VGG19 pretrained on ImageNet
# -------------------------------------------------------
## base model: VGG19 with weights
input_tensor = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))  # input tensor
base_model = VGG19(weights='imagenet',
                   include_top=False,
                   input_tensor=input_tensor)  # load VGG19 model

# classifier (Output layers)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(
    x)  # Outputs are between 0 to 1. 0 means cat, 1 means dog.
model = Model(inputs=base_model.input, outputs=predictions)

# freeze base model parameters
for layer in base_model.layers[:15]:
    layer.trainable = False

# configure the model
import keras
from keras.applications.vgg19 import VGG19
from keras.applications.resnet50 import ResNet50
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.imagenet_utils import decode_predictions
from keras.metrics import top_k_categorical_accuracy

from loader import imagenetData

import os

data_dir = "/data/imagenet"
batch_size = 16

vgg = VGG19(weights='imagenet')
resnet = ResNet50(weights='imagenet')
vgg.compile(optimizer='rmsprop',
            loss='categorical_crossentropy',
            metrics=[top_k_categorical_accuracy])
resnet.compile(optimizer='rmsprop',
               loss='categorical_crossentropy',
               metrics=[top_k_categorical_accuracy])

data = imagenetData(img_set='train', batch_size=batch_size)

i = 0
# while i < data.num_images//batch_size:
while i < 3:
    x, y = data._get_next_minibatch()
    preds = resnet.predict_on_batch(x)
    score = resnet.test_on_batch(x, y)
Exemplo n.º 25
0
                 activation='relu',
                 padding='same',
                 name='block5_conv3')(x18)
    x20 = Conv2D(512, (3, 3),
                 activation='relu',
                 padding='same',
                 name='block5_conv4')(x19)
    x21 = AveragePooling2D((2, 2), strides=(2, 2), name='block5_pool')(x20)

    model = Model(
        inputs=[img_input],
        outputs=[x1, x2, x4, x5, x7, x8, x9, x10, x12, x13, x14, x15])
    model_orig = VGG19(weights='imagenet',
                       input_shape=(256, 256, 3),
                       include_top=False)

    for i in range(len(model.layers)):
        weights = model_orig.layers[i].get_weights()
        model.layers[i].set_weights(weights)

    return model


if __name__ == '__main__':
    model = vgg_norm()
    model.summary()
    print("#####################################################")
    model_orig = VGG19(weights='imagenet',
                       input_shape=(256, 256, 3),
                       include_top=False)
    model_orig.summary()
Exemplo n.º 26
0
# the data, shuffled and split between train and test sets
# (X_train, y_train), (X_test, y_test) = cifar10.load_data()
(X_train, y_train), (X_vali, y_vali) = load_data()
print('X_train shape:', X_train.shape)
print('X_vali shape:', X_vali.shape)
print(X_train.shape[0], 'train samples')
print(X_vali.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_vali = np_utils.to_categorical(y_vali, nb_classes)

# create the base pre-trained model
K.set_image_dim_ordering("tf")
base_model = VGG19(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5, name="drp1")(x)
# x = Flatten(name='flatten', input_shape=(img_rows, img_cols, img_channels))(x)
x = Dense(120, activation='relu', name='fc1', W_regularizer=l2(0.01))(x)
x = Dropout(0.5, name="drp2")(x)
x = Dense(120, activation='relu', name='fc2', W_regularizer=l2(0.01))(x)
predictions = Dense(nb_classes, activation='softmax', name='predictions')(x)
# x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
# x = Dense(100, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
# predictions = Dense(2, activation='softmax')(x)
Exemplo n.º 27
0
model.fit_generator(generator,
                    validation_data=val_generator,
                    epochs=20,
                    verbose=1,
                    callbacks=[csv_logger, checkpointer])

# serialize weights to HDF5
model.save_weights(filename + "_modelweights.h5")
print("Saved model to disk")

########### VGG
K.clear_session()
# base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3)))
# base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3)))
base_model = VGG19(weights='imagenet',
                   include_top=False,
                   input_tensor=Input(shape=(299, 299, 3)))
# base_model = Xception(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3)))

x = base_model.output
x = GlobalAveragePooling2D()(x)
# # x = Flatten()(x)
x = Dense(4096)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(.5)(x)
predictions = Dense(101, activation='softmax')(x)

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

for layer in base_model.layers:
Exemplo n.º 28
0
        "densenet201": "https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5"

    }
# 必须使用该方法下载模型,然后加载
from flyai.utils import remote_helper
path = remote_helper.get_remote_date(MODEL_URL[model_name])
for rt, dirs, files in os.walk('./'):
    for f in files:
        if model_name in f:
            weight_path = os.path.join(rt, f)
            print(weight_path)

if model_name=="vgg16":
    base_model=VGG16(include_top=False, weights=weight_path, input_shape=(size, size, 3))
elif model_name=="vgg19":
    base_model=VGG19(include_top=False, weights=weight_path, input_shape=(size, size, 3))
elif model_name=="xception":
    base_model=Xception(include_top=False, weights=weight_path, input_shape=(size, size, 3))
elif model_name=="resnet50":
    base_model=ResNet50(include_top=False, weights=weight_path, input_shape=(size, size, 3))
elif model_name=="densenet121":
    base_model=DenseNet121(include_top=False, weights=weight_path, input_shape=(size, size, 3))
elif model_name=="densenet201":
    base_model=DenseNet201(include_top=False, weights=weight_path, input_shape=(size, size, 3))
else:
    base_model=VGG16(include_top=False, weights=weight_path, input_shape=(size, size, 3))

def get_model():
    model = Sequential()
    feature=base_model
    model.add(feature)
By default, the model loads weights pre-trained on [ImageNet.](http://www.image-net.org/about-overview)

The default input size for this model is 224x224.

#[For more information](https://keras.io/api/applications/vgg/#vgg16-function)
"""

# re-size all the images to this
IMAGE_SIZE = [224, 224]                   

#put directories to the training and validation data
train_path = '/content/drive/My Drive/Study/INTRODUCTION TO DATA SCIENCE/Other/Deep-Learning-in-Medical-Science-master/chest_xray/train'
valid_path = '/content/drive/My Drive/Study/INTRODUCTION TO DATA SCIENCE/Other/Deep-Learning-in-Medical-Science-master/chest_xray/val'

# add preprocessing layer to the front of VGG
data = VGG19(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)        

# don't train existing weights   
for layer in data.layers:
  layer.trainable = False
    
# useful for getting number of classes in the given data set
folders = glob('/content/drive/My Drive/Study/INTRODUCTION TO DATA SCIENCE/Other/Deep-Learning-in-Medical-Science-master/chest_xray/train/*')
  
#add more layers if you want
x = Flatten()(data.output)

prediction = Dense(len(folders), activation='softmax')(x)

"""#Visualizing the model
[VGG19](https://miro.medium.com/max/2408/1*6U9FJ_se7SIuFKJRyPMHuA.png)
import cv2
import numpy as np
import tensorflow as tf

from keras.models import load_model
from keras.preprocessing import image
from keras.applications.resnet50 import ResNet50
from keras.applications.vgg19 import VGG19, preprocess_input

from model.define_model import dog_names, convert_to_label

#Loading pre-built model for prediction
breed_predictor = load_model('../model/Breed_Predictor.hdf5')

#Loading VGG19 and resnet models to extract features and detect dogs
model_VGG19_features = VGG19(weights='imagenet', include_top=False)
ResNet50_model = ResNet50(weights='imagenet')

#Introducing default tensforflow graph for flask compatibility
graph = tf.get_default_graph()


def path_to_tensor(img_path):
    """Takes a file path of an image, loads it and then extracts information into a fixed shape numpy 
    array, ready to be used for VGG19 feature extracrion.

    Parameters:
    img_path (String): Path to a file containing the image.

    Returns:
    (np.array): A numpy array with fixed dimensions and information from an image