Exemplo n.º 1
0
def efn():
    inputs = Inputs(shape=(*IMAGE_SIZE, 3))
    model = EfficientNetB0(include_top=False,
                           input_tensor=inputs,
                           weights='imagenet')
    model.trainable = False

    for layer in model.layers[-12:]:
        if not isinstance(layer, BatchNormalization):
            layer.trainable = True

    x = GlobalAveragePooling2D(name='avg_pool')(model.output)
    x = BatchNormalization()(x)

    top_dropout_rate = 0.2
    x = Dropout(top_dropout_rate, name='top_dropout')(x)
    outputs = Dense(5, activation='softmax', name='prediction')(x)

    # Compile

    model = tf.keras.Model(inputs, outputs, name='EfficientNet')
    optimizer = Adam(learning_rate=1.5e-3)

    model_metrics = [tf.keras.metrics.SparseCategoricalAccuracy(name='spa')]
    model_loss = tf.keras.losses.SparseCategoricalCrossentropy(name='scc')
    model.compile(optimizer=optimizer, loss=model_loss, metrics=model_metrics)
    return model
Exemplo n.º 2
0
def build_efficent_nets(n_classes,
                        type_eff='b0',
                        regularizer=None,
                        target_size=(224, 224)):
    # Clear memory for new model
    K.clear_session()
    img_input = Input(shape=(target_size[0], target_size[1], 3))
    label = Input(shape=(n_classes, ))

    if type_eff == 'b0':
        base_model = EfficientNetB0(weights='imagenet',
                                    include_top=False,
                                    input_tensor=img_input)
    elif type_eff == 'b3':
        base_model = EfficientNetB3(weights='imagenet',
                                    include_top=False,
                                    input_tensor=img_input)
    elif type_eff == 'b6':
        base_model = EfficientNetB6(weights='imagenet',
                                    include_top=False,
                                    input_tensor=img_input)
    # Custom top
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    output = ArcFace(n_classes=11, regularizer=regularizer)([x, label])

    return Model([img_input, label], output)
Exemplo n.º 3
0
def get_efficeintNet_model():
 output_classes=2
 base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(1024,1024, 3))
 x = base_model.output
 x = BatchNormalization()(x)
 x=Flatten()(x)
 predictions = Dense(2, activation='softmax')(x) 
 model = Model(inputs=base_model.input, outputs=predictions)
 return model
Exemplo n.º 4
0
def _EffnetB0():
    model = EfficientNetB0(input_shape=(224, 224, 3),
                           include_top=False,
                           weights='imagenet')
    input = Input((224, 224, 3))
    x = model(input)
    x = GlobalAveragePooling2D()(x)
    x = LeakyReLU(0.1)(x)
    x = Dense(1, activation='sigmoid')(x)
    model = Model(inputs=input, outputs=x)
    model.summary()
    plot_model(model, to_file='effnet.png', show_shapes=True)
    return model
Exemplo n.º 5
0
def efficientnet_B0(input, num_class = None):
    #input_layer = Input(shape = (None, None, input_channel_num))
    dense = EfficientNetB0(include_top = False, weights = None, input_tensor = input, pooling = 'max')
    x = dense.get_layer('swish_16').output

    x = Permute((2, 1, 3), name='permute')(x) #(b, None, 8, 240)
    x = TimeDistributed(Flatten(), name='flatten')(x)

    # add blstm layers
    rnnunit = 256
    x = Bidirectional(LSTM(rnnunit, return_sequences=True, implementation=2), name='blstm1')(x)
    #x = Dense(rnnunit, name='blstm1_out', activation='linear')(x)
    x = Bidirectional(LSTM(rnnunit, return_sequences=True, implementation=2), name='blstm2')(x)
    
    # softmax output layer
    y_pred = Dense(num_class, name='out', activation='softmax')(x)

    return y_pred
