Пример #1
0
def build_model(classes=2):
    inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
    x = preprocess_input(inputs)
    x = InceptionResNetV2(weights=None, classes=classes)(x)
    model = Model(inputs=inputs, outputs=x)
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
    return model
    def create_model(self):
        self.pretrain_model = InceptionResNetV2(include_top=False,
                                                weights='imagenet',
                                                input_shape=self.input_shape +
                                                [3],
                                                pooling='avg',
                                                classes='avg')

        input_tensor = Input(shape=self.input_shape + [4])
        out = Conv2D(3,
                     kernel_size=1,
                     strides=1,
                     padding='same',
                     activation='tanh',
                     kernel_regularizer=regularizers.l2(1e-4))(input_tensor)
        bn = BatchNormalization()(out)
        x = self.pretrain_model(bn)
        '''
        x = Conv2D(128, kernel_size=(1,1), activation='relu')(x)
        x = Flatten()(x)
        x = Dropout(0.5)(x)
        x = Dense(512, activation='relu')(x)
        x = Dropout(0.5)(x)
        output = Dense(n_out, activation='sigmoid')(x)
        '''
        output = Dense(self.n_out)(x)
        output = Activation('sigmoid')(output)
        self.model = Model(input_tensor, output)
def base_model(input_shape=(128, 128, 3)):
    googleNet_model = InceptionResNetV2(weights='imagenet',
                                        include_top=False,
                                        input_shape=input_shape)
    googleNet_model.trainable = True
    print('Model loaded...\n', googleNet_model.summary())
    return googleNet_model
Пример #4
0
 def __init__(self, model_name=None):
     if model_name == 'Xception':
         base_model = Xception(weights='imagenet')
         self.preprocess_input = xception.preprocess_input
     elif model_name == 'VGG19':
         base_model = VGG19(weights='imagenet')
         self.preprocess_input = vgg19.preprocess_input
     elif model_name == 'ResNet50':
         base_model = ResNet50(weights='imagenet')
         self.preprocess_input = resnet.preprocess_input
     elif model_name == 'ResNet101':
         base_model = ResNet101(weights='imagenet')
         self.preprocess_input = resnet.preprocess_input
     elif model_name == 'ResNet152':
         base_model = ResNet152(weights='imagenet')
         self.preprocess_input = resnet.preprocess_input
     elif model_name == 'ResNet50V2':
         base_model = ResNet50V2(weights='imagenet')
         self.preprocess_input = resnet_v2.preprocess_input
     elif model_name == 'ResNet101V2':
         base_model = ResNet101V2(weights='imagenet')
         self.preprocess_input = resnet_v2.preprocess_input
     elif model_name == 'ResNet152V2':
         base_model = ResNet152V2(weights='imagenet')
         self.preprocess_input = resnet_v2.preprocess_input
     elif model_name == 'InceptionV3':
         base_model = InceptionV3(weights='imagenet')
         self.preprocess_input = inception_v3.preprocess_input
     elif model_name == 'InceptionResNetV2':
         base_model = InceptionResNetV2(weights='imagenet')
         self.preprocess_input = inception_resnet_v2.preprocess_input
     elif model_name == 'DenseNet121':
         base_model = DenseNet121(weights='imagenet')
         self.preprocess_input = densenet.preprocess_input
     elif model_name == 'DenseNet169':
         base_model = DenseNet169(weights='imagenet')
         self.preprocess_input = densenet.preprocess_input
     elif model_name == 'DenseNet201':
         base_model = DenseNet201(weights='imagenet')
         self.preprocess_input = densenet.preprocess_input
     elif model_name == 'NASNetLarge':
         base_model = NASNetLarge(weights='imagenet')
         self.preprocess_input = nasnet.preprocess_input
     elif model_name == 'NASNetMobile':
         base_model = NASNetMobile(weights='imagenet')
         self.preprocess_input = nasnet.preprocess_input
     elif model_name == 'MobileNet':
         base_model = MobileNet(weights='imagenet')
         self.preprocess_input = mobilenet.preprocess_input
     elif model_name == 'MobileNetV2':
         base_model = MobileNetV2(weights='imagenet')
         self.preprocess_input = mobilenet_v2.preprocess_input
     else:
         base_model = VGG16(weights='imagenet')
         self.preprocess_input = vgg16.preprocess_input
     self.model = Model(inputs=base_model.input,
                        outputs=base_model.layers[-2].output)
