コード例 #1
0
def get_model(input_shape):

    # Input Layer
    input = Input(shape=input_shape)

    x_model = efn.EfficientNetB3(weights='imagenet',
                                 include_top=False,
                                 input_tensor=input,
                                 pooling=None,
                                 classes=None)

    # UnFreeze all layers
    for layer in x_model.layers:
        layer.trainable = True

    lambda_layer = Lambda(generalized_mean_pool_2d)
    lambda_layer.trainable_weights.extend([gm_exp])
    x = lambda_layer(x_model.output)

    # multi output
    season = Dense(4, activation="sigmoid", name="season")(x)

    # model
    model = Model(inputs=x_model.input, outputs=[season])

    return model
コード例 #2
0
def model_define(modeltype, inputshape):

    if modeltype == 'define':
        model = customize_mode()
        print('Model: define !')
    elif modeltype == 'EfficientNetB3':
        model = efn.EfficientNetB3(include_top=False,
                                   weights='imagenet',
                                   input_tensor=None,
                                   input_shape=inputshape,
                                   pooling=None)
        freeze_layers(model)
        print('Model: EfficientNetB3, weights loaded!')
    elif modeltype == 'ResNet50':
        model = applications.ResNet50(include_top=False,
                                      weights='imagenet',
                                      input_shape=inputshape,
                                      pooling='avg')
        freeze_layers(model)
        print('Model: ResNet50, weights loaded!')
    elif modeltype == 'Xception':
        model = applications.Xception(include_top=False,
                                      weights='imagenet',
                                      input_shape=inputshape)
        freeze_layers(model)
        print('Model: Xception, weights loaded!')
    else:
        pass

    return model
def create_model(input_shape):
    # Input Layer
    input = Input(shape = input_shape)
    
    # Create and Compile Model and show Summary
    x_model = efn.EfficientNetB3(weights = 'imagenet', include_top = False, input_tensor = input, pooling = None, 
                                classes = None)
    
    # UnFreeze all layers
    for layer in x_model.layers:
        layer.trainable = True
    
    # GeM
    lambda_layer = Lambda(generalized_mean_pool_2d)
    lambda_layer.trainable_weights.extend([gm_exp])
    x = lambda_layer(x_model.output)
    
    # multi output
    grapheme_root = Dense(168, activation = 'softmax', name = 'root')(x)
    vowel_diacritic = Dense(11, activation = 'softmax', name = 'vowel')(x)
    consonant_diacritic = Dense(7, activation = 'softmax', name = 'consonant')(x)

    # model
    model = Model(inputs = x_model.input, outputs = [grapheme_root, vowel_diacritic, consonant_diacritic])

    return model
コード例 #4
0
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)
コード例 #5
0
def frozen_efficientnet(input_size, n_classes):
    get_custom_objects().update({'swish_act': SwishActivation(swish_act)})

    model_ = efn.EfficientNetB3(
        include_top=False,
        input_tensor=Input(shape=input_size),
        weights='imagenet')


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

    x = model_.output
    x = GlobalAveragePooling2D()(x)
    #x = BatchNormalization()(x)
    #x = Dropout(0.2)(x)

    #x = Dense(512)(x)
    #x = BatchNormalization()(x)
    #x = Activation(swish_act)(x)
    #x = Dropout(0.5)(x)

    x = Dense(256, activation='relu')(x)
    #x = BatchNormalization()(x)
    #x = Activation(swish_act)(x)
    x = Dropout(0.5)(x)
    # = Flatten(input_shape=model_.output_shape[1:])(model_.layers[-1].output)
    x = Dense(n_classes, activation='softmax')(x)

    frozen_model = Model(model_.input, x)

    return frozen_model
コード例 #6
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
コード例 #7
0
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)
def create_model_b3():
    K.clear_session()

    base_model = efn.EfficientNetB3(weights='imagenet',
                                    include_top=False,
                                    pooling='avg',
                                    input_shape=(HEIGHT, WIDTH, 3))
    x = base_model.output
    x = Dropout(0.125)(x)
    y_pred = Dense(6, activation='sigmoid')(x)

    return Model(inputs=base_model.input, outputs=y_pred)
