Ejemplo n.º 1
0
def get_model(model_name="ResNet50"):
    base_model = None

    if model_name == "ResNet50":
        base_model = ResNet50(include_top=False,
                              weights='imagenet',
                              input_shape=(224, 224, 3),
                              pooling="avg")
    elif model_name == "Xception":
        base_model = Xception(include_top=False,
                              weights='imagenet',
                              input_shape=(299, 299, 3),
                              pooling="avg")

    prediction = Dense(units=101,
                       kernel_initializer="he_normal",
                       use_bias=False,
                       activation="softmax",
                       name="pred_age")(base_model.output)

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

    return model
Ejemplo n.º 2
0
def createCnn(name):
    if name == "VGG16":
        model = VGG16(weights="imagenet", include_top=False, pooling="avg")
    elif name == "VGG19":
        model = VGG19(weights="imagenet", include_top=False, pooling="avg")
    elif name == "ResNet50":
        model = ResNet50(weights="imagenet", include_top=False, pooling="avg")
    elif name == "InceptionV3":
        model = InceptionV3(weights="imagenet",
                            include_top=False,
                            pooling="avg")
    elif name == "Xception":
        model = Xception(weights="imagenet", include_top=False, pooling="avg")
    # 意外报错 ValueError: When setting`include_top=True`, `input_shape` should be (224, 224, 3).
    # elif name == "MobileNet":
    #     model = MobileNet(weights="imagenet", include_top=False, input_shape=(200, 200, 3),
    #                       pooling="avg")
    else:
        model = None
        print("请输入正确的cnn预训练模型名")
        exit()
    # model.summary()
    return model
Ejemplo n.º 3
0
def optimiser_experiment_model(hp):
    PRE_TRAINED_MODEL = Xception(input_shape=(IMG_HEIGHT, IMG_WIDTH, 3),
                                 include_top=False,
                                 pooling='avg',
                                 weights='imagenet')

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

    x = layers.Flatten()(PRE_TRAINED_MODEL.output)
    x = layers.Dense(1, activation='sigmoid')(x)

    MODEL = Model(PRE_TRAINED_MODEL.input, x)

    MODEL.compile(optimizer=hp.Choice('optimiser',
                                      values=['Adam', 'RMSprop', 'SGD']),
                  loss='binary_crossentropy',
                  metrics=[
                      metrics.BinaryAccuracy(name='acc'),
                      metrics.AUC(name='auc'),
                      metrics.FalsePositives(name='fp')
                  ])
    return MODEL
Ejemplo n.º 4
0
def create_model():
    """
    Create our fine-tuned model & load the weights
    """
    base_model = Xception(include_top=False, weights=None)

    # Add Custom FC
    x = base_model.output
    x = layers.Dense(128,
                     activation='relu',
                     kernel_regularizer=regularizers.l2(0.0075))(x)
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dropout(0.5)(x)
    predictions = layers.Dense(12, activation='softmax')(x)

    # Turn into model & load the weights
    model = models.Model(inputs=base_model.input, outputs=predictions)
    model.load_weights("../models/xception_finetune_12_reg_75_block4.h5")

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

    return model
Ejemplo n.º 5
0
def create_transfer_model(input_size, n_categories, weights = 'imagenet'):
    '''
    Builds transfer learning model based on Xception https://arxiv.org/abs/1610.02357

    Parameters
    ----------
    input_size: tuple
    n_categories: int
    weights: hf5 file

    Returns
    -------
    model: Keras model
    '''
        base_model = Xception(weights=weights,
                          include_top=False,
                          input_shape=input_size)

        model = base_model.output
        model = GlobalAveragePooling2D()(model)
        predictions = Dense(n_categories, activation='softmax')(model)
        model = Model(inputs=base_model.input, outputs=predictions)

        return model
def setXception(dropout_rate, lr):
    main_input = layers.Input([config.img_size, config.img_size, 1])

    x = layers.BatchNormalization()(main_input)

    base_model = Xception(weights=None, input_tensor=x, include_top=False)

    flatten = layers.GlobalAveragePooling2D()(base_model.output)
    # flatten = layers.Flatten()(base_model.output)

    fc = Dense(
        2048,
        activation='relu',
        kernel_regularizer=l2(0.001),
        bias_regularizer=l2(0.001),
    )(flatten)
    fc = Dropout(dropout_rate)(fc)
    fc = Dense(
        2048,
        activation='relu',
        kernel_regularizer=l2(0.001),
        bias_regularizer=l2(0.001),
    )(fc)
    fc = Dropout(dropout_rate)(fc)

    predictions = Dense(config.class_num, activation="softmax")(fc)

    model = keras.Model(inputs=main_input,
                        outputs=predictions,
                        name='xception')

    optimizer = keras.optimizers.Adam(lr)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['categorical_accuracy'])
    return model
Ejemplo n.º 7
0
def XceModel(size=71):

    basemodel = Xception(include_top=True,
                         weights="imagenet",
                         input_shape=(size, size, 3))
    model = Model(input=basemodel.input, output=basemodel.layers[-2].output)
    y = Dense(230, activation="softmax", name="classifylayer")(model.output)

    my_model = Model(inputs=model.input, outputs=y)
    trainable = False
    for lay in my_model.layers:
        if (lay.name == "block14_sepconv1"):
            trainable = True
        lay.trainable = trainable

    my_model.summary()
    keras.utils.plot_model(my_model,
                           to_file='output/xce_model.png',
                           show_shapes=True)

    my_model.compile(loss='categorical_crossentropy',
                     optimizer="adam",
                     metrics=['accuracy'])
    return my_model