Exemplo n.º 6
0
def efficient_yolo_body(inputs, num_anchors, num_classes):
    # efficientnet
    efficientnet_modle = EfficientNetB0(input_tensor=inputs,
                                        weights='imagenet',
                                        include_top=False)
    # input: 416 x 416 x 3
    # conv_pw_13_relu :13 x 13 x 1280
    # conv_pw_11_relu :26 x 26 x 672
    # conv_pw_5_relu : 52 x 52 x 240
    f1 = efficientnet_modle.get_layer('swish_49').output
    x, y1 = make_last_layers(f1, 640, num_anchors * (num_classes + 5))
    x = compose(DarknetConv2D_BN_Leaky(320, (1, 1)), UpSampling2D(2))(x)
    f2 = efficientnet_modle.get_layer('swish_34').output
    x = Concatenate()([x, f2])
    x, y2 = make_last_layers(x, 336, num_anchors * (num_classes + 5))
    x = compose(DarknetConv2D_BN_Leaky(168, (1, 1)), UpSampling2D(2))(x)
    f3 = efficientnet_modle.get_layer('swish_16').output
    x = Concatenate()([x, f3])
    x, y3 = make_last_layers(x, 120, num_anchors * (num_classes + 5))
    return Model(inputs=inputs, outputs=[y1, y2, y3])
Exemplo n.º 7
0
from keras.preprocessing import image
from keras.applications import imagenet_utils
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import matplotlib.pyplot as plt
from keras_efficientnets import EfficientNetB0

train_path='C:/Users/legend698/Desktop/train'
test_path='C:/Users/legend698/Desktop/ANIMESH/python/Cats-and-Dogs/test'
valid_path='C:/Users/legend698/Desktop/valid'

train_batches=ImageDataGenerator().flow_from_directory(train_path,target_size=(224,224),classes=['dog','cat'],batch_size=10)
test_batches =ImageDataGenerator().flow_from_directory(test_path,target_size=(224,224),classes=['dog','cat'],batch_size=10)
valid_batches=ImageDataGenerator().flow_from_directory(valid_path,target_size=(224,224),classes=['dog','cat'],batch_size=10)

eff_model=EfficientNetB0()
eff_model.summary()
x=eff_model.layers[-3].output
prediction=Dense(2,activation='softmax')(x)
model=Model(inputs=eff_model.input,outputs=prediction)
model.summary()	


# model.add(Dense(2,activation='softmax')) 	

#model=EfficientNetB0((224,224,3),classes=['cat','dog'])
model.compile(Adam(lr=0.0001),loss='categorical_crossentropy',metrics=['accuracy'])
model.fit_generator(train_batches,steps_per_epoch=2500,validation_data=valid_batches,validation_steps=40,epochs=10,verbose=2)

model.save('effnet.h5')
Exemplo n.º 8
0
def cnn_model(model_name, img_size):
    """
    Model definition using Xception net architecture
    """
    input_size = (img_size, img_size, 3)

    if model_name == "xception":
        print("Loading Xception wts...")
        baseModel = Xception(weights="imagenet",
                             include_top=False,
                             input_shape=(img_size, img_size, 3))
    elif model_name == "iv3":
        baseModel = InceptionV3(weights="imagenet",
                                include_top=False,
                                input_shape=(img_size, img_size, 3))
    elif model_name == "irv2":
        baseModel = InceptionResNetV2(weights="imagenet",
                                      include_top=False,
                                      input_shape=(img_size, img_size, 3))
    elif model_name == "resnet":
        baseModel = ResNet50(weights="imagenet",
                             include_top=False,
                             input_shape=(img_size, img_size, 3))
    elif model_name == "nasnet":
        baseModel = NASNetLarge(weights="imagenet",
                                include_top=False,
                                input_shape=(img_size, img_size, 3))
    elif model_name == "ef0":
        baseModel = EfficientNetB0(input_size,
                                   weights="imagenet",
                                   include_top=False)
    elif model_name == "ef5":
        baseModel = EfficientNetB5(input_size,
                                   weights="imagenet",
                                   include_top=False)

    headModel = baseModel.output
    headModel = GlobalAveragePooling2D()(headModel)
    headModel = Dense(512, activation="relu",
                      kernel_initializer="he_uniform")(headModel)
    headModel = Dropout(0.4)(headModel)
    # headModel = Dense(512, activation="relu", kernel_initializer="he_uniform")(
    #     headModel
    # )
    # headModel = Dropout(0.5)(headModel)
    predictions = Dense(5,
                        activation="softmax",
                        kernel_initializer="he_uniform")(headModel)
    model = Model(inputs=baseModel.input, outputs=predictions)

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

    optimizer = Nadam(lr=0.002,
                      beta_1=0.9,
                      beta_2=0.999,
                      epsilon=1e-08,
                      schedule_decay=0.004)
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizer,
                  metrics=["accuracy"])
    return model