Пример #5
0
    def myLSTM(self, input_shape=(224, 224, 3)):
        googleNet_model = InceptionResNetV2(weights='imagenet',
                                            include_top=False,
                                            input_shape=input_shape)
        googleNet_model.trainable = True

        model = Sequential()
        model.add(TimeDistributed(googleNet_model))
        model.add(Flatten())
        model.add(LSTM(units=10))
        return model
Пример #6
0
def create_model(model_name, input_shape=(IMG_SIZE, IMG_SIZE, 3)):
    if model_name == 'efn_b4':
        model = efn.EfficientNetB4(weights=None, classes=4)
    elif model_name == 'efn_b4_p':
        model = tf.keras.models.Sequential()
        model.add(
            efn.EfficientNetB4(input_shape=input_shape,
                               weights='imagenet',
                               include_top=False))
    elif model_name == 'efn_b5_p':
        model = tf.keras.models.Sequential()
        model.add(
            efn.EfficientNetB5(input_shape=input_shape,
                               weights='imagenet',
                               include_top=False))
    elif model_name == 'efn_b6_p':
        model = tf.keras.models.Sequential()
        model.add(
            efn.EfficientNetB6(input_shape=input_shape,
                               weights='imagenet',
                               include_top=False))
    elif model_name == 'efn_b7_p':
        model = tf.keras.models.Sequential()
        model.add(
            efn.EfficientNetB7(input_shape=input_shape,
                               weights='imagenet',
                               include_top=False))
    elif model_name == 'densenet121_p':
        model = tf.keras.models.Sequential()
        model.add(
            DenseNet121(input_shape=input_shape,
                        weights='imagenet',
                        include_top=False))
    elif model_name == 'densenet201_p':
        model = tf.keras.models.Sequential()
        model.add(
            DenseNet201(input_shape=input_shape,
                        weights='imagenet',
                        include_top=False))
    elif model_name == 'inceptionResV2_p':
        model = tf.keras.models.Sequential()
        model.add(
            InceptionResNetV2(input_shape=input_shape,
                              weights='imagenet',
                              include_top=False))
    if model_name.split('_')[-1] == 'p':
        model.add(GlobalAveragePooling2D())
        #model.add(Dense(128, activation='relu'))
        #model.add(Dense(64, activation='relu'))
        model.add(Dense(4, activation='softmax'))
    model.summary()
    return model
Пример #7
0
def make_face_recognition(shape):
    """Builds the Face Recognition Network.
    """
    model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=shape, pooling='avg')
    image = model.input
    x = model.layers[-1].output
    outputs = Dense(128)(x)
    embedding_model = Model(inputs=[image], outputs=[outputs])

    input_layer = Input(shape=shape)
    x = embedding_model(input_layer)
    outputs = Lambda(lambda x: tf.keras.backend.l2_normalize(x, -1))(x)
    return Model(inputs=[input_layer], outputs=[outputs])
Пример #8
0
def get_model():
    # base_model = VGG16(weights='imagenet', include_top=True)
    # model = Model(inputs=base_model.input,
    #             outputs=base_model.get_layer(layer).output)
    model = InceptionResNetV2(weights='imagenet', include_top=True)

    return model


# from torchvision import models
# import torch
# def get_model(weight):
#     model = models.resnet50()
#     # model.eval()
Пример #9
0
def download_pre_trained_model():
    '''
    Downloads the pretrained model and saves it to disk
    '''

    # downloads the model, without the top layers
    pre_trained_model = InceptionResNetV2(include_top=False,
                                          weights='imagenet',
                                          input_shape=img_shape)

    # saves the model to disk
    pre_trained_model.save('Inception_ResNet_V2.h5', save_format='h5')

    return None
Пример #10
0
def kin_net(input_shape, weights=None):
    """Kin net model"""
    img_input1 = Input(shape=input_shape)
    img_input2 = Input(shape=input_shape)
    encoder = InceptionResNetV2(include_top=False,
                                input_shape=input_shape,
                                pooling="max")
    encoder.set_weights(weights["kinnet_inception_weights"])
    x = Concatenate(axis=1,
                    name='concat')([encoder(img_input1),
                                    encoder(img_input2)])
    outputs = Dense(4, weights=weights["kinnet_logits_weights"],
                    name="logits")(x)
    model = Model(inputs=[img_input1, img_input2], outputs=outputs)
    return model