Ejemplo n.º 8
0
def loss_experiment_model(hp):
    PRE_TRAINED_MODEL = Xception(input_shape=(IMG_HEIGHT, IMG_WIDTH, 3),
                                 include_top=False,
                                 pooling='avg',
                                 weights='imagenet')

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

    x = layers.Flatten()(PRE_TRAINED_MODEL.output)
    x = layers.Dense(1, activation='sigmoid')(x)

    MODEL = Model(PRE_TRAINED_MODEL.input, x)

    chosen_loss = hp.Choice(
        'loss', values=['SigmoidFocalCrossEntropy', 'BinaryCrossentropy'])
    MODEL.compile(optimizer='adam',
                  loss=eval(chosen_loss)(),
                  metrics=[
                      metrics.BinaryAccuracy(name='acc'),
                      metrics.AUC(name='auc'),
                      metrics.FalsePositives(name='fp')
                  ])
    return MODEL
Ejemplo n.º 9
0
def create_model():
    # Create model
    # model = models.Sequential()
    #
    # model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(299, 299, 3), name='block1_conv1'))
    # model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', name='block1_conv2'))
    # model.add(MaxPooling2D(pool_size=(2, 2), name='block1_pool1'))
    # model.add(Dropout(0.25, name='block1_dropout1'))
    #
    # model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', name='block2_conv1'))
    # model.add(MaxPooling2D(pool_size=(2, 2), name='block2_pool1'))
    # model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', name='block2_conv2'))
    # model.add(MaxPooling2D(pool_size=(2, 2), name='block2_pool2'))
    # model.add(Dropout(0.25, name='block2_dropout1'))
    #
    # model.add(Flatten(name='block3_flatten'))
    # model.add(Dense(512, activation='relu', name='block3_dense1'))
    # model.add(Dropout(0.5, name='block3_dropout1'))
    # model.add(Dense(3, activation='softmax', name='block3_dense2'))

    # model = InceptionResNetV2(weights=None, classes=4)
    model = Xception(weights=None, input_shape=(299, 299, 3), classes=4)

    return model
Ejemplo n.º 10
0
    def build(self):

        if self.model_name == 'VGG16':
            model = VGG16(include_top=True,
                          weights=None,
                          input_tensor=None,
                          input_shape=self.image_shape,
                          pooling='max',
                          classes=self.classes)
        elif self.model_name == 'VGG19':
            model = VGG19(include_top=True,
                          weights=None,
                          input_tensor=None,
                          input_shape=self.image_shape,
                          pooling='max',
                          classes=self.classes)
        elif self.model_name == 'ResNet50':
            model = ResNet50(include_top=True,
                             weights=None,
                             input_tensor=None,
                             input_shape=self.image_shape,
                             pooling='max',
                             classes=self.classes)
        elif self.model_name == 'InceptionV3':
            model = InceptionV3(include_top=True,
                                weights=None,
                                input_tensor=None,
                                input_shape=self.image_shape,
                                pooling='max',
                                classes=self.classes)
        elif self.model_name == 'Xception':
            model = Xception(include_top=True,
                             weights=None,
                             input_tensor=None,
                             input_shape=self.image_shape,
                             pooling='max',
                             classes=self.classes)
        elif self.model_name == 'MobileNet':
            model = MobileNet(include_top=True,
                              weights=None,
                              input_tensor=None,
                              input_shape=self.image_shape,
                              pooling='max',
                              classes=self.classes)
        elif self.model_name == 'DenseNet':
            model = DenseNet_Model(self.image_shape,
                                   self.classes).build_model()
        elif self.model_name == 'ResNet18':
            model = Resnet_Model(self.image_shape,
                                 self.classes).build_resnet18()
        elif self.model_name == 'ResNet34':
            model = Resnet_Model(self.image_shape,
                                 self.classes).build_resnet34()
        elif self.model_name == 'ResNet101':
            model = Resnet_Model(self.image_shape,
                                 self.classes).build_resnet101()
        elif self.model_name == 'ResNet152':
            model = Resnet_Model(self.image_shape,
                                 self.classes).build_resnet152()
        elif self.model_name == 'AlexNet':
            model = MODEL(self.image_shape, self.classes).AlexNet()
        elif self.model_name == 'LeNet':
            model = MODEL(self.image_shape, self.classes).LeNet()
        elif self.model_name == 'ZF_Net':
            model = MODEL(self.image_shape, self.classes).ZF_Net()
        elif self.model_name == 'mnist_net':
            model = MODEL(self.image_shape, self.classes).mnist_net()
        elif self.model_name == 'VGG16_TSL':
            model = MODEL(self.image_shape, self.classes).VGG16_TSL()

        adam = Adam(lr=self.lr,
                    beta_1=0.9,
                    beta_2=0.999,
                    epsilon=None,
                    decay=0.0)
        model.compile(loss="categorical_crossentropy",
                      optimizer=adam,
                      metrics=["accuracy"])

        return model
Ejemplo n.º 11
0
auto_lr = ReduceLROnPlateau(monitor='val_loss',
                            factor=0.1,
                            patience=3,
                            verbose=0,
                            mode='auto',
                            epsilon=0.0001,
                            cooldown=0,
                            min_lr=0)

if os.path.exists('dog_single_xception.h5'):
    model = load_model('dog_single_xception.h5')