Exemplo n.º 9
0
def efficientnet_retinanet(num_classes,
                           backbone='efficientnet-b0',
                           inputs=None,
                           modifier=None,
                           **kwargs):
    """ Constructs a retinanet model using a vgg backbone.

    Args
        num_classes: Number of classes to predict.
        backbone: Which backbone to use (one of ('vgg16', 'vgg19')).
        inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)).
        modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example).

    Returns
        RetinaNet model with a VGG backbone.
    """
    # choose default input
    if inputs is None:
        inputs = keras.layers.Input(shape=(None, None, 3))

    # create the vgg backbone
    if backbone == 'efficientnet-b0':
        efficientnet = EfficientNetB0(input_tensor=inputs,
                                      include_top=False,
                                      weights='imagenet')
        layer_names = ["swish_16", "swish_34", "swish_49"]
    elif backbone == 'efficientnet-b1':
        efficientnet = EfficientNetB1(input_tensor=inputs,
                                      include_top=False,
                                      weights='imagenet')
        layer_names = ["swish_24", "swish_48", "swish_69"]
    elif backbone == 'efficientnet-b2':
        efficientnet = EfficientNetB2(input_tensor=inputs,
                                      include_top=False,
                                      weights='imagenet')
        layer_names = ["swish_24", "swish_48", "swish_69"]
    elif backbone == 'efficientnet-b3':
        efficientnet = EfficientNetB3(input_tensor=inputs,
                                      include_top=False,
                                      weights='imagenet')
        layer_names = ["swish_24", "swish_54", "swish_78"]
    elif backbone == 'efficientnet-b4':
        efficientnet = EfficientNetB4(input_tensor=inputs,
                                      include_top=False,
                                      weights='imagenet')
        layer_names = ["swish_30", "swish_66", "swish_96"]
    elif backbone == 'efficientnet-b':
        efficientnet = EfficientNetB4(input_tensor=inputs,
                                      include_top=False,
                                      weights='imagenet')
        layer_names = ["swish_30", "swish_66", "swish_96"]
    else:
        raise ValueError("Backbone '{}' not recognized.".format(backbone))

    if modifier:
        efficientnet = modifier(efficientnet)

    efficientnet.summary()
    # create the full model

    layer_outputs = [
        efficientnet.get_layer(name).output for name in layer_names
    ]
    return retinanet.retinanet(inputs=inputs,
                               num_classes=num_classes,
                               backbone_layers=layer_outputs,
                               **kwargs)