コード例 #9
0
ファイル: classify.py プロジェクト: Omenranr/Dcase2020_Task5
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
コード例 #10
0
    def getB3Net(self, shape, model_name):
        effnet = efn.EfficientNetB3(weights=None,\
                                include_top=False,\
                                input_shape=shape)
        #effnet.load_weights(self.weight_path + 'efficientnet-b3_imagenet_1000_notop.h5')

        for i, layer in enumerate(effnet.layers):
            effnet.layers[i].name = str(model_name) + "_" + layer.name
            if "batch_normalization" in layer.name:
                effnet.layers[i] = GroupNormalization(groups=self.batch_size,
                                                      axis=-1,
                                                      epsilon=0.00001)
        return effnet
コード例 #11
0
ファイル: train.py プロジェクト: wishgale/garbage_classify
def model_fn(FLAGS, objective, optimizer, metrics):
    base_model = efn.EfficientNetB3(
        include_top=False,
        shape=(FLAGS.input_size, FLAGS.input_size, 3),
        n_class=FLAGS.num_classes,
    )

    x = base_model.output
    x = GlobalAveragePooling2D(name='avg_pool')(x)
    predictions = Dense(FLAGS.num_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
    model.summary()
    return model
コード例 #12
0
    def load_model(self):
        FACTOR = 0.70
        HEIGHT = 137
        WIDTH = 236
        HEIGHT_NEW = int(HEIGHT * FACTOR)
        WIDTH_NEW = int(WIDTH * FACTOR)
        HEIGHT_NEW = 137
        WIDTH_NEW = 236

        # base_model=EfficientNetB3(include_top=False, weights='imagenet',input_shape=(HEIGHT_NEW,WIDTH_NEW,3))
        base_model = efn.EfficientNetB3(include_top=False,
                                        weights='imagenet',
                                        input_shape=(HEIGHT_NEW, WIDTH_NEW, 3))
        # base_model.trainable=False
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)

        grapheme_root = layers.Dense(168, activation='softmax', name='root')(x)
        vowel_diacritic = layers.Dense(11, activation='softmax',
                                       name='vowel')(x)
        consonant_diacritic = layers.Dense(7,
                                           activation='softmax',
                                           name='consonant')(x)

        model = Model(
            inputs=base_model.input,
            outputs=[grapheme_root, vowel_diacritic, consonant_diacritic])
        # for layer in base_model.layers:
        #     layer.trainable = True
        model.compile(optimizer='adam',
                      loss={
                          'root': 'categorical_crossentropy',
                          'vowel': 'categorical_crossentropy',
                          'consonant': 'categorical_crossentropy'
                      },
                      loss_weights={
                          'root': 0.5,
                          'vowel': 0.25,
                          'consonant': 0.25
                      },
                      metrics={
                          'root': 'accuracy',
                          'vowel': 'accuracy',
                          'consonant': 'accuracy'
                      })
        print(model.summary())

        return model
コード例 #13
0
def create_model(input_shape):
    '''
    base model is efficientnetB3(B4)
    '''
    input = keras.Input(input_shape)
    base_model = efn.EfficientNetB3(weights='imagenet',include_top=False,
                                   input_tensor=input,pooling='avg')
    for layer in base_model.layers:
        layer.trainable=True
    x = base_model.output
    root = keras.layers.Dense(units=168,activation='softmax',name='root')(x)
    vowel=keras.layers.Dense(units=11,activation='softmax',name='vowel')(x)
    consonant=keras.layers.Dense(units=7,activation='softmax',name='consonant')(x)
    model = keras.models.Model(base_model.input,[root,vowel,consonant],name='efficientnet3')

    return model
コード例 #14
0
def frozen_efficientnet(input_size, n_classes):
    model_ = efn.EfficientNetB3(include_top=False,
                                input_tensor=Input(shape=input_size),
                                weights='imagenet')

    print(model_)

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

    x = Flatten(input_shape=model_.output_shape[1:])(model_.layers[-1].output)
    x = Dense(n_classes, activation='softmax')(x)

    frozen_model = Model(model_.input, x)

    return frozen_model