else:
    # create the base pre-trained model
    input_tensor = Input(shape=(299, 299, 3))
    base_model1 = Xception(include_top=True,
                           weights='imagenet',
                           input_tensor=None,
                           input_shape=None)
    base_model1 = Model(inputs=[base_model1.input],
                        outputs=[base_model1.get_layer('avg_pool').output],
                        name='xception')

    base_model2 = InceptionV3(include_top=True,
                              weights='imagenet',
                              input_tensor=None,
                              input_shape=None)
    base_model2 = Model(inputs=[base_model2.input],
                        outputs=[base_model2.get_layer('avg_pool').output],
                        name='inceptionv3')

    img1 = Input(shape=(299, 299, 3), name='img_1')
Ejemplo n.º 12
0
"""

import matplotlib.pyplot as plt
from sklearn.datasets import load_files
import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Input, Conv2D
from keras.applications import Xception
from keras.models import Model

train_directory = "../../Dermatologist_Dataset/data/train/"
test_directory = "../../Dermatologist_Dataset/data/test/"
validation_directory = "../../Dermatologist_Dataset/data/valid/"

xception = Xception(weights="imagenet", include_top = False, input_shape=(224,224,3))

model = xception.output
model = Flatten()(model)
model = Dense(256, activation='relu', input_dim=7 * 7 * 512)(model)
model = Dropout(0.5)(model)
output_ = Dense(3, activation='softmax')(model)

model = Model(inputs = xception.input, outputs = output_)

model.compile(loss="categorical_crossentropy", optimizer = 'adam', metrics=["accuracy"])


from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1./255)
batch_size = 20
from keras.applications import VGG16, ResNet50, InceptionV3, Xception, DenseNet121
from keras.utils import plot_model

# https://github.com/keras-team/keras-applications/tree/master/keras_applications

vgg16 = VGG16(weights=None)
vgg16.summary()
plot_model(vgg16, "vgg16.png", show_layer_names=True, show_shapes=True)

resnet50 = ResNet50(weights=None)
resnet50.summary()
plot_model(resnet50, "resnet50.png", show_layer_names=True, show_shapes=True)

inception = InceptionV3(weights=None)
inception.summary()
plot_model(inception, "inception.png", show_layer_names=True, show_shapes=True)

xception = Xception(weights=None)
xception.summary()
plot_model(xception, "xception.png", show_layer_names=True, show_shapes=True)

densenet = DenseNet121(weights=None)
densenet.summary()
plot_model(densenet, "densenet.png", show_layer_names=True, show_shapes=True)
Ejemplo n.º 14
0
def inception_get_features(run_evaluation_model=False):
    def run_model():
        print("here")
        with tf.device('/gpu:0'):
            model = Sequential()
            model.add(Dense(1024, input_shape=(2048, ), activation='relu'))
            model.add(Dense(512, input_shape=(1024, ), activation='relu'))
            model.add(Dropout(0.5))
            model.add(Dense(128, input_shape=(64, )))
            model.add(Dense(num_classes, activation='softmax'))
            adamax = Adamax()
            model.compile(loss='categorical_crossentropy',
                          optimizer=adamax,
                          metrics=['accuracy'])
            model.fit(x_train_feature_map,
                      y_train,
                      validation_data=(x_train_feature_map, y_train),
                      nb_epoch=70,
                      batch_size=64)

        score = model.evaluate(x_test_feature_map,
                               y_public_test,
                               batch_size=64)
        print("Result")
        print(score)

    raw_data = pd.read_csv(raw_data_csv_file_name)
    emotion = raw_data[['emotion']]
    pixels = raw_data[['pixels']]
    # Get data in matrix form
    (x_train_matrix, x_public_test_matrix, x_private_test_matrix, y_train,
     y_public_test,
     y_private_test) = get_data_in_matrix_format(emotion, pixels)
    # Only used train and public test for now
    x_train_matrix = x_train_matrix.astype('float32')
    x_public_test_matrix = x_public_test_matrix.astype('float32')
    # Put values between 1 and 0
    x_train_matrix = x_train_matrix / 255.0
    x_public_test_matrix = x_public_test_matrix / 255.0
    # 7 Classes
    num_classes = y_train.shape[1]

    if os.path.exists("./pre_saved_features/inceptiontrainfeatures.npy"):
        x_train_feature_map = np.load(
            "./pre_saved_features/inceptiontrainfeatures.npy")
        x_test_feature_map = np.load(
            "./pre_saved_features/inceptiontestfeatures.npy")
    else:

        xception_pre_trained = Xception(include_top=False,
                                        input_shape=(96, 96, 3),
                                        pooling='avg',
                                        weights='imagenet')

        x_train_feature_map = np.empty([N_TEST, 2048])

        f_l = np.empty([int(N_TEST), 48 * 2, 48 * 2, 3])
        for index, item in enumerate(f_l):  # Refill the list
            im = Image.fromarray(x_train_matrix[index])
            resized_image = im.resize((48 * 2, 48 * 2), Image.NEAREST)
            arr = np.reshape(
                np.fromiter(iter(resized_image.getdata()), np.uint8), (96, 96))
            for d in range(3):
                item[:, :, d] = arr

        picture_train_features = xception_pre_trained.predict(f_l)
        del f_l

        # BUILD NEW TRAIN FEATURE INPUT
        for idx_pic, picture in enumerate(picture_train_features):
            x_train_feature_map[idx_pic] = picture

        print("converting test features")
        f_t = np.empty([3588, 96, 96, 3])
        for index, item in enumerate(f_t):  # Refill the list
            im = Image.fromarray(x_public_test_matrix[index])
            resized_image = im.resize((48 * 2, 48 * 2), Image.NEAREST)
            arr = np.reshape(
                np.fromiter(iter(resized_image.getdata()), np.uint8), (96, 96))
            for d in range(3):
                item[:, :, d] = arr

        picture_test_features = xception_pre_trained.predict(f_t)
        del f_t

        # BUILD NEW TEST
        x_test_feature_map = np.empty([3588, 2048])
        for idx_pic, picture in enumerate(picture_test_features):
            x_test_feature_map[idx_pic] = picture

        np.save("./pre_saved_features/inceptiontestfeatures",
                x_test_feature_map)
        np.save("./pre_saved_features/inceptiontrainfeatures",
                x_train_feature_map)

    if run_evaluation_model:
        run_model()

    return x_train_feature_map, x_test_feature_map
Ejemplo n.º 15
0
from keras.layers import Input

sys.path.append(os.path.abspath(os.path.join('.')))
from data import process

TOTAL_EPOCH = 300

# from data import data_input
input_tensor = Input(shape=(75, 75, 3))
factory = {
    # 'vgg16': lambda: VGG16(include_top=False, input_tensor=input_tensor, weights='imagenet'),
    'vgg19':
    lambda: VGG19(
        include_top=False, input_tensor=input_tensor, weights='imagenet'),
    'xception':
    lambda: Xception(
        include_top=False, input_tensor=input_tensor, weights='imagenet'),
    'InceptionV3':
    lambda: InceptionV3(
        include_top=False, input_tensor=input_tensor, weights='imagenet'),
    'InceptionResNetV2':
    lambda: InceptionResNetV2(
        include_top=False, input_tensor=input_tensor, weights='imagenet')
}


def get_model(name='simple',
              train_base=True,
              use_angle=False,
              dropout=0.8,
              layers=(512, 256)):
Ejemplo n.º 16
0
X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                  y_train,
                                                  test_size=0.25,
                                                  random_state=42,
                                                  stratify=y_train)
# print(X_train)
# print(y_train)
# print(X_test)
# print(y_test)

## define the model (preset weights)
print('[INFO] defining model...')

if worker == 'single':
    model = Xception(include_top=True,
                     weights=None,
                     classes=len(labeltonumber))
elif worker == 'parallel':
    with tf.device('/cpu:0'):
        model = Xception(include_top=True,
                         weights=None,
                         classes=len(labeltonumber))

## print a summary of the model
print(model.summary())

## values from Olafenwa and Olafenva - 2018 and
## https://machinelearningmastery.com/evaluate-performance-deep-learning-models-keras/
EPOCHS = 200
print('epochs: {}'.format(EPOCHS))
## batch normalization batch sizes: 64, 128, 256, 512
Ejemplo n.º 17
0
from keras.applications import Xception 
import argparse

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--include-top", type=int, default=1,
	help="whether or not to include top of CNN")
args = vars(ap.parse_args())

# load the VGG16 network
print("[INFO] loading network...")
model = Xception(weights="imagenet",
	include_top=args["include_top"] > 0)
print("[INFO] showing layers...")

# loop over the layers in the network and display them to the
# console
for (i, layer) in enumerate(model.layers):
	print("[INFO] {}\t{}".format(i, layer))
Ejemplo n.º 18
0
])
x_test = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((71, 71)))
    for im in x_test
])

print(x_train.shape)

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
print(y_train)
model = Sequential()
# conv_base = VGG16(weights="imagenet", include_top=False, input_shape=(48,48,3))
# conv_base = VGG19(weights="imagenet", include_top=False, input_shape=(48,48,3))
conv_base = Xception(weights="imagenet",
                     include_top=False,
                     input_shape=(71, 71, 3))

# conv_base = VGG16()
# conv_base2 = VGG19()

model.add(conv_base)

model.add(Flatten())
# model.add(Dense(50))

model.add(Dense(10, activation="softmax"))
model.summary()

model.compile(loss="categorical_crossentropy",
              optimizer="adadelta",
Ejemplo n.º 19
0
 models = {
     "vgg16":
     VGG16(weights="imagenet",
           include_top=False,
           input_shape=(target_size, target_size, 3)),
     "vgg19":
     VGG19(weights="imagenet",
           include_top=False,
           input_shape=(target_size, target_size, 3)),
     "inceptionv3":
     InceptionV3(weights="imagenet",
                 include_top=False,
                 input_shape=(target_size, target_size, 3)),
     "xception":
     Xception(weights="imagenet",
              include_top=False,
              input_shape=(target_size, target_size, 3)),
     "resnet50":
     ResNet50(weights="imagenet",
              include_top=False,
              input_shape=(target_size, target_size, 3)),
 }
 freezing_layers = {
     "vgg16": ("block5_conv1", 15, 16),
     "vgg19": ("block5_conv1", 17, 16),
     "inceptionv3": ("conv2d_65", 197, 32),
     "xception": ("add_10", 105, 32),
     "resnet50": ("add_8", 89, 32),
 }
 for i in range(3):
     for n, m in models.items():
Ejemplo n.º 20
0
from keras.applications import VGG16, VGG19, InceptionV3, ResNet50, Xception

__network_model_dictionary = {
    'Xception':
    Xception(weights='imagenet',
             include_top=False,
             input_shape=input_layer_shape),
    'VGG16':
    VGG16(weights='imagenet', include_top=False,
          input_shape=input_layer_shape),
    'VGG19':
    VGG19(weights='imagenet', include_top=False,
          input_shape=input_layer_shape),
    'ResNet50':
    ResNet50(weights='imagenet',
             include_top=False,
             input_shape=input_layer_shape),
    'InceptionV3':
    InceptionV3(weights='imagenet',
                include_top=False,
                input_shape=input_layer_shape)
}


def load_base_network(network_model='VGG16', input_layer_shape=(197, 197, 3)):
    """Load a pretrained base network model.

    Args:
        network_model (string): A string which represents the base network model.
        input_layer_shape (3d tensor): A 3d tensor (height x width x channels) which represents the input layer's shape.
    
