def build_model(self):
     if (self.tl):
         base_model = efn.EfficientNetB0(weights="imagenet",input_shape=(self.input_shape[0],self.input_shape[1],3), include_top=False)
     else:
         base_model = efn.EfficientNetB0(weights=None,
                                         input_shape=(self.input_shape[0], self.input_shape[1], 3),
                                         include_top=False)
     output = keras.layers.GlobalAveragePooling2D()(base_model.output)
     output = keras.layers.Dense(32, activation='relu')(output)
     # Nueva capa de salida
     output = keras.layers.Dense(self.classes, activation='softmax')(output)  # cambiar cantidad de clases
     model = Model(inputs=base_model.input, outputs=output)
     # Entrenar con nuevos datos
     model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
     return model
Exemplo n.º 2
0
def load_efficientnet():
    model = efn.EfficientNetB0(weights='imagenet',
                               include_top=False,
                               input_tensor=Input(shape=(config.IMAGE_HEIGHT,
                                                         config.IMAGE_WIDTH,
                                                         3)))
    return model
Exemplo n.º 3
0
def effcient0_unet(input_shape, filters=512, out_dim=4, act="sigmoid"):
    model = efn.EfficientNetB0(include_top=False,
                               input_shape=input_shape,
                               weights=None)
    blockid = [
        'block2a_expand_activation', 'block3a_expand_activation',
        'block4a_expand_activation', 'block6a_expand_activation',
        'block7a_project_bn'
    ]
    block_outputs = []
    for i in range(len(blockid)):
        blockout = model.get_layer(blockid[i]).output
        block_outputs.append(blockout)

    input = model.input
    inputc = Conv2D(16, (3, 3), padding="same", activation="relu")(input)
    x = block_outputs.pop()
    for i in range(len(blockid) - 1):
        x = transconv_block(x,
                            int(filters / (2**i)),
                            3,
                            2,
                            skip=block_outputs.pop())
    x = transconv_block(x, 16, 3, 2, skip=inputc)
    output_layer = Conv2D(out_dim, (1, 1), padding="same", activation=act)(x)
    model = Model(input, output_layer)
    return model