Пример #11
0
    def buildTunerModel(self, img_size):
        """
        This function builds a base Inception-ResNet-V2 model for keras tuner
        
        Parameters:
            img_size (int): the size of input images (img_size, img_size).

        Returns:
            model (class): the Inception-ResNet-V2 model used for keras tuner.

        """
        base_model = InceptionResNetV2(weights='imagenet', include_top=False, 
                                 input_shape = (img_size,img_size,3))
        base_model.load_weights(self.weights, by_name = True)
        return base_model
Пример #12
0
def get_pre_trained_model(code, h, w, d):
    if code == "inception_v3":
        return InceptionV3(input_shape=(h, w, d),
                           weights="imagenet",
                           include_top=False), [12, 17]
    elif code == "inception_resnet_v2":
        return InceptionResNetV2(input_shape=(h, w, d),
                                 weights="imagenet",
                                 include_top=False), [2, 4]
    elif code == "resnet_50":
        return ResNet50(input_shape=(h, w, d),
                        weights="imagenet",
                        include_top=False), [4, 7]
    elif code == "xception":
        return Xception(input_shape=(h, w, d),
                        weights="imagenet",
                        include_top=False), [3, 6]
def get_encoder_model(name, in_shape, pooling):
    if name == "InceptionV3":
        model = InceptionV3(include_top=False,
                            input_shape=in_shape,
                            weights=None,
                            pooling=pooling)
    elif name == "ResNet50":
        model = ResNet50(include_top=False,
                         input_shape=in_shape,
                         weights=None,
                         pooling=pooling)
    elif name == "ResNet50V2":
        model = ResNet50V2(include_top=False,
                           input_shape=in_shape,
                           weights=None,
                           pooling=pooling)
    elif name == "ResNet101":
        model = ResNet101(include_top=False,
                          input_shape=in_shape,
                          weights=None,
                          pooling=pooling)
    elif name == "ResNet101V2":
        model = ResNet101V2(include_top=False,
                            input_shape=in_shape,
                            weights=None,
                            pooling=pooling)
    elif name == "ResNet152":
        model = ResNet152(include_top=False,
                          input_shape=in_shape,
                          weights=None,
                          pooling=pooling)
    elif name == "InceptionResNetV2":
        model = InceptionResNetV2(include_top=False,
                                  input_shape=in_shape,
                                  weights=None,
                                  pooling=pooling)
    elif name == "DenseNet121":
        model = DenseNet121(include_top=False,
                            input_shape=in_shape,
                            weights=None,
                            pooling=pooling)
    else:
        raise ValueError("model " + name + " not found")

    return model
def main():
    optimizer = tf.keras.optimizers.Adam(lr=0.001, decay=0.99)
    base_model = InceptionResNetV2(
        include_top=False,
        weights='imagenet',
        classes=NUM_CLASSES)
    model = finetune_model(base_model, NUM_CLASSES)
    model.compile(optimizer=optimizer,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    validation_data = get_data(mode='validation')
    train_data = get_data(mode='train')
    history = model.fit(
        train_data,
        epochs=NUM_EPOCH,
        validation_data=validation_data,
        steps_per_epoch=int(np.ceil(NUM_TRAIN / BATCH_SIZE)),
        validation_steps=int(np.ceil(NUM_VALIDATION / BATCH_SIZE)))
    print((history.history['val_acc']))
Пример #15
0
    def buildBaseModel(self, img_size):
        """
        This function builds an Inception-ResNet-V2 model which includes a global
        average pooling layer and a dense layer with sigmoid activation function.
        
        Parameters:
            img_size (int): the size of input images (img_size, img_size).

        Returns:
            model (class): the base Inception-ResNet-V2 model that can be used later in training.

        """
        base_model = InceptionResNetV2(weights='imagenet', include_top=False, 
                                 input_shape = (img_size,img_size,3))
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        predictions = layers.Dense(1, activation='sigmoid', name='last')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
        model.load_weights(self.weights)
        return model
Пример #16
0
    def make_model(self):
        conv_base = InceptionResNetV2(weights='imagenet',
                                      include_top=False,
                                      input_shape=(*self.input_size, 3))
        for layer in conv_base.layers:
            layer.trainable = False
        x = layers.GlobalAveragePooling2D()(conv_base.output)
        x = layers.Dense(512)(x)
        x = layers.BatchNormalization()(x)
        x = layers.Activation('relu')(x)
        x = layers.Dropout(0.3)(x)
        x = layers.Dense(64, activation='relu')(x)
        x = layers.Dropout(0.1)(x)
        output = layers.Dense(4, activation='sigmoid')(x)

        model = models.Model(conv_base.input, output)
        model.compile(optimizer='adam',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])
        self.model = model