Ejemplo n.º 21
0
def extract_features(input_dir,
                     output_dir,
                     model_type='inceptionv3',
                     batch_size=32):
    """
    Extracts features from a CNN trained on ImageNet classification from all
    videos in a directory.

    Args:
        input_dir (str): Input directory of videos to extract from.
        output_dir (str): Directory where features should be stored.
        model_type (str): Model type to use.
        batch_size (int): Batch size to use when processing.
    """

    input_dir = os.path.expanduser(input_dir)
    output_dir = os.path.expanduser(output_dir)

    if not os.path.isdir(input_dir):
        sys.stderr.write("Input directory '%s' does not exist!\n" % input_dir)
        sys.exit(1)

    # Load desired ImageNet model

    # Note: import Keras only when needed so we don't waste time revving up
    #       Theano/TensorFlow needlessly in case of an error

    model = None
    input_shape = (224, 224)

    if model_type.lower() == 'inceptionv3':
        from keras.applications import InceptionV3
        model = InceptionV3(include_top=True, weights='imagenet')
    elif model_type.lower() == 'xception':
        from keras.applications import Xception
        model = Xception(include_top=True, weights='imagenet')
    elif model_type.lower() == 'resnet50':
        from keras.applications import ResNet50
        model = ResNet50(include_top=True, weights='imagenet')
    elif model_type.lower() == 'vgg16':
        from keras.applications import VGG16
        model = VGG16(include_top=True, weights='imagenet')
    elif model_type.lower() == 'vgg19':
        from keras.applications import VGG19
        model = VGG19(include_top=True, weights='imagenet')
    else:
        sys.stderr.write("'%s' is not a valid ImageNet model.\n" % model_type)
        sys.exit(1)

    if model_type.lower() == 'inceptionv3' or model_type.lower() == 'xception':
        shape = (299, 299)

    # Get outputs of model from layer just before softmax predictions

    from keras.models import Model
    model = Model(model.inputs, output=model.layers[-2].output)

    # Create output directories

    visual_dir = os.path.join(output_dir, 'visual')  # RGB features
    #motion_dir = os.path.join(output_dir, 'motion') # Spatiotemporal features
    #opflow_dir = os.path.join(output_dir, 'opflow') # Optical flow features

    for directory in [visual_dir]:  #, motion_dir, opflow_dir]:
        if not os.path.exists(directory):
            os.makedirs(directory)

    # Find all videos that need to have features extracted

    def is_video(x):
        return x.endswith('.mp4') or x.endswith('.avi') or x.endswith('.mov')

    vis_existing = [x.split('.')[0] for x in os.listdir(visual_dir)]
    #mot_existing = [os.path.splitext(x)[0] for x in os.listdir(motion_dir)]
    #flo_existing = [os.path.splitext(x)[0] for x in os.listdir(opflow_dir)]

    video_filenames = [
        x for x in sorted(os.listdir(input_dir))
        if is_video(x) and os.path.splitext(x)[0] not in vis_existing
    ]

    # Go through each video and extract features

    from keras.applications.imagenet_utils import preprocess_input

    for video_filename in tqdm(video_filenames):

        # Open video clip for reading
        try:
            clip = VideoFileClip(os.path.join(input_dir, video_filename))
        except Exception as e:
            sys.stderr.write("Unable to read '%s'. Skipping...\n" %
                             video_filename)
            sys.stderr.write("Exception: {}\n".format(e))
            continue

        # Sample frames at 1fps
        fps = int(np.round(clip.fps))
        frames = [
            scipy.misc.imresize(crop_center(x.astype(np.float32)), shape)
            for idx, x in enumerate(clip.iter_frames())
            if idx % fps == fps // 2
        ]

        n_frames = len(frames)

        frames_arr = np.empty((n_frames, ) + shape + (3, ), dtype=np.float32)
        for idx, frame in enumerate(frames):
            frames_arr[idx, :, :, :] = frame

        frames_arr = preprocess_input(frames_arr)

        features = model.predict(frames_arr, batch_size=batch_size)

        name, _ = os.path.splitext(video_filename)
        feat_filepath = os.path.join(visual_dir, name + '.npy')

        with open(feat_filepath, 'wb') as f:
            np.save(f, features)
