Exemple #1
0
print('Y_train2 shape : ', Y_train2.shape)
print('Y_train3 shape : ', Y_train3.shape)
# print('Y_train4 shape : ',Y_train4.shape)
# print('Y_train5 shape : ',Y_train5.shape)

print('X_test shape : ', X_test.shape)
print('Y_test1 shape : ', Y_test1.shape)
print('Y_test2 shape : ', Y_test2.shape)
print('Y_test3 shape : ', Y_test3.shape)
# print('Y_test4 shape : ',Y_test4.shape)
# print('Y_test5 shape : ',Y_test5.shape)

#Resnet50
model1 = VGG16(include_top=False,
               weights='imagenet',
               input_tensor=None,
               input_shape=(224, 224, 3),
               pooling='avg',
               classes=[4, 5, 3])

x1 = model1.output
#使用Droupout
x1 = Dropout(0.5)(x1)
#Multi tasking
Influ = Dense(4, activation='softmax', name='softmax1')(x1)
Para = Dense(5, activation='softmax', name='softmax2')(x1)
EV = Dense(3, activation='softmax', name='softmax3')(x1)
# RSV = Dense(3, activation='softmax', name='softmax4')(x1)
# ADV = Dense(3, activation='softmax', name='softmax5')(x1)

# model_final = keras.models.load_model('/home/pmcn/workspace/CPE_AI/Resnet50/checkpoint2/0120_all_Multi_model_1.h5')
Exemple #2
0
validation_data_dir = DATA_ROOT + 'val'

nb_train_samples = (80 * 11)
nb_val_samples = (20 * 11 - 6 - 1)
nb_epoch = 50

result_dir = 'results'
if not os.path.exists(result_dir):
    os.mkdir(result_dir)

if __name__ == '__main__':
    # VGG16モデルと学習済み重みをロード
    # Fully-connected層(FC)はいらないのでinclude_top=False)
    input_tensor = Input(shape=(img_rows, img_cols, 3))
    vgg16 = VGG16(include_top=False,
                  weights='imagenet',
                  input_tensor=input_tensor)
    # vgg16.summary()

    # FC層を構築
    # Flattenへの入力指定はバッチ数を除く
    top_model = Sequential()
    top_model.add(Flatten(input_shape=vgg16.output_shape[1:]))
    top_model.add(Dense(256, activation='relu'))
    top_model.add(Dropout(0.5))
    top_model.add(Dense(nb_classes, activation='softmax'))

    # 学習済みのFC層の重みをロード
    # top_model.load_weights(os.path.join(result_dir, 'bottleneck_fc_model.h5'))

    # VGG16とFCを接続