Exemplo n.º 10
0
def build(size, seq_len , learning_rate ,
          optimizer_class ,\
          initial_weights ,\
          cnn_class ,\
          pre_weights , \
          lstm_conf , \
          cnn_train_type, classes = 1, dropout = 0.0):
    input_layer = Input(shape=(seq_len, size, size, 3))
    if(cnn_train_type!='train'):
        if cnn_class.__name__ == "ResNet50":
            cnn = cnn_class(weights=pre_weights, include_top=False,input_shape =(size, size, 3))
        elif cnn_class.__name__=="MobileNetV3_Large":
            cnn=cnn_class(shape =(size, size, 3),n_class=2,include_top=False).build()
        elif cnn_class.__name__=='MobileNetV3_Small':
             cnn=cnn_class(shape =(size, size, 3),n_class=2,include_top=False).build()
        elif cnn_class.__name__=='efn.EfficientNetB0':
            cnn = EfficientNetB0(input_shape=(size,size,3), classes=2, include_top=False, weights='imagenet')
        elif cnn_class.__name__=='efn.EfficientNetB1':
            cnn = EfficientNetB1(input_shape=(size,size,3), classes=2, include_top=False, weights='imagenet')
        elif cnn_class.__name__=="ShuffleNetV2":
            cnn=ShuffleNetV2(include_top=False,input_shape=(224, 224, 3),bottleneck_ratio=1)
        else:
            cnn = cnn_class(weights=pre_weights,include_top=False)
    else:
        cnn = cnn_class(include_top=False)

    #control Train_able of CNNN
    if(cnn_train_type=='static'):
        for layer in cnn.layers:
            layer.trainable = False
    if(cnn_train_type=='retrain'):
        for layer in cnn.layers:
            layer.trainable = True

    cnn = TimeDistributed(cnn)(input_layer)
    print(cnn)
    #the resnet output shape is 1,1,20148 and need to be reshape for the ConvLSTM filters
    # if cnn_class.__name__ == "ResNet50":
        # cnn = Reshape((seq_len,4, 4, 128), input_shape=(seq_len,1, 1, 2048))(cnn)
    # print(lstm_conf)
    # print(lstm_conf[0])
    # print(lstm_conf[1])
    lstm = lstm_conf[0](**lstm_conf[1])(cnn)
    lstm = MaxPooling2D(pool_size=(2, 2))(lstm)
    attention_mul = attention_3d_block(lstm)
    # print(lstm)
    # lstm = MaxPooling2D(pool_size=(2, 2))(lstm)
    flat = Flatten()(attention_mul)

    flat = BatchNormalization()(flat)
    # flag=LayerNormalization()(flat)
    flat = Dropout(dropout)(flat)
    linear = Dense(512)(flat)

    relu = Activation('relu')(linear)
    linear = Dense(256)(relu)
    linear = Dropout(dropout)(linear)
    relu = Activation('relu')(linear)
    linear = Dense(10)(relu)
    linear = Dropout(dropout)(linear)
    relu = Activation('relu')(linear)

    activation = 'sigmoid'
    loss_func = 'binary_crossentropy'

    if classes > 1:
        activation = 'softmax'
        loss_func = 'categorical_crossentropy'
    predictions = Dense(classes,  activation=activation)(relu)

    model = Model(inputs=input_layer, outputs=predictions)
    optimizer = optimizer_class[0](lr=learning_rate, **optimizer_class[1])
    model.compile(optimizer=optimizer, loss=loss_func,metrics=['acc'])

    print(model.summary())
    plot_model(model,show_shapes=True,to_file="model.png")


    return model