Ejemplo n.º 22
0
from keras.utils import multi_gpu_model
import numpy as np

num_samples = 1000
height = 224
width = 224
num_classes = 1000

# Instantiate the base model (or "template" model).
# We recommend doing this with under a CPU device scope,
# so that the model's weights are hosted on CPU memory.
# Otherwise they may end up hosted on a GPU, which would
# complicate weight sharing.
with tf.device('/cpu:0'):
    model = Xception(weights=None,
                     input_shape=(height, width, 3),
                     classes=num_classes)

# Replicates the model on 8 GPUs.
# This assumes that your machine has 8 available GPUs.
parallel_model = multi_gpu_model(model, gpus=8)
parallel_model.compile(loss='categorical_crossentropy',
                       optimizer='rmsprop')

# Generate dummy data.
x = np.random.random((num_samples, height, width, 3))
y = np.random.random((num_samples, num_classes))

# This `fit` call will be distributed on 8 GPUs.
# Since the batch size is 256, each GPU will process 32 samples.
parallel_model.fit(x, y, epochs=20, batch_size=256)
    f.write(mean)
    f.write('\n std is \n')
    f.write(std)
    X_test -= mean
    X_test /= (std + K.epsilon())


# X_test,Y_test = load_test()
# print ('X_test is',len(X_test[0]))
#print ('Y_test is',Y_test[0])

