efficientnet/model.py
def EfficientNet(model_name,
                 dropout_connection_rate=0.2,
                 depth_divisor=8,
                 load_weights=False,
                 num_classes=23):
    """Instantiates the EfficientNet architecture using given scaling coefficients.
    Optionally loads weights pre-trained on part of ImageNet.
    # Arguments
        model_name: name of the model, efficientnet-b<0-8 or l2>
        dropout_connection_rate: float between 0 and 1, dropout rate at mb_conv connections
        depth_divisor: int
        load_weights: whether to load or not the weights of the previously trained model
        num_classes: number of classes from the ImageNet dataset
    # Returns
        A Keras model
    """
    
    model_params = params_dict[model_name]
    width_coefficient = model_params[0]
    depth_coefficient = model_params[1]
    resolution = model_params[2]
    dropout_rate = model_params[3] # dropout of the classifier

    activation = tf.keras.activations.swish

    # region Build bottom
    input_tensor = layers.Input(shape=(resolution, resolution, 3), dtype='float32', name='input_tensor')

    x = layers.Conv2D(round_filters(32, width_coefficient, depth_divisor),
                      kernel_size=3,
                      strides=(2, 2),
                      padding='same',
                      use_bias='false',
                      name='stem_conv')(input_tensor)
    x = layers.BatchNormalization(name='stem_bn')(x)
    x = layers.Activation(activation, name='stem_activation')(x)

    num_blocks_total = sum(block_args.num_repeat for block_args in blocks_args)
    block_num = 0
    #endregion

    # region Build blocks
    for idx, block_arg in enumerate(blocks_args):
        block_arg = block_arg._replace(
            input_filters = round_filters(block_arg.input_filters, width_coefficient, depth_divisor),
            output_filters = round_filters(block_arg.output_filters, width_coefficient, depth_divisor),
            num_repeat = round_repeats(block_arg.num_repeat, depth_coefficient)
        )

        dropout_connection_rate_current = dropout_connection_rate * float(block_num) / num_blocks_total
        x = mb_conv_block(x,
                          block_arg,
                          activation=activation,
                          dropout_rate=dropout_connection_rate_current,
                          prefix=f'block{idx + 1}a_'
                          )
        block_num += 1

        if block_arg.num_repeat > 1:
            block_arg = block_arg._replace(input_filters=block_arg.output_filters, strides=[1, 1])
            for idx2 in range(block_arg.num_repeat - 1):
                dropout_rate_blocks = dropout_connection_rate * float(block_num) / num_blocks_total
                print(dropout_rate_blocks)
                x = mb_conv_block(x,
                                  block_arg,
                                  activation=activation,
                                  dropout_rate=dropout_rate_blocks,
                                  prefix=f'block{idx + 1}{idx2 + 1}_'
                                  )
                block_num += 1
    # endregion

    # region Build top
    x = layers.Conv2D(round_filters(1280, width_coefficient, depth_divisor), 1,
                      padding='same',
                      use_bias=False,
                      name='top_conv')(x)
    x = layers.BatchNormalization(name='top_bn')(x)
    x = layers.Activation(activation, name='top_activation')(x)
    
    x = layers.GlobalAveragePooling2D(name='top_avg_pool')(x)
    if dropout_rate and dropout_rate > 0:
        x = layers.Dropout(dropout_rate, name='top_dropout')(x)
    x = layers.Dense(num_classes,
                     activation='softmax',
                     name='probs')(x)
    # endregion

    model = Model(input_tensor, x, name=model_name)

    if load_weights:
        model.load_weights('./MnasEfficientNet.h5')

    return model
Instantiates the EfficientNet architecture using given scaling coefficients.
    Optionally loads weights pre-trained on part of ImageNet.
    # Arguments
        model_name: name of the model, efficientnet-b<0-8 or l2>
        dropout_connection_rate: float between 0 and 1, dropout rate at mb_conv connections
        depth_divisor: int
        load_weights: whether to load or not the weights of the previously trained model
        num_classes: number of classes from the ImageNet dataset
    # Returns
        A Keras model
    