Exemplo n.º 11
0
    def build_efficientNetSTN(self):
        inp = Input(shape=(self.config.model.img_width,
                           self.config.model.img_height, 3),
                    name='main_input')
        detedction_window_w = int(0.5 * self.config.model.img_width)
        detedction_window_h = int(0.5 * self.config.model.img_height)
        base_model1 = EfficientNetB0(
            (detedction_window_w, detedction_window_h, 3),
            include_top=False,
            weights='imagenet')
        base_model2 = EfficientNetB0(
            (detedction_window_w, detedction_window_h, 3),
            include_top=False,
            weights='imagenet')

        base_model_loc = EfficientNetB0(
            (detedction_window_w, detedction_window_h, 3),
            include_top=False,
            weights='imagenet')

        inp_downsize = AvgPool2D(pool_size=(2, 2))(inp)
        locnet = base_model_loc(inp_downsize)
        locnet = Conv2D(128, (1, 1),
                        activation='relu',
                        name='loc2',
                        kernel_regularizer=self.regularizer)(locnet)
        locnet = Flatten()(locnet)
        locnet = Dense(128,
                       activation='relu',
                       name='loc3',
                       kernel_regularizer=self.regularizer)(locnet)
        weights = get_initial_weights_translation_only(128)
        locnet = Dense(4,
                       weights=weights,
                       name='loc4',
                       kernel_regularizer=self.regularizer)(locnet)

        # locnet = MaxPool2D(pool_size=(2, 2))(inp)
        # locnet = Conv2D(100, (5, 5),activation='relu', name='loc1')(locnet)  # 20
        # locnet = MaxPool2D(pool_size=(4, 4))(locnet)  # 2, 2
        # locnet = Conv2D(200, (5, 5), activation='relu',name='loc2')(locnet)  # 20
        # # locnet = MaxPool2D(pool_size=(2, 2))(locnet)
        # locnet = Flatten()(locnet)
        # locnet = Dense(20, name='loc3')(locnet)  # 50
        # locnet = Activation('relu')(locnet)
        # weights = get_initial_weights_translation_only(20)  # 50
        # locnet = Dense(4, weights=weights, name='loc4')(locnet)

        self.STN_id = 1
        transform1 = Lambda(self.convert_to_transform_matrix)([locnet, inp])
        self.STN_id = 2
        transform2 = Lambda(self.convert_to_transform_matrix)([locnet, inp])

        stn1 = BilinearInterpolation(
            (detedction_window_w, detedction_window_h),
            name='stn1')([inp, transform1])
        stn2 = BilinearInterpolation(
            (detedction_window_w, detedction_window_h),
            name='stn2')([inp, transform2])
        x1 = base_model1(stn1)
        x2 = base_model2(stn2)
        embeddings1 = GlobalAveragePooling2D(name='embeddings1')(x1)
        embeddings2 = GlobalAveragePooling2D(name='embeddings2')(x2)
        embeddings = concatenate([embeddings1, embeddings2],
                                 axis=1,
                                 name='embeddings')
        x = Dense(int(self.config.data_loader.num_of_classes), )(embeddings)
        out = Activation("softmax", name='out')(x)

        # if self.config.model.num_of_fc_at_net_end == 2:
        #     x = GlobalAveragePooling2D(name='gpa_f')(x)
        #     x = Dropout(0.5)(x)
        #     embeddings = Dense(int(self.config.model.embedding_dim), name='embeddings')(x)
        #     x = Dense(int(self.config.data_loader.num_of_classes), )(embeddings)
        #     out = Activation("softmax", name='out')(x)
        # else:
        #     embeddings = GlobalAveragePooling2D(name='embeddings')(x)
        #     x = Dense(int(self.config.data_loader.num_of_classes), )(embeddings)
        #     out = Activation("softmax", name='out')(x)

        self.model = Model(
            inputs=inp,
            outputs=[embeddings, out, transform1, transform2, stn1, stn2])
# -*- coding: utf-8 -*-
"""
@author: 代码医生工作室 
@公众号:xiangyuejiqiren   (内有更多优秀文章及学习资料)
@来源: <TensorFlow项目实战2.x>配套代码 
@配套代码技术支持:bbs.aianaconda.com  
"""
import numpy as np
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions, preprocess_input
from keras_efficientnets import EfficientNetB0
# from keras_efficientnets import EfficientNetB4

#6.7.10节 动态图
#tf.enable_eager_execution()						#启动动态图
#model = EfficientNetB4(weights='efficientnet-b4.h5')
model = EfficientNetB0(weights='efficientnet-b0.h5')

img_path = 'dog.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])