コード例 #15
0
def build_efficientnet(weights='imagenet', fine_tune=None):
    """
    Builds and compiles the EfficientNetB3 model using pre-trained weights and custom classification head.

    :param weights: Pre-trained weights to be used
    :param fine_tune: layers to be fine-tuned
    :return: The compiled model
    """

    if fine_tune is None:
        fine_tune = 'block6b_add'
    input_shape = (224, 224, 3)

    # Configure base model from pretrained
    base_model = efn.EfficientNetB3(weights='imagenet', include_top=False, input_shape=input_shape)
    output = base_model.layers[-1].output
    output = keras.layers.Flatten()(output)
    model_efn = Model(base_model.input, output)

    # Set blocks to be fine tuneable
    model_efn.trainable = True
    set_trainable = False
    for layer in model_efn.layers:
        if layer.name == fine_tune:
            set_trainable = True
        if set_trainable:
            layer.trainable = True
        else:
            layer.trainable = False

    model = Sequential()
    model.add(model_efn)
    model.add(Dense(256, activation='relu', input_dim=input_shape))
    model.add(Dropout(0.5))
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.3))
    model.add(Dense(1, activation='sigmoid'))

    # compile model
    model.compile(loss='binary_crossentropy',
                  optimizer=optimizers.Adam(0.0001),
                  metrics=['accuracy'])

    # print model summary
    print(model.summary())

    return model
コード例 #16
0
def frozen_efnet3(input_size, n_classes):
    model_ = efn.EfficientNetB3(
        include_top=False,
        input_tensor=Input(shape=input_size),
    )
    #여기까지 초기값 주는 부분
    #다른거 다 똑같이 하면 되는데 local weight같은 경우에 한번 찾아봐야 함

    #돌려봤는데, 인터넷에서 알아서 초기값을 받아온다. 그냥 괜찮은듯 성능도 한번 돌려본 바에 의하면 좋고

    for layer in model_.layers:
        layer.trainable = False  # 전이학습을 위해 freeze 한다는것 같다.

    x = Flatten(input_shape=model_.output_shape[1:])(model_.layers[-1].output)
    x = Dense(n_classes, activation='softmax')(x)

    frozen_model = Model(model_.input, x)

    return frozen_model
コード例 #17
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
コード例 #18
0
ファイル: backbone.py プロジェクト: karry0298/testSSD
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
コード例 #19
0
def model_fn(FLAGS, objective, optimizer, metrics):
    model = efn.EfficientNetB3(weights=None,
                               include_top=False,
                               input_shape=(FLAGS.input_size, FLAGS.input_size,
                                            3),
                               classes=FLAGS.num_classes)
    model.load_weights(
        '/home/work/user-job-dir/src/efficientnet-b3_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5'
    )
    for i, layer in enumerate(model.layers):
        if "batch_normalization" in layer.name:
            model.layers[i] = GroupNormalization(groups=32,
                                                 axis=-1,
                                                 epsilon=0.00001)
    x = model.output
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.3)(x)
    predictions = Dense(FLAGS.num_classes, activation='softmax')(
        x)  # activation="linear",activation='softmax'
    model = Model(input=model.input, output=predictions)
    model = multi_gpu_model(model, 4)
    model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
    return model
コード例 #20
0
ファイル: app.py プロジェクト: yunika5699/skripsi_yunika
import efficientnet.keras as efn
from xgboost import XGBClassifier
from sklearn.metrics import roc_auc_score
from PIL import Image
from flask_dropzone import Dropzone

app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
model = pickle.load(open('model.pkl', 'rb'))
sigmaX = 10
labelArray = []
inp_shape = (224, 224, 3)
feature_extractor = Sequential()
feature_extractor.add(
    efn.EfficientNetB3(weights='imagenet',
                       include_top=False,
                       input_shape=inp_shape))
