Exemple #1
0
def Bisnet(input_height, input_width, n_classes=20):

    input_image = tf.keras.Input(shape=(input_height, input_width, 3),
                                 name="input_image",
                                 dtype=tf.float32)

    # x = Lambda(lambda image: preprocess_input(image))(inputs)
    x = input_image

    xception = Xception(weights='imagenet', input_tensor=x, include_top=False)

    tail_prev = xception.get_layer('block13_pool').output
    tail = xception.output

    layer_13, layer_14 = tail_prev, tail

    x = ConvAndBatch(x, 32, strides=2)
    x = ConvAndBatch(x, 64, strides=2)
    x = ConvAndBatch(x, 156, strides=2)

    # context path
    cp = ContextPath(layer_13, layer_14)
    # fusion = self.FeatureFusionModule(cp, x, 32)
    fusion = FeatureFusionModule(cp, x, n_classes)
    ans = UpSampling2D(size=(8, 8), interpolation='bilinear')(fusion)

    output = tf.keras.layers.Softmax(axis=3, dtype=tf.float32)(ans)

    return tf.keras.Model(inputs=input_image, outputs=output, name="bisnet")
Exemple #2
0
def create_model(shape):   
    
    from keras import layers
    # from keras import models
    from keras import optimizers
    # from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
    from tensorflow.keras import Model
    
    # from tensorflow.keras.initializers import glorot_uniform
    from tensorflow.keras.regularizers import l2
    # from tensorflow.keras.preprocessing.image import ImageDataGenerator
    from tensorflow.keras.applications import Xception
    model = Xception(input_shape = (shape, shape, 3), include_top = False, weights = 'imagenet')

    x = model.output
    x = layers.AveragePooling2D(pool_size = (2, 2))(x)
    x = layers.Dense(32, activation = 'relu')(x)
    x = layers.Flatten()(x)
    # spostato dopo Flatten
    x = layers.Dropout(0.1)(x)
    # a caso
    x = layers.Dense(128)(x)
    x = layers.Dense(4, activation = 'softmax', kernel_regularizer = l2(.0005))(x)
    model = Model(inputs = model.inputs, outputs = x)
    opt = optimizers.SGD(lr = 0.0001, momentum = .9)
    model.compile(loss = 'categorical_crossentropy', optimizer = opt, metrics = ['accuracy'])
    return model
 def __init__(self):
     with graph.as_default():
         self.model = Xception(
             weights="imagenet",
             classes=1000,
             classifier_activation="softmax",
         )
Exemple #4
0
def xception_model(num_classes, input_shape, dropout_rate=0.25):
    """
    xception_model(num_classes, input_shape, dropout_rate=0.25)
    This function creates an implementation of a convolutional deep learning model for estimating
	a discrete category based on xception, trained from scratch
    INPUTS:
        * num_classes = number of classes (output nodes on classification layer)
        * input_shape = size of input layer (i.e. image tensor)
    OPTIONAL INPUTS:
        * dropout_rate = proportion of neurons to randomly set to zero, after the pooling layer
    GLOBAL INPUTS: None
    OUTPUTS: keras model instance
    """
    EXTRACTOR = Xception(weights=None, include_top=False,
                        input_shape=input_shape)

    EXTRACTOR.trainable = True
    # Construct the head of the model that will be placed on top of the
    # the base model
    class_head = EXTRACTOR.output
    class_head = tf.keras.layers.GlobalAveragePooling2D()(class_head)
    class_head = tf.keras.layers.Dense(256, activation="relu")(class_head)
    class_head = tf.keras.layers.Dropout(dropout_rate)(class_head)
    class_head = tf.keras.layers.Dense(num_classes, activation="softmax")(class_head)

    # Create the new model
    model = tf.keras.Model(inputs=EXTRACTOR.input, outputs=class_head)

    return model