Instantiates the EfficientNet architecture using given scaling coefficients.
Optionally loads weights pre-trained on part of ImageNet.
Instantiates the EfficientNet architecture using given scaling coefficients . Optionally loads weights pre - trained on part of ImageNet .
def EfficientNet(model_name,
                 dropout_connection_rate=0.2,
                 depth_divisor=8,
                 load_weights=False,
                 num_classes=23):
    model_params = params_dict[model_name]
    width_coefficient = model_params[0]
    depth_coefficient = model_params[1]
    resolution = model_params[2]
    dropout_rate = model_params[3] 
    activation = tf.keras.activations.swish
    input_tensor = layers.Input(shape=(resolution, resolution, 3), dtype='float32', name='input_tensor')
    x = layers.Conv2D(round_filters(32, width_coefficient, depth_divisor),
                      kernel_size=3,
                      strides=(2, 2),
                      padding='same',
                      use_bias='false',
                      name='stem_conv')(input_tensor)
    x = layers.BatchNormalization(name='stem_bn')(x)
    x = layers.Activation(activation, name='stem_activation')(x)
    num_blocks_total = sum(block_args.num_repeat for block_args in blocks_args)
    block_num = 0
    for idx, block_arg in enumerate(blocks_args):
        block_arg = block_arg._replace(
            input_filters = round_filters(block_arg.input_filters, width_coefficient, depth_divisor),
            output_filters = round_filters(block_arg.output_filters, width_coefficient, depth_divisor),
            num_repeat = round_repeats(block_arg.num_repeat, depth_coefficient)
        )
        dropout_connection_rate_current = dropout_connection_rate * float(block_num) / num_blocks_total
        x = mb_conv_block(x,
                          block_arg,
                          activation=activation,
                          dropout_rate=dropout_connection_rate_current,
                          prefix=f'block{idx + 1}a_'
                          )
        block_num += 1
        if block_arg.num_repeat > 1:
            block_arg = block_arg._replace(input_filters=block_arg.output_filters, strides=[1, 1])
            for idx2 in range(block_arg.num_repeat - 1):
                dropout_rate_blocks = dropout_connection_rate * float(block_num) / num_blocks_total
                print(dropout_rate_blocks)
                x = mb_conv_block(x,
                                  block_arg,
                                  activation=activation,
                                  dropout_rate=dropout_rate_blocks,
                                  prefix=f'block{idx + 1}{idx2 + 1}_'
                                  )
                block_num += 1
    x = layers.Conv2D(round_filters(1280, width_coefficient, depth_divisor), 1,
                      padding='same',
                      use_bias=False,
                      name='top_conv')(x)
    x = layers.BatchNormalization(name='top_bn')(x)
    x = layers.Activation(activation, name='top_activation')(x)
    x = layers.GlobalAveragePooling2D(name='top_avg_pool')(x)
    if dropout_rate and dropout_rate > 0:
        x = layers.Dropout(dropout_rate, name='top_dropout')(x)
    x = layers.Dense(num_classes,
                     activation='softmax',
                     name='probs')(x)
    model = Model(input_tensor, x, name=model_name)
    if load_weights:
        model.load_weights('./MnasEfficientNet.h5')
    return model
def EfficientNet ( model_name , dropout_connection_rate = 0.2 , depth_divisor = 8 , load_weights = False , num_classes = 23 ) : model_params = params_dict [ model_name ] width_coefficient = model_params [ 0 ] depth_coefficient = model_params [ 1 ] resolution = model_params [ 2 ] dropout_rate = model_params [ 3 ] activation = tf . keras . activations . swish input_tensor = layers . Input ( shape = ( resolution , resolution , 3 ) , dtype = 'float32' , name = 'input_tensor' ) x = layers . Conv2D ( round_filters ( 32 , width_coefficient , depth_divisor ) , kernel_size = 3 , strides = ( 2 , 2 ) , padding = 'same' , use_bias = 'false' , name = 'stem_conv' ) ( input_tensor ) x = layers . BatchNormalization ( name = 'stem_bn' ) ( x ) x = layers . Activation ( activation , name = 'stem_activation' ) ( x ) num_blocks_total = sum ( block_args . num_repeat for block_args in blocks_args ) block_num = 0 for idx , block_arg in enumerate ( blocks_args ) : block_arg = block_arg . _replace ( input_filters = round_filters ( block_arg . input_filters , width_coefficient , depth_divisor ) , output_filters = round_filters ( block_arg . output_filters , width_coefficient , depth_divisor ) , num_repeat = round_repeats ( block_arg . num_repeat , depth_coefficient ) ) dropout_connection_rate_current = dropout_connection_rate * float ( block_num ) / num_blocks_total x = mb_conv_block ( x , block_arg , activation = activation , dropout_rate = dropout_connection_rate_current , prefix = f'block{idx + 1}a_' ) block_num += 1 if block_arg . num_repeat > 1 : block_arg = block_arg . _replace ( input_filters = block_arg . output_filters , strides = [ 1 , 1 ] ) for idx2 in range ( block_arg . num_repeat - 1 ) : dropout_rate_blocks = dropout_connection_rate * float ( block_num ) / num_blocks_total print ( dropout_rate_blocks ) x = mb_conv_block ( x , block_arg , activation = activation , dropout_rate = dropout_rate_blocks , prefix = f'block{idx + 1}{idx2 + 1}_' ) block_num += 1 x = layers . Conv2D ( round_filters ( 1280 , width_coefficient , depth_divisor ) , 1 , padding = 'same' , use_bias = False , name = 'top_conv' ) ( x ) x = layers . BatchNormalization ( name = 'top_bn' ) ( x ) x = layers . Activation ( activation , name = 'top_activation' ) ( x ) x = layers . GlobalAveragePooling2D ( name = 'top_avg_pool' ) ( x ) if dropout_rate and dropout_rate > 0 : x = layers . Dropout ( dropout_rate , name = 'top_dropout' ) ( x ) x = layers . Dense ( num_classes , activation = 'softmax' , name = 'probs' ) ( x ) model = Model ( input_tensor , x , name = model_name ) if load_weights : model . load_weights ( './MnasEfficientNet.h5' ) return model
Instantiates the EfficientNet architecture using given scaling coefficients.
Instantiates the EfficientNet architecture using given scaling coefficients .
"""Instantiates the EfficientNet architecture using given scaling coefficients.
    Optionally loads weights pre-trained on part of ImageNet.
    # Arguments
        model_name: name of the model, efficientnet-b<0-8 or l2>
        dropout_connection_rate: float between 0 and 1, dropout rate at mb_conv connections
        depth_divisor: int
        load_weights: whether to load or not the weights of the previously trained model
        num_classes: number of classes from the ImageNet dataset
    # Returns
        A Keras model
    """ # dropout of the classifier # region Build bottom #endregion # region Build blocks # endregion # region Build top # endregion
model_name
dropout_connection_rate
depth_divisor
load_weights
num_classes