def ggnModel(input_shape=(128, 128, 3)):
    googleNet_model = InceptionResNetV2(weights='imagenet',
                                        include_top=False,
                                        input_shape=input_shape)
    googleNet_model.trainable = True

    model = Sequential()
    model.add(googleNet_model)
    model.add(GlobalAveragePooling2D())
    model.compile(loss='binary_crossentropy',
                  optimizer=optimizers.Adam(lr=1e-5,
                                            beta_1=0.9,
                                            beta_2=0.999,
                                            epsilon=None,
                                            decay=0.0,
                                            amsgrad=False),
                  metrics=['accuracy'])
    model.add(Dense(2, activation='softmax'))
    print(model.summary())
    return model
Пример #18
0
def build_InceptionResNetV2_pretrain(img_shape=(128, 128, 3), num_classes=1):

    resnet = InceptionResNetV2(include_top=False, weights='imagenet')

    resnet.trainable = True

    x = resnet.get_layer('block8_10').output

    x = GlobalAveragePooling2D()(x)

    outputs = Dense(num_classes,
                    kernel_initializer="he_normal",
                    activation='sigmoid')(x)
    final_model = Model(inputs=[resnet.input], outputs=[outputs])
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0)

    final_model.compile(optimizer=adam,
                        loss='binary_crossentropy',
                        metrics=['accuracy'])

    return final_model
Пример #19
0
    def buildDropModel(self, img_size, dropout):
        """
        This function builds an Inception-ResNet-V2 model with dropout layer.
        
        Parameters:
            img_size (int): the size of input images (img_size, img_size).
            dropout (float): the drop out rate for the dropout layer. Must be less than 1.

        Returns:
            model (class): the Inception-ResNet-V2 model with dropout layer.

        """
        base_model = InceptionResNetV2(weights=None, include_top=False, 
                                 input_shape = (img_size,img_size,3))
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        x = layers.Dropout(dropout)(x)
        predictions = layers.Dense(1, activation='sigmoid', name='last')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
        model.load_weights(self.weights)
        return model
Пример #20
0
    def buildNihModel(self, img_size, label_len):
        """
        This function builds a base Inception-ResNet-V2 model for pretraining with the NIH
        dataset.
        
        Parameters:
            img_size (int): the size of input images (img_size, img_size).
            label_len (int): the length of the labels from the NIH dataset.

        Returns:
            model (class): the Inception-ResNet-V2 model used in pretraining.

        """
        base_model = InceptionResNetV2(weights='imagenet', include_top=False, 
                                 input_shape = (img_size,img_size,3))
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)
        predictions = layers.Dense(label_len, activation='sigmoid', name='last')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
        if not self.weights == 'imagenet':
            model.load_weights(self.weights)
        return model
Пример #21
0
def get_model():
    # Create Model..........................................
    
    # Input layer
    baseModel = InceptionResNetV2(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))
    for layer in baseModel.layers:
        layer.trainable = False
    x = baseModel.output
    
#     x = AveragePooling2D(pool_size=(3,3), name='avg_pool')(x)

    # LSTM layer
    x = Reshape((25, 1536))(x)
    x = ((LSTM(512, activation="relu", return_sequences=True, trainable=False)))(x)
    x = BatchNormalization()(x)
#     x = Dropout(0.5)(x)
    
    # FC layer
    x = Flatten(name="flatten")(x)
    
#     # fc1 layer
#     x = Dense(units=4096, activation='relu')(x)
#     x = BatchNormalization()(x)
#     x = Dropout(0.5)(x)

    # fc2 layer
    x = Dense(units=4096, activation='relu')(x)
    x = BatchNormalization()(x)