Exemple #3
0
def go(args):
    drop_rate = args[0]

    now = datetime.datetime.now()
    current_time = '{:04d}_{:02d}_{:02d}_{:02d}{:02d}{:02d}'.format(
        now.year, now.month, now.day, now.hour, now.minute, now.second)

    DATASET_PATH = './dataset'
    BATCH_SIZE = 20
    NUM_CLASS = 13
    NUM_EPOCHS = 20
    SAVE_PATH = current_time
    os.mkdir(SAVE_PATH)

    train_datagen = ImageDataGenerator(
        # rotation_range=rotation_range,
        # width_shift_range=width_shift_range,
        # height_shift_range=height_shift_range,
        # shear_range=shear_range,
        # zoom_range=zoom_range,
        preprocessing_function=preprocess_input,
        horizontal_flip=True,
        fill_mode='reflect',
        cval=0,
        validation_split=0.1)

    train_generator = train_datagen.flow_from_directory(
        os.path.join(DATASET_PATH, 'train'),
        target_size=(256, 256),
        interpolation='bicubic',
        class_mode='categorical',
        shuffle=True,
        batch_size=BATCH_SIZE,
        subset='training')

    validation_generator = train_datagen.flow_from_directory(
        os.path.join(DATASET_PATH, 'train'),
        target_size=(256, 256),
        interpolation='bicubic',
        class_mode='categorical',
        batch_size=BATCH_SIZE,
        subset='validation')  # set as validation data

    net = VGG16(include_top=False,
                weights='imagenet',
                input_tensor=None,
                input_shape=(256, 256, 3))
    x = net.output
    x = Flatten()(x)
    x = Dropout(drop_rate)(x)

    x = Dense(1024, activation='relu', name='fc1')(x)
    x = Dropout(drop_rate)(x)
    x = Dense(512, activation='relu', name='fc2')(x)
    output_layer = Dense(NUM_CLASS, activation='softmax', name='softmax')(x)

    net_final = Model(inputs=net.input, outputs=output_layer)

    for layer in net_final.layers[:-4]:
        layer.trainable = False
    for layer in net_final.layers[-4:]:
        layer.trainable = True

    net_final.compile(optimizer=Adam(lr=1e-4),
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

    mcp_save = ModelCheckpoint(os.path.join(SAVE_PATH,
                                            'model-vgg16-final_best.h5'),
                               monitor='val_loss',
                               verbose=1,
                               save_best_only=True,
                               save_weights_only=False,
                               mode='min',
                               period=1)
    print(net_final.summary())
    history = net_final.fit_generator(
        train_generator,
        steps_per_epoch=train_generator.samples // BATCH_SIZE,
        validation_data=validation_generator,
        validation_steps=validation_generator.samples // BATCH_SIZE,
        epochs=NUM_EPOCHS,
        callbacks=[mcp_save],
        verbose=1)

    net_final.save(os.path.join(SAVE_PATH, 'model-vgg16-final.h5'))
    with open(os.path.join(SAVE_PATH, 'min_val_loss.txt'), 'a') as out_file:
        out_file.write(str(min(history.history['val_loss'])))
    print('Loss:', min(history.history['val_loss']))

    return min(history.history['val_loss'])
Exemple #4
0
            yield shuffle(X_train, Y_train)


train_generator = generator(train_data, batch_size=32)
valid_generator = generator(valid_data, batch_size=32)

from keras.applications.vgg16 import VGG16
from keras.layers import Input, Lambda, Flatten, Dense, Cropping2D, Dropout
from keras.models import Model
import math

# Use pre-trained VGG16 model as a start point.
input_height = 65
input_width = 320
vgg = VGG16(include_top=False,
            weights='imagenet',
            input_shape=(input_height, input_width, 3))

# freeze all layers weight
for layer in vgg.layers:
    layer.trainable = False
#vgg.summary()

input_shape = Input(shape=(160, 320, 3))
normalize = Lambda(lambda x: x / 255.0 - 0.5)(input_shape)  # Normalize inputs
crop_input = Cropping2D(cropping=((70, 25), (0, 0)))(normalize)
vgg16 = vgg(crop_input)
flatten = Flatten()(vgg16)
fc1 = Dense(2048, activation='relu')(flatten)
d1 = Dropout(0.5)(fc1)  # dropout regularization
fc2 = Dense(2048, activation='relu')(d1)
Exemple #5
0
    def __init__(self,
                 model='resnet152',
                 input_shape=(224, 224, 3),
                 conv_output=True,
                 conv7_output=False):
        """The :class:`BaseNetE2E` object provides different implementations of CNN Classifiers used as Base Model to extract features from images.
        If :paramref:`utils.base_net_e2e.BaseNetE2E.conv_output is set to True, the Classifier is slightly changed to have a 3D feature map as output.

        :param str model: the name of the classifier
        :param tuple input_shape: shape of the input
        :param bool conv_output: whether to change the model to output a 3D feature map or a 1D flatten output
        :param bool freeze_layers: whether to set the layers from the Classifier as not trainable or trainable
        """

        self.input_shape = input_shape
        self.conv_output = conv_output
        self.added_layers = 9
        weights = 'imagenet'

        if conv7_output:
            self.added_layers = 0
            if model == 'vgg16':
                self.model = VGG16(input_shape=input_shape, weights=weights)
                self.conv_model = self.model.layers[-5].output
            elif model == 'mobilenet':
                self.model = MobileNetV2(input_shape=input_shape,
                                         weights=weights)
                self.conv_model = self.model.layers[-3].output
            elif model == 'resnet50':
                self.model = ResNet50(input_shape=input_shape, weights=weights)
                self.conv_model = self.model.layers[-3].output
            elif model == 'resnet152':
                self.model = ResNet152(input_shape=input_shape,
                                       weights=weights)
                self.conv_model = self.model.layers[-4].output
            elif model == 'inceptionresnet':
                self.model = InceptionResNetV2(input_shape=input_shape,
                                               weights=weights)
                self.conv_model = Conv2D(1024, (2, 2),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output1')(
                                             self.model.layers[-4].output)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.added_layers = 3
        elif conv_output:
            if model == 'vgg16':
                self.model = VGG16(input_shape=input_shape, weights=weights)
                self.conv_model = Conv2D(1024, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='fc6')(
                                             self.model.layers[-5].output)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(1024, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='fc7')(self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(256, (1, 1),
                                         padding='same',
                                         kernel_initializer='he_normal',
                                         name='bottleneck_output')(
                                             self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
            elif model == 'mobilenet':
                self.model = MobileNetV2(input_shape=input_shape,
                                         weights=weights)
                self.conv_model = Conv2D(512, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output1')(
                                             self.model.layers[-3].output)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(512, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output2')(self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(256, (1, 1),
                                         padding='same',
                                         kernel_initializer='he_normal',
                                         name='bottleneck_output')(
                                             self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
            elif model == 'resnet50':
                self.model = ResNet50(input_shape=input_shape, weights=weights)
                self.conv_model = Conv2D(512, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output1')(
                                             self.model.layers[-3].output)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(512, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output2')(self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(256, (1, 1),
                                         padding='same',
                                         kernel_initializer='he_normal',
                                         name='bottleneck_output')(
                                             self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
            elif model == 'resnet152':
                self.model = ResNet152(input_shape=input_shape,
                                       weights=weights)
                self.conv_model = Conv2D(512, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output1')(
                                             self.model.layers[-4].output)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(512, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output2')(self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(256, (1, 1),
                                         padding='same',
                                         kernel_initializer='he_normal',
                                         name='bottleneck_output')(
                                             self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
            elif model == 'inceptionresnet':
                self.model = InceptionResNetV2(input_shape=input_shape,
                                               weights=weights)
                self.conv_model = Conv2D(1024, (3, 3),
                                         padding='valid',
                                         kernel_initializer='he_normal',
                                         name='output1')(
                                             self.model.layers[-4].output)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.conv_model = Conv2D(256, (1, 1),
                                         padding='same',
                                         kernel_initializer='he_normal',
                                         name='bottleneck_output')(
                                             self.conv_model)
                self.conv_model = BatchNormalization()(self.conv_model)
                self.conv_model = ReLU()(self.conv_model)
                self.added_layers = 6
            else:
                raise ValueError(
                    "The model you want to initialize as base is unknown.")
        else:
            if model == 'vgg16':
                self.model = VGG16(input_shape=input_shape, weights=weights)
            elif model == 'mobilenet':
                self.model = MobileNetV2(input_shape=input_shape,
                                         weights=weights)
            elif model == 'resnet50':
                self.model = ResNet50(input_shape=input_shape, weights=weights)
            elif model == 'resnet152':
                self.model = ResNet152(input_shape=input_shape,
                                       weights=weights)
            elif model == 'inceptionresnet':
                self.model = InceptionResNetV2(input_shape=input_shape,
                                               weights=weights)
            else:
                raise ValueError(
                    "The model you want to initialize as base is unknown.")
Exemple #6
0
import tensorflow as tf
from keras.layers import Dense, Flatten
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import ImageDataGenerator
from glob import glob
import matplotlib.pyplot as plt

IMAGE_SIZE = [224, 224]

train_path = 'Datasets/Train'
valid_path = 'Datasets/Validate'

vgg = VGG16(input_shape=IMAGE_SIZE + [3],
            weights='imagenet',
            include_top=False)

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

folders = glob(train_path + '/*')

x = Flatten()(vgg.output)
prediction = Dense(len(folders), activation='sigmoid')(x)

model = Model(inputs=vgg.input, outputs=prediction)

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
from keras.models import Model
from keras.preprocessing import image
from keras.optimizers import SGD
from keras.applications.vgg16 import VGG16
import matplotlib.pyplot as plt
import numpy as np
import cv2

# imagenet에 대해 사전 학습된 가중치를 갖는 사전에 빌드된 모델
model = VGG16(weights='imagenet', include_top=True)
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')

# VGG16 학습된 이미지 포맷으로 리사이즈
im = cv2.resize(cv2.imread('./data/steam-locomotive.jpg'), (224, 224))
im = np.expand_dims(im, axis=0)

# 예측
out = model.predict(im)
plt.plot(out.ravel())
plt.show()
print(np.argmax(out))
# 증기 기관차를 뜻하는 820이 나와야 한다.

from imblearn.under_sampling import RandomUnderSampler
from keras.applications.vgg19 import VGG19
from keras.applications.resnet50 import ResNet50
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications import Xception
from keras.utils.np_utils import to_categorical
from keras.callbacks import Callback, EarlyStopping, ReduceLROnPlateau, ModelCheckpoint

# %cd /content
wkdir = "./weights/"
pb_filename = "./drive/My Drive/Colab Notebooks/weights/retrained_graph.pb"
label_file = "./drive/My Drive/Colab Notebooks/weights/retrained_labels.txt"
weight_path1 = './drive/My Drive/Colab Notebooks/weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
weight_path2 = './drive/My Drive/Colab Notebooks/weights/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
pretrained_model_1 = VGG16(weights=weight_path1,
                           include_top=False,
                           input_shape=(299, 299, 3))
#pretrained_model_2 = InceptionV3(weights = weight_path2, include_top=False, input_shape=(299, 299, 3))

IMG_SIZE = 299
ARCHITECTURE = 1

device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
    raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))


class MetricsCheckpoint(Callback):
    """Callback that saves metrics after each epoch"""
    def __init__(self, savepath):
def main():
    map_characters1 = {0: 'No COVID', 1: 'Yes COVID'}
    dict_characters = map_characters1

    df = pd.DataFrame()

    print(dict_characters)

    model_file = "weights/covid.pb"

    weight_path1 = './drive/My Drive/Colab Notebooks/weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
    weight_path2 = './drive/My Drive/Colab Notebooks/weights/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'

    train_dir = "./drive/My Drive/Colab Notebooks/covid_dataset/train/"
    test_dir = "./drive/My Drive/Colab Notebooks/covid_dataset/test/"

    with tf.device('/device:GPU:0'):
        X_train, y_train = get_data(train_dir)
        X_test, y_test = get_data(test_dir)

        # Encode labels to hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])
        y_trainHot = to_categorical(y_train, num_classes=2)
        y_testHot = to_categorical(y_test, num_classes=2)
        plotHistogram(X_train[1])
        plt.show()

        class_weight1 = class_weight.compute_class_weight(
            'balanced', np.unique(y_train), y_train)
        pretrained_model_1 = VGG16(weights=weight_path1,
                                   include_top=False,
                                   input_shape=(299, 299, 3))
        #pretrained_model_2 = InceptionV3(weights = weight_path2, include_top=False, input_shape=(299, 299, 3))

        optimizer1 = keras.optimizers.SGD(learning_rate=0.1)
        optimizer2 = keras.optimizers.Adam(lr=0.01, epsilon=0.0001)

        # Deal with imbalanced class sizes below
        # Make Data 1D for compatability upsampling methods
        X_trainShape = X_train.shape[1] * X_train.shape[2] * X_train.shape[3]
        X_testShape = X_test.shape[1] * X_test.shape[2] * X_test.shape[3]

        X_trainFlat = X_train.reshape(X_train.shape[0], X_trainShape)
        X_testFlat = X_test.reshape(X_test.shape[0], X_testShape)

        Y_train = y_train
        Y_test = y_test
        ros = RandomUnderSampler(sampling_strategy='auto')

        X_trainRos, Y_trainRos = ros.fit_sample(X_trainFlat, Y_train)
        X_testRos, Y_testRos = ros.fit_sample(X_testFlat, Y_test)

        Y_trainRosHot = to_categorical(Y_trainRos, num_classes=2)
        Y_testRosHot = to_categorical(Y_testRos, num_classes=2)

        df["labels"] = y_train
        lab = df['labels']
        dist = lab.value_counts()
        sns.countplot(lab)

        # Make Data 2D again
        for i in range(len(X_trainRos)):
            height, width, channels = IMG_SIZE, IMG_SIZE, 3
            X_trainRosReshaped = X_trainRos.reshape(len(X_trainRos), height,
                                                    width, channels)
        for i in range(len(X_testRos)):
            height, width, channels = IMG_SIZE, IMG_SIZE, 3
            X_testRosReshaped = X_testRos.reshape(len(X_testRos), height,
                                                  width, channels)

        # Plot Label Distribution
        dfRos = pd.DataFrame()
        dfRos["labels"] = Y_trainRos
        labRos = dfRos['labels']
        distRos = lab.value_counts()
        sns.countplot(labRos)

        class_weight2 = class_weight.compute_class_weight(
            'balanced', np.unique(Y_trainRos), Y_trainRos)
        print("New Class Weights: ", class_weight2)
        pretrainedNetwork(X_trainRosReshaped, Y_trainRosHot, X_testRosReshaped,
                          Y_testRosHot, pretrained_model_1, weight_path1,
                          class_weight2, 2, 100, optimizer2, map_characters1)

    return 1
Exemple #10
0
style_array[:, :, :, 0] -= 103.939
style_array[:, :, :, 1] -= 116.779
style_array[:, :, :, 2] -= 123.68
style_array=style_array[:, :, :, ::-1]
style_array.shape

height=512
width=512
content_image=backend.variable(content_array)
style_image=backend.variable(style_array)
combination_image=backend.placeholder((1,height,width,3))


input_tensor = backend.concatenate([content_image,style_image,combination_image], axis=0)

model=VGG16(input_tensor=input_tensor,weights='imagenet', include_top=False)

content_weight = 0.05
style_weight = 5.0
total_variation_weight = 1.0

layers=dict([(layer.name, layer.output) for layer in model.layers])

loss = backend.variable(0.)

def content_loss(content, combination):
    return backend.sum(backend.square(content-combination))

layer_features = layers['block2_conv2']
content_image_features = layer_features[0, :, :, :]
combination_features = layer_features[2, :, :, :]
Exemple #11
0
conv_depth_2 = 64
conv_depth_3 = 128
conv_depth_4 = 256
conv_depth_5 = 512
drop_prob_1 = 0.5
drop_prob_2 = 0.25
hidden_size = 512
print("Training About to start")
print
########################################################################################################################################
input_shape = (224, 224, 3)
nClasses = 10

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

model = VGG16(input_tensor=image_input, include_top=True, weights='imagenet')

model.summary()

last_layer = model.get_layer('block5_pool').output

x = AveragePooling2D(pool_size=(4, 4), strides=2)(last_layer)
x = Flatten(name='flatten')(x)
x = Dense(128, activation='relu', name='fc1')(x)
x = Dense(128, activation='relu', name='fc2')(x)
out = Dense(num_classes, activation='softmax', name='output')(x)
custom_vgg_model2 = Model(image_input, out)
custom_vgg_model2.summary()

# freeze all the layers except the dense layers
for layer in custom_vgg_model2.layers[:-3]:
Exemple #12
0
def nn_base(input, trainable):
    base_model = VGG16(weights=None, include_top=False, input_shape=input)

    return base_model.input, base_model.get_layer('block5_conv3').output
Exemple #13
0
 def __init__(self):
     """
     Download VGG16 Model from Github
     """
     self._model = VGG16(weights='imagenet')
Exemple #14
0
def transform(content_img, style_img, height, width, iterations):
    content_array = image_to_array(content_img)
    style_array = image_to_array(style_img)

    content_array = transform_color_and_flip_colorchannels(content_array)
    style_array = transform_color_and_flip_colorchannels(style_array)

    content_image = backend.variable(content_array)
    style_image = backend.variable(style_array)
    combination_image = backend.placeholder((1, height, width, 3))

    input_tensor = backend.concatenate(
        [content_image, style_image, combination_image], axis=0)

    model = VGG16(input_tensor=input_tensor,
                  weights='imagenet',
                  include_top=False)

    layers = dict([(layer.name, layer.output) for layer in model.layers])

    content_weight = 0.025
    style_weight = 5.0
    total_variation_weight = 1.0

    loss = backend.variable(0.)

    layer_features = layers['block2_conv2']
    content_image_features = layer_features[0, :, :, :]
    combination_features = layer_features[2, :, :, :]

    loss += content_weight * backend.sum(
        backend.square(combination_features - content_image_features))

    feature_layers = [
        'block1_conv2', 'block2_conv2', 'block3_conv3', 'block4_conv3',
        'block5_conv3'
    ]

    for layer_name in feature_layers:
        layer_features = layers[layer_name]
        style_features = layer_features[1, :, :, :]
        combination_features = layer_features[2, :, :, :]
        sl = style_loss(style_features, combination_features, height, width)
        loss += (style_weight / len(feature_layers)) * sl

    loss += total_variation_weight * total_variation_loss(
        combination_image, height, width)

    grads = backend.gradients(loss, combination_image)

    outputs = [loss]
    outputs += grads
    f_outputs = backend.function([combination_image], outputs)

    evaluator = Evaluator(f_outputs, height, width)

    x = np.random.uniform(0, 255, (1, height, width, 3)) - 128.

    for i in range(iterations):
        print('Start of iteration', i)
        start_time = time.time()
        x, min_val, info = fmin_l_bfgs_b(evaluator.loss,
                                         x.flatten(),
                                         fprime=evaluator.grads,
                                         maxfun=20)
        print('Current loss value:', min_val)
        end_time = time.time()
        print('Iteration %d completed in %ds' % (i, end_time - start_time))

        x = x.reshape((height, width, 3))
        x = x[:, :, ::-1]
        x[:, :, 0] += 103.939
        x[:, :, 1] += 116.779
        x[:, :, 2] += 123.68
        x = np.clip(x, 0, 255).astype('uint8')

    img = Image.fromarray(x)
    return img
x_train = x_train.reset_index(drop=True)
x_test = x_test.reset_index(drop=True)
y_train = y_train.reset_index(drop=True)
y_test = y_test.reset_index(drop=True)


train_steps = int(x_train.shape[0]/batch_size)
valid_steps = int(x_test.shape[0]/batch_size)


train_gen = KinImageReader(x_train,y_train,batch_size,height,width)
valid_gen = KinImageReader(x_test,y_test,batch_size,height,width)


# Define Pretrained VGG16 Application from Keras
model1 = VGG16(weights='imagenet', include_top=False,input_shape=(height,width, 3))
model2 = VGG16(weights='imagenet', include_top=False,input_shape=(height,width, 3))

# Append label for each head so merge layer doesnt get confused
for layer in model1.layers:
    layer.name = layer.name + str("_1")
    layer.trainable = False

for layer in model2.layers:
    layer.name = layer.name + str("_2")
    layer.trainable = False


# Flatten the layers before merging
a = Flatten()(model1.output)
b = Flatten()(model2.output)
    def create_model(self):
        input_tensor = Input(shape=self.input_shape)
        h, w, c = self.input_shape
        depth_tensor = Input(
            shape=(h, w, 1))  # Already prepared depthmap with PyDNet

        # VGG16 encodes with 5 successives blocks of Conv2D and 2x2 MaxPooling (s=2)
        # So it divides input size by 2**5 = 32
        # We zero pad image so that both sides are multiples of 32
        # If we don't, information is lost at encoding step
        # TODO: Investigate Mirror Padding instead of Zero Padding
        modVGG16 = 32
        hpad = (modVGG16 - w % modVGG16) % modVGG16
        vpad = (modVGG16 - h % modVGG16) % modVGG16

        ceil_hpad = int(np.ceil(hpad / 2))
        floor_hpad = int(np.floor(hpad / 2))

        ceil_vpad = int(np.ceil(vpad / 2))
        floor_vpad = int(np.floor(vpad / 2))

        encoder_input_tensor = ZeroPadding2D(
            padding=((ceil_vpad, floor_vpad), (ceil_hpad,
                                               floor_hpad)))(input_tensor)

        concat_depth_tensor = ZeroPadding2D(
            padding=((ceil_vpad, floor_vpad), (ceil_hpad,
                                               floor_hpad)))(depth_tensor)

        encoder = VGG16(include_top=False,
                        weights=self.pre_trained_encoder,
                        input_tensor=encoder_input_tensor,
                        input_shape=self.input_shape,
                        pooling="None")  # type: tModel

        L = [layer
             for i, layer in enumerate(encoder.layers)]  # type: List[Layer]
        for layer in L:
            layer.trainable = False  # freeze VGG16
        L.reverse()

        x = encoder.output
        x = Dropout(0.2)(x)

        # Block 5
        if self.dsegnet_indices:
            x = DePool2D(L[0],
                         size=L[0].pool_size,
                         input_shape=encoder.output_shape[1:],
                         name='DePool5')(x)
        else:
            x = UpSampling2D(size=L[0].pool_size,
                             input_shape=encoder.output_shape[1:],
                             name='UpSampling5')(x)

        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[1].filters,
            L[1].kernel_size,
            padding=L[1].padding,
            kernel_initializer=self.weights_init,
            name='block5_deconv3')(x)))
        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[2].filters,
            L[2].kernel_size,
            padding=L[2].padding,
            kernel_initializer=self.weights_init,
            name='block5_deconv5')(x)))
        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[3].filters,
            L[3].kernel_size,
            padding=L[3].padding,
            kernel_initializer=self.weights_init,
            name='block5_deconv1')(x)))
        x = Dropout(0.2)(x)
        # Block 4
        if self.dsegnet_indices:
            x = DePool2D(L[4], size=L[4].pool_size, name='DePool4')(x)
        else:
            x = UpSampling2D(size=L[4].pool_size, name='UpSampling4')(x)

        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[5].filters,
            L[5].kernel_size,
            padding=L[5].padding,
            kernel_initializer=self.weights_init,
            name='block4_deconv3')(x)))
        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[6].filters,
            L[6].kernel_size,
            padding=L[6].padding,
            kernel_initializer=self.weights_init,
            name='block4_deconv2')(x)))
        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[7].filters,
            L[7].kernel_size,
            padding=L[7].padding,
            kernel_initializer=self.weights_init,
            name='block4_deconv1')(x)))
        x = Dropout(0.3)(x)
        # Block 3
        if self.dsegnet_indices:
            x = DePool2D(L[8], size=L[8].pool_size, name='DePool3')(x)
        else:
            x = UpSampling2D(size=L[8].pool_size, name='UpSampling3')(x)

        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[10].filters,
            L[10].kernel_size,
            padding=L[10].padding,
            kernel_initializer=self.weights_init,
            name='block3_deconv2')(x)))
        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[11].filters,
            L[11].kernel_size,
            padding=L[11].padding,
            kernel_initializer=self.weights_init,
            name='block3_deconv1')(x)))
        x = Dropout(0.3)(x)
        # Block 2
        if self.dsegnet_indices:
            x = DePool2D(L[12], size=L[12].pool_size, name='DePool2')(x)
        else:
            x = UpSampling2D(size=L[12].pool_size, name='UpSampling2')(x)

        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[13].filters,
            L[13].kernel_size,
            padding=L[13].padding,
            kernel_initializer=self.weights_init,
            name='block2_deconv2')(x)))

        # Next layer drops a filters to make sure we have a power of two filters after concatenation
        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[14].filters - 1,
            L[14].kernel_size,
            padding=L[14].padding,
            kernel_initializer=self.weights_init,
            name='block2_deconv1')(x)))
        # Block 1
        if self.dsegnet_indices:
            x = DePool2D(L[15], size=(L[15].pool_size), name='DePool1')(x)
        else:
            x = UpSampling2D(size=L[15].pool_size, name='UpSampling1')(x)

        # Plugging in the depth
        x = Concatenate()([x, concat_depth_tensor])

        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[16].filters,
            L[16].kernel_size,
            padding=L[16].padding,
            kernel_initializer=self.weights_init,
            name='block1_deconv2')(x)))

        x = Activation('relu')(BatchNormalization()(Conv2D(
            L[17].filters,
            L[17].kernel_size,
            padding=L[17].padding,
            kernel_initializer=self.weights_init,
            name='block1_deconv1')(x)))

        x = Conv2D(self.num_classes, (1, 1),
                   padding='valid',
                   kernel_initializer=self.weights_init,
                   name='block1_deconv_classes' + str(self.num_classes))(x)

        x = Cropping2D(cropping=(vpad // 2, hpad // 2))(x)

        x = Activation('softmax')(x)
        predictions = x

        segnet = Model(inputs=[encoder.inputs[0], depth_tensor],
                       outputs=predictions)  # type: tModel

        if self.pre_trained_weights is not None:
            if os.path.exists(self.pre_trained_weights):
                segnet.load_weights(self.pre_trained_weights,
                                    by_name=self.load_weights_by_name)
            else:
                raise AssertionError(
                    'Not able to load weights. File does not exist')

        return segnet
Exemple #17
0
POOLING = 'avg'
x_train = np.zeros((len(labels), INPUT_SIZE, INPUT_SIZE, 3), dtype='float32')
for i, img_id in tqdm(enumerate(labels['id'])):
    img = read_img(img_id, 'train', (INPUT_SIZE, INPUT_SIZE))
    x = preprocess_input(np.expand_dims(img.copy(), axis=0))
    x_train[i] = x
print('Train Images shape: {} size: {:,}'.format(x_train.shape, x_train.size))


# In[ ]:


Xtr = x_train[train_idx]
Xv = x_train[valid_idx]
print((Xtr.shape, Xv.shape, ytr.shape, yv.shape))
vgg_bottleneck = VGG16(weights='imagenet', include_top=False, pooling=POOLING)
train_vgg_bf = vgg_bottleneck.predict(Xtr, batch_size=32, verbose=1)
valid_vgg_bf = vgg_bottleneck.predict(Xv, batch_size=32, verbose=1)
print('VGG train bottleneck features shape: {} size: {:,}'.format(train_vgg_bf.shape, train_vgg_bf.size))
print('VGG valid bottleneck features shape: {} size: {:,}'.format(valid_vgg_bf.shape, valid_vgg_bf.size))


# # LogReg on VGG bottleneck features

# In[ ]:


logreg = LogisticRegression(multi_class='multinomial', solver='lbfgs', random_state=SEED)
logreg.fit(train_vgg_bf, (ytr * range(NUM_CLASSES)).sum(axis=1))
valid_probs = logreg.predict_proba(valid_vgg_bf)
valid_preds = logreg.predict(valid_vgg_bf)
Exemple #18
0
    nb_train_samples, img_rows, img_cols = x_train.shape
    nb_val_samples = x_val.shape[0]

    train_batchsize = 10
    val_batchsize = 5
    tst_batchsize = 5
    epochs = 10
    lr = 1e-4

    image_size = 224
    target_angles = [0, 180, 270, 90]

    # Load the VGG model
    vgg_conv = VGG16(weights='imagenet',
                     include_top=False,
                     input_shape=(224, 224, 3))
    # Freeze n number of layers from the last
    for layer in vgg_conv.layers[:]:
        layer.trainable = False

    # Check the trainable status of the individual layers
    for layer in vgg_conv.layers:
        print(layer, layer.trainable)

    # Create a sequential model
    model = createModel()
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.RMSprop(lr=lr),
                  metrics=['acc'])
Exemple #19
0
 def __init__(self):
     base_model = VGG16(weights='imagenet')
     self.model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)  #FC1 as the output feature
     self.graph = tf.get_default_graph()