# Manual_Preporcessing(X_test);

model = Xception(include_top=False,
                 weights='imagenet',
                 classes=200,
                 pooling='avg',
                 input_shape=(img_size, img_size, 3))

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

output_model = model.output
predictions = Dense(200, activation='softmax',
                    name='predictions')(output_model)

new_model = Model(input=model.input, output=predictions)
new_model.summary()

# Loading best weights
new_model.load_weights("./weights/weights_3.hdf5")
Ejemplo n.º 24
0
def generate_model_base(preset, width, height, channel, weights_init):
    '''
    モデルを作成する

    # Arguments
        preset: プリセットモデルの名前
        width: 入力画像の幅
        height: 入力画像の高さ
        channel: 入力画像のチャンネル数
        class_num: 分類クラス数
        weights_init: 初期値(None, imagenet)

    # Returns
        keras.models.Model オブジェクト
    '''
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    # os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

    from keras.layers import Dense, BatchNormalization, Dropout, Input, Conv2D
    # from keras.layers import GlobalAveragePooling2D
    from keras.models import Model

    input_tensor = Input(shape=(width, height, channel))
    conv_base = None
    # output_layer = None
    prediction_layer = None

    if preset.upper() == "bench".upper():
        conv_base = create_bench_model(input_tensor)
        prediction_layer = conv_base
    elif preset.upper() == "VGG16".upper():
        from keras.applications import VGG16
        conv_base = None
        if channel == 3:
            conv_base = VGG16(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, channel)
                              )
        else:
            conv_base = VGG16(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, 3)
                              )

            conv_base.layers.pop(0)
            conv_base.layers.pop(0)
            input_layer = Input(shape=(width, height, channel), name='multi_input')
            block1_conv1_new = Conv2D(64, (3, 3),
                                      activation='relu',
                                      padding='same',
                                      kernel_initializer='glorot_uniform',
                                      name='block1_conv1_new')

            conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new])

        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.get_output_at(-1)  # x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.2, name='fc_dropout')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "VGG19".upper():
        from keras.applications import VGG19
        conv_base = None
        if channel == 3:
            conv_base = VGG19(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, channel)
                              )
        else:
            conv_base = VGG19(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, 3)
                              )

            conv_base.layers.pop(0)
            conv_base.layers.pop(0)
            input_layer = Input(shape=(width, height, channel), name='multi_input')
            block1_conv1_new = Conv2D(64, (3, 3),
                                      activation='relu',
                                      padding='same',
                                      kernel_initializer='glorot_uniform',
                                      name='block1_conv1_new')

            conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new])

        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.get_output_at(-1)  # x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.2, name='fc_dropout')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "VGG16BN".upper():
        from model import VGG16BN
        conv_base = None
        if channel == 3:
            conv_base = VGG16BN(weights=weights_init,
                                include_top=False,
                                pooling='avg',
                                kernel_initializer='glorot_uniform',
                                input_shape=(width, height, channel)
                                )
        else:
            conv_base = VGG16BN(weights=weights_init,
                                include_top=False,
                                pooling='avg',
                                kernel_initializer='glorot_uniform',
                                input_shape=(width, height, 3)
                                )

            conv_base.layers.pop(0)
            conv_base.layers.pop(0)
            input_layer = Input(shape=(width, height, channel), name='multi_input')
            block1_conv1_new = Conv2D(64, (3, 3),
                                      activation='relu',
                                      padding='same',
                                      kernel_initializer='glorot_uniform',
                                      name='block1_conv1_new')

            conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new])

        # conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.get_output_at(-1)  # x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.2, name='fc_dropout')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "VGG19BN".upper():
        from model import VGG19BN
        conv_base = None
        if channel == 3:
            conv_base = VGG19BN(weights=weights_init,
                                include_top=False,
                                pooling='avg',
                                kernel_initializer='glorot_uniform',
                                input_shape=(width, height, channel)
                                )
        else:
            conv_base = VGG19BN(weights=weights_init,
                                include_top=False,
                                pooling='avg',
                                kernel_initializer='glorot_uniform',
                                input_shape=(width, height, 3)
                                )

            conv_base.layers.pop(0)
            conv_base.layers.pop(0)
            input_layer = Input(shape=(width, height, channel), name='multi_input')
            block1_conv1_new = Conv2D(64, (3, 3),
                                      activation='relu',
                                      padding='same',
                                      kernel_initializer='glorot_uniform',
                                      name='block1_conv1_new')

            conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new])

        # conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.get_output_at(-1)  # x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.2, name='fc_dropout')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet20".upper():
        # from keras.applications import ResNet50
        from model.resnet import ResNet20
        conv_base = ResNet20(weights=weights_init,
                             include_top=True,
                             input_shape=(width, height, channel),
                             input_tensor=input_tensor
                             )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet50".upper():
        # from keras.applications import ResNet50
        from model.resnet import ResNet50
        conv_base = ResNet50(weights=weights_init,
                             include_top=True,
                             input_shape=(width, height, channel),
                             input_tensor=input_tensor
                             )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet101".upper():
        from model.resnet import ResNet101
        conv_base = ResNet101(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, channel),
                              input_tensor=input_tensor
                              )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet152".upper():
        from model.resnet import ResNet152
        conv_base = ResNet152(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, channel),
                              input_tensor=input_tensor
                              )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet50V2".upper():
        from model.resnet_v2 import ResNet50V2
        conv_base = ResNet50V2(weights=weights_init,
                               include_top=True,
                               input_shape=(width, height, channel),
                               input_tensor=input_tensor
                               )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet101V2".upper():
        from model.resnet_v2 import ResNet101V2
        conv_base = ResNet101V2(weights=weights_init,
                                include_top=True,
                                input_shape=(width, height, channel),
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNet152V2".upper():
        from model.resnet_v2 import ResNet152V2
        conv_base = ResNet152V2(weights=weights_init,
                                include_top=True,
                                input_shape=(width, height, channel),
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNeXt50".upper():
        from model.resnext import ResNeXt50
        conv_base = ResNeXt50(weights=weights_init,
                              include_top=True,
                              input_shape=(width, height, channel),
                              input_tensor=input_tensor
                              )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "ResNeXt101".upper():
        from model.resnext import ResNeXt101
        conv_base = ResNeXt101(weights=weights_init,
                               include_top=True,
                               input_shape=(width, height, channel),
                               input_tensor=input_tensor
                               )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        x = Dropout(0.5, name='fc_dropout')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "InceptionV3".upper():
        from keras.applications import InceptionV3
        conv_base = InceptionV3(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "InceptionResNetV2".upper():
        from keras.applications import InceptionResNetV2
        conv_base = InceptionResNetV2(weights=weights_init,
                                      include_top=True,
                                      input_tensor=input_tensor
                                      )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "DenseNet121".upper():
        from keras.applications import DenseNet121
        conv_base = DenseNet121(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "DenseNet169".upper():
        from keras.applications import DenseNet169
        conv_base = DenseNet169(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "DenseNet201".upper():
        from keras.applications import DenseNet201
        conv_base = DenseNet201(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "Xception".upper():
        from keras.applications import Xception
        conv_base = Xception(weights=weights_init,
                             include_top=True,
                             input_tensor=input_tensor
                             )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEDenseNetImageNet121".upper():
        from model import SEDenseNetImageNet121
        conv_base = SEDenseNetImageNet121(weights=weights_init,
                                          include_top=True,
                                          input_tensor=input_tensor
                                          )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEDenseNetImageNet169".upper():
        from model import SEDenseNetImageNet169
        conv_base = SEDenseNetImageNet169(weights=weights_init,
                                          include_top=True,
                                          input_tensor=input_tensor
                                          )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEDenseNetImageNet201".upper():
        from model import SEDenseNetImageNet201
        conv_base = SEDenseNetImageNet201(weights=weights_init,
                                          include_top=True,
                                          input_tensor=input_tensor
                                          )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEDenseNetImageNet264".upper():
        from model import SEDenseNetImageNet264
        conv_base = SEDenseNetImageNet264(weights=weights_init,
                                          include_top=True,
                                          input_tensor=input_tensor
                                          )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEDenseNetImageNet161".upper():
        from model import SEDenseNetImageNet161
        conv_base = SEDenseNetImageNet161(weights=weights_init,
                                          include_top=True,
                                          input_tensor=input_tensor
                                          )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEInceptionResNetV2".upper():
        from model import SEInceptionResNetV2
        conv_base = SEInceptionResNetV2(weights=weights_init,
                                        include_top=True,
                                        input_tensor=input_tensor
                                        )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEInceptionV3".upper():
        from model import SEInceptionV3
        conv_base = SEInceptionV3(weights=weights_init,
                                  include_top=True,
                                  input_tensor=input_tensor
                                  )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEMobileNet".upper():
        from model import SEMobileNet
        conv_base = SEMobileNet(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet6".upper():
        from model import SEResNet6
        conv_base = SEResNet6(weights=weights_init,
                              include_top=True,
                              input_tensor=input_tensor
                              )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet8".upper():
        from model import SEResNet8
        conv_base = SEResNet8(weights=weights_init,
                              include_top=True,
                              input_tensor=input_tensor
                              )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet10".upper():
        from model import SEResNet10
        conv_base = SEResNet10(weights=weights_init,
                               include_top=True,
                               input_tensor=input_tensor
                               )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet18".upper():
        from model import SEResNet18
        conv_base = SEResNet18(weights=weights_init,
                               include_top=True,
                               input_tensor=input_tensor
                               )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet34".upper():
        from model import SEResNet34
        conv_base = SEResNet34(weights=weights_init,
                               include_top=True,
                               input_tensor=input_tensor
                               )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet50".upper():
        from model import SEResNet50
        conv_base = SEResNet50(weights=weights_init,
                               include_top=True,
                               input_tensor=input_tensor
                               )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet101".upper():
        from model import SEResNet101
        conv_base = SEResNet101(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNet154".upper():
        from model import SEResNet154
        conv_base = SEResNet154(weights=weights_init,
                                include_top=True,
                                input_tensor=input_tensor
                                )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    elif preset.upper() == "SEResNext".upper():
        from model import SEResNext
        conv_base = SEResNext(weights=weights_init,
                              include_top=True,
                              input_tensor=input_tensor
                              )
        conv_base.layers.pop()
        output_layer = conv_base.layers[-1]
        x = output_layer.output
        x = BatchNormalization(name='fc_bachnorm')(x)
        # x = GlobalAveragePooling2D(name='avg_pool')(x)
        prediction_layer = Dense(class_num, activation='softmax',
                                 kernel_initializer='glorot_uniform', name='prediction')(x)

    else:
        raise ValueError('unknown model name : {}'.format(preset))

    # x = output_layer.output
    # # x = Flatten()(x)
    # # x = Dense(512, activation='relu', kernel_initializer='glorot_uniform')(x)
    # # # x = Dropout(0.7)(x)
    # # x = BatchNormalization(name='fc_bachnorm')(x)
    # prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x)

    model = Model(inputs=conv_base.input,
                  outputs=prediction_layer, name='classification_model')
    # #weights_filepath = 'work/test/vgg19_weights_tf_dim_ordering_tf_kernels.h5'
    # weights_filepath = 'work/test/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5'
    # model.load_weights(weights_filepath, by_name=True, skip_mismatch=True)
    return model
Ejemplo n.º 25
0
NB_EPOCH = 20

# 1 - CNN construction
from keras.models import Sequential

# Loading model if you want to
# from keras.models import load_model
# classifier = tensorflow.keras.models.load_model(<PATH_TO_THE_MODEL>)
# classifier.summary()

# CNN initialization
classifier = Sequential()

from keras.applications import Xception
xception = Xception(weights='imagenet',
                    include_top=False,
                    input_shape=(INPUT_SIZE, INPUT_SIZE, 3),
                    pooling='max')
classifier.add(xception)
# Freezing the XCeption layers
for layer in classifier.layers:
    layer.trainable = False

from keras.layers import Dense, Dropout, BatchNormalization
classifier.add(BatchNormalization())

# Fully connected
classifier.add(Dropout(0.1))
classifier.add(Dense(units=1024, activation="relu"))
classifier.add(Dropout(0.1))
classifier.add(Dense(units=1024, activation="relu"))
classifier.add(Dropout(0.1))
Ejemplo n.º 26
0
from keras.utils import to_categorical
from keras import losses,metrics
from keras.callbacks import ModelCheckpoint
import Dataset_mobile
import os

if __name__ == '__main__':

    save_dir = os.path.join(os.getcwd(), 'saved_models')
    model_name = 'xceptionMobile_wood_model.h5'
    model_path = os.path.join(save_dir, model_name)

    x_train, y_train, x_test, y_test, dictionary = Dataset_mobile.read_data()

    x_train_resized = Dataset_mobile.resize_imgs(x_train)
    x_test_resized = Dataset_mobile.resize_imgs(x_test)

    y_train = to_categorical(y_train, num_classes=2)
    y_test = to_categorical(y_test, num_classes=2)

    model = Xception(include_top=True, weights=None, classes=2)

    opt = Adam(lr=5e-6)
    model.compile(optimizer=opt, loss=losses.categorical_crossentropy, metrics=[metrics.categorical_accuracy])
    model.fit(x_train_resized,y_train,epochs=20,batch_size=6)
    model.save(model_path)

    score1 = model.evaluate(x_train_resized, y_train, batch_size=6)
    score2 = model.evaluate(x_test_resized, y_test, batch_size=6)
    print(score1)
    print(score2)
if CNN == "IV3":
    # Inception V3 layer with pre-trained weights from ImageNet
    # base_iv3_model = InceptionV3(include_top=False, weights="imagenet")
    base_iv3_model = InceptionV3(weights="imagenet")
    # Inception V3 output from input layer
    output_vgg16 = base_iv3_model(image_input)
    # flattening it #why?
    # flat_iv3 = Flatten()(output_vgg16)
elif CNN == "RN50":
    # ResNet50 layer with pre-trained weights from ImageNet
    base_rn50_model = ResNet50(weights="imagenet")
    # ResNet50 output from input layer
    output_rn50 = base_rn50_model(image_input)
elif CNN == "Xception":
    # Xception layer with pre-trained weights from ImageNet
    base_xp_model = Xception(weights="imagenet")
    # Xception output from input layer
    output_xp = base_xp_model(image_input)

# Gender input layer
gdr_input = Input(shape=(1, ), name="gdr_input")
# Gender dense layer
gdr_dense = Dense(32, activation="relu")
# Gender dense output
output_gdr_dense = gdr_dense(gdr_input)

if CNN == "IV3":
    # Concatenating iv3 output with sex_dense output after going through shared layer
    x = keras.layers.concatenate([output_vgg16, output_gdr_dense])
elif CNN == "RN50":
    # Concatenating ResNet50 output with gender_dense output after going through shared layer
for layers in num_layers:
    for nodes in num_nodes:
        for p in dropout_prob:

            model_param = str(layers) + "-" + str(nodes) + "-" + str(p)
            print(
                "**********************************************************************************************"
            )
            print(model_param)
            print(
                "**********************************************************************************************"
            )

            xception_base = Xception(input_shape=(img_cols, img_rows, 3),
                                     weights='imagenet',
                                     include_top=False)
            for layer in xception_base.layers:
                layer.trainable = False

            dense_model = Sequential()
            dense_model.add(
                GlobalAveragePooling2D(
                    input_shape=xception_base.output_shape[1:]))

            for i in range(0, layers):
                dense_model.add(Dense(nodes, activation='relu'))
                dense_model.add(Dropout(p))
                dense_model.add(BatchNormalization())

            dense_model.add(Dense(1, activation='sigmoid'))
Ejemplo n.º 29
0
from keras.applications import VGG16
from keras.applications import VGG19
from keras.applications import Xception
from keras.applications import ResNet50
from keras.applications import MobileNet
from keras.applications import MobileNetV2
from keras.applications import InceptionV3
from keras.applications import InceptionResNetV2

# conv_base = VGG16(weights='imagenet', include_top=False,
#                   input_shape=(150,150,3))
# conv_base = VGG16()   # 224,224,3
conv_base = Xception()
conv_base.summary()

from keras import models, layers
model = models.Sequential()
model.add(conv_base)
# model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
# print(x_test.shape)

# print(y_train.shape)
# print(y_test.shape)

x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float') / 255

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# print(y_train.shape)
# print(y_test.shape)

conv_base = Xception(weights='imagenet',
                     include_top=False,
                     input_shape=(112, 112, 3))

# conv_base.summary()

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(10, activation='sigmoid'))

# model.summary()

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