#     x = Dropout(0.5)(x)
    
    # Output layer
    output = Dense(units=3, activation='softmax')(x)

    model = Model(inputs=baseModel.input, outputs=output)
    opt = RMSprop(lr=0.01, clipvalue=100)
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=["accuracy"])
    
    return model
Пример #22
0
def create_model(input_shape, num_classes):

    input_tensor = Input(shape=input_shape)
    inceptionresnetv2_model = InceptionResNetV2(include_top=False,
                                                weights=None,
                                                input_tensor=input_tensor)

    x = inceptionresnetv2_model.output
    #x = GlobalAveragePooling2D()(x)

    # (bs, y, x, c) --> (bs, x, y, c)
    x = Permute((2, 1, 3))(x)

    # (bs, x, y, c) --> (bs, x, y * c)
    _x, _y, _c = [int(s) for s in x.shape[1:]]
    x = Reshape((_x, _y * _c))(x)
    x = Bidirectional(LSTM(512, return_sequences=False),
                      merge_mode="concat")(x)
    predictions = Dense(num_classes, activation='softmax')(x)

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

    return model
Пример #23
0
def model(weight_path):
    model = InceptionResNetV2(include_top=False,
                              weights="imagenet",
                              input_tensor=None,
                              input_shape=(256, 256, 3),
                              pooling=None,
                              classes=40)

    final_model = Sequential()
    final_model.add(model)
    final_model.add(Flatten())
    final_model.add(Dense(units=2048, activation="relu"))
    final_model.add(Dense(units=2048, activation="relu"))
    final_model.add(Dense(units=40, activation="softmax"))

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

    final_model.load_weights(weight_path)
    # final_model = load_model(weight_path)

    return final_model
Пример #24
0
    # Flatten().

    print("\n - Training using {} as backbone".format(BACKBONE))

    # define backbone
    if BACKBONE.lower() == "VGG16".lower():
        base_model = VGG16(input_shape=(img_height, img_width, 3),
                           include_top=False,
                           weights=None)
    elif BACKBONE.lower() == "ResNet50V2".lower():
        base_model = ResNet50V2(input_shape=(img_height, img_width, 3),
                                include_top=False,
                                weights=None)
    elif BACKBONE.lower() == "InceptionResNetV2".lower():
        base_model = InceptionResNetV2(input_shape=(img_height, img_width, 3),
                                       include_top=False,
                                       weights=None)
    elif BACKBONE.lower() == "EfficientNet".lower():
        base_model = efn.EfficientNetB5(
            input_shape=(img_height, img_width, 3),
            weights=None,
            include_top=False,
        )
    elif BACKBONE.lower() == "MobileNetV2".lower():
        base_model = MobileNetV2(input_shape=(img_height, img_width, 3),
                                 include_top=False,
                                 weights=None)
    else:
        raise NotImplementedError("Unknown backbone \'{}\' ".format())
    base_model.trainable = True
### part 0
from tensorflow import keras
from tensorflow.keras import Input, layers, models
from tensorflow.keras import optimizers, regularizers
from tensorflow.keras.applications import InceptionResNetV2

import numpy as np

### part 1

input_tensor = Input(shape=(128, 128, 3), dtype='float32', name='input')

# pre_trained InceptionResNetV2
pre_trained_InceptionResNetV2 = InceptionResNetV2(weights='imagenet',
                                                  include_top=False,
                                                  input_shape=(128, 128, 3))
pre_trained_InceptionResNetV2.trainable = False
pre_trained_InceptionResNetV2.summary()

# pre_trained

model = models.Sequential()
model.add(pre_trained_InceptionResNetV2)
model.add(layers.Flatten())
model.add(
    layers.Dense(4096,
                 kernel_regularizer=regularizers.l1_l2(l1=0.001, l2=0.001),
                 activation='relu'))
model.add(layers.Dropout(0.5))
model.add(
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.2, random_state=5)

from tensorflow.keras.applications import InceptionResNetV2
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import InputLayer
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.models import Model
from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping

googleNet_model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=input_shape)
googleNet_model.trainable = True
model = Sequential()
model.add(googleNet_model)
model.add(GlobalAveragePooling2D())
model.add(Dense(units=2, activation='softmax'))
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False),
              metrics=['accuracy'])
