Ejemplo n.º 1
0
def create_model_box(n_classes, train_batch_norm=False, num_frozen=250):
    # head_layers = [n_classes * 4, n_classes * 2]

    trained_model = applications.InceptionV3(weights="imagenet")

    head_layers = []
    layer_inner = trained_model.layers[-2].output

    for n_nodes in head_layers:
        layer_inner = Dense(n_nodes, activation="relu")(layer_inner)

    last_layer = Dense(n_classes, activation="softmax",
                       name="class_probs")(layer_inner)

    box_coords = Dense(N_COORDS,
                       activation=LeakyReLU(alpha=0.1),
                       name="box_coords")(layer_inner)

    seq2 = models.Model(inputs=trained_model.input,
                        outputs=[last_layer, box_coords])
    for i, layer in enumerate(seq2.layers):
        if layer.name.find("batch_normalization") != -1:
            layer.trainable = train_batch_norm
        if i < num_frozen:
            layer.trainable = False
    return seq2
    def build(self):
        # build the Inception V3 network, use pretrained weights from ImageNet
        # remove top fully connected layers by include_top=False
        base_model = applications.InceptionV3(weights='imagenet',
                                              include_top=False,
                                              input_shape=(self.img_width,
                                                           self.img_height, 3))

        # Add new layers on top of the model
        # build a classifier model to put on top of the convolutional model
        # This consists of a global average pooling layer and a fully connected layer with 256 nodes
        # Then apply dropout and sigmoid activation
        model_top = Sequential()
        model_top.add(
            GlobalAveragePooling2D(input_shape=base_model.output_shape[1:],
                                   data_format=None)),
        model_top.add(Dense(256, activation='relu'))
        model_top.add(Dropout(0.5))
        model_top.add(Dense(1, activation='sigmoid'))
        model = KerasModel(inputs=base_model.input,
                           outputs=model_top(base_model.output))

        # Compile model using Adam optimizer with common values and binary cross entropy loss
        # Use low learning rate (lr) for transfer learning
        model.compile(optimizer=Adam(lr=self.learning_rate,
                                     beta_1=self.beta_1,
                                     beta_2=self.beta_2,
                                     epsilon=self.epsilon,
                                     decay=self.decay),
                      loss='binary_crossentropy',
                      metrics=['accuracy'])

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

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

    return features.mean(axis=0), features.std(axis=0)
Ejemplo n.º 4
0
def inceptionv3_func():
    base_model = applications.InceptionV3(include_top=False, weights='imagenet', input_shape=(225, 225, 3))

    add_model = Sequential()
    add_model.add(BatchNormalization())
    add_model.add(MaxPool2D(pool_size=(2,2)))
    add_model.add(Dropout(0.5))
    add_model.add(Dense(3, activation='sigmoid'))


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

    return model
Ejemplo n.º 5
0
def create_model(n_classes, train_batch_norm=False, num_frozen=250):
    # head_layers = [n_classes * 4, n_classes * 2]

    trained_model = applications.InceptionV3(weights="imagenet")

    head_layers = []
    layer_inner = trained_model.layers[-2].output

    for n_nodes in head_layers:
        layer_inner = Dense(n_nodes, activation="relu")(layer_inner)

    last_layer = Dense(n_classes, activation="softmax")(layer_inner)

    seq2 = models.Model(inputs=trained_model.input, outputs=last_layer)
    for i, layer in enumerate(seq2.layers):
        if layer.name.find("batch_normalization") != -1:
            layer.trainable = train_batch_norm
        if i < num_frozen:
            layer.trainable = False
    return seq2
def create_model(nclass):
    base_model = applications.InceptionV3(weights='imagenet',
                                          include_top=False,
                                          input_shape=input_shape)
    base_model.trainable = False

    add_model = Sequential()
    add_model.add(base_model)
    add_model.add(BatchNormalization())
    add_model.add(GlobalAveragePooling2D())
    add_model.add(Dense(1024, activation='relu'))
    add_model.add(BatchNormalization())
    add_model.add(Dense(512, activation='relu'))
    add_model.add(BatchNormalization())
    add_model.add(Dense(256, activation='relu'))
    add_model.add(Dropout(0.5))
    add_model.add(BatchNormalization())
    add_model.add(Dense(nclass, activation="softmax"))

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

    n_features = base_model.output.shape[-1]

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

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

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

    return model
all_combinations = [(bs, it, op, lr) for bs in BATCH_SIZE
                               	  for it in INTERPOLATION
                                  for op in OPTIMIZER_TYPE
                                  for lr in LEARNING_RATE]

