def build_efficientNet(self):
        base_model = EfficientNetB3(
            (self.config.model.img_width, self.config.model.img_height, 3),
            include_top=False,
            weights='imagenet')
        inp = Input(shape=(self.config.model.img_width,
                           self.config.model.img_height, 3),
                    name='main_input')
        x = base_model(inp)

        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])
Exemple #2
0
def model():
    convnet = EfficientNetB3(
        weights="imagenet",
        include_top=False,
        input_shape=(config.IMAGE_SIZE, config.IMAGE_SIZE, 3),
    )
    
    # custom Layers
    out = convnet.output
    out = Flatten()(out)
    out = Dense(256, activation="relu")(out)
    out = Dropout(0.5)(out)
    out = Dense(128, activation="relu")(out)
    out = Dropout(0.5)(out)
    out = Dense(64, activation="relu")(out)
    out = Dropout(0.5)(out)
    out = Dense(32, activation="relu")(out)
    out = Dropout(0.5)(out)
    out = Dense(16, activation="relu")(out)
    predictions = Dense(1, activation="sigmoid")(out)

    # creating the final model
    model = Model(inputs=convnet.input, outputs=predictions)

    # compile the model
    model.compile(
        loss="binary_crossentropy",
        optimizer=optimizers.SGD(lr=0.0001, momentum=0.9),
        metrics=["accuracy"])

    return model
Exemple #3
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)
Exemple #4
0
def efficientnet(in_shape, num_classes=4, dense_blocks=[64]):
    base_model = EfficientNetB3(in_shape,
                                classes=num_classes,
                                include_top=False,
                                weights=None)
    x = base_model.output

    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(num_classes, activation='softmax', name='final_output')(x)
    model = Model(inputs=base_model.input, outputs=x)
    return model
Exemple #5
0
def create_model(args, label_count):
    if args.base_model_name == 'nasnetlarge':
        base_model = NASNetLarge(weights='imagenet',
                                 input_shape=(args.width, args.width, 3),
                                 include_top=False,
                                 pooling='avg')
        preprocess_func = nasnet.preprocess_input
    elif args.base_model_name == 'mobilenetv2':
        base_model = MobileNetV2(weights='imagenet',
                                 input_shape=(args.width, args.width, 3),
                                 include_top=False,
                                 pooling='avg')
        preprocess_func = mobilenet_v2.preprocess_input
    elif args.base_model_name == 'xception':
        base_model = Xception(weights='imagenet',
                              input_shape=(args.width, args.width, 3),
                              include_top=False,
                              pooling='avg')
        preprocess_func = xception.preprocess_input
    elif args.base_model_name == 'inceptionv3':
        base_model = InceptionV3(weights='imagenet',
                                 input_shape=(args.width, args.width, 3),
                                 include_top=False,
                                 pooling='avg')
        preprocess_func = inception_v3.preprocess_input
    elif args.base_model_name == 'efficientnet':
        base_model = EfficientNetB3(weights='imagenet',
                                    input_shape=(args.width, args.width, 3),
                                    include_top=False)
        preprocess_func = efficientnet.preprocess_input
    else:
        raise Exception('Unsupported base model: %s' % base_model_name)

    input_tensor = Input((args.width, args.width, 3))
    print("input_tensor", input_tensor)
    x = input_tensor
    x = Lambda(preprocess_func)(x)
    x = base_model(x)
    if args.base_model_name == 'efficientnet':
        x = GlobalAveragePooling2D(data_format='channels_last')(x)
    x = Dropout(0.5)(x)
    x = [
        Dense(count, activation='softmax', name=name)(x)
        for name, count in label_count.items()
    ]
    model = Model(input_tensor, x)
    return model
Exemple #6
0
batch_size = 16
input_dimensions = (300, 300, 3)
num_epochs = 1000000
patience = 100
num_classes = len(os.listdir(train_dir))
num_training_samples = sum([
    len(os.listdir(os.path.join(train_dir, cat_train_dir)))
    for cat_train_dir in os.listdir(train_dir)
])
num_valid_samples = sum([
    len(os.listdir(os.path.join(valid_dir, cat_valid_dir)))
    for cat_valid_dir in os.listdir(valid_dir)
])

conv_base = EfficientNetB3(input_dimensions,
                           include_top=False,
                           weights='imagenet')
conv_base.summary()

model = models.Sequential()
model.add(conv_base)
model.add(layers.GlobalMaxPooling2D())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes, activation='softmax'))
model.summary()

train_datagen = ImageDataGenerator(rotation_range=20.,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   zoom_range=0.2,
                                   horizontal_flip=True)
Exemple #7
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)
    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