#6.7.10节 动态图
#preds = model(x)
#print('Predicted:', decode_predictions(preds.numpy(), top=3)[0])
    def model_confirm(self, choosed_model):
        if choosed_model == 'VGG16':
            model = MODEL(self.config).VGG16()
        elif choosed_model == 'VGG19':
            model = MODEL(self.config).VGG19()
        elif choosed_model == 'AlexNet':
            model = MODEL(self.config).AlexNet()
        elif choosed_model == 'LeNet':
            model = MODEL(self.config).LeNet()
        elif choosed_model == 'ZF_Net':
            model = MODEL(self.config).ZF_Net()
        elif choosed_model == 'ResNet18':
            model = ResnetBuilder().build_resnet18(self.config)
        elif choosed_model == 'ResNet34':
            model = ResnetBuilder().build_resnet34(self.config)
        elif choosed_model == 'ResNet101':
            model = ResnetBuilder().build_resnet101(self.config)
        elif choosed_model == 'ResNet152':
            model = ResnetBuilder().build_resnet152(self.config)
        elif choosed_model == 'mnist_net':
            model = MODEL(self.config).mnist_net()
        elif choosed_model == 'TSL16':
            model = MODEL(self.config).TSL16()
        elif choosed_model == 'ResNet50':
            model = keras.applications.ResNet50(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classNumber)
        elif choosed_model == 'InceptionV3':
            model = keras.applications.InceptionV3(
                include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=(self.normal_size, self.normal_size,
                             self.channles),
                pooling='max',
                classes=self.classNumber)

        elif choosed_model == 'Xception':
            model = keras.applications.Xception(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classNumber)
        elif choosed_model == 'MobileNet':
            model = keras.applications.MobileNet(include_top=True,
                                                 weights=None,
                                                 input_tensor=None,
                                                 input_shape=(self.normal_size,
                                                              self.normal_size,
                                                              self.channles),
                                                 pooling='max',
                                                 classes=self.classNumber)
        elif choosed_model == 'InceptionResNetV2':
            model = keras.applications.InceptionResNetV2(
                include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=(self.normal_size, self.normal_size,
                             self.channles),
                pooling='max',
                classes=self.classNumber)
        elif choosed_model == 'SEResNetXt':
            model = SEResNetXt(self.config).model

        elif choosed_model == 'DenseNet':
            depth = 40
            nb_dense_block = 3
            growth_rate = 12
            nb_filter = 12
            bottleneck = False
            reduction = 0.0
            dropout_rate = 0.0

            img_dim = (self.channles, self.normal_size
                       ) if K.image_data_format == 'channels_last' else (
                           self.normal_size, self.normal_size, self.channles)

            model = densenet.DenseNet(img_dim,
                                      classNumber=self.classNumber,
                                      depth=depth,
                                      nb_dense_block=nb_dense_block,
                                      growth_rate=growth_rate,
                                      nb_filter=nb_filter,
                                      dropout_rate=dropout_rate,
                                      bottleneck=bottleneck,
                                      reduction=reduction,
                                      weights=None)

        elif choosed_model == 'SENet':
            model = sm.Unet('senet154',
                            input_shape=(self.normal_size, self.normal_size,
                                         self.channles),
                            classes=4,
                            activation='softmax',
                            encoder_weights=None)

            #model.summary()

        elif choosed_model == 'EfficientNetB5':
            model = EfficientNetB5(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB4':
            model = EfficientNetB4(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB3':
            model = EfficientNetB3(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB2':
            model = EfficientNetB2(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB1':
            model = EfficientNetB1(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB0':
            model = EfficientNetB0(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'MobileNetV3_Large':
            model = MobileNetV3_Large(shape=(self.normal_size,
                                             self.normal_size, self.channles),
                                      n_class=4).build()

        elif choosed_model == 'MobileNetV3_Small':
            model = MobileNetV3_Small(shape=(self.normal_size,
                                             self.normal_size, self.channles),
                                      n_class=4).build()

        elif choosed_model == 'NASNetLarge':
            model = NASNetLarge(input_shape=(self.normal_size,
                                             self.normal_size, self.channles),
                                weights=None,
                                use_auxiliary_branch=False,
                                classes=4)

        elif choosed_model == 'NASNetMobile':
            model = NASNetMobile(input_shape=(self.normal_size,
                                              self.normal_size, self.channles),
                                 weights=None,
                                 use_auxiliary_branch=False,
                                 classes=4)

        elif choosed_model == 'NASNetMiddle':
            model = NASNetMiddle(input_shape=(self.normal_size,
                                              self.normal_size, self.channles),
                                 weights=None,
                                 use_auxiliary_branch=False,
                                 classes=4)

        elif choosed_model == 'ShuffleNet':
            model = ShuffleNet(input_shape=(self.normal_size, self.normal_size,
                                            self.channles),
                               classes=4)

        elif choosed_model == 'ShuffleNetV2':
            model = ShuffleNetV2(input_shape=(self.normal_size,
                                              self.normal_size, self.channles),
                                 classes=4)

        return model
Exemplo n.º 14
0
img_path = x[0]
user = x[1]

img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (96, 96))
img = img / 255.
img = np.reshape(img, [1, 96, 96, 3])

vgg_model = load_model("ml_models/vgg16_2_model.hdf5")
vgg_prob = vgg_model.predict(img)

################################################################################################################################

effb0_model = EfficientNetB0(include_top=False,
                             weights='ml_models/efficientnet-b0_notop.h5',
                             pooling='avg',
                             input_shape=(96, 96, 3))
x = effb0_model.output

x = Dense(32)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(0.25)(x)

x = Dense(16)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(0.25)(x)

x = Dense(8)(x)
x = BatchNormalization()(x)
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from keras import backend as K
from PIL import Image
img_width, img_height = 224, 224
"""
if K.image_data_format() == 'channels_first':
    input_shape = (1, img_width, img_height)
else:
    input_shape = (img_width, img_height, 1)
    
"""
from keras_efficientnets import EfficientNetB0

model = EfficientNetB0(include_top=False,
                       weights='imagenet',
                       pooling='avg',
                       input_shape=(224, 224, 3))

for layer in model.layers[0:-1]:
    layer.trainable = True

x = model.output

x = Dense(64)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(0.3)(x)

x = Dense(32)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
Exemplo n.º 16
0
    preprocessing_function=preprocess_input,
    # Below are the parameters used for crop
    # zoom_range=[0.8, 1.2],
    # width_shift_range=[-50,50]
    # Below are the parameters used for no crop
    zoom_range=[0.7, 1.3],
    # width shift means up and down
    width_shift_range=0.2,
    height_shift_range=0.2)

train_generator = train_datagen.flow_from_directory(TRAIN_DIR,
                                                    target_size=(HEIGHT,
                                                                 WIDTH),
                                                    batch_size=BATCH_SIZE)
base_model = EfficientNetB0(input_shape=(HEIGHT, WIDTH, 3),
                            include_top=False,
                            weights='imagenet')
# base_model = ResNet50(weights='imagenet',
#                       include_top=False,
#                       input_shape=(HEIGHT, WIDTH, 3))
base_output = base_model.layers[-1].output
# base_output = base_model.layers[5].output
# base_output = Flatten()(base_output)
base_model = Model(base_model.input, output=base_output)

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

# base_model.trainable = True
# set_trainable = False
# for layer in base_model.layers:
Exemplo n.º 17
0
train_labels = objCount.tolist()
#train_labels
#print(len(train_labels)) # 116

from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
train_labels

#!pip install -U efficientnet
!pip install keras_efficientnets

import keras
from keras_efficientnets import EfficientNetB0

new_input = keras.Input(shape=(400, 320, 3))
base_model = EfficientNetB0(weights="imagenet", include_top=False, input_tensor=new_input)
print(base_model.summary())

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical 

model = Sequential()
model.add(base_model)
base_model.trainable = False
#model.add(Dense(units = 120, activation='relu'))
#model.add(Dense(units = 120, activation = 'relu'))
model.add(Dense(units = 1, activation='relu'))
model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['accuracy'])
#model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
Exemplo n.º 18
0
def train(epoch, FRAME_NUM, _num, batch_size, _random=False):
    FRAME_NUM = FRAME_NUM
    HEIGHT = 224
    WIDTH = 224
    CHANNEL = 3

    base = EfficientNetB0(input_shape=(224, 224, 3),
                          include_top=False,
                          weights='imagenet')
    base_ = Model(base.inputs, base.outputs)

    # base = load_model('../models/nt/efficientb0-weights-improvement-027-0.9912.hdf5')
    # base_ = Model(base.get_layer('model_1').inputs, base.get_layer('model_1').outputs)

    seq_input = Input((FRAME_NUM, HEIGHT, WIDTH, CHANNEL))
    x = TimeDistributed(base_)(seq_input)

    # x = DepthwiseConv3D(kernel_size=(3,3,3),strides = (1,1,1),depth_multiplier=1)(x)
    # x = LeakyReLU(0.1)(x)
    # x = DepthwiseConv3D(kernel_size=(4,3,3),strides = (1,1,1),depth_multiplier=1)(x)
    # x = LeakyReLU(0.1)(x)
    # x = DepthwiseConv3D(kernel_size=(3,3,3), depth_multiplier=1)(x)
    # x = LeakyReLU(0.1)(x)
    # x = GlobalAveragePooling3D()(x)
    # x = LeakyReLU(0.1)(x)

    # base method
    x = Lambda(feature_reset)(x)
    x = TimeDistributed(BatchNormalization())(x)
    x = TimeDistributed(Conv2D(filters=128, kernel_size=(3, 3)))(x)
    x = TimeDistributed(BatchNormalization())(x)
    x = LeakyReLU(0.1)(x)
    x = TimeDistributed(Conv2D(filters=64, kernel_size=(3, 3)))(x)
    x = LeakyReLU(0.1)(x)
    x = TimeDistributed(Conv2D(filters=1, kernel_size=(3, 3)))(x)
    x = LeakyReLU(0.1)(x)
    ##

    # x = Flatten()(x)
    x = Dense(1, activation='sigmoid')(x)

    model_ = Model(inputs=seq_input, outputs=x, name='FJWnet')

    multi_model = multi_gpu_model(model_, gpus=4)

    model_.summary()
    multi_model.compile(loss='binary_crossentropy',
                        metrics=['accuracy'],
                        optimizer=SGD(0.01, momentum=0.9,
                                      decay=0.0095 / epoch))

    train_gen = make_videoclips_4dfdc(model='train',
                                      batch_size=batch_size,
                                      FRAME_NUM=FRAME_NUM,
                                      _random=_random)
    test_gen = make_videoclips_4dfdc(model='test',
                                     batch_size=batch_size,
                                     FRAME_NUM=FRAME_NUM,
                                     _random=_random)

    if _random == False:
        filepath = r"../models/NewIdea/dfdc_f=" + _num + r"/efficientb0-weights-improvement-{epoch:03d}-{val_acc:.4f}.hdf5"
    else:
        filepath = r"../models/NewIdea/dfdc_f=" + _num + r"/random_efficientb0-weights-improvement-{epoch:03d}-{val_acc:.4f}.hdf5"
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_acc',
                                 verbose=1,
                                 save_best_only=True,
                                 save_weights_only=False,
                                 mode='auto')

    Checkpoint = ParallelModelCheckpoint(model_,
                                         filepath,
                                         monitor='val_acc',
                                         verbose=1,
                                         save_best_only=True,
                                         save_weights_only=False,
                                         mode='auto')

    history = multi_model.fit_generator(train_gen,
                                        class_weight={
                                            0: 1.,
                                            1: 1.
                                        },
                                        steps_per_epoch=100,
                                        validation_steps=200,
                                        validation_data=test_gen,
                                        epochs=epoch,
                                        verbose=1,
                                        callbacks=[Checkpoint])

    if _random == False:
        WriteIN(
            history, r'../records/dfdc_efficientb0-new_' + _num +
            r'_frames_224_0-40.txt')
    else:
        WriteIN(
            history, r'../records/dfdc_efficientb0-shuffled_new_' + _num +
            r'_frames_224_0-40.txt')