Exemple #5
0
def build_Xception_FT(img_shape=(128,128),num_classes=1):
    
    base_model = Xception(include_top=False, input_shape = img_shape,weights='imagenet')
    
    base_model.trainable = True
    
    fine_tune_at = 130
    
    for layer in base_model.layers[:fine_tune_at]:
        layer.trainable =  False   
        
    x = Flatten()(base_model.output)
    x = Dense(4096)(x)
    x = ReLU()(x)
    x = Dropout(0.2)(x)
    x = Dense(512)(x)
    x = ReLU()(x)

    outputs = Dense(num_classes
                    ,kernel_initializer = "he_normal"
                    , activation='sigmoid')(x)  
    finetuning = Model(inputs=[base_model.input], outputs=[outputs])
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0)
    
    finetuning.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])

    return finetuning
Exemple #6
0
def cnn_model():
    takemodel = Xception(include_top=False, input_shape=(1024, 1024, 3))
    takemodel.trainable = False
    model = Sequential()
    model.add(takemodel)
    model.add(Flatten())
    model.add(Dense(12, activation='softmax'))

    model.summary()

    return model
Exemple #7
0
def predict(img):
    model = Xception(weights="imagenet",
                     include_top=True,
                     input_tensor=Input(shape=(224, 224, 3)))
    img = img_to_array(img)
    img = img.reshape((-1, img.shape[0], img.shape[1], 3)) / 255
    rez = model.predict(img)
    rez = pd.DataFrame(decode_predictions(rez)[0],
                       columns=['_', 'breed',
                                'prct']).drop(columns='_').sort_values(
                                    by='prct', ascending=False)
    np.array(rez)
    return rez
Exemple #8
0
def deep_lstm_model(image_shape, sequence_length, classes):
    (h, w, c) = image_shape
    sequence_shape = (sequence_length, h, w, c)
    video = Input(shape=sequence_shape)
    # cnn_base = VGG16(input_shape=image_shape,
    #                  weights="imagenet",
    #                  include_top=False)
    extractor_base = Xception(
        include_top=False,
        weights='imagenet',
        input_shape=image_shape,
        pooling='avg',
    )
    # cnn_out = GlobalAveragePooling2D()(cnn_base.output)
    extractor = Model(extractor_base.input, extractor_base.output)
    extractor.trainable = False
    encoded_frames = TimeDistributed(extractor)(video)
    encoded_sequences = LSTM(64, return_sequences=True)(encoded_frames)
    encoded_sequence = LSTM(64)(encoded_sequences)
    hidden_layer = Dense(256, activation="relu")(encoded_sequence)
    outputs = Dense(classes, activation="softmax")(hidden_layer)
    model = Model(video, outputs)
    optimizer = Nadam(lr=0.002,
                      beta_1=0.9,
                      beta_2=0.999,
                      epsilon=1e-08,
                      schedule_decay=0.004)
    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer=optimizer,
                  metrics=["categorical_accuracy"])
    return model
Exemple #9
0
def transferlearning(modelname, learning_rate, image_shape, training_path, epoch):
    train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        validation_split=0.2)

    train_generator = train_datagen.flow_from_directory(
        training_path,
        target_size=(image_shape, image_shape),
        batch_size=128,
        class_mode='categorical',
        subset='training',
        shuffle=True)

    validation_generator = train_datagen.flow_from_directory(
        training_path,
        target_size=(image_shape, image_shape),
        batch_size=128,
        class_mode='categorical',
        subset='validation')

    num_classes = train_generator.num_classes

    if modelname == 'VGG19':
        base_model = VGG19(input_shape=(image_shape, image_shape, 3),
                           include_top=False, weights='imagenet')
    elif modelname == 'MobileNetV2':
        base_model = MobileNetV2(
            input_shape=(image_shape, image_shape, 3), include_top=False, weights='imagenet')
    elif modelname == 'DenseNet201':
        base_model = DenseNet201(
            input_shape=(image_shape, image_shape, 3), include_top=False, weights='imagenet')
    elif modelname == 'InceptionV3':
        base_model = InceptionV3(
            input_shape=(image_shape, image_shape, 3), include_top=False, weights='imagenet')
    elif modelname == 'ResNet50':
        base_model = ResNet50(input_shape=(image_shape, image_shape, 3),
                              include_top=False, weights='imagenet')
    elif modelname == 'Xception':
        base_model = Xception(input_shape=(image_shape, image_shape, 3),
                              include_top=False, weights='imagenet')

    base_model.output
    global_layer = keras.layers.GlobalAveragePooling2D()(base_model.output)
    prediction_layer = keras.layers.Dense(
        num_classes, activation='softmax')(global_layer)
    model = keras.models.Model(
        inputs=base_model.input, outputs=prediction_layer)
    model.summary()
    model.compile(optimizer=Adam(lr=learning_rate),
                  loss='categorical_crossentropy', metrics=["acc"])
    history = model.fit_generator(
        train_generator,
        epochs=epoch,
        validation_data=validation_generator,
        validation_steps=1000)

    return history