Exemple #20
0
    y_true_cont = true_labels[:, 1]
    reduced_pred = tf.reduce_sum(
        predicted_labels * idx_tensor,
        1) * 3 - 99  # reducing 1st axis of predicted labels

    mse_loss = tf.keras.losses.mean_squared_error(y_true_cont, reduced_pred)

    angle_loss = cls_loss + alpha * mse_loss

    return angle_loss


inputs = Input(shape=(INPUT_SIZE, INPUT_SIZE, 3))

# # VGG16 backbone
net = VGG16(weights=None, include_top=False)
feature = net(inputs)

# feature = Conv2D(filters=64, kernel_size=(11, 11), strides=4, padding='same', activation=tf.nn.relu)(inputs)
# feature = MaxPool2D(pool_size=(3, 3), strides=2)(feature)
# feature = Conv2D(filters=192, kernel_size=(5, 5), padding='same', activation=tf.nn.relu)(feature)
# feature = MaxPool2D(pool_size=(3, 3), strides=2)(feature)
# feature = Conv2D(filters=384, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)(feature)
# feature = Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)(feature)
# feature = Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation=tf.nn.relu)(feature)
# feature = MaxPool2D(pool_size=(3, 3), strides=2)(feature)
feature = Flatten()(feature)
feature = Dropout(0.5)(feature)
feature = Dense(units=4096, activation=tf.nn.relu)(feature)