model.summary()

#Currently not used
early_stopping = EarlyStopping(monitor='val_loss',
                               min_delta=0,
                               patience=2,
                               verbose=0, mode='auto')
Пример #27
0
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = data_generator.flow_from_dataframe(dataframe = training_set,
                                                    directory=img_dir,
                                                    x_col="Filenames",
                                                    y_col="labels",
                                                    class_mode="categorical",
                                                    classes=['1850', '3211', '3715', '5203', '5601', '8584'],
                                                    target_size=(IMAGE_SIZE,IMAGE_SIZE),
                                                    batch_size=32)

cb_early_stopper = EarlyStopping(monitor='loss', patience=EARLY_STOP_PATIENCE)
cb_checkpointer = ModelCheckpoint(filepath=saved_path + dataset_name + '/' + time + '/' + weight_file_name,
                                  monitor='accuracy', save_best_only=True, mode='auto')

model = Sequential()
model.add(InceptionResNetV2(include_top=False, pooling='avg', weights='imagenet'))
model.add(Dense(6, activation='sigmoid'))
model.layers[0].trainable = True
model.summary()

optimizer = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
# optimizer = optimizers.Adam()
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])

fit_history = model.fit_generator(train_generator,
                                  steps_per_epoch=train_generator.n / BATCH_SIZE,
                                  epochs=NUM_EPOCHS,
                                #   validation_data=validation_generator,
                                #   validation_steps=validation_generator.n / BATCH_SIZE,
                                  callbacks=[cb_early_stopper, cb_checkpointer])
Пример #28
0
# model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Dropout(0.3))
# model.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
# model.add(Conv2D(64, (3, 3), activation='relu'))
# model.add(Dropout(0.3))
# model.add(Conv2D(128, (3, 3), activation='relu'))
# model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Dropout(0.3))
# model.add(Flatten())
# model.add(Dense(20, activation='softmax'))
# model.compile(loss='categorical_crossentropy',
#              optimizer='adam',
#              metrics=['acc', f1_m])
model = Sequential()
model.add(
    InceptionResNetV2(include_top=False, pooling='max', weights='imagenet'))
model.add(Dense(20, activation='softmax'))
model.layers[0].trainable = False
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['acc', f1_m])

# %% [code]
history = model.fit_generator(train_gen,
                              epochs=EPOCHS,
                              validation_data=valid_gen,
                              validation_steps=total_valid // BATCH_SIZE,
                              steps_per_epoch=total_train // BATCH_SIZE)

# %% [code]
test_df = pd.read_csv("../input/input/test/test.tsv",
Пример #29
0
from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications import VGG19
from tensorflow.keras.applications import InceptionResNetV2
from tensorflow.keras.applications import InceptionV3

s1 = (224, 224, 3)
s2 = (299, 299, 3)

conv = VGG16(weights='imagenet', include_top=False, input_shape=s1)
conv = VGG19(weights='imagenet', include_top=False, input_shape=s1)
conv = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=s2)
conv = InceptionV3(weights='imagenet', include_top=False, input_shape=s2)
        # coming out of the MLP
        x = Dense(4)(x)
        x = Activation("relu")(x)

        x = Dense(1, activation="linear")(x)
        model = Model(input_tensor, x)
        model.compile(loss="mean_absolute_percentage_error", optimizer=opt)

    if keras_model_type == 'inceptionresnetv2application':
        trainX = inception_resnet_v2.preprocess_input(trainX)
        testX = inception_resnet_v2.preprocess_input(testX)

        input_tensor = Input(shape=(montage_image_size, montage_image_size, 3))
        base_model = InceptionResNetV2(include_top=False,
                                       weights=keras_model_weights,
                                       input_tensor=input_tensor,
                                       input_shape=(montage_image_size,
                                                    montage_image_size, 3),
                                       pooling='avg')

        # flatten the volume, then FC => RELU => BN => DROPOUT
        x = Flatten()(base_model.output)
        x = Dense(16)(x)
        x = Activation("relu")(x)
        x = BatchNormalization()(x)
        x = Dropout(0.5)(x)

        # apply another FC layer, this one to match the number of nodes
        # coming out of the MLP
        x = Dense(4)(x)
        x = Activation("relu")(x)