class FeatureExtractor:
    def __init__(self):
        with graph.as_default():
            self.model = Xception(
                weights="imagenet",
                classes=1000,
                classifier_activation="softmax",
            )

    def extract(self, img):
        """
        Extract a deep feature from an input image
        Args:
            img: from PIL.Image.open(path) or tensorflow.keras.preprocessing.image.load_img(path)
        Returns:
            feature (np.ndarray): deep feature with the shape=(4096, )
        """
        img = Image.open(img)
        img = img.resize(
            (299, 299))  # Xception must take a 299x299 img as an input
        img = img.convert('RGB')  # Make sure img is color
        x = image.img_to_array(
            img)  # To np.array. Height x Width x Channel. dtype=float32
        x = np.expand_dims(
            x, axis=0
        )  # (H, W, C)->(1, H, W, C), where the first elem is the number of img
        x = preprocess_input(x)  # Subtracting avg values for each pixel
        with graph.as_default():
            set_session(sess)
            feature = self.model.predict(x)[0]  # (1, 4096) -> (4096, )
        return feature / np.linalg.norm(feature)  # Normalize
    def buildNihModel(self, img_size, label_len):
        """
        This function builds a base Xception 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 Xception model used in pretraining.

        """
        base_model = Xception(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
Exemple #12
0
def xception(input_shape: Tuple[int, int, int],
             output_shape: Tuple[int, ...],
             weights: str = 'imagenet',
             include_top: bool = False) -> Model:

    base_model = Xception(weights=weights,
                          include_top=include_top,
                          input_tensor=Input(shape=input_shape))
    if include_top:
        return base_model
    else:
        # Construct the head of the model that will be placed on top of the base model
        num_classes = output_shape[0]
        head_model = base_model.output
        head_model = AveragePooling2D(pool_size=(4, 4))(head_model)
        head_model = Flatten(name='flatten')(head_model)
        head_model = Dense(64, activation='relu')(head_model)
        head_model = Dropout(0.5)(head_model)

        if num_classes > 2:
            activation = 'softmax'
        else:
            activation = 'sigmoid'

        head_model = Dense(num_classes, activation=activation)(head_model)

        # Place the head Fully Connected model on top of the base model (actual model)
        model = Model(base_model.input, head_model)

        # Loop over all layers in the base model and freeze them so they won't be updated during the first training process
        for layer in base_model.layers:
            layer.trainable = False

        return model
def create_model():
    base_model = Xception(
        weights='imagenet',
        include_top=False,
        pooling='avg'
    )

    # Add new fully connected layers for ifcb
    # dense layer to classify with softmax
    model_output = base_model.output
    model_output = Dense(1024, activation='relu')(model_output)
    model_output = Dense(50, activation='softmax')(model_output)
    model = Model(inputs=base_model.input, outputs=model_output)

    # Only train the dense layers, user transfer learning for the rest
    for layer in model.layers[:-2]:
        layer.trainable = False
    for layer in model.layers[-2::]:
        layer.trainable = True

    model.compile(
        optimizer='Adam',
        loss='categorical_crossentropy',
        metrics=['categorical_accuracy'],
    )

    return model
def make_model():
    base_model = Xception(include_top=False,
                          input_shape=(71, 71, 3),
                          weights=None)

    for layer in base_model.layers:
        layer.trainable = True

    model = base_model.output
    model = Flatten()(model)

    model = Dense(512, kernel_initializer='he_uniform')(model)
    model = Dropout(0.2)(model)
    model = BatchNormalization()(model)
    model = Activation('relu')(model)

    model = Dense(128, kernel_initializer='he_uniform')(model)
    model = Dropout(0.2)(model)
    model = BatchNormalization()(model)
    model = Activation('relu')(model)

    model = Dense(52, kernel_initializer='he_uniform')(model)
    model = Dropout(0.2)(model)
    model = BatchNormalization()(model)
    model = Activation('relu')(model)

    model = Dense(16, kernel_initializer='he_uniform')(model)
    model = Dropout(0.2)(model)
    model = BatchNormalization()(model)
    model = Activation('relu')(model)

    output = Dense(7, activation='softmax')(model)

    return Model(inputs=base_model.input, outputs=output)
Exemple #15
0
def build_model(classes=2):
    inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
    x = preprocess_input(inputs)
    x = Xception(weights=None, classes=classes)(x)
    model = Model(inputs=inputs, outputs=x)
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
    return model
Exemple #16
0
def get_feature_extractor(weights_file):
    """"""
    from tensorflow.keras.applications import Xception
    xception = Xception(include_top=False, weights=weights_file)
    outputs = keras.layers.GlobalAveragePooling2D(name="avg_pool")(xception.output)
    model = keras.Model(xception.input, outputs)
    return model
def pretrainedmodel(classes,
                    name_model='VGG16',
                    use_l2=True,
                    is_trainable=False):
    models = ['VGG16', 'ResNet50', 'InceptionV3', 'Xception']

    if name_model not in models:
        print('Name model not found. Try {}'.format(models))
        return

    if name_model == 'VGG16':
        expected_dim = (224, 224, 3)
        vgg16 = VGG16(weights='imagenet', include_top=False, pooling='avg')
        basemodel = Model(inputs=vgg16.inputs,
                          outputs=vgg16.get_layer(index=-1).output)

    if name_model == 'ResNet50':
        expected_dim = (224, 224, 3)
        resnet = ResNet50(weights='imagenet', include_top=False, pooling='avg')
        basemodel = Model(inputs=resnet.inputs,
                          outputs=resnet.get_layer(index=-1).output)

    if name_model == 'InceptionV3':
        expected_dim = (299, 299, 3)
        iv3 = InceptionV3(weights='imagenet', include_top=False, pooling='avg')
        basemodel = Model(inputs=iv3.inputs,
                          outputs=iv3.get_layer(index=-1).output)

    if name_model == 'Xception':
        expected_dim = (299, 299, 3)
        xcep = Xception(weights='imagenet', include_top=False, pooling='avg')
        basemodel = Model(inputs=xcep.inputs,
                          outputs=xcep.get_layer(index=-1).output)

    # freeze weights
    basemodel.trainable = is_trainable
    x = basemodel.output

    # add l2-norm constraint
    if use_l2:
        x = L2Layer()(x)

# predictions
    y = Dense(classes, activation='softmax', name='predictions')(x)
    model = Model(inputs=basemodel.input, outputs=y)

    return model, expected_dim
Exemple #18
0
def build_Xception_pretrain(img_shape=(128,128,3),num_classes=1, lr=0.001):
    
    base_model = Xception(include_top=False, input_shape = img_shape,weights='imagenet')
    
    base_model.trainable = False
    
    x = base_model.get_layer('avg_pool').output
        
    outputs = Dense(num_classes
                    ,kernel_initializer = "he_normal"
                    , activation='sigmoid')(x)  
    final_model = Model(inputs=[base_model.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
    def buildTunerModel(self, img_size):
        """
        This function builds a base Xception model for keras tuner
        
        Parameters:
            img_size (int): the size of input images (img_size, img_size).

        Returns:
            model (class): the Xception model used for keras tuner.

        """
        base_model = Xception(weights='imagenet',
                              include_top=False,
                              input_shape=(img_size, img_size, 3))
        if not self.weights == 'imagenet':
            base_model.load_weights(self.weights, by_name=True)
        return base_model
Exemple #20
0
def build_model(input_shape, num_classes):

    base_model = Xception(weights='imagenet', include_top=False, input_shape=input_shape)
    base_model.trainable = True

    for _i, _layer in enumerate(base_model.layers):
        if _i < 100:
            _layer.trainable = False
        else:
            _layer.trainable = True

    model = Sequential()
    model.add(base_model)

    model.add(GlobalAveragePooling2D())
    model.add(Dense(num_classes, activation='softmax'))

    return model
 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)
Exemple #22
0
    def __init__(self, layer_name='add_3', weight_decay=0.005):
        super(FTCF_Net, self).__init__()
        self.weight_decay = weight_decay
        self.layer_name = layer_name
        self.basemodel = Xception(weights='imagenet',
                                  input_shape=(224, 224, 3),
                                  include_top=False)

        # Feature Extractor for Every Frames
        self.backbone = tf.keras.Model(inputs=self.basemodel.input,
                                       outputs=self.basemodel.get_layer(
                                           self.layer_name).output)
        self.backbone.trainable = False

        self.cbm1 = CBM(filters=364, kernel=(1, 1, 2))

        # Fusion Stage 1
        self.ftcf1 = FTCF_Block(32)
        self.avgpooling3d_1 = AveragePooling3D((2, 2, 2),
                                               strides=(2, 2, 2),
                                               padding='same')

        # Fusion Stage 2
        self.ftcf2 = FTCF_Block(32)
        self.avgpooling3d_2 = AveragePooling3D((2, 2, 2),
                                               strides=(2, 2, 2),
                                               padding='same')

        # Fusion Stage 3
        self.ftcf3 = FTCF_Block(64)
        self.avgpooling3d_3 = AveragePooling3D((2, 2, 2),
                                               strides=(2, 2, 2),
                                               padding='same')

        self.cbm2 = CBM(filters=128, kernel=(2, 2, 2), padding='valid')
        self.globalpooling3d = GlobalAveragePooling3D()

        self.dense_2 = Dense(512,
                             activation='relu',
                             kernel_regularizer=l2(self.weight_decay))
        self.dropout_2 = Dropout(0.5)
        self.output_tensor = Dense(2,
                                   activation='softmax',
                                   kernel_regularizer=l2(self.weight_decay))
Exemple #23
0
    def __init__(self, conf):
        super(MNISTDigitClassifier, self).__init__()

        # Design layers.
        input_shape = (28, 28, 1)

        self.nobody_convnet2d = NobodyConvNet2D(conf, input_shape)
        self.xception = Xception(include_top=False, weights=None)
        self.flatten = Flatten()
        self.dense = Dense(10)
def create_transfer_model(input_size, n_categories, weights = 'imagenet'):
        # note that the "top" is not included in the weights below
        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
Exemple #25
0
def create_xception_layer(x,
                          weights='imagenet',
                          include_top=False,
                          classes=100,
                          activation='softmax'):
    model = Xception(input_tensor=x,
                     include_top=include_top,
                     classes=classes,
                     classifier_activation=activation,
                     weights=weights)
    return model.output
def build_xception_encoder(image_shape=(320, 320, 3), embedding_length=16):

    encoder_input = Input(image_shape)

    # we are using xception as our base encoder
    # Xception output shape ?, 10, 10, 2048
    pre_trained = Xception(include_top=False,
                           weights="imagenet",
                           input_tensor=encoder_input,
                           input_shape=image_shape)
    pre_trained.trainable = False

    # after xception we are adding conv layers
    # we are using 1x1 convolutions to reduce volume to flat vector
    # using 1x1 convolutions because it is easier to ensure output shape with varying input
    first = add_inception_module(pre_trained.output,
                                 channel_multiplier=32,
                                 layer_name_prefix="encoder_1",
                                 activation="tanh")
    second = Conv2D(256, (3, 3), activation="selu", name="encoder_2")(first)
    third = add_inception_module(second,
                                 channel_multiplier=16,
                                 layer_name_prefix="encoder_3",
                                 activation="tanh")
    forth = Conv2D(128, (3, 3), activation="selu", name="encoder_4")(third)
    fifth = add_inception_module(forth,
                                 channel_multiplier=8,
                                 layer_name_prefix="encoder_5",
                                 activation="tanh")
    sixth = Conv2D(64, (2, 2), activation="selu", name="encoder_6")(fifth)
    seventh = Conv2D(embedding_length, (3, 3),
                     activation="selu",
                     name="encoder_7")(sixth)

    # output layer
    embedding = Flatten()(seventh)

    # wrap the whole thing in a keras.Model instance
    encoder_model = Model(inputs=[encoder_input], outputs=[embedding])
    return encoder_model
Exemple #27
0
def get_model(weight_dir, out_dir, fullyconnected=False, finetune=False):
    """
    Creates custom model on top of Xception for user engagement
    recognition.

    Returns:
        model for the training.
    """
    weights = Path(weight_dir)
    if finetune:
        if fullyconnected:
            base_model = load_model(str(out_dir) + "/Xception_on_DAiSEE_fc.h5")
        else:
            base_model = load_model(str(out_dir) + "/Xception_on_DAiSEE.h5")

        base_model.trainable = True
        for layer in base_model.layers[:finetune_at]:
            layer.trainable = False
        return base_model
    else:
        xception = "xception_weights_tf_dim_ordering_tf_kernels_notop.h5"
        weights = weights / xception
        base_model = Xception(weights=str(weights),
                              include_top=False,
                              input_shape=(img_width, img_height, 3))

        base_model.trainable = False
        x = GlobalAveragePooling2D()(base_model.output)
        if fullyconnected:
            x = Dense(128, activation="relu", name="fc1")(x)
            x = Dense(64, activation="relu", name="fc2")(x)
        boredom = Dense(4, name="y1")(x)
        engagement = Dense(4, name="y2")(x)
        confusion = Dense(4, name="y3")(x)
        frustration = Dense(4, name="y4")(x)
        model = Model(inputs=base_model.input,
                      outputs=[boredom, engagement, confusion, frustration])
    return model
Exemple #28
0
def transfer_model(input_size, weights = 'imagenet'):
        # note that the "top" is not included in the weights below
        base_model = Xception(weights=weights,
                          include_top=False,
                          input_shape=input_size)
        
        model = base_model.output
        
        # add new head
        model = GlobalAveragePooling2D()(model)
        predictions = Dense(1, activation='sigmoid', name='visualized_layer')(model)
        model = Model(inputs=base_model.input, outputs=predictions)

        return model
Exemple #29
0
def xception_build_model():
    w_init = glorot_normal()
    b_init = Zeros()
    xception = Xception(input_shape=(512, 512, 3),
                        include_top=False,
                        weights="imagenet",
                        pooling="avg")

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

    model = Sequential()
    model.add(xception)
    model.add(Flatten())
    model.add(Dropout(0.2))
    model.add(
        Dense(512,
              activation='relu',
              kernel_initializer=w_init,
              bias_initializer=b_init,
              kernel_regularizer='l2'))
    model.add(BatchNormalization())
    model.add(
        Dense(3,
              activation='softmax',
              kernel_initializer=w_init,
              bias_initializer=b_init,
              kernel_regularizer='l2'))
    model.summary()

    optimizer = tf.keras.optimizers.Adam(
        learning_rate=0.00001,
        beta_1=0.9,
        beta_2=0.999,
        epsilon=1e-08,
        amsgrad=False,
        name="Adam",
    )

    loss = tf.keras.losses.CategoricalCrossentropy(
        from_logits=False,
        label_smoothing=0.05,
        reduction="auto",
        name="categorical_crossentropy",
    )
    model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])

    return model
Exemple #30
0
    def build_xception_model(self):
        conv_base = Xception(
            weights='imagenet',
            include_top=False,
            input_shape=self.INPUT_SHAPE)

        disable_training_layers(conv_base)

        flattened = layers.Flatten()(conv_base.output)
        x = layers.Dense(128, activation='relu')(flattened)
        x = layers.Dropout(rate=0.5)(x)
        x = layers.Dense(64, activation='relu')(x)
        answer = layers.Dense(1, activation='sigmoid')(x) 

        model = models.Model(conv_base.input, answer)
        return model