feature_extractor.add(GlobalAveragePooling2D())
UPLOAD_FOLDER = '/Users/user/Desktop/projects/skripsi_yunika/uploads'
WRITE_PATH = '/Users/user/Desktop/projects/skripsi_yunika/uploads/filtered'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config.update(
    UPLOADED_PATH=os.path.join(basedir, 'uploads'),
    # Flask-Dropzone config:
    DROPZONE_ALLOWED_FILE_TYPE='image',
    DROPZONE_MAX_FILE_SIZE=25,
    DROPZONE_MAX_FILES=30,
    DROPZONE_UPLOAD_MULTIPLE=True,  # enable upload multiple
)
dropzone = Dropzone(app)
コード例 #21
0
def get_backbone(name):
    """ Chooses a backbone/ base network.

        Args:
            name: the name of the base network.

        Returns:
            backbone: the Keras model of the chosen network.
    """
    if name == 'EfficientNetB0':
        backbone = efn.EfficientNetB0(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB1':
        backbone = efn.EfficientNetB1(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB2':
        backbone = efn.EfficientNetB2(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB3':
        backbone = efn.EfficientNetB3(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB4':
        backbone = efn.EfficientNetB4(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB5':
        backbone = efn.EfficientNetB5(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB6':
        backbone = efn.EfficientNetB6(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'EfficientNetB7':
        backbone = efn.EfficientNetB7(include_top=c.INCLUDE_TOP,
                                      weights=c.WEIGHTS,
                                      input_shape=c.INPUT_SHAPE,
                                      pooling=c.POOLING)
    elif name == 'VGG16':
        backbone = VGG16(weights=c.WEIGHTS,
                         include_top=c.INCLUDE_TOP,
                         input_shape=c.INPUT_SHAPE,
                         pooling=c.POOLING)
    elif name == 'ResNet50':
        backbone = ResNet50(include_top=c.INCLUDE_TOP,
                            weights=c.WEIGHTS,
                            input_shape=c.INPUT_SHAPE,
                            pooling=c.POOLING)
    elif name == 'InceptionV3':
        backbone = InceptionV3(include_top=c.INCLUDE_TOP,
                               weights=c.WEIGHTS,
                               input_shape=c.INPUT_SHAPE,
                               pooling=c.POOLING)
    elif name == 'DenseNet201':
        backbone = DenseNet201(weights=c.WEIGHTS,
                               include_top=c.INCLUDE_TOP,
                               input_shape=c.INPUT_SHAPE,
                               pooling=c.POOLING)
    else:
        backbone = None
    try:
        backbone.trainable = True
        return backbone
    except Exception as e:
        print(str(e))
コード例 #22
0
b_name = os.getenv("B", "2")

model = None

if b_name == "0":
    model = efn.EfficientNetB0(weights=weights)

if b_name == "1":
    model = efn.EfficientNetB1(weights=weights)

if b_name == "2":
    model = efn.EfficientNetB2(weights=weights)

if b_name == "3":
    model = efn.EfficientNetB3(weights=weights)

if b_name == "4":
    model = efn.EfficientNetB4(weights=weights)

if b_name == "5":
    model = efn.EfficientNetB5(weights=weights)

if b_name == "6":
    model = efn.EfficientNetB6(weights=weights)

if b_name == "7":
    model = efn.EfficientNetB7(weights=weights)

image_size = model.input_shape[1]
コード例 #23
0
ファイル: run.py プロジェクト: jurader/covid19_xp
def build_model(input_shape, args):
    D = args.d
    F = args.f
    V = args.v

    input_tensor = Input(shape=input_shape)

    if args.tf == "in":
        base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = in_pi
    elif args.tf == "inr":
        base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = inr_pi
    elif args.tf == "vg":
        base_model = VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = vg_pi
    elif args.tf == "xc":
        base_model = Xception(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = xc_pi
    elif args.tf == "re":
        base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = re_pi
    elif args.tf == "de":
        base_model = DenseNet121(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = de_pi
    elif args.tf == "mo":
        base_model = MobileNet(weights='imagenet', include_top=False, input_tensor=input_tensor)
        #pi = mo_pi
    elif args.tf.find("ef") > -1:
        if args.tf == "ef0":
            base_model = efn.EfficientNetB0(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef1":
            base_model = efn.EfficientNetB1(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef2":
            base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef3":
            base_model = efn.EfficientNetB3(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef4":
            base_model = efn.EfficientNetB4(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef5":
            base_model = efn.EfficientNetB5(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef6":
            base_model = efn.EfficientNetB6(weights='imagenet', include_top=False, input_tensor=input_tensor)
        elif args.tf == "ef7":
            base_model = efn.EfficientNetB7(weights='imagenet', include_top=False, input_tensor=input_tensor)
    else:
        print("unknown network type:", args.tf)
        exit()

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(F, activation='relu')(x)
    if D > 0:
        x = Dropout(D)(x)
 
    pred = Dense(nb_classes, activation='softmax')(x)

    model = Model(inputs=base_model.input, outputs=pred)

    layer_num = len(base_model.layers)
    for layer in base_model.layers[:int(layer_num * V)]:
        layer.trainable = False

    return model #, pi
コード例 #24
0
    train_data_generator = datagen.flow_from_dataframe(
        training_data,
        directory=image_dir,
        x_col="image_id",
        y_col="label",
        class_mode="categorical",
        shuffle=True)
    valid_data_generator = datagen.flow_from_dataframe(
        validation_data,
        directory=image_dir,
        x_col="image_id",
        y_col="label",
        class_mode="categorical",
        shuffle=True)
    base_model = efn.EfficientNetB3(weights='noisy-student',
                                    input_shape=(512, 512, 3))

    base_model.trainable = True
    model = tf.keras.Sequential([
        tf.keras.layers.Input((512, 512, 3)),
        tf.keras.layers.BatchNormalization(renorm=True), base_model,
        BatchNormalization(),
        tf.keras.layers.LeakyReLU(),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(256),
        BatchNormalization(),
        tf.keras.layers.LeakyReLU(),
        tf.keras.layers.Dense(128),
        BatchNormalization(),
        tf.keras.layers.LeakyReLU(),
        BatchNormalization(),
コード例 #25
0
def get_b3_backbone():
    backbone = efn.EfficientNetB3(input_shape=(128, 128, 3), include_top=False,  weights='imagenet')
    backbone_output = GlobalAveragePooling2D()(backbone.output)
    return backbone, backbone_output
コード例 #26
0
from keras.utils.vis_utils import model_to_dot
import keras.backend as K
import tensorflow as tf
import pickle
import matplotlib.pyplot as plt
import efficientnet.keras as efn
from keras import callbacks
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam

X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("y.pickle", "rb"))

efficient_net = efn.EfficientNetB3(weights='imagenet',
                                   input_shape=(64, 64, 3),
                                   include_top=False,
                                   pooling='max')

model = Sequential()
model.add(efficient_net)
model.add(Dense(120, activation='relu'))
model.add(Dense(120, activation='relu'))
model.add(Dense(3, activation='softmax'))

model.summary()

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

history = model.fit(X,
コード例 #27
0
    class_mode='categorical')
"""
model = Sequential()
model.add(efn.EfficientNetB3(weights="imagenet", include_top=False, pooling='avg'))
model.add(layers.Dense(label_size, activation="softmax"))
model = utils.multi_gpu_model(model, gpus=4)
model.compile(metrics=['acc'], loss='categorical_crossentropy', optimizer='adam')
"""

mirrored_strategy = tf.distribute.MirroredStrategy()

with mirrored_strategy.scope():
    model = Sequential()
    model.add(
        efn.EfficientNetB3(weights="imagenet",
                           include_top=False,
                           pooling='avg'))
    model.add(layers.Dense(label_size, activation="softmax"))
    model.compile(metrics=['acc'],
                  loss='categorical_crossentropy',
                  optimizer='adam')
# """

checkpointer = ModelCheckpoint(filepath='best.hdf5',
                               verbose=1,
                               save_best_only=True)  # Save best weight file
csv_logger = CSVLogger('history.log')

history = model.fit(train_generator,
                    validation_data=validation_generator,
                    epochs=150,