for bs, it, op, lr in all_combinations:
	train_data, valid_data = load_preprocess(DIRECTORY,
						 bs,
						 IMAGE_SIZE,
						 it)

	print("Parameter Tuning: Batch_size = {}, Interpolation = {}, Optimizer = {}, Learning_rate = {}".format(bs, it, op, lr))

	base_model = applications.InceptionV3(
				include_top=False,
				weights="imagenet",
				input_shape=(299,299,3)
			)

	base_model.summary()
	train_feat, train_labels = extract_features(base_model, train_data, 1442, bs, 2048, 8)
	valid_feat, valid_labels = extract_features(base_model, valid_data, 618, bs, 2048, 8)

	inputs = keras.Input(shape=(8,8,2048))
	x = layers.Flatten()(inputs)
	x = layers.Dense(2048, activation='relu')(x)
	#x = layers.Dropout(0.3)(x)
	x = layers.Dense(512, activation='relu')(x)
	#x = layers.Dropout(0.3)(x)
	x = layers.Dense(128, activation='relu')(x)
	#x = layers.Dropout(0.3)(x)
Ejemplo n.º 9
0
"""
if os.path.isfile(FLAGS.SAVE_CKPT) and FLAGS.RELOAD:
    # load the trained weight
    mdl = load_model(FLAGS.SAVE_CKPT,
                     custom_objects={
                         'softmax_cross_entropy':
                         tf.losses.softmax_cross_entropy
                     })
    print('Model loaded from {}'.format(FLAGS.SAVE_CKPT))
else:
    # load the pre-train model
    if FLAGS.MODEL == 'Inception-v3':
        base_model = applications.InceptionV3(weights=None,
                                              include_top=False,
                                              input_shape=(FLAGS.TARGET_SIZE,
                                                           FLAGS.TARGET_SIZE,
                                                           3))
        base_model.load_weights(FLAGS.INCEPTION_V3)
    elif FLAGS.MODEL == 'Inception-ResNet-v2':
        base_model = applications.inception_resnet_v2.InceptionResNetV2(
            weights=None,
            include_top=False,
            input_shape=(FLAGS.TARGET_SIZE, FLAGS.TARGET_SIZE, 3))
        base_model.load_weights(FLAGS.INCEPTION_RES_V2)
    else:
        print('weight loading error')
        sys.exit(1)

    # Add customer layers
    mdl = Sequential([
Ejemplo n.º 10
0
#     print(y.dtype)
"""
This cell train a pre-trained model with extra layers

"""
# keras pretrained inception v3 model
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Flatten, Activation, Dropout, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers, applications
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
from tensorflow.keras import backend as K

# load the model
base_model = applications.InceptionV3(weights='imagenet',
                                      include_top=False,
                                      input_shape=(target_size, target_size,
                                                   3))
# Freeze some layers
for layer in base_model.layers[:-20]:
    layer.trainable = False
# Adding custom layers
# x = model.output
# x = Flatten()(x)
# x = Dense(1024, activation='relu')(x)
# x = Dropout(0.5)(x)
# predictions = Dense(num_classes, activation='linear')(x)
# mdl = Model(input = model.input, output = predictions)
mdl = Sequential([
    base_model,
    Flatten(),
    Dense(1024, activation='relu'),
Ejemplo n.º 11
0
# load the model
if do_save_and_load and os.path.isfile(last_ckpt):
    mdl = load_model(last_ckpt,
                     custom_objects={
                         'softmax_cross_entropy':
                         tf.losses.softmax_cross_entropy
                     })
    mdl.compile(tf.keras.optimizers.RMSprop(lr=learning_rate),
                loss=tf.losses.softmax_cross_entropy,
                metrics=['accuracy'])
    print('Model loaded from {}'.format(last_ckpt))
else:
    print('Start training from scratch')
    if model_name == 'inceptionV3':
        base_model = applications.InceptionV3(weights=inception_v3_path,
                                              include_top=False,
                                              input_shape=(target_size,
                                                           target_size, 3))
    elif model_name == 'Xception':
        base_model = applications.xception.Xception(include_top=False,
                                                    weights=Xception_path,
                                                    input_tensor=None,
                                                    input_shape=(target_size,
                                                                 target_size,
                                                                 3),
                                                    pooling=None,
                                                    classes=1000)
    elif model_name == 'inception_resnet_v2':
        base_model = applications.inception_resnet_v2.InceptionResNetV2(
            include_top=False,
            weights=inception_resnet_v2_path,
            input_tensor=None,