# Output layers for pitch, roll and yaw
Exemple #21
0
    train_img = preprocess_input(np.array(train_img))

    # applying the same procedure with the valid dataset
    valid_img = []
    valid_y = []
    for i in range(len(valid)):
        temp_img = image.load_img(valid_path + valid['filename'][i],
                                  target_size=(224, 224))
        temp_img = image.img_to_array(temp_img)
        valid_img.append(temp_img)
        valid_y.append(0 if 'negative' in valid['filename'][i] else 1)

    valid_img = preprocess_input(np.array(valid_img))

    # loading VGG16 model weights
    model = VGG16(weights='imagenet', include_top=False)
    # Extracting features from the train dataset using the VGG16 pre-trained model

    features_train = model.predict(train_img)
    # Extracting features from the train dataset using the VGG16 pre-trained model

    features_valid = model.predict(valid_img)

    train_x = features_train.reshape((len(train), 7 * 7 * 512))
    train_y = np.asarray(train_y)

    valid_x = features_valid.reshape((len(valid), 7 * 7 * 512))
    valid_y = np.asarray(valid_y)

    X_train, X_valid, Y_train, Y_valid = train_x, valid_x, train_y, valid_y
'''
Created on Jul 16, 2018

@author: User
'''
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16
import numpy as np