def get_model(model='b2', shape=(320,320)):
    K.clear_session()
    h,w = shape
    if model == 'b0':
        base_model = efn.EfficientNetB0(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    elif model == 'b1':
        base_model = efn.EfficientNetB1(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    elif model == 'b2':
        base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    elif model == 'b3':
        base_model =  efn.EfficientNetB3(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    elif model == 'b4':
        base_model =  efn.EfficientNetB4(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    elif model == 'b5':
        base_model =  efn.EfficientNetB5(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    elif model == 'b6':
        base_model =  efn.EfficientNetB6(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))

    else:
        base_model =  efn.EfficientNetB7(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3))


    x = base_model.output
    y_pred = Dense(4, activation='sigmoid')(x)
    return Model(inputs=base_model.input, outputs=y_pred)
Exemplo n.º 5
0
def model_fn(FLAGS, objective, optimizer, metrics):

    # base_model = efn.EfficientNetB3(include_top=False,
    #                                 input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
    #                                 classes=FLAGS.num_classes, )
    # # input_size =  380
    model = efn.EfficientNetB0(
        include_top=False,
        input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
        classes=FLAGS.num_classes,
    )
    # # input_size =  456
    # base_model = efn.EfficientNetB5(include_top=False,
    #                                 input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
    #                                 classes=FLAGS.num_classes, )

    # # input_size =  528
    # base_model = efn.EfficientNetB6(include_top=False,
    #                                 input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
    #                                 classes=FLAGS.num_classes, )

    x = model.output
    # 插入双线性池化操作
    # x = bilinear_pooling(x)

    # x = GlobalAveragePooling2D()(x)
    # x = Dropout(0.2)(x)
    predictions = Dense(FLAGS.num_classes, activation='softmax')(
        x)  # activation="linear",activation='softmax'
    model = Model(input=model.input, output=predictions)

    p_model = multi_gpu_model(model, gpus=2)
    p_model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
    p_model.summary()
    return model, p_model
Exemplo n.º 6
0
    def build_basicRegressionCNN(self):
        """Builds basic convolution-only model  with MSE loss function
        -you dont need dense connections - thats what inception thought us

        Returns:
            keras.models.Model

        :return:
        """
        print("build_basicCNN input_shape:" + str(self.input_shape))
        rgb_efficientNetB0 = efn.EfficientNetB0(include_top=False,
                                                weights='imagenet',
                                                input_shape=self.input_shape,
                                                classes=1)
        z = rgb_efficientNetB0.output
        z = GlobalMaxPooling2D()(z)
        z = Dense(1, activation='linear')(z)
        model = Model(inputs=rgb_efficientNetB0.input, outputs=z)

        optimizer = optimizers.Adam(lr=0.001, decay=0)
        # optimizer = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=None, decay=0.0)
        model.compile(optimizer=optimizer,
                      loss="MSE",
                      metrics=["MSE", "accuracy"])
        print("BasicCNN model built as child model.\n Model summary:")
        print(model.summary())
        return model
Exemplo n.º 7
0
def get_efficientnet_model(
    model_name='efficientnetb0',
    input_shape=(224, 224, 3),
    input_tensor=None,
    include_top=True,
    classes=1000,
    weights='imagenet',
):

    layer_names = [
        'block3a_expand_activation',  #C2
        'block4a_expand_activation',  #C3
        'block6a_expand_activation',  #C4
        'top_activation'  #C5
    ]

    Args = {
        'input_shape': input_shape,
        'weights': weights,
        'include_top': include_top,
        'input_tensor': input_tensor
    }

    if model_name == 'efficientnetb0':
        backbone = efn.EfficientNetB0(**Args)

    elif model_name == 'efficientnetb1':
        backbone = efn.EfficientNetB1(**Args)

    elif model_name == 'efficientnetb2':
        backbone = efn.EfficientNetB2(**Args)

    elif model_name == 'efficientnetb3':
        backbone = efn.EfficientNetB3(**Args)

    elif model_name == 'efficientnetb4':
        backbone = efn.EfficientNetB4(**Args)

    elif model_name == 'efficientnetb5':
        backbone = efn.EfficientNetB5(**Args)

    elif model_name == 'efficientnetb6':
        backbone = efn.EfficientNetB6(**Args)

    elif model_name == 'efficientnetb7':
        backbone = efn.EfficientNetB7(**Args)

    else:
        raise ValueError('No such model {}'.format(model_name))

    several_layers = []

    several_layers.append(backbone.get_layer(layer_names[0]).output)
    several_layers.append(backbone.get_layer(layer_names[1]).output)
    several_layers.append(backbone.get_layer(layer_names[2]).output)
    several_layers.append(backbone.get_layer(layer_names[3]).output)

    model = keras.models.Model(inputs=[backbone.input], outputs=several_layers)
    return model
def effnet_retinanet(num_classes, backbone='EfficientNetB0', inputs=None, modifier=None, **kwargs):
    """ Constructs a retinanet model using a resnet backbone.

    Args
        num_classes: Number of classes to predict.
        backbone: Which backbone to use (one of ('resnet50', 'resnet101', 'resnet152')).
        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 ResNet backbone.
    """
    # choose default input
    if inputs is None:
        if keras.backend.image_data_format() == 'channels_first':
            inputs = keras.layers.Input(shape=(3, None, None))
        else:
            # inputs = keras.layers.Input(shape=(224, 224, 3))
            inputs = keras.layers.Input(shape=(None, None, 3))

    # get last conv layer from the end of each block [28x28, 14x14, 7x7]
    if backbone == 'EfficientNetB0':
        model = efn.EfficientNetB0(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB1':
        model = efn.EfficientNetB1(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB2':
        model = efn.EfficientNetB2(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB3':
        model = efn.EfficientNetB3(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB4':
        model = efn.EfficientNetB4(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB5':
        model = efn.EfficientNetB5(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB6':
        model = efn.EfficientNetB6(input_tensor=inputs, include_top=False, weights=None)
    elif backbone == 'EfficientNetB7':
        model = efn.EfficientNetB7(input_tensor=inputs, include_top=False, weights=None)
    else:
        raise ValueError('Backbone (\'{}\') is invalid.'.format(backbone))

    layer_outputs = ['block4a_expand_activation', 'block6a_expand_activation', 'top_activation']

    layer_outputs = [
        model.get_layer(name=layer_outputs[0]).output,  # 28x28
        model.get_layer(name=layer_outputs[1]).output,  # 14x14
        model.get_layer(name=layer_outputs[2]).output,  # 7x7
    ]
    # create the densenet backbone
    model = keras.models.Model(inputs=inputs, outputs=layer_outputs, name=model.name)

    # invoke modifier if given
    if modifier:
        model = modifier(model)

    # create the full model
    return retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=model.outputs, **kwargs)
Exemplo n.º 9
0
    def __init__(self):
        self.earlystop_callback = EarlyStopping(monitor='accuracy',
                                                min_delta=0.001,
                                                patience=2)

        self.base_model = efn.EfficientNetB0(input_shape=(224, 224, 3),
                                             include_top=False,
                                             weights='imagenet')
        for layer in self.base_model.layers:
            layer.trainable = False
Exemplo n.º 10
0
    def create_effnet_model(self):
        base_model = efn.EfficientNetB0(weights='imagenet',
                                        include_top=False,
                                        pooling='avg',
                                        input_shape=(self.input_dims[0],
                                                     self.input_dims[0], 3))
        x = base_model.output
        x = keras.layers.Dropout(0.05)(x)
        out = keras.layers.Dense(6, activation="sigmoid",
                                 name='dense_output')(x)

        return keras.models.Model(inputs=base_model.input, outputs=out)
Exemplo n.º 11
0
def construct_mlp(input_size, num_classes, num_frames,
                  dropout_size=0.5, ef_mode=4, l2_reg=1e-5):
    """
    Construct a MLP model for urban sound tagging.
    Parameters
    ----------
    num_frames
    input_size
    num_classes
    dropout_size
    ef_mode
    l2_reg
    Returns
    -------
    model
    """

    # Add hidden layers
    from keras.layers import Flatten, Conv1D, Conv2D, GlobalMaxPooling1D, GlobalAveragePooling1D, LSTM, Concatenate, GlobalAveragePooling2D, LeakyReLU

    import efficientnet.keras as efn

    if ef_mode == 0:
        base_model = efn.EfficientNetB0(weights='noisy-student', include_top=False, pooling='avg')
    elif ef_mode == 1:
        base_model = efn.EfficientNetB1(weights='noisy-student', include_top=False, pooling='avg')
    elif ef_mode == 2:
        base_model = efn.EfficientNetB2(weights='noisy-student', include_top=False, pooling='avg')
    elif ef_mode == 3:
        base_model = efn.EfficientNetB3(weights='noisy-student', include_top=False, pooling='avg')
    elif ef_mode == 4:
        base_model = efn.EfficientNetB4(weights='noisy-student', include_top=False, pooling='avg')  #imagenet or weights='noisy-student'
    elif ef_mode == 5:
        base_model = efn.EfficientNetB5(weights='noisy-student', include_top=False, pooling='avg')
    elif ef_mode == 6:
        base_model = efn.EfficientNetB6(weights='noisy-student', include_top=False, pooling='avg')
    elif ef_mode == 7:
        base_model = efn.EfficientNetB7(weights='noisy-student', include_top=False, pooling='avg')

    input1 = Input(shape=input_size, dtype='float32', name='input')
    input2 = Input(shape=(num_frames,85), dtype='float32', name='input2') #1621
    y = TimeDistributed(base_model)(input1)
    y = TimeDistributed(Dropout(dropout_size))(y)
    y = Concatenate()([y, input2])
    y = TimeDistributed(Dense(num_classes, activation='sigmoid', kernel_regularizer=regularizers.l2(l2_reg)))(y)
    y = AutoPool1D(axis=1, name='output')(y)

    m = Model(inputs=[input1, input2], outputs=y)
    m.summary()
    m.name = 'urban_sound_classifier'

    return m
Exemplo n.º 12
0
 def test_custom(self):
     from efficientnet import keras as efn
     keras.backend.set_learning_phase(0)
     base_model = efn.EfficientNetB0(input_shape=(600, 600, 3),
                                     weights=None)
     backbone = keras.Model(base_model.input,
                            base_model.get_layer("top_activation").output)
     res = run_image(backbone,
                     self.model_files,
                     img_path,
                     target_size=(600, 600),
                     rtol=1e-1)
     self.assertTrue(*res)
def get_model():
    "Returns a classifier model"
    efficient_net = efn.EfficientNetB0(
        include_top=False,
        weights="imagenet",
        input_shape=(input_image_size, input_image_size, 3),
    )

    x = efficient_net.output
    x = GlobalAveragePooling2D()(x)
    predictions = Dense(6, activation='sigmoid')(x)
    model = Model(inputs=efficient_net.input, outputs=predictions)

    return model
Exemplo n.º 14
0
  def __init__(self, num_classes, **kwargs):
    super(Model, self).__init__(**kwargs)
    if FLAGS.arch == 'resnet':
      self.resnet_model = resnet.resnet(
          resnet_depth=FLAGS.resnet_depth,
          width_multiplier=FLAGS.width_multiplier,
          cifar_stem=FLAGS.image_size <= 32)
    elif FLAGS.arch == 'efficientnet':
      import efficientnet.keras as efn 
      self.resnet_model = efn.EfficientNetB0(weights=None, include_top=False)

    self._projection_head = ProjectionHead()
    if FLAGS.train_mode == 'finetune' or FLAGS.lineareval_while_pretraining:
      self.supervised_head = SupervisedHead(num_classes)
Exemplo n.º 15
0
def get_b0():

    backbone = efn.EfficientNetB0(input_shape=(96, 96, 3), include_top=False,  pooling=None, classes=None, weights='imagenet')

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

    avg = GlobalAveragePooling2D()(backbone.output)

    grapheme_root_head = Dense(168, activation='softmax', name='grapheme_root')(avg)
    vowel_diacritic_head = Dense(11, activation='softmax', name='vowel_diacritic')(avg)
    consonant_diacritic_head = Dense(7, activation='softmax', name='consonant_diacritic')(avg)

    return Model(backbone.input, outputs=[grapheme_root_head, vowel_diacritic_head, consonant_diacritic_head])
Exemplo n.º 16
0
def build_efficientnet(height,
                       width,
                       depth,
                       include_top=False,
                       weights='imagenet'):
    efficientnet_model = efn.EfficientNetB0(weights=weights,
                                            include_top=include_top,
                                            input_shape=(height, width, depth))

    model = Sequential()
    model.add(efficientnet_model)
    model.add(GlobalAveragePooling2D())
    model.add(Dropout(0.5))
    model.add(Dense(2))
    model.add(Activation("softmax"))

    return model
Exemplo n.º 17
0
    def build(height, width, depth, num_classes):
        input_shape = (height, width, depth)
        chanDim = -1

        model = efn.EfficientNetB0(weights='imagenet',
                                   include_top=False,
                                   input_shape=input_shape)
        x = Flatten()(model.output)
        x = Dense(500)(x)
        x = Activation("relu")(x)
        #x = Dense(100)(x)
        #x = Activation("relu")(x)
        x = Dense(num_classes)(x)
        output = Activation("softmax")(x)
        model = Model(model.input, output, name='efficientnetb0')

        return model
def cnn_model(model_name, img_size, weights):
    """
    Model definition using Xception net architecture
    """
    input_size = (img_size, img_size, 3)

    if model_name == "efn0":
        baseModel = efn.EfficientNetB0(weights="imagenet", include_top=False,
            input_shape=input_size)
    elif model_name == "efn_noisy":
        baseModel = efn.EfficientNetB5(weights="noisy-student", include_top=False,
            input_shape=input_size)

    headModel = baseModel.output
    headModel = GlobalAveragePooling2D()(headModel)
    headModel = Dense(1024, activation="relu", kernel_initializer="he_uniform", name="fc1")(
        headModel
    )
    headModel = Dropout(0.4)(headModel)
    predictions = Dense(
        200,
        activation="softmax",
        kernel_initializer="he_uniform")(
        headModel
    )
    model = Model(inputs=baseModel.input, outputs=predictions)
    model.load_weights(weights)

    model_fc = Model(
        inputs=baseModel.input,
        outputs=model.get_layer("fc1").output
    )

    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",
        # loss=joint_loss,
        optimizer=optimizer,
        metrics=["accuracy"]
    )
    return model_fc
def build_model(input_shape):
    input_tensor = Input(shape=input_shape)
    base = efn.EfficientNetB0(weights='imagenet', include_top=False)

    for l in base.layers:
        l.trainable = True
    conv = base.output

    x = GlobalAveragePooling2D(name='pool1')(conv)
    x = BatchNormalization()(x)
    x = Dense(512, name='fc1')(x)
    x = Activation('relu', name='relu1')(x)
    x = BatchNormalization()(x)
    #x = Dropout(0.5)(x)
    x = Dense(4, name='fc3')(x)
    x = Activation('sigmoid', name='sigmoid')(x)
    model = Model(inputs=[base.input], outputs=[x])
    model.compile(optimizer=RectifiedAdam(lr=LR), loss='binary_crossentropy')
    return model
Exemplo n.º 20
0
def get_model_effnet(img_shape, img_input, weights, effnet_version):

    if effnet_version == 'B0':
        effnet = efn.EfficientNetB0(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    elif effnet_version == 'B1':
        effnet = efn.EfficientNetB1(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    elif effnet_version == 'B2':
        effnet = efn.EfficientNetB2(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    elif effnet_version == 'B3':
        effnet = efn.EfficientNetB3(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    elif effnet_version == 'B4':
        effnet = efn.EfficientNetB4(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    elif effnet_version == 'B5':
        effnet = efn.EfficientNetB5(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    elif effnet_version == 'B6':
        effnet = efn.EfficientNetB6(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)
    else:
        effnet = efn.EfficientNetB7(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape)

    return effnet
Exemplo n.º 21
0
def create_base_model(base_model_name, pretrained=True, IMAGE_SIZE=[300, 300]):
    if pretrained is False:
        weights = None
    else:
        weights = "imagenet"
    if base_model_name == 'B0':
        base = efn.EfficientNetB0(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B1':
        base = efn.EfficientNetB1(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B2':
        base = efn.EfficientNetB2(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B3':
        base = efn.EfficientNetB3(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B4':
        base = efn.EfficientNetB4(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B5':
        base = efn.EfficientNetB5(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B6':
        base = efn.EfficientNetB6(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    elif base_model_name == 'B7':
        base = efn.EfficientNetB7(weights=weights,
                                  include_top=False,
                                  input_shape=[*IMAGE_SIZE, 3])
    base = remove_dropout(base)
    base.trainable = True
    return base
Exemplo n.º 22
0
def make_model():
    """
    Creates an EfficientNet model with parameters specified `num_classes` outputs
    and `img_width` x `img_height` input shape.
    Args:
    Returns: an EfficientNet model with specified global parameters.
    """
    # create the base pre-trained model

    base_model = efn.EfficientNetB0(input_shape=(img_width, img_height, 3),
                                    include_top=False)
    # add a global spatial average pooling layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    predictions = Dense(num_classes, activation="softmax")(x)
    model = Model(inputs=base_model.input, outputs=predictions)

    model.compile(optimizer="adam",
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])

    return base_model, model
Exemplo n.º 23
0
    "models": keras.models,
    "utils": keras.utils
}

if args.network == "resnet101":
    zero = ResNet101(include_top=False,
                     weights=None if args.random_init else "imagenet",
                     input_tensor=input_tensor,
                     **model_kwargs)
    model = ResNet101(include_top=False,
                      weights=None if args.random_init else "imagenet",
                      input_tensor=input_tensor,
                      **model_kwargs)
elif args.network == "enb0":
    zero = en.EfficientNetB0(include_top=False,
                             weights=None if args.random_init else "imagenet",
                             input_tensor=input_tensor)
    model = en.EfficientNetB0(include_top=False,
                              weights=None if args.random_init else "imagenet",
                              input_tensor=input_tensor)
else:
    raise Error("Unknown network: " + args.network)

num_frozen = int(float(args.freeze_frac) * len(model.layers))

for l in model.layers[:num_frozen]:
    if type(l) != keras.layers.normalization.BatchNormalization:
        l.trainable = False

zero_features = zero.outputs[0]
model_features = model.outputs[0]
class SwishActivation(Activation):
    def __init__(self, activation, **kwargs):
        super(SwishActivation, self).__init__(activation, **kwargs)
        self.__name__ = 'swish_act'


def swish_act(x, beta=1):
    return (x * sigmoid(beta * x))


get_custom_objects().update({'swish_act': SwishActivation(swish_act)})

#______________________CLASSIFIER, Distance Models_____________________________
model = enet.EfficientNetB0(include_top=False,
                            input_shape=(70, 70, 3),
                            pooling='avg',
                            weights='efficientnet_b0.h5')
x = model.output
x = BatchNormalization()(x)
x = Dropout(0.4)(x)
x = Dense(512)(x)
x = BatchNormalization()(x)
x = Activation('swish_act')(x)
x = Dropout(0.5)(x)
x = Dense(256)(x)
x = Activation('swish_act')(x)
x = Dense(128)(x)
x = Activation('swish_act')(x)
x = Dense(64)(x)
x = Activation('swish_act')(x)
x = Dense(32)(x)
Exemplo n.º 25
0
print('loading model...')

import efficientnet.keras as efn
from keras.applications.imagenet_utils import decode_predictions
from efficientnet.keras import center_crop_and_resize, preprocess_input
import numpy as np

import keras.backend.tensorflow_backend as tb

model = efn.EfficientNetB0(weights='noisy-student')

image_size = model.input_shape[1]

print('model loading complete!')


def predict(image):

    x = center_crop_and_resize(image, image_size=image_size)
    image_modified = x
    x = preprocess_input(x)
    x = np.expand_dims(x, 0)

    # make prediction and decode
    tb._SYMBOLIC_SCOPE.value = True
    y = model.predict(x)
    res = decode_predictions(y)
    # collect result and the modified image
    data = {'res': res[0][0], 'new_image': image_modified}
    return data
Exemplo n.º 26
0
def do_train():
    base_dir = '/gpfs/gpfs0/deep/data/Otoliths_cod/codotoliths/'

    dirs = set()

    df_cod = pd.DataFrame(columns=['age', 'path'])

    max_dataset_size = 1029  #6330
    new_shape = (380, 380, 3)
    IMG_SHAPE = (380, 380)
    rb_imgs = np.empty(shape=(max_dataset_size, ) + new_shape)
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"
    tensorboard_path = './tensorboard_cod_age_softmax2'
    checkpoint_path = './checkpoints_cod_age_softmax2/cod_oto_efficientnetB4.{epoch:03d}-{val_loss:.2f}.hdf5'

    add_count = 0
    for filename in Path(base_dir).glob('**/*.JPG'):
        filepath = str(filename)
        dirname = os.path.dirname(filepath)
        if (dirname not in dirs):
            dirs.add(dirname)
            begin_age = filepath.find('age')
            age = filepath[begin_age + 3:begin_age + 5]
            age = int(age)
            pil_img = load_img(filepath,
                               target_size=IMG_SHAPE,
                               grayscale=False)
            array_img = img_to_array(pil_img, data_format='channels_last')
            rb_imgs[add_count] = array_img
            df_cod = df_cod.append({
                'age': age,
                'path': filepath
            },
                                   ignore_index=True)
            add_count += 1
            #print(add_count)

    a_batch_size = 12
    age = df_cod.age.values

    early_stopper = EarlyStopping(patience=20)
    train_datagen = ImageDataGenerator(
        zca_whitening=True,
        width_shift_range=0.,
        height_shift_range=0.,  #20,
        zoom_range=0.,
        rotation_range=360,
        horizontal_flip=False,
        vertical_flip=True,
        rescale=1. / 255)

    #EXPERIMENT_FOLDER_PATH = os.path.join(parent_dir_of_file, f"reports/experiments/{EXPERIMENT_NAME}")
    DEFAULT_CONFIG = {
        "model": "basiccnn",
        "method": "bayesian_optimization",
        "train_set_size": 1029,
        "opt_samples": 3,
        "opt_last_n_epochs": 3,
        "opt_initial_points": 10,
        "child_epochs": 50,
        "child_first_train_epochs": 0,
        "child_batch_size": 64,
        "pre_aug_weights_path": "pre_aug_weights.h5"
    }
    deepaug = DeepAugment(rb_imgs, age, config=DEFAULT_CONFIG)

    train_idx, val_idx, test_idx = train_validate_test_split(
        range(0, len(rb_imgs)))
    train_rb_imgs = np.empty(shape=(len(train_idx), ) + new_shape)
    train_age = []
    for i in range(0, len(train_idx)):
        train_rb_imgs[i] = rb_imgs[train_idx[i]]
        train_age.append(age[train_idx[i]])

    val_rb_imgs = np.empty(shape=(len(val_idx), ) + new_shape)
    val_age = []
    for i in range(0, len(val_idx)):
        val_rb_imgs[i] = rb_imgs[val_idx[i]]
        val_age.append(age[val_idx[i]])

    test_rb_imgs = np.empty(shape=(len(test_idx), ) + new_shape)
    test_age = []
    for i in range(0, len(test_idx)):
        test_rb_imgs[i] = rb_imgs[test_idx[i]]
        test_age.append(age[test_idx[i]])

    train_age = np.vstack(train_age)
    val_age = np.vstack(val_age)
    test_age = np.vstack(test_age)

    val_rb_imgs = np.multiply(val_rb_imgs, 1. / 255)
    test_rb_imgs = np.multiply(test_rb_imgs, 1. / 255)

    train_generator = train_datagen.flow(train_rb_imgs,
                                         train_age,
                                         batch_size=a_batch_size)

    efn.EfficientNetB0(weights='imagenet')
    rgb_efficientNetB4 = EfficientNetB4(include_top=False,
                                        weights='imagenet',
                                        input_shape=new_shape,
                                        classes=2)
    z = dense1_linear_output(rgb_efficientNetB4)
    scales = Model(inputs=rgb_efficientNetB4.input, outputs=z)

    learning_rate = 0.0001
    adam = optimizers.Adam(lr=learning_rate)

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

    scales.compile(loss='mse',
                   optimizer=adam,
                   metrics=['accuracy', 'mse', 'mape'])
    tensorboard, checkpointer = get_checkpoint_tensorboard(
        tensorboard_path, checkpoint_path)

    classWeight = None

    history_callback = scales.fit_generator(
        train_generator,
        steps_per_epoch=1600,
        epochs=150,
        callbacks=[early_stopper, tensorboard, checkpointer],
        validation_data=(val_rb_imgs, val_age),
        class_weight=classWeight)

    test_metrics = scales.evaluate(x=test_rb_imgs, y=test_age)
    print("test metric:" + str(scales.metrics_names))
    print("test metrics:" + str(test_metrics))

    print("precision, recall, f1")
    y_pred_test = scales.predict(test_rb_imgs, verbose=1)
    y_pred_test_bool = np.argmax(y_pred_test, axis=1)
    y_true_bool = np.argmax(test_age, axis=1)
    #np.argmax inverse of to_categorical
    argmax_test = np.argmax(test_age, axis=1)
    unique, counts = np.unique(argmax_test, return_counts=True)
    print("test ocurrence of each class:" + str(dict(zip(unique, counts))))

    print("cslassification_report")
    print(classification_report(y_true_bool, y_pred_test_bool))
    print("confusion matrix")
    print(str(confusion_matrix(y_true_bool, y_pred_test_bool)))
def create_model_efficientnet(existing='', is_halffeatures=True):
    if len(existing) == 0:
        print('Loading base model (efficientNetB0)..')

        # Encoder Layers
        base_model = efn.EfficientNetB0(weights='imagenet',
                                        include_top=False,
                                        input_shape=(None, None, 3))

        print('Base model loaded.')

        # Starting point for decoder
        base_model_output_shape = base_model.layers[
            -1].output.shape  #15, 20, 1280

        # Layer freezing?
        # mid_start = base_model.get_layer('activation_22')
        # for i in range(base_model.layers.index(mid_start)):
        #     base_model.layers[i].trainable = False
        for layer in base_model.layers:
            layer.trainable = True

        # Starting number of decoder filters
        if is_halffeatures:
            decode_filters = int(int(base_model_output_shape[-1]) / 2)
        else:
            decode_filters = int(base_model_output_shape[-1])

        # Define upsampling layer
        def upproject(tensor, filters, name, concat_with):
            up_i = BilinearUpSampling2D((2, 2),
                                        name=name + '_upsampling2d')(tensor)
            up_i = Concatenate(name=name + '_concat')(
                [up_i,
                 base_model.get_layer(concat_with).output])  # Skip connection
            up_i = Conv2D(filters=filters,
                          kernel_size=3,
                          strides=1,
                          padding='same',
                          name=name + '_convA')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            up_i = Conv2D(filters=filters,
                          kernel_size=3,
                          strides=1,
                          padding='same',
                          name=name + '_convB')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            return up_i

        # Decoder Layers
        decoder = Conv2D(filters=decode_filters,
                         kernel_size=1,
                         padding='same',
                         input_shape=base_model_output_shape,
                         name='conv2')(base_model.output)

        decoder = upproject(decoder,
                            int(decode_filters / 2),
                            'up1',
                            concat_with='block4a_dwconv')  #30, 40, 256
        decoder = upproject(decoder,
                            int(decode_filters / 4),
                            'up2',
                            concat_with='block3a_dwconv')  # 60, 80, 128
        decoder = upproject(decoder,
                            int(decode_filters / 8),
                            'up3',
                            concat_with='block2a_dwconv')  #120,  160, 64
        decoder = upproject(decoder,
                            int(decode_filters / 16),
                            'up4',
                            concat_with='stem_conv')  #240, 320, 64
        if False:
            decoder = upproject(decoder,
                                int(decode_filters / 32),
                                'up5',
                                concat_with='input_1')

        # Extract depths (final layer)
        conv3 = Conv2D(filters=1,
                       kernel_size=3,
                       strides=1,
                       padding='same',
                       name='conv3')(decoder)

        # Create the model
        model = Model(inputs=base_model.input, outputs=conv3)
    else:
        # Load model from file
        if not existing.endswith('.h5'):
            sys.exit(
                'Please provide a correct model file when using [existing] argument.'
            )
        custom_objects = {
            'BilinearUpSampling2D': BilinearUpSampling2D,
            'depth_loss_function': depth_loss_function
        }
        model = load_model(existing, custom_objects=custom_objects)
        print('\nExisting model loaded.\n')

    print('Model created.')

    return model
    msg = "Accuracy on test set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))

    if show_confusion_matrix:
        print("Confusion Matrix:")
        plot_test_confusion_matrix(cls_pred=cls_pred,
                                   labels_test=labels_test,
                                   classes=classes)

    return float(acc)


#%%

model = efn.EfficientNetB0(include_top=False,
                           input_shape=(128, 128, 1),
                           weights=None)

x = model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.7)(x)

predictions = Dense(3, activation="softmax")(x)

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

model_final.summary()

#%%
#Hyper Parameters
Exemplo n.º 29
0
def UNetPlusPlus(img_rows,
                 img_cols,
                 color_type=3,
                 num_class=1,
                 deep_supervision=True):
    nb_filter = [32, 64, 128, 256, 512]
    import efficientnet.keras as efn
    from keras.models import Model
    from keras.layers.convolutional import Conv2D
    from keras.layers import LeakyReLU, Add, Input, MaxPool2D, UpSampling2D, concatenate, Conv2DTranspose, \
        BatchNormalization, Dropout
    base_model = efn.EfficientNetB0(weights='imagenet',
                                    include_top=False,
                                    input_shape=(224, 224, 3))
    input_model = base_model.input
    # Handle Dimension Ordering for different backends
    global bn_axis
    bn_axis = 3
    conv6_1 = base_model.get_layer('top_activation').output
    conv5_1 = base_model.get_layer('block6a_expand_activation').output
    conv4_1 = base_model.get_layer('block4a_expand_activation').output
    conv3_1 = base_model.get_layer('block3a_expand_activation').output
    conv2_1 = base_model.get_layer('block2a_expand_activation').output

    print(conv6_1.shape)  # 7*7
    print(conv5_1.shape)  # 14*14
    print(conv4_1.shape)  # 28*28
    print(conv3_1.shape)  # 56*56
    print(conv2_1.shape)  # 112*112

    conv1_1 = standard_unit(input_model, stage='11', nb_filter=nb_filter[0])
    # pool1 = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(conv1_1)

    # conv2_1 = standard_unit(pool1, stage='21', nb_filter=nb_filter[1])
    # pool2 = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(conv2_1)

    up1_2 = Conv2DTranspose(nb_filter[0], (2, 2),
                            strides=(2, 2),
                            name='up12',
                            padding='same')(conv2_1)
    conv1_2 = concatenate([up1_2, conv1_1], name='merge12', axis=bn_axis)
    conv1_2 = standard_unit(conv1_2, stage='12', nb_filter=nb_filter[0])

    #     conv3_1 = standard_unit(pool2, stage='31', nb_filter=nb_filter[2])
    #     pool3 = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(conv3_1)

    up2_2 = Conv2DTranspose(nb_filter[1], (2, 2),
                            strides=(2, 2),
                            name='up22',
                            padding='same')(conv3_1)
    conv2_2 = concatenate([up2_2, conv2_1], name='merge22', axis=bn_axis)
    conv2_2 = standard_unit(conv2_2, stage='22', nb_filter=nb_filter[1])

    up1_3 = Conv2DTranspose(nb_filter[0], (2, 2),
                            strides=(2, 2),
                            name='up13',
                            padding='same')(conv2_2)
    conv1_3 = concatenate([up1_3, conv1_1, conv1_2],
                          name='merge13',
                          axis=bn_axis)
    conv1_3 = standard_unit(conv1_3, stage='13', nb_filter=nb_filter[0])

    #     conv4_1 = standard_unit(pool3, stage='41', nb_filter=nb_filter[3])
    #     pool4 = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(conv4_1)

    up3_2 = Conv2DTranspose(nb_filter[2], (2, 2),
                            strides=(2, 2),
                            name='up32',
                            padding='same')(conv4_1)
    conv3_2 = concatenate([up3_2, conv3_1], name='merge32', axis=bn_axis)
    conv3_2 = standard_unit(conv3_2, stage='32', nb_filter=nb_filter[2])

    up2_3 = Conv2DTranspose(nb_filter[1], (2, 2),
                            strides=(2, 2),
                            name='up23',
                            padding='same')(conv3_2)
    conv2_3 = concatenate([up2_3, conv2_1, conv2_2],
                          name='merge23',
                          axis=bn_axis)
    conv2_3 = standard_unit(conv2_3, stage='23', nb_filter=nb_filter[1])

    up1_4 = Conv2DTranspose(nb_filter[0], (2, 2),
                            strides=(2, 2),
                            name='up14',
                            padding='same')(conv2_3)
    conv1_4 = concatenate([up1_4, conv1_1, conv1_2, conv1_3],
                          name='merge14',
                          axis=bn_axis)
    conv1_4 = standard_unit(conv1_4, stage='14', nb_filter=nb_filter[0])

    #     conv5_1 = standard_unit(pool4, stage='51', nb_filter=nb_filter[4])
    conv5_1_upsampling_16 = Conv2DTranspose(nb_filter[0], (2, 2),
                                            strides=(16, 16),
                                            name='conv5_1_upsampling_16',
                                            padding='same')(
                                                conv5_1)  # changes made

    up4_2 = Conv2DTranspose(nb_filter[3], (2, 2),
                            strides=(2, 2),
                            name='up42',
                            padding='same')(conv5_1)
    conv4_2 = concatenate([up4_2, conv4_1], name='merge42', axis=bn_axis)
    conv4_2 = standard_unit(conv4_2, stage='42', nb_filter=nb_filter[3])
    conv4_2_upsampling_8 = Conv2DTranspose(nb_filter[0], (2, 2),
                                           strides=(8, 8),
                                           name='conv4_2_upsampling_8',
                                           padding='same')(
                                               conv4_2)  # changes made

    up3_3 = Conv2DTranspose(nb_filter[2], (2, 2),
                            strides=(2, 2),
                            name='up33',
                            padding='same')(conv4_2)
    conv3_3 = concatenate([up3_3, conv3_1, conv3_2],
                          name='merge33',
                          axis=bn_axis)
    conv3_3 = standard_unit(conv3_3, stage='33', nb_filter=nb_filter[2])
    conv3_3_upsampling_4 = Conv2DTranspose(nb_filter[0], (2, 2),
                                           strides=(4, 4),
                                           name='conv3_3_upsampling_4',
                                           padding='same')(
                                               conv3_3)  # changes made

    up2_4 = Conv2DTranspose(nb_filter[1], (2, 2),
                            strides=(2, 2),
                            name='up24',
                            padding='same')(conv3_3)
    conv2_4 = concatenate([up2_4, conv2_1, conv2_2, conv2_3],
                          name='merge24',
                          axis=bn_axis)
    conv2_4 = standard_unit(conv2_4, stage='24', nb_filter=nb_filter[1])
    conv2_4_upsampling_2 = Conv2DTranspose(nb_filter[0], (2, 2),
                                           strides=(2, 2),
                                           name='conv2_4_upsampling_2',
                                           padding='same')(
                                               conv2_4)  # changes made

    up1_5 = Conv2DTranspose(nb_filter[0], (2, 2),
                            strides=(2, 2),
                            name='up15',
                            padding='same')(conv2_4)
    conv1_5 = concatenate([up1_5, conv1_1, conv1_2, conv1_3, conv1_4],
                          name='merge15',
                          axis=bn_axis)
    conv1_5 = standard_unit(conv1_5, stage='15', nb_filter=nb_filter[0])

    nestnet_output_1 = Conv2D(num_class, (1, 1),
                              activation='sigmoid',
                              name='output_1',
                              kernel_initializer='he_normal',
                              padding='same',
                              kernel_regularizer=l2(1e-4))(conv1_2)
    nestnet_output_2 = Conv2D(num_class, (1, 1),
                              activation='sigmoid',
                              name='output_2',
                              kernel_initializer='he_normal',
                              padding='same',
                              kernel_regularizer=l2(1e-4))(conv1_3)
    nestnet_output_3 = Conv2D(num_class, (1, 1),
                              activation='sigmoid',
                              name='output_3',
                              kernel_initializer='he_normal',
                              padding='same',
                              kernel_regularizer=l2(1e-4))(conv1_4)
    nestnet_output_4 = Conv2D(num_class, (1, 1),
                              activation='sigmoid',
                              name='output_4',
                              kernel_initializer='he_normal',
                              padding='same',
                              kernel_regularizer=l2(1e-4))(conv1_5)
    # changes code
    nested_conv2_4_upsampling_2 = Conv2D(
        num_class, (1, 1),
        activation='sigmoid',
        name='output_5',
        kernel_initializer='he_normal',
        padding='same',
        kernel_regularizer=l2(1e-4))(conv2_4_upsampling_2)
    nested_conv3_3_upsampling_4 = Conv2D(
        num_class, (1, 1),
        activation='sigmoid',
        name='output_6',
        kernel_initializer='he_normal',
        padding='same',
        kernel_regularizer=l2(1e-4))(conv3_3_upsampling_4)
    nested_conv4_2_upsampling_8 = Conv2D(
        num_class, (1, 1),
        activation='sigmoid',
        name='output_7',
        kernel_initializer='he_normal',
        padding='same',
        kernel_regularizer=l2(1e-4))(conv4_2_upsampling_8)
    nested_conv5_1_upsampling_16 = Conv2D(
        num_class, (1, 1),
        activation='sigmoid',
        name='output_8',
        kernel_initializer='he_normal',
        padding='same',
        kernel_regularizer=l2(1e-4))(conv5_1_upsampling_16)
    nestnet_output_all = keras.layers.Average()([
        nestnet_output_1, nestnet_output_2, nestnet_output_3, nestnet_output_4,
        nested_conv2_4_upsampling_2, nested_conv3_3_upsampling_4,
        nested_conv4_2_upsampling_8, nested_conv5_1_upsampling_16
    ])

    model = Model(inputs=[input_model], outputs=[nestnet_output_all])
    #     if deep_supervision:
    #         model = Model(input=img_input, output=[nestnet_output_1,
    #                                                nestnet_output_2,
    #                                                nestnet_output_3,
    #                                                nestnet_output_4,
    #                                                nested_conv2_4_upsampling_2,
    #                                                nested_conv3_3_upsampling_4,
    #                                                nested_conv4_2_upsampling_8,
    #                                                nested_conv5_1_upsampling_16
    #                                                ])
    #     else:
    #         model = Model(input=img_input, output=[nestnet_output_4])

    return model
Exemplo n.º 30
0
def model(input_form="all", aux_size=0, hyperparameters=dict()):
    print("using the following hyperparameters: {}".format(hyperparameters))

    if input_form == "features":
        return features_model(aux_size, hyperparameters)

    parameters = INPUT_FORM_PARAMETERS[input_form]

    inputs = list()
    outputs = list()

    #retreiving the hyperparameters
    DROPOUT = hyperparameters.get("dropout", 0.5)
    OPTIMIZER = hyperparameters.get("optimizer", "sgd-0001-0.9")
    DEEP_DENSE_TOP = hyperparameters.get("deep-dense-top", True)
    CONVNET_FREEZE_PERCENT = hyperparameters.get("convnet-freeze-percent", 0.0)

    #skip for now
    '''
    if parameters["t2"]:
        convnet = applications.ResNet50(
            weights="imagenet",
            include_top=False,
            input_shape=(config.IMAGE_SIZE, config.IMAGE_SIZE, 3),
        )
        for layer in convnet.layers:
            layer.name = "{}_t2".format(layer.name)
        apply_layer_freeze(convnet, CONVNET_FREEZE_PERCENT)
        out = convnet.output
        out = Flatten()(out)
        inputs.append(convnet.input)
        outputs.append(out)
    '''
    if parameters["t1"]:
        # init ResNet
        convnet = efn.EfficientNetB0(
            weights="imagenet",
            include_top=False,
            input_shape=(config.IMAGE_SIZE, config.IMAGE_SIZE, 3),
        )
        apply_layer_freeze(convnet, CONVNET_FREEZE_PERCENT)
        out = convnet.output
        out = Flatten()(out)
        inputs.append(convnet.input)
        outputs.append(out)

    if len(outputs) > 1:
        out = concatenate(outputs)
    else:
        out = outputs[0]

    out = Dense(256, activation="relu", kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(out)
    out = BatchNormalization()(out)

    if DEEP_DENSE_TOP:
        out = Dropout(DROPOUT)(out)
        out = Dense(128, activation="relu", kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(out)
        out = BatchNormalization()(out)
        out = Dropout(DROPOUT)(out)
        out = Dense(64, activation="relu", kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(out)
        out = BatchNormalization()(out)
        out = Dropout(DROPOUT)(out)
        out = Dense(32, activation="relu", kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(out)
        out = BatchNormalization()(out)
        out = Dropout(DROPOUT)(out)

    if parameters["features"]:
        aux_input = Input(shape=(aux_size,), name='aux_input')
        inputs.append(aux_input)
        out = concatenate([out, aux_input])

    out = Dense(16, activation="relu", kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(out)
    out = BatchNormalization()(out)
    predictions = Dense(1, activation="sigmoid", kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(out)

    # creating the final model
    if len(inputs) > 1:
        model = Model(inputs=inputs, outputs=predictions)
    else:
        model = Model(inputs=inputs[0], outputs=predictions)

    # compile the model
    model.compile(
        loss="binary_crossentropy",
        optimizer=OPTIMIZERS[OPTIMIZER](),
        metrics=["accuracy"])

    return model