model = VGG16(include_top=False, weights='imagenet')
batch_size = 16
img_width, img_height = 150, 150
train_size = 19984
validation_size = 4992
test_size = 12500
train_data_dir = '../data/train/'
validation_data_dir = '../data/validation/'
test_data_dir = '../data/test/'


def extract_features():
    datagen = ImageDataGenerator(rescale=1. / 255)

    generator = datagen.flow_from_directory(train_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=batch_size,
                                            class_mode=None,
                                            shuffle=False)
    bottleneck_features_train = model.predict_generator(
        generator, train_size // batch_size)
Exemple #23
0
plt.plot(epochs, train_acc, 'g', label='Training accuracy')
plt.plot(epochs, test_acc, 'r', label='Test accuracy')
plt.plot(epochs, valid_acc, 'b', label='valid accuracy')
plt.title('Training, Validation and Test accuracy')
plt.xlabel('Epochs')
plt.title('Training, Validation and Test accuracy')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend()
plt.show()

# cannot easily visualize filters lower down
from keras.applications.vgg16 import VGG16
from matplotlib import pyplot
# load the model
model = VGG16()
filters, biases = model.layers[4].get_weights()
# normalize filter values to 0-1 so we can visualize them
f_min, f_max = filters.min(), filters.max()
filters = (filters - f_min) / (f_max - f_min)
# plot first few filters
n_filters, ix = 8, 1
for i in range(n_filters):
    # get the filter
    f = filters[:, :, :, i]
    # plot each channel separately
    for j in range(1):
        # specify subplot and turn of axis
        ax = pyplot.subplot(n_filters, 4, ix)
        ax.set_xticks([])
        ax.set_yticks([])
def InitialiazeModel(head_only,weights,model_name,lr):
    
    if model_name == 'VGG19':
        from keras.applications.vgg19 import VGG19
        base_model = VGG19(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'VGG16':
        from keras.applications.vgg16 import VGG16
        base_model = VGG16(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'MobileNet':
        from keras.applications.mobilenet import MobileNet
        base_model = MobileNet(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'ResNet50':
        from keras.applications.resnet50 import ResNet50
        base_model = ResNet50(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'Xception':
        from keras.applications.xception import Xception
        base_model = Xception(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'InceptionV3':
        from keras.applications.inception_v3 import InceptionV3
        base_model = InceptionV3(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'InceptionResNetV2':
        from keras.applications.inception_resnet_v2 import InceptionResNetV2
        base_model = InceptionResNetV2(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'NASNetLarge':
        from keras.applications.nasnet import NASNetLarge
        base_model = NASNetLarge(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    elif model_name == 'NASNetMobile':
        from keras.applications.nasnet import NASNetMobile
        base_model = NASNetMobile(include_top=False, weights='imagenet',
                      input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg')
    else:
        raise ValueError('Network name is undefined')
        
    if head_only:
        for lay in base_model.layers:
            lay.trainable = False


    #manipulated = Input(shape=(1,))
    x = base_model.output
    #x = concatenate([x, manipulated])
    x = Dense(NUM_CATEGS, activation='softmax', name='predictions')(x)
    #model = Model([base_model.input,manipulated], x)
    model = Model(base_model.input, x)
    
    #print(model.summary())
    if weights != '':
        model.load_weights(weights)
    
    MODEL_OPTIMIZER = optimizers.SGD(lr=lr, momentum=0.9, nesterov=True)
    model.compile(loss=MODEL_LOSS, optimizer=MODEL_OPTIMIZER, metrics=[MODEL_METRIC])

    return model
Exemple #25
0
import numpy as np
import pandas as pd
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
HEADERS = "author_id, tench, goldfish, great_white_shark, tiger_shark, hammerhead, electric_ray, stingray, c**k, hen, ostrich, brambling, goldfinch, house_finch, junco, indigo_bunting, robin, bulbul, jay, magpie, chickadee, water_ouzel, kite, bald_eagle, vulture, great_grey_owl, European_fire_salamander, common_newt, eft, spotted_salamander, axolotl, bullfrog, tree_frog, tailed_frog, loggerhead, leatherback_turtle, mud_turtle, terrapin, box_turtle, banded_gecko, common_iguana, American_chameleon, whiptail, agama, frilled_lizard, alligator_lizard, Gila_monster, green_lizard, African_chameleon, Komodo_dragon, African_crocodile, American_alligator, triceratops, thunder_snake, ringneck_snake, hognose_snake, green_snake, king_snake, garter_snake, water_snake, vine_snake, night_snake, boa_constrictor, rock_python, Indian_cobra, green_mamba, sea_snake, horned_viper, diamondback, sidewinder, trilobite, harvestman, scorpion, black_and_gold_garden_spider, barn_spider, garden_spider, black_widow, tarantula, wolf_spider, tick, centipede, black_grouse, ptarmigan, ruffed_grouse, prairie_chicken, peacock, quail, partridge, African_grey, macaw, sulphur-crested_cockatoo, lorikeet, coucal, bee_eater, hornbill, hummingbird, jacamar, toucan, drake, red-breasted_merganser, goose, black_swan, tusker, echidna, platypus, wallaby, koala, wombat, jellyfish, sea_anemone, brain_coral, flatworm, nematode, conch, snail, slug, sea_slug, chiton, chambered_nautilus, Dungeness_crab, rock_crab, fiddler_crab, king_crab, American_lobster, spiny_lobster, crayfish, hermit_crab, isopod, white_stork, black_stork, spoonbill, flamingo, little_blue_heron, American_egret, bittern, crane, limpkin, European_gallinule, American_coot, bustard, ruddy_turnstone, red-backed_sandpiper, redshank, dowitcher, oystercatcher, pelican, king_penguin, albatross, grey_whale, killer_whale, dugong, sea_lion, Chihuahua, Japanese_spaniel, Maltese_dog, Pekinese, Shih-Tzu, Blenheim_spaniel, papillon, toy_terrier, Rhodesian_ridgeback, Afghan_hound, basset, beagle, bloodhound, bluetick, black-and-tan_coonhound, Walker_hound, English_foxhound, redbone, borzoi, Irish_wolfhound, Italian_greyhound, whippet, Ibizan_hound, Norwegian_elkhound, otterhound, Saluki, Scottish_deerhound, Weimaraner, Staffordshire_bullterrier, American_Staffordshire_terrier, Bedlington_terrier, Border_terrier, Kerry_blue_terrier, Irish_terrier, Norfolk_terrier, Norwich_terrier, Yorkshire_terrier, wire-haired_fox_terrier, Lakeland_terrier, Sealyham_terrier, Airedale, cairn, Australian_terrier, Dandie_Dinmont, Boston_bull, miniature_schnauzer, giant_schnauzer, standard_schnauzer, Scotch_terrier, Tibetan_terrier, silky_terrier, soft-coated_wheaten_terrier, West_Highland_white_terrier, Lhasa, flat-coated_retriever, curly-coated_retriever, golden_retriever, Labrador_retriever, Chesapeake_Bay_retriever, German_short-haired_pointer, vizsla, English_setter, Irish_setter, Gordon_setter, Brittany_spaniel, clumber, English_springer, Welsh_springer_spaniel, cocker_spaniel, Sussex_spaniel, Irish_water_spaniel, kuvasz, schipperke, groenendael, malinois, briard, kelpie, komondor, Old_English_sheepdog, Shetland_sheepdog, collie, Border_collie, Bouvier_des_Flandres, Rottweiler, German_shepherd, Doberman, miniature_pinscher, Greater_Swiss_Mountain_dog, Bernese_mountain_dog, Appenzeller, EntleBucher, boxer, bull_mastiff, Tibetan_mastiff, French_bulldog, Great_Dane, Saint_Bernard, Eskimo_dog, malamute, Siberian_husky, dalmatian, affenpinscher, basenji, pug, Leonberg, Newfoundland, Great_Pyrenees, Samoyed, Pomeranian, chow, keeshond, Brabancon_griffon, Pembroke, Cardigan, toy_poodle, miniature_poodle, standard_poodle, Mexican_hairless, timber_wolf, white_wolf, red_wolf, coyote, dingo, dhole, African_hunting_dog, hyena, red_fox, kit_fox, Arctic_fox, grey_fox, tabby, tiger_cat, Persian_cat, Siamese_cat, Egyptian_cat, cougar, lynx, leopard, snow_leopard, jaguar, lion, tiger, cheetah, brown_bear, American_black_bear, ice_bear, sloth_bear, mongoose, meerkat, tiger_beetle, ladybug, ground_beetle, long-horned_beetle, leaf_beetle, dung_beetle, rhinoceros_beetle, weevil, fly, bee, ant, grasshopper, cricket, walking_stick, cockroach, mantis, cicada, leafhopper, lacewing, dragonfly, damselfly, admiral, ringlet, monarch, cabbage_butterfly, sulphur_butterfly, lycaenid, starfish, sea_urchin, sea_cucumber, wood_rabbit, hare, Angora, hamster, porcupine, fox_squirrel, marmot, beaver, guinea_pig, sorrel, zebra, hog, wild_boar, warthog, hippopotamus, ox, water_buffalo, bison, ram, bighorn, ibex, hartebeest, impala, gazelle, Arabian_camel, llama, weasel, mink, polecat, black-footed_ferret, otter, skunk, badger, armadillo, three-toed_sloth, orangutan, gorilla, chimpanzee, gibbon, siamang, guenon, patas, baboon, macaque, langur, colobus, proboscis_monkey, marmoset, capuchin, howler_monkey, titi, spider_monkey, squirrel_monkey, Madagascar_cat, indri, Indian_elephant, African_elephant, lesser_panda, giant_panda, barracouta, eel, coho, rock_beauty, anemone_fish, sturgeon, gar, lionfish, puffer, abacus, abaya, academic_gown, accordion, acoustic_guitar, aircraft_carrier, airliner, airship, altar, ambulance, amphibian, analog_clock, apiary, apron, ashcan, assault_rifle, backpack, bakery, balance_beam, balloon, ballpoint, Band_Aid, banjo, bannister, barbell, barber_chair, barbershop, barn, barometer, barrel, barrow, baseball, basketball, bassinet, bassoon, bathing_cap, bath_towel, bathtub, beach_wagon, beacon, beaker, bearskin, beer_bottle, beer_glass, bell_cote, bib, bicycle-built-for-two, bikini, binder, binoculars, birdhouse, boathouse, bobsled, bolo_tie, bonnet, bookcase, bookshop, bottlecap, bow, bow_tie, brass, brassiere, breakwater, breastplate, broom, bucket, buckle, bulletproof_vest, bullet_train, butcher_shop, cab, caldron, candle, cannon, canoe, can_opener, cardigan, car_mirror, carousel, carpenter's_kit, carton, car_wheel, cash_machine, cassette, cassette_player, castle, catamaran, CD_player, cello, cellular_telephone, chain, chainlink_fence, chain_mail, chain_saw, chest, chiffonier, chime, china_cabinet, Christmas_stocking, church, cinema, cleaver, cliff_dwelling, cloak, clog, cocktail_shaker, coffee_mug, coffeepot, coil, combination_lock, computer_keyboard, confectionery, container_ship, convertible, corkscrew, cornet, cowboy_boot, cowboy_hat, cradle, crane, crash_helmet, crate, crib, Crock_Pot, croquet_ball, crutch, cuirass, dam, desk, desktop_computer, dial_telephone, diaper, digital_clock, digital_watch, dining_table, dishrag, dishwasher, disk_brake, dock, dogsled, dome, doormat, drilling_platform, drum, drumstick, dumbbell, Dutch_oven, electric_fan, electric_guitar, electric_locomotive, entertainment_center, envelope, espresso_maker, face_powder, feather_boa, file, fireboat, fire_engine, fire_screen, flagpole, flute, folding_chair, football_helmet, forklift, fountain, fountain_pen, four-poster, freight_car, French_horn, frying_pan, fur_coat, garbage_truck, gasmask, gas_pump, goblet, go-kart, golf_ball, golfcart, gondola, gong, gown, grand_piano, greenhouse, grille, grocery_store, guillotine, hair_slide, hair_spray, half_track, hammer, hamper, hand_blower, hand-held_computer, handkerchief, hard_disc, harmonica, harp, harvester, hatchet, holster, home_theater, honeycomb, hook, hoopskirt, horizontal_bar, horse_cart, hourglass, iPod, iron, jack-o'-lantern, jean, jeep, jersey, jigsaw_puzzle, jinrikisha, joystick, kimono, knee_pad, knot, lab_coat, ladle, lampshade, laptop, lawn_mower, lens_cap, letter_opener, library, lifeboat, lighter, limousine, liner, lipstick, Loafer, lotion, loudspeaker, loupe, lumbermill, magnetic_compass, mailbag, mailbox, maillot, maillot, manhole_cover, maraca, marimba, mask, matchstick, maypole, maze, measuring_cup, medicine_chest, megalith, microphone, microwave, military_uniform, milk_can, minibus, miniskirt, minivan, missile, mitten, mixing_bowl, mobile_home, Model_T, modem, monastery, monitor, moped, mortar, mortarboard, mosque, mosquito_net, motor_scooter, mountain_bike, mountain_tent, mouse, mousetrap, moving_van, muzzle, nail, neck_brace, necklace, nipple, notebook, obelisk, oboe, ocarina, odometer, oil_filter, organ, oscilloscope, overskirt, oxcart, oxygen_mask, packet, paddle, paddlewheel, padlock, paintbrush, pajama, palace, panpipe, paper_towel, parachute, parallel_bars, park_bench, parking_meter, passenger_car, patio, pay-phone, pedestal, pencil_box, pencil_sharpener, perfume, Petri_dish, photocopier, pick, pickelhaube, picket_fence, pickup, pier, piggy_bank, pill_bottle, pillow, ping-pong_ball, pinwheel, pirate, pitcher, plane, planetarium, plastic_bag, plate_rack, plow, plunger, Polaroid_camera, pole, police_van, poncho, pool_table, pop_bottle, pot, potter's_wheel, power_drill, prayer_rug, printer, prison, projectile, projector, puck, punching_bag, purse, quill, quilt, racer, racket, radiator, radio, radio_telescope, rain_barrel, recreational_vehicle, reel, reflex_camera, refrigerator, remote_control, restaurant, revolver, rifle, rocking_chair, rotisserie, rubber_eraser, rugby_ball, rule, running_shoe, safe, safety_pin, saltshaker, sandal, sarong, sax, scabbard, scale, school_bus, schooner, scoreboard, screen, screw, screwdriver, seat_belt, sewing_machine, shield, shoe_shop, shoji, shopping_basket, shopping_cart, shovel, shower_cap, shower_curtain, ski, ski_mask, sleeping_bag, slide_rule, sliding_door, slot, snorkel, snowmobile, snowplow, soap_dispenser, soccer_ball, sock, solar_dish, sombrero, soup_bowl, space_bar, space_heater, space_shuttle, spatula, speedboat, spider_web, spindle, sports_car, spotlight, stage, steam_locomotive, steel_arch_bridge, steel_drum, stethoscope, stole, stone_wall, stopwatch, stove, strainer, streetcar, stretcher, studio_couch, stupa, submarine, suit, sundial, sunglass, sunglasses, sunscreen, suspension_bridge, swab, sweatshirt, swimming_trunks, swing, switch, syringe, table_lamp, tank, tape_player, teapot, teddy, television, tennis_ball, thatch, theater_curtain, thimble, thresher, throne, tile_roof, toaster, tobacco_shop, toilet_seat, torch, totem_pole, tow_truck, toyshop, tractor, trailer_truck, tray, trench_coat, tricycle, trimaran, tripod, triumphal_arch, trolleybus, trombone, tub, turnstile, typewriter_keyboard, umbrella, unicycle, upright, vacuum, vase, vault, velvet, vending_machine, vestment, viaduct, violin, volleyball, waffle_iron, wall_clock, wallet, wardrobe, warplane, washbasin, washer, water_bottle, water_jug, water_tower, whiskey_jug, whistle, wig, window_screen, window_shade, Windsor_tie, wine_bottle, wing, wok, wooden_spoon, wool, worm_fence, wreck, yawl, yurt, web_site, comic_book, crossword_puzzle, street_sign, traffic_light, book_jacket, menu, plate, guacamole, consomme, hot_pot, trifle, ice_cream, ice_lolly, French_loaf, bagel, pretzel, cheeseburger, hotdog, mashed_potato, head_cabbage, broccoli, cauliflower, zucchini, spaghetti_squash, acorn_squash, butternut_squash, cucumber, artichoke, bell_pepper, cardoon, mushroom, Granny_Smith, strawberry, orange, lemon, fig, pineapple, banana, jackfruit, custard_apple, pomegranate, hay, carbonara, chocolate_sauce, dough, meat_loaf, pizza, potpie, burrito, red_wine, espresso, cup, eggnog, alp, bubble, cliff, coral_reef, geyser, lakeside, promontory, sandbar, seashore, valley, volcano, ballplayer, groom, scuba_diver, rapeseed, daisy, yellow_lady's_slipper, corn, acorn, hip, buckeye, coral_fungus, agaric, gyromitra, stinkhorn, earthstar, hen-of-the-woods, bolete, ear, toilet_tissue, class\n"
# HEADERS = "author_id,"
# HEADERS += ", ".join([str(i) for i in range( 7 * 7 * 512)])
# HEADERS += ",class\n"
# MODEL = VGG16(weights='imagenet', include_top=False)
MODEL = VGG16(weights='imagenet')
BASE_DIR_PHOTOS = sys.argv[1]
OUTPUT_FILE = open('/media/ivan/DDE/datasets_proyecto_mt/notop/en_test_2.csv', "w")
OUTPUT_FILE.write(HEADERS)
LABELS_FILE = sys.argv[2]
IDX = 0

with open(LABELS_FILE) as json_file:
    GENDER_LABELS = json.load(json_file)

def get_list_of_labels(image_path, include_top=True):
    try:
        image = load_img(image_path, target_size=(224, 224))
    except Exception as e:
        print(e)
        return
def get_vgg_7conv(input_shape):
    img_input = Input(input_shape)
    vgg16_base = VGG16(input_tensor=img_input, include_top=False)
    for l in vgg16_base.layers:
        l.trainable = True
    conv1 = vgg16_base.get_layer("block1_conv2").output
    conv2 = vgg16_base.get_layer("block2_conv2").output
    conv3 = vgg16_base.get_layer("block3_conv3").output
    pool3 = vgg16_base.get_layer("block3_pool").output

    conv4 = Conv2D(384, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block4_conv1")(pool3)
    conv4 = Conv2D(384, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block4_conv2")(conv4)
    pool4 = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(conv4)

    conv5 = Conv2D(512, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block5_conv1")(pool4)
    conv5 = Conv2D(512, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block5_conv2")(conv5)
    pool5 = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(conv5)

    conv6 = Conv2D(512, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block6_conv1")(pool5)
    conv6 = Conv2D(512, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block6_conv2")(conv6)
    pool6 = MaxPooling2D((2, 2), strides=(2, 2), name='block6_pool')(conv6)

    conv7 = Conv2D(512, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block7_conv1")(pool6)
    conv7 = Conv2D(512, (3, 3),
                   activation="relu",
                   padding='same',
                   kernel_initializer="he_normal",
                   name="block7_conv2")(conv7)

    up8 = concatenate([
        Conv2DTranspose(384, (3, 3),
                        activation="relu",
                        kernel_initializer="he_normal",
                        strides=(2, 2),
                        padding='same')(conv7), conv6
    ],
                      axis=3)
    conv8 = Conv2D(384, (3, 3),
                   activation="relu",
                   kernel_initializer="he_normal",
                   padding='same')(up8)

    up9 = concatenate([
        Conv2DTranspose(256, (3, 3),
                        activation="relu",
                        kernel_initializer="he_normal",
                        strides=(2, 2),
                        padding='same')(conv8), conv5
    ],
                      axis=3)
    conv9 = Conv2D(256, (3, 3),
                   activation="relu",
                   kernel_initializer="he_normal",
                   padding='same')(up9)

    up10 = concatenate([
        Conv2DTranspose(192, (3, 3),
                        activation="relu",
                        kernel_initializer="he_normal",
                        strides=(2, 2),
                        padding='same')(conv9), conv4
    ],
                       axis=3)
    conv10 = Conv2D(192, (3, 3),
                    activation="relu",
                    kernel_initializer="he_normal",
                    padding='same')(up10)

    up11 = concatenate([
        Conv2DTranspose(128, (3, 3),
                        activation="relu",
                        kernel_initializer="he_normal",
                        strides=(2, 2),
                        padding='same')(conv10), conv3
    ],
                       axis=3)
    conv11 = Conv2D(128, (3, 3),
                    activation="relu",
                    kernel_initializer="he_normal",
                    padding='same')(up11)

    up12 = concatenate([
        Conv2DTranspose(64, (3, 3),
                        activation="relu",
                        kernel_initializer="he_normal",
                        strides=(2, 2),
                        padding='same')(conv11), conv2
    ],
                       axis=3)
    conv12 = Conv2D(64, (3, 3),
                    activation="relu",
                    kernel_initializer="he_normal",
                    padding='same')(up12)

    up13 = concatenate([
        Conv2DTranspose(32, (3, 3),
                        activation="relu",
                        kernel_initializer="he_normal",
                        strides=(2, 2),
                        padding='same')(conv12), conv1
    ],
                       axis=3)
    conv13 = Conv2D(32, (3, 3),
                    activation="relu",
                    kernel_initializer="he_normal",
                    padding='same')(up13)

    conv13 = Conv2D(1, (1, 1))(conv13)
    conv13 = Activation("sigmoid")(conv13)
    model = Model(img_input, conv13)
    return model
Exemple #27
0
from tensorflow.contrib import keras
from keras.applications.vgg16 import VGG16
import keras.preprocessing
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
import numpy as np
import os


def features(img_path):
    img = keras.preprocessing.image.load_img(img_path, target_size=(224, 224))
    x = keras.preprocessing.image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    fc2_features = model.predict(x)
    return fc2_features


if __name__ == "__main__":
    directory = './data/test_images/'
    base_model = VGG16(weights='imagenet')
    model = Model(inputs=base_model.input,
                  outputs=base_model.get_layer('fc2').output)
    files = os.listdir(directory)
    img_output = np.zeros((1, 4096))
    for i in files:
        img_output = np.vstack((img_output, features(directory + i)))
    np.save('./data/test_features', img_output[1:])
Exemple #28
0
#Create training dataset to feed into the training model
train_datagen = training_augmentation.flow_from_directory(
    training_dataset,
    target_size=(x_ray_width, x_ray_height),
    batch_size=27,
    class_mode="categorical")
#Create testing dataset to feed into the training model
test_datagen = testing_augmentation.flow_from_directory(
    testing_dataset,
    target_size=(x_ray_width, x_ray_height),
    batch_size=27,
    class_mode="categorical")

#Initialize VGG16 model
vgg_network = VGG16(input_shape=(x_ray_width, x_ray_height, 3),
                    weights="imagenet",
                    include_top=True)

vgg_network.summary()

#To not retrain the pre-trained layers
for layers in (vgg_network.layers)[:19]:
    print(layers)
    layers.trainable = False

#Customized last layers
X = vgg_network.layers[-2].output
prediction = Dense(len(training_folders), activation="softmax")(X)

#Declare an object model
vgg_network_final = Model(inputs=vgg_network.input, outputs=prediction)
Exemple #29
0
y_test = keras.utils.to_categorical(y_test, num_classes)

if args.resume:
    print('resume from checkpoint')
    message = job_name + ' b_end'
    send_signal.send(args.node, 10002, message)
    model = keras.models.load_model(save_file)
    message = job_name + ' c_end'
    send_signal.send(args.node, 10002, message)
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(
Exemple #30
0
 def __init__(self):
     vgg_model = VGG16(weights='imagenet', include_top=True)
     self.model = Model(input=vgg_model.input,
                        output=vgg_model.get_layer('fc2').output)