Пример #1
0
def Deeplabv3(weights='pascal_voc',
              input_tensor=None,
              input_shape=(512, 512, 3),
              classes=2,
              backbone='xception',
              OS=16,
              alpha=1.):
    """ Instantiates the Deeplabv3+ architecture

    Optionally loads weights pre-trained
    on PASCAL VOC. This model is available for TensorFlow only,
    and can only be used with inputs following the TensorFlow
    data format `(width, height, channels)`.
    # Arguments
        weights: one of 'pascal_voc' (pre-trained on pascal voc)
            or None (random initialization)
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images
        classes: number of desired classes. If classes != 21,
            last layer is initialized randomly
        backbone: backbone to use. one of {'xception','mobilenetv2'}
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - If `alpha` < 1.0, proportionally decreases the number
                    of filters in each layer.
                - If `alpha` > 1.0, proportionally increases the number
                    of filters in each layer.
                - If `alpha` = 1, default number of filters from the paper
                    are used at each layer.
            Used only for mobilenetv2 backbone

    # Returns
        A Keras model instance.

    # Raises
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
        ValueError: in case of invalid argument for `weights` or `backbone`

    """

    if not (weights in {'pascal_voc', 'deeplab', None}):
        raise ValueError(
            'The `weights` argument should be either '
            '`None` (random initialization)   `deeplab` or `pascal_voc` '
            '(pre-trained on PASCAL VOC)')

    if K.backend() != 'tensorflow':
        raise RuntimeError('The Deeplabv3+ model is only available with '
                           'the TensorFlow backend.')

    if not (backbone in {'xception', 'mobilenetv2'}):
        raise ValueError('The `backbone` argument should be either '
                         '`xception`  or `mobilenetv2` ')

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    if backbone == 'xception':
        if OS == 8:
            entry_block3_stride = 1
            middle_block_rate = 2  # ! Not mentioned in paper, but required
            exit_block_rates = (2, 4)
            atrous_rates = (12, 24, 36)
        else:
            entry_block3_stride = 2
            middle_block_rate = 1
            exit_block_rates = (1, 2)
            atrous_rates = (6, 12, 18)

        x = Conv2D(32, (3, 3),
                   strides=(2, 2),
                   name='entry_flow_conv1_1',
                   use_bias=False,
                   padding='same')(img_input)
        x = BatchNormalization(name='entry_flow_conv1_1_BN')(x)
        x = Activation('relu')(x)

        x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1)
        x = BatchNormalization(name='entry_flow_conv1_2_BN')(x)
        x = Activation('relu')(x)

        x = _xception_block(x, [128, 128, 128],
                            'entry_flow_block1',
                            skip_connection_type='conv',
                            stride=2,
                            depth_activation=False)
        x, skip1 = _xception_block(x, [256, 256, 256],
                                   'entry_flow_block2',
                                   skip_connection_type='conv',
                                   stride=2,
                                   depth_activation=False,
                                   return_skip=True)

        x = _xception_block(x, [728, 728, 728],
                            'entry_flow_block3',
                            skip_connection_type='conv',
                            stride=entry_block3_stride,
                            depth_activation=False)
        for i in range(16):
            x = _xception_block(x, [728, 728, 728],
                                'middle_flow_unit_{}'.format(i + 1),
                                skip_connection_type='sum',
                                stride=1,
                                rate=middle_block_rate,
                                depth_activation=False)

        x = _xception_block(x, [728, 1024, 1024],
                            'exit_flow_block1',
                            skip_connection_type='conv',
                            stride=1,
                            rate=exit_block_rates[0],
                            depth_activation=False)
        x = _xception_block(x, [1536, 1536, 2048],
                            'exit_flow_block2',
                            skip_connection_type='none',
                            stride=1,
                            rate=exit_block_rates[1],
                            depth_activation=True)

    else:
        OS = 8
        first_block_filters = _make_divisible(32 * alpha, 8)
        x = Conv2D(first_block_filters,
                   kernel_size=3,
                   strides=(2, 2),
                   padding='same',
                   use_bias=False,
                   name='Conv')(img_input)
        x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)
        x = Activation(relu6, name='Conv_Relu6')(x)

        x = _inverted_res_block(x,
                                filters=16,
                                alpha=alpha,
                                stride=1,
                                expansion=1,
                                block_id=0,
                                skip_connection=False)

        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=1,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=2,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=3,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=4,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=5,
                                skip_connection=True)

        # stride in block 6 changed from 2 -> 1, so we need to use rate = 2
        x = _inverted_res_block(
            x,
            filters=64,
            alpha=alpha,
            stride=1,  # 1!
            expansion=6,
            block_id=6,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=7,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=8,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=9,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=10,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=11,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=12,
                                skip_connection=True)

        x = _inverted_res_block(
            x,
            filters=160,
            alpha=alpha,
            stride=1,
            rate=2,  # 1!
            expansion=6,
            block_id=13,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=14,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=15,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=320,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=16,
                                skip_connection=False)

    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling

    # Image Feature branch
    #out_shape = int(np.ceil(input_shape[0] / OS))
    b4 = AveragePooling2D(pool_size=(int(np.ceil(input_shape[0] / OS)),
                                     int(np.ceil(input_shape[1] / OS))))(x)
    b4 = Conv2D(256, (1, 1),
                padding='same',
                use_bias=False,
                name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation('relu')(b4)
    b4 = BilinearUpsampling((int(np.ceil(input_shape[0] / OS)),
                             int(np.ceil(input_shape[1] / OS))))(b4)

    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation('relu', name='aspp0_activation')(b0)

    # there are only 2 branches in mobilenetV2. not sure why
    if backbone == 'xception':
        # rate = 6 (12)
        b1 = SepConv_BN(x,
                        256,
                        'aspp1',
                        rate=atrous_rates[0],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 12 (24)
        b2 = SepConv_BN(x,
                        256,
                        'aspp2',
                        rate=atrous_rates[1],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 18 (36)
        b3 = SepConv_BN(x,
                        256,
                        'aspp3',
                        rate=atrous_rates[2],
                        depth_activation=True,
                        epsilon=1e-5)

        # concatenate ASPP branches & project
        x = Concatenate()([b4, b0, b1, b2, b3])
    else:
        x = Concatenate()([b4, b0])

    x = Conv2D(256, (1, 1),
               padding='same',
               use_bias=False,
               name='concat_projection')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation('relu')(x)
    x = Dropout(0.1)(x)

    # DeepLab v.3+ decoder

    if backbone == 'xception':
        # Feature projection
        # x4 (x2) block
        x = BilinearUpsampling(output_size=(int(np.ceil(input_shape[0] / 4)),
                                            int(np.ceil(input_shape[1] /
                                                        4))))(x)
        dec_skip1 = Conv2D(48, (1, 1),
                           padding='same',
                           use_bias=False,
                           name='feature_projection0')(skip1)
        dec_skip1 = BatchNormalization(name='feature_projection0_BN',
                                       epsilon=1e-5)(dec_skip1)
        dec_skip1 = Activation('relu')(dec_skip1)
        x = Concatenate()([x, dec_skip1])
        x = SepConv_BN(x,
                       256,
                       'decoder_conv0',
                       depth_activation=True,
                       epsilon=1e-5)
        x = SepConv_BN(x,
                       256,
                       'decoder_conv1',
                       depth_activation=True,
                       epsilon=1e-5)

    # you can use it with arbitary number of classes
    if classes == 21:
        last_layer_name = 'logits_semantic'
    else:
        last_layer_name = 'custom_logits_semantic'


#    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)
#    x = BilinearUpsampling(output_size=(input_shape[0], input_shape[1]))(x)

# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    weight_decay = 5e-4,
    x = GlobalAveragePooling2D()(x)
    x = Dense(classes,
              use_bias=False,
              kernel_regularizer=l2(weight_decay),
              kernel_initializer='he_normal',
              activation='softmax')(x)
    model = Model(inputs, x, name='deeplabv3+')

    # load weights

    if weights == 'pascal_voc':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_X,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_MOBILE,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)

    if weights == 'deeplab':
        model.load_weights(weights_path_deeplab, by_name=True)

    return model
def DenseNet(nb_dense_block=4,
             growth_rate=32,
             nb_filter=64,
             reduction=0.0,
             dropout_rate=0.0,
             weight_decay=1e-4,
             classes=1000,
             weights_path=None,
             category_num=6):
    '''Instantiate the DenseNet 121 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(224, 224, 3), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 24, 16]  # For DenseNet-121

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter,
                      7,
                      7,
                      subsample=(2, 2),
                      name='conv1',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
    x = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)

    x = Dense(classes, name='fc6')(x)
    x = Activation('relu', name='prob')(x)

    model = Model(img_input, x, name='densenet')

    # if weights_path is not None:
    #   model.load_weights(weights_path)

    # for layer in model.layers:
    #     layer.trainable = False
    x = model.output
    x = Dense(category_num, name='fc61')(x)
    x = Activation('softmax', name='prob1')(x)
    model = Model(img_input, x, name='densenet1')
    return model
Пример #3
0
_inputBN = BatchNormalization(axis=-1)(_input)

C1 = ConvSN2D(15, (5, 5), padding='valid',
              data_format='channels_last')(_inputBN)
C1 = LeakyReLU()(C1)

C2 = ConvSN2D(25, (7, 7), padding='valid', data_format='channels_last')(C1)
C2 = LeakyReLU()(C2)

C3 = ConvSN2D(40, (9, 9), padding='valid', data_format='channels_last')(C2)
C3 = LeakyReLU()(C3)

C4 = ConvSN2D(50, (11, 11), padding='valid', data_format='channels_last')(C3)
C4 = LeakyReLU()(C4)

Average_score = GlobalAveragePooling2D(name='Average_score')(
    C4)  #(batch_size, channels)

D1 = DenseSN(50)(Average_score)
D1 = LeakyReLU()(D1)

D2 = DenseSN(10)(D1)
D2 = LeakyReLU()(D2)

Score = DenseSN(1)(D2)

Discriminator = Model(outputs=Score, inputs=_input)

Discriminator.trainable = True
Discriminator.compile(loss='mse', optimizer='adam')

#### Combine the two networks to become MetricGAN
Пример #4
0
nb_kernels = 2 * nb_kernels
block6 = residual_block(block5, nb_kernels, doPooling=True)
block7 = residual_block(block6, nb_kernels, doPooling=False)
block8 = residual_block(block7, nb_kernels, doPooling=False)

nb_kernels = 2 * nb_kernels
block9 = residual_block(block8, nb_kernels, doPooling=True)
block10 = residual_block(block9, nb_kernels, doPooling=False)
block11 = residual_block(block10, nb_kernels, doPooling=False)

nb_kernels = 2 * nb_kernels
block12 = residual_block(block11, nb_kernels, doPooling=True)
block13 = residual_block(block12, nb_kernels, doPooling=False)
block14 = residual_block(block13, nb_kernels, doPooling=False)

g_avep = GlobalAveragePooling2D()(block14)
g_avep = Dropout(0.3)(g_avep)
output = Dense(2,
               kernel_initializer='normal',
               kernel_regularizer=regularizers.l2(L2_regularizers))(g_avep)
output = Activation('softmax')(output)

model = Model(inputs=input, outputs=output)

#SGD + momentum
#model.compile provides the cross entropy loss function
sgd = SGD(lr=0.01, decay=1e-7, momentum=0.95, nesterov=True)
model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])
model.summary()

##################
Пример #5
0
def create_model(nb_classes, input_shape, config=None):
    """Create a VGG-16 like model."""
    if len(input_shape) != 3:
        raise Exception("Input shape should be a tuple (nb_channels, nb_rows, "
                        "nb_cols) or (nb_rows, nb_cols, nb_channels), "
                        "depending on your backend.")
    if config is None:
        config = {'model': {}}

    # input_shape = (None, None, 3)  # for fcn
    input_ = Input(shape=input_shape)
    x = input_
    min_feature_map_dimension = min(input_shape[:2])
    if min_feature_map_dimension < 32:
        print("ERROR: Please upsample the feature maps to have at least "
              "a size of 32 x 32. Currently, it has {}".format(input_shape))
    tmp = min_feature_map_dimension / 32.
    nb_filter = 32
    if tmp >= 2:
        while tmp >= 2.:
            y = x
            for _ in range(2):
                x = Convolution2D(nb_filter / 2, (3, 3),
                                  padding='same',
                                  kernel_initializer='he_uniform',
                                  kernel_regularizer=l2(0.0001))(x)
                x = BatchNormalization()(x)
                x = Activation('elu')(x)
                y = Convolution2D(nb_filter / 2, (3, 3),
                                  padding='same',
                                  kernel_initializer='he_uniform',
                                  kernel_regularizer=l2(0.0001))(y)
                y = BatchNormalization()(y)
                y = Activation('relu')(y)
            x = concatenate([x, y], axis=-1)
            x = MaxPooling2D(pool_size=(2, 2))(x)
            # nb_filter *= 2
            tmp /= 2

    if 'c1' in config['model']:
        c1_nb_filter = config['model']['c1']['nb_filter']
    else:
        c1_nb_filter = nb_filter
    if 'c8' in config['model']:
        c8_nb_filter = config['model']['c8']['nb_filter']
    else:
        c8_nb_filter = 512

    y = x
    x = Convolution2D(c1_nb_filter / 2, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    y = Convolution2D(c1_nb_filter / 2, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    x = concatenate([x, y], axis=-1)
    #
    y = x
    x = Convolution2D(nb_filter / 2, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    y = Convolution2D(nb_filter / 2, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    x = concatenate([x, y], axis=-1)
    #
    x = MaxPooling2D(pool_size=(2, 2))(x)

    y = x
    x = Convolution2D(nb_filter, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    y = Convolution2D(nb_filter, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    x = concatenate([x, y], axis=-1)
    #
    y = x
    x = Convolution2D(nb_filter, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    y = Convolution2D(nb_filter, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    x = concatenate([x, y], axis=-1)
    #
    x = MaxPooling2D(pool_size=(2, 2))(x)

    y = x
    x = Convolution2D(nb_filter, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    y = Convolution2D(nb_filter, (3, 3),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    x = concatenate([x, y], axis=-1)
    #
    x = MaxPooling2D(pool_size=(2, 2))(x)

    y = x
    x = Convolution2D(256, (4, 4),
                      padding='valid',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Dropout(0.5)(x)
    y = Convolution2D(256, (4, 4),
                      padding='valid',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = Dropout(0.5)(y)
    x = concatenate([x, y], axis=-1)
    #
    y = x
    x = Convolution2D(c8_nb_filter / 2, (1, 1),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Dropout(0.5)(x)
    y = Convolution2D(c8_nb_filter / 2, (1, 1),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = Dropout(0.5)(y)
    x = concatenate([x, y], axis=-1)
    #
    x = Convolution2D(nb_classes, (1, 1),
                      padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = GlobalAveragePooling2D()(x)  # Adjust for FCN
    x = BatchNormalization()(x)
    x = Activation('softmax')(x)
    model = Model(inputs=input_, outputs=x)
    return model
Пример #6
0
def create_dense_net(nb_classes,
                     img_dim,
                     depth=40,
                     nb_dense_block=3,
                     growth_rate=12,
                     nb_filter=-1,
                     bottleneck=False,
                     reduction=0.0,
                     dropout_rate=None,
                     weight_decay=1E-4,
                     verbose=True):
    ''' Build the create_dense_net model

    Args:
        nb_classes: number of classes
        img_dim: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        depth: number or layers
        nb_dense_block: number of dense blocks to add to end (generally = 3)
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate
        bottleneck: add bottleneck blocks
        reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression
        dropout_rate: dropout rate
        weight_decay: weight decay
        verbose: print the model type

    Returns: keras tensor with nb_layers of conv_block appended

    '''

    model_input = Input(shape=img_dim)

    concat_axis = 1 if K.image_dim_ordering() == "th" else -1

    assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"
    if reduction != 0.0:
        assert reduction <= 1.0 and reduction > 0.0, "reduction value must lie between 0.0 and 1.0"

    # layers in each dense block
    nb_layers = int((depth - 4) / 3)

    if bottleneck:
        nb_layers = int(nb_layers // 2)

    # compute initial nb_filter if -1, else accept users initial nb_filter
    if nb_filter <= 0:
        nb_filter = 2 * growth_rate

    # compute compression factor
    compression = 1.0 - reduction

    # Initial convolution
    x = Convolution2D(nb_filter,
                      3,
                      3,
                      init="he_uniform",
                      border_mode="same",
                      name="initial_conv2D",
                      bias=False,
                      W_regularizer=l2(weight_decay))(model_input)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = dense_block(x,
                                   nb_layers,
                                   nb_filter,
                                   growth_rate,
                                   bottleneck=bottleneck,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)
        # add transition_block
        x = transition_block(x,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    # The last dense_block does not have a transition_block
    x, nb_filter = dense_block(x,
                               nb_layers,
                               nb_filter,
                               growth_rate,
                               bottleneck=bottleneck,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(mode=0,
                           axis=concat_axis,
                           gamma_regularizer=l2(weight_decay),
                           beta_regularizer=l2(weight_decay))(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)
    x = Dense(nb_classes,
              activation='softmax',
              W_regularizer=l2(weight_decay),
              b_regularizer=l2(weight_decay))(x)

    densenet = Model(input=model_input, output=x, name="create_dense_net")

    if verbose:
        if bottleneck and not reduction:
            print("Bottleneck DenseNet-B-%d-%d created." %
                  (depth, growth_rate))
        elif not bottleneck and reduction > 0.0:
            print("DenseNet-C-%d-%d with %0.1f compression created." %
                  (depth, growth_rate, compression))
        elif bottleneck and reduction > 0.0:
            print(
                "Bottleneck DenseNet-BC-%d-%d with %0.1f compression created."
                % (depth, growth_rate, compression))
        else:
            print("DenseNet-%d-%d created." % (depth, growth_rate))

    return densenet
Пример #7
0
def create_model(img_rows=224,
                 img_cols=224,
                 color_type=3,
                 nb_dense_block=4,
                 growth_rate=32,
                 nb_filter=64,
                 reduction=0.5,
                 dropout_rate=0.0,
                 weight_decay=1e-4):
    '''
        DenseNet 169 Model for Keras

        Model Schema is based on
        https://github.com/flyyufelix/DenseNet-Keras

        ImageNet Pretrained Weights
        Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfN0d3T1F1MXg0NlU
        TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfSEc5UC1ROUFJdmM

        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras models instance.
        '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 32, 32]  # For DenseNet-169

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter,
                      7,
                      7,
                      subsample=(2, 2),
                      name='conv1',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    x_fc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_fc = Dense(1000, name='fc6')(x_fc)
    x_fc = Activation('softmax', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')

    return model, x, final_stage, img_input
Пример #8
0
def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1,
                       nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1e-4,
                       subsample_initial_block=False, activation='softmax'):
    concat_axis = 1 if K.image_data_format() == 'channels_first' else -1

    if reduction != 0.0:
        assert 1.0 >= reduction > 0.0, 'reduction value must lie between 0.0 and 1.0'

    # layers in each dense block
    if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
        nb_layers = list(nb_layers_per_block)  # Convert tuple to list

        assert len(nb_layers) == nb_dense_block, 'If list, nb_layer is used as provided. ' \
                                                   'Note that list size must be (nb_dense_block)'
        final_nb_layer = nb_layers[-1]
        nb_layers = nb_layers[:-1]
    else:
        if nb_layers_per_block == -1:
            assert (depth - 4) % 3 == 0, 'Depth must be 3 N + 4 if nb_layers_per_block == -1'
            count = int((depth - 4) / 3)

            if bottleneck:
                count = count // 2

            nb_layers = [count for _ in range(nb_dense_block)]
            final_nb_layer = count
        else:
            final_nb_layer = nb_layers_per_block
            nb_layers = [nb_layers_per_block] * nb_dense_block

    # compute initial nb_filter if -1, else accept users initial nb_filter
    if nb_filter <= 0:
        nb_filter = 2 * growth_rate

    # compute compression factor
    compression = 1.0 - reduction

    # Initial convolution
    if subsample_initial_block:
        initial_kernel = (7, 7)
        initial_strides = (2, 2)
    else:
        initial_kernel = (3, 3)
        initial_strides = (1, 1)

    x = Conv2D(nb_filter, initial_kernel, kernel_initializer='he_normal', padding='same',
               strides=initial_strides, use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)

    if subsample_initial_block:
        x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck,
                                     dropout_rate=dropout_rate, weight_decay=weight_decay)
        # add transition_block
        x = __transition_block(x, nb_filter, compression=compression, weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck,
                                 dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)

    if include_top:
        x = Dense(nb_classes, activation=activation)(x)

    return x
Пример #9
0
def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1,
                       nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1E-4):
    ''' Build the DenseNet model

    Args:
        nb_classes: number of classes
        img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        include_top: flag to include the final Dense layer
        depth: number or layers
        nb_dense_block: number of dense blocks to add to end (generally = 3)
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate
        nb_layers_per_block: number of layers in each dense block.
                Can be a -1, positive integer or a list.
                If -1, calculates nb_layer_per_block from the depth of the network.
                If positive integer, a set number of layers per dense block.
                If list, nb_layer is used as provided. Note that list size must
                be (nb_dense_block + 1)
        bottleneck: add bottleneck blocks
        reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression
        dropout_rate: dropout rate
        weight_decay: weight decay

    Returns: keras tensor with nb_layers of conv_block appended
    '''

    concat_axis = 1 if K.image_dim_ordering() == "th" else -1

    assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"
    if reduction != 0.0:
        assert reduction <= 1.0 and reduction > 0.0, "reduction value must lie between 0.0 and 1.0"

    # layers in each dense block
    if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
        nb_layers = list(nb_layers_per_block)  # Convert tuple to list

        assert len(nb_layers) == (nb_dense_block + 1), "If list, nb_layer is used as provided. " \
                                                       "Note that list size must be (nb_dense_block + 1)"
        final_nb_layer = nb_layers[-1]
        nb_layers = nb_layers[:-1]
    else:
        if nb_layers_per_block == -1:
            count = int((depth - 4) / 3)
            nb_layers = [count for _ in range(nb_dense_block)]
            final_nb_layer = count
        else:
            final_nb_layer = nb_layers_per_block
            nb_layers = [nb_layers_per_block] * nb_dense_block

    if bottleneck:
        nb_layers = [int(layer // 2) for layer in nb_layers]

    # compute initial nb_filter if -1, else accept users initial nb_filter
    if nb_filter <= 0:
        nb_filter = 2 * growth_rate

    # compute compression factor
    compression = 1.0 - reduction

    # Initial convolution
    x = Conv2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", name="initial_conv2D", use_bias=False,
               kernel_regularizer=l2(weight_decay))(img_input)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck,
                                     dropout_rate=dropout_rate, weight_decay=weight_decay)
        # add transition_block
        x = __transition_block(x, nb_filter, compression=compression, dropout_rate=dropout_rate,
                               weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck,
                                 dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay),
                           beta_regularizer=l2(weight_decay))(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)

    if include_top:
        x = Dense(nb_classes, activation='softmax', kernel_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay))(x)

    return x
Пример #10
0
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D

model = Sequential()
model.add(
    Conv2D(32, 3, padding="same", activation="relu",
           input_shape=(180, 180, 3)))
model.add(MaxPooling2D())
model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPooling2D())
model.add(Conv2D(128, 3, padding="same", activation="relu"))
model.add(MaxPooling2D())
model.add(GlobalAveragePooling2D())
model.add(Dense(num_classes, activation="softmax"))

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

model.summary()

# To train the model:
model.fit_generator(
    train_gen,
    steps_per_epoch=10,  #num_train_images // batch_size,
    epochs=3,
    validation_data=val_gen,
    validation_steps=10,  #num_val_images // batch_size,
def IceNet(classes=1000,
           input_shape=(1, 30, 30, 3),
           weights_path=None,
           include_top=True,
           only_preprocess_fn_needed=False):
    if only_preprocess_fn_needed:
        return None, preprocess_input_vgg
    img_input_1 = Input(shape=input_shape, name='band_1')
    # img_input_2 = Input(shape=input_shape,name = 'band_2')
    # angle_input = Input(shape=[1],name='inc_angle')

    x = residual_block(img_input_1, 64, (1, 3, 3), 1, 1)
    x = residual_block(x, 128, (1, 3, 3), 1, 2)
    x = residual_block(x, 256, (1, 3, 3), 1, 3)

    # y = residual_block(img_input_2,64,(3,3),2,1)
    # y = residual_block(y,128,(3,3),2,2)
    # y = residual_block(y,256,(3,3),2,3)

    # x = Concatenate()([x,y,angle_input])
    # x = Concatenate(axis = -1)([x,y])
    ##x = residual_block(x,512,(3,3),3,1)
    # x = residual_block(x,512,(3,3),3,2)
    ##x = residual_block(x,1024,(3,3),3,3)
    x = Conv3D(256, (1, 1, 1), padding='same', name='block%d_conv' % 4)(x)
    #x = K.squeeze(x,1)

    x = Lambda(lambda z: K.squeeze(z, 1), name='squeeze')(x)
    x = GlobalAveragePooling2D(name='avgpool3')(x)

    # x = Dense(512,name="fc1")(x)
    # x = Activation('relu', name='DenseRelu1')(x)
    # x = Dropout(0.1)(x)

    # x = Concatenate()([x,angle_input])
    # x =  L2Norm(alpha = 20, name='L2Norm_Scaled1')(x)
    # x = AlphaL2Norm(alpha = 20, name='AlphaL2Norm')(x)
    """x = Dense(256,name="fc1")(x)
    x = Activation('relu', name='DenseRelu1')(x)
    x = Dropout(0.1)(x)

    # x =  L2Norm(alpha = 20, name='L2Norm_Scaled2')(x)
    # x = L2Norm(alpha = 20, name='L2Norm_Scaled2')(x)
    # x = Dense(128,name="fc2")(x)
    # x = Activation('relu', name='DenseRelu2')(x)
    # x = Dropout(0.2)(x)

    x = Dense(classes, name='fc_pred')(x)
        # x = Activation('softmax', name='prob')(x)
    x = Activation('sigmoid', name='prob')(x)"""

    # x = Flatten()(x)
    x = Dense(16, name="fc1")(x)
    x = Activation('relu', name='DenseRelu1')(x)
    x = Dense(16, name="fc2")(x)
    x = Activation('relu', name='DenseRelu2')(x)
    x = Dense(16, name="fc3")(x)
    x = Activation('relu', name='DenseRelu3')(x)
    x = Dense(classes, activation='linear')(x)

    # model = Model([img_input_1,img_input_2,angle_input], x, name='icenet')
    model = Model(img_input_1, x, name='icenet')
    return model, preprocess_input_vgg
size = (512, 512)

#Load trainig and validation data
train_X, train_Y, valid_X, valid_Y = loadTrainAndValidationDatasets(size)

input_shape = (512, 512, 3)

#Initialize the base inception model.
base_model = InceptionV3(weights='imagenet',
                         include_top=False,
                         input_shape=input_shape)

#Add dense and 8-softmax layer on top for classifying the 8 fish categories
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(8, activation='softmax')(x)

model = Model(input=base_model.input, output=predictions)

#Now make base model untrainable and only train the layers added on top.
for layer in base_model.layers:
    layer.trainable = False

#Compile and fit this model
model.compile(optimizer=Adam(lr=0.001),
              loss="categorical_crossentropy",
              metrics=['accuracy'])

model.fit(x=train_X,
Пример #13
0
def create_ResNet(nb_classes,
                  img_dim,
                  nb_blocks=[4, 4, 4],
                  k=1,
                  weight_decay=1E-4,
                  droprate=0.):
    """
    :param nb_classes: the number of your dataset classes,
    for cifar-10, nb_classes should be 10
    :param img_dim: the input shape of the model input
    :param nb_blocks: the number of blocks in each stage
    :param k: the widen fatcor, k=1 indicates that the model
    is original ResNet, when k>1 the model is a wide ResNet
    :param weight_decay: weight decay for L2 regularization
    :param droprate: the dropout between two convolutons of
    each block and the default drop rate is set to 0.0
    :return: ResNet model or WRN model
    """
    model_input = Input(shape=img_dim)
    stack = [16 * k, 32 * k, 64 * k]
    nb_filter = 16
    # Initial convolution
    y = Conv2D(nb_filter, (3, 3),
               kernel_initializer="he_normal",
               padding="same",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(model_input)
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
    x = Activation('relu')(x)

    # stage 1
    x = residual_block(x, stack[0], dropout_rate=droprate)
    if stack[0] != 16:
        y = Conv2D(stack[0], (1, 1),
                   kernel_initializer="he_normal",
                   padding="same",
                   use_bias=False,
                   kernel_regularizer=l2(weight_decay))(y)
    y = add([x, y])
    for j in range(nb_blocks[0] - 1):
        x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
        x = Activation('relu')(x)
        x = residual_block(x, stack[0], dropout_rate=droprate)

        y = add([x, y])

    # stage 2
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
    y = Activation('relu')(x)

    x = residual_block(y, stack[1], strides=(2, 2), dropout_rate=droprate)
    y = Conv2D(stack[1], (1, 1),
               strides=(2, 2),
               kernel_initializer="he_normal",
               padding="valid",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(y)
    y = add([x, y])

    for j in range(nb_blocks[1] - 1):
        x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
        x = Activation('relu')(x)
        x = residual_block(x, stack[1], dropout_rate=droprate)

        y = add([x, y])

    # stage 3
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
    y = Activation('relu')(x)

    x = residual_block(y, stack[2], strides=(2, 2), dropout_rate=droprate)
    y = Conv2D(stack[2], (1, 1),
               strides=(2, 2),
               kernel_initializer="he_normal",
               padding="valid",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(y)
    y = add([x, y])

    for j in range(nb_blocks[2] - 1):
        x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
        x = Activation('relu')(x)
        x = residual_block(x, stack[2], dropout_rate=droprate)

        y = add([x, y])

    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
    x = Activation('relu')(x)

    x = GlobalAveragePooling2D()(x)
    x = Dense(nb_classes,
              activation='softmax',
              kernel_regularizer=l2(weight_decay),
              bias_regularizer=l2(weight_decay))(x)

    model = Model(input=[model_input], output=[x])

    return model
def mgcNetArchRes(outLayer, l2_val, **kwargs):
    """
    CNN architecture - without maximum pooling (replaced by convolutional layer of stride 2)
    Network architecture summary and plot
    The output layers, either multiple layer perceptron network or maximum pooling
    Return end-to-end network architecture to be compiled and trained
    
    Argumnents:
        input_img_rows: horizontal dimension in pixel of input image
        input_img_cols:vertical dimension in pixel of input image
        channels: number of colour channel
        nb_classes: number of unique classification class exist in the dataset target
    """

    def_vals = {
        "input_img_rows": 72,
        "input_img_cols": 72,
        "channels": 1,
        "nb_classes": 13
    }  # default parameters value

    for k, v in def_vals.items():
        kwargs.setdefault(k, v)

    input_img_rows = kwargs['input_img_rows']
    input_img_cols = kwargs['input_img_cols']
    channels = kwargs['channels']
    nb_classes = kwargs['nb_classes']

    # Input: 72 x 72 x 1
    img_shape = layers.Input(shape=(input_img_rows, input_img_cols, channels))

    # Layer 1
    #------------------------
    conv1 = layers.Conv2D(
        filters=32,
        kernel_size=(2, 2),
        padding='same',
        kernel_regularizer=regularizers.l2(l2_val))(img_shape)
    conv1 = layers.Activation('relu')(conv1)
    conv1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)
    conv1 = layers.Dropout(0.4)(conv1)

    conv1 = residual_block(img_shape,
                           32,
                           _strides=(1, 1),
                           _project_shortcut=False)

    # Layer 2
    #------------------------
    conv2 = residual_block(conv1, 32, _strides=(1, 1), _project_shortcut=False)

    # Layer 3
    #------------------------
    conv3 = residual_block(conv2, 32, _strides=(1, 1), _project_shortcut=False)

    # Layer 4
    # -----------------------
    #residual = residual_block(conv3, 64, _strides=(1, 1), _project_shortcut=False)

    # Layer 5
    #------------------------
    output = layers.Conv2D(filters=128,
                           kernel_size=(2, 2),
                           padding='same',
                           kernel_regularizer=regularizers.l2(l2_val))(
                               conv3)  # skip layer 4
    output = layers.Activation('relu')(output)
    output = layers.Conv2D(filters=64,
                           kernel_size=(2, 2),
                           padding='same',
                           activation='relu',
                           strides=2)(output)
    output = layers.Dropout(0.4)(output)

    # FC Layer
    #------------------------
    outputmlp = layers.Flatten()(output)
    outputmlp = layers.Dense(64, activation='relu')(outputmlp)
    outputmlp = layers.Dropout(0.5)(outputmlp)

    predictionsMlp = layers.Dense(nb_classes, activation='softmax')(outputmlp)

    # global averaging
    weight_decay = 1E-4
    concat_axis = 1

    x = BatchNormalization(
        axis=concat_axis,
        gamma_regularizer=regularizers.l2(weight_decay),
        beta_regularizer=regularizers.l2(weight_decay))(output)
    x = Activation('relu')(x)
    x = layers.Dropout(0.4)(x)
    x = GlobalAveragePooling2D(data_format=K.image_data_format())(x)

    predictionsGloAvg = layers.Dense(
        nb_classes,
        activation='softmax',
        kernel_regularizer=regularizers.l2(weight_decay),
        bias_regularizer=regularizers.l2(weight_decay))(x)

    if outLayer == "gloAvg":
        predictions = predictionsGloAvg
    elif outLayer == "mlp":
        predictions = predictionsMlp

    # prediction model
    model = Model(img_shape, predictions, name='resblock')

    return model
Пример #15
0
def incepfusionv1(input,
                  demographics,
                  dropout_keep_prob=0.8,
                  num_classes=1000,
                  is_training=True,
                  scope='IncepFusionV1'):
    '''Creates the IncepFusionV1 network.'''
    with tf.variable_scope(scope, 'IncepFusionV1', [input]):
        # Input shape is 299 * 299 * 3
        x = resnet_v2_stem(input)  # Output: 35 * 35 * 256

        # 5 x Inception A
        for i in range(5):
            x = inception_resnet_v2_A(x)
            # Output: 35 * 35 * 256

        # Reduction A
        x = reduction_resnet_A(x, k=256, l=256, m=384,
                               n=384)  # Output: 17 * 17 * 896

        # 10 x Inception B
        for i in range(10):
            x = inception_resnet_v2_B(x)
            # Output: 17 * 17 * 896

        # auxiliary
        loss2_ave_pool = AveragePooling2D(pool_size=(5, 5),
                                          strides=(3, 3),
                                          name='loss2/ave_pool')(x)

        loss2_conv_a = Conv2D(128, (1, 1),
                              kernel_regularizer=l2(0.0002),
                              activation="relu",
                              padding="same")(loss2_ave_pool)
        loss2_conv_b = Conv2D(768, (5, 5),
                              kernel_regularizer=l2(0.0002),
                              activation="relu",
                              padding="same")(loss2_conv_a)

        loss2_conv_b = BatchNormalization(axis=3)(loss2_conv_b)

        loss2_conv_b = Activation('relu')(loss2_conv_b)

        loss2_flat = Flatten()(loss2_conv_b)

        loss2_fc = Dense(1024,
                         activation='relu',
                         name='loss2/fc',
                         kernel_regularizer=l2(0.0002))(loss2_flat)

        loss2_drop_fc = Dropout(dropout_keep_prob)(loss2_fc,
                                                   training=is_training)

        loss2_classifier = Dense(num_classes,
                                 name='loss2/classifier',
                                 kernel_regularizer=l2(0.0002))(loss2_drop_fc)

        # Reduction B
        x = reduction_resnet_v2_B(x)  # Output: 8 * 8 * 1792

        # 5 x Inception C
        for i in range(5):
            x = inception_resnet_v2_C(x)
            # Output: 8 * 8 * 1792

        net = x

        # Average Pooling
        x = GlobalAveragePooling2D(name='avg_pool')(x)  # Output: 1792

        pool5_drop_10x10_s1 = Dropout(dropout_keep_prob)(x,
                                                         training=is_training)

        demographics = Dense(5,
                             name='demographic_fc1',
                             activation="relu",
                             kernel_regularizer=l2(0.0002))(demographics)

        merged = concatenate([pool5_drop_10x10_s1, demographics])

        loss3_classifier_W = Dense(num_classes,
                                   name='loss3/classifier',
                                   kernel_regularizer=l2(0.0002))

        loss3_classifier = loss3_classifier_W(merged)

        w_variables = loss3_classifier_W.get_weights()[0]

        logits = tf.cond(
            tf.equal(is_training, tf.constant(True)),
            lambda: tf.add(loss3_classifier,
                           tf.scalar_mul(tf.constant(0.3), loss2_classifier)),
            lambda: loss3_classifier)

        return logits, net, tf.convert_to_tensor(w_variables)
Пример #16
0
    def _build_base_model(self):
        model = Sequential()

        # section 1

        model.add(
            Convolution2D(filters=32,
                          kernel_size=5,
                          strides=2,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal",
                          input_shape=(self.n_frames, self.n_bands,
                                       self.n_channels)))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(
            Convolution2D(filters=32,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Dropout(0.3))

        # section 2
        model.add(
            Convolution2D(filters=64,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(
            Convolution2D(filters=64,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.3))

        # section 3
        model.add(
            Convolution2D(filters=128,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(
            Convolution2D(filters=128,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(
            Convolution2D(filters=128,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(
            Convolution2D(filters=128,
                          kernel_size=3,
                          strides=1,
                          padding="same",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))

        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.3))

        # section 4
        model.add(
            Convolution2D(filters=512,
                          kernel_size=3,
                          strides=1,
                          padding="valid",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Dropout(0.5))

        model.add(
            Convolution2D(filters=512,
                          kernel_size=1,
                          strides=1,
                          padding="valid",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Dropout(0.5))

        # section 5
        model.add(
            Convolution2D(filters=10,
                          kernel_size=1,
                          strides=1,
                          padding="valid",
                          kernel_regularizer=l2(0.0001),
                          kernel_initializer="normal"))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(GlobalAveragePooling2D())

        model.add(Activation('softmax'))

        # load the saved checkpoint weights
        # model.load_weights('/Users/nbip/proj/dtu/dtu-bach/dev/sound_classification_weights.hdf5')
        model.load_weights(self.weights_path)

        # pop the top layers?
        # Try to remove a different number of layers. Removing 2 seems to work okay
        # Previous one was model.layers.pop() which did not work
        for _ in range(2):
            model.pop()

        # needed fix, to have an output of the model
        model.outputs = [model.layers[-1].output]
        model.layers[-1].outbound_nodes = []

        return model
Пример #17
0
def create_model(nb_classes, input_shape, config=None):
    """Create a VGG-16 like model."""
    if len(input_shape) != 3:
        raise Exception("Input shape should be a tuple (nb_channels, nb_rows, "
                        "nb_cols) or (nb_rows, nb_cols, nb_channels), "
                        "depending on your backend.")
    if config is None:
        config = {'model': {}}

    min_feature_map_dimension = min(input_shape[:2])
    if min_feature_map_dimension < 32:
        print("ERROR: Please upsample the feature maps to have at least "
              "a size of 32 x 32. Currently, it has {}".format(input_shape))
    nb_filter = 32

    # Network definition
    # input_shape = (None, None, 3)  # for fcn
    input_ = Input(shape=input_shape)
    x = input_

    # Scale feature maps down to [63, 32] x [63, 32]
    tmp = min_feature_map_dimension / 32.
    if tmp >= 2:
        while tmp >= 2.:
            for _ in range(2):
                x = Convolution2D(nb_filter, (3, 3), padding='same',
                                  kernel_initializer='he_uniform',
                                  kernel_regularizer=l2(0.0001))(x)
                x = BatchNormalization()(x)
                x = Activation('elu')(x)
            x = MaxPooling2D(pool_size=(2, 2))(x)
            nb_filter *= 2
            tmp /= 2

    # 32x32
    # Color transformation
    x = Convolution2D(10, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = Activation('elu')(x)
    x = Convolution2D(3, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = Activation('elu')(x)
    # Normal network
    x = Convolution2D(nb_filter, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Convolution2D(nb_filter, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Convolution2D(nb_filter, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)

    # 16x16
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Convolution2D(2 * nb_filter, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Convolution2D(2 * nb_filter, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)

    # 8x8
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Convolution2D(2 * nb_filter, (3, 3), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)

    # 4x4
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Convolution2D(505, (4, 4),
                      padding='valid',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Dropout(0.5)(x)

    # 1x1
    x = Convolution2D(512, (1, 1), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = BatchNormalization()(x)
    x = Activation('elu')(x)
    x = Dropout(0.5)(x)
    x = Convolution2D(nb_classes, (1, 1), padding='same',
                      kernel_initializer='he_uniform',
                      kernel_regularizer=l2(0.0001))(x)
    x = GlobalAveragePooling2D()(x)  # Adjust for FCN
    x = BatchNormalization()(x)
    x = Activation('softmax')(x)
    model = Model(inputs=input_, outputs=x)
    return model
Пример #18
0
squeeze.add(ELU(alpha=1.0))

squeeze_left = squeeze
squeeze_left.add(
    Convolution2D(2, 3, 3, border_mode='valid', input_shape=input_shape))
squeeze_left.add(ELU(alpha=1.0))

squeeze_right = squeeze
squeeze_right.add(
    Convolution2D(2, 3, 3, border_mode='valid', input_shape=input_shape))
squeeze_right.add(ELU(alpha=1.0))

squeeze0 = Sequential()
squeeze0.add(Merge([squeeze_left, squeeze_right], mode='concat'))
squeeze0.add(Dropout(0.2))
squeeze0.add(GlobalAveragePooling2D((1, 28, 28, 1)))
squeeze0.add(Dense(1))
squeeze0.add(Activation('sigmoid'))
squeeze0.compile(loss='mean_squared_error',
                 optimizer=sgd,
                 metrics=['accuracy'])
squeeze0.summary()

squeeze0.fit(x_train_CNN,
             np.array(y_train2),
             nb_epoch=15,
             batch_size=30,
             verbose=1)

a1 = squeeze0.predict_classes(x_train_CNN, verbose=1)
a1
Пример #19
0
def __create_nas_cell_net(nb_classes, img_input, include_top, init_filters=DEFAULT_INIT_FILTERS, 
                        num_reduction_cells=DEFAULT_NUM_REDUCTION, repeat_val=DEFAULT_NUM_REPEAT_VALUE,
                        use_droppath=USE_DROPPATH, dropout_rate=DEFAULT_DROPOUT_RATE, 
                        weight_decay=DEFAULT_WEIGHT_DECAY, activation='softmax'):
    ''' Build the NAS Cell model
    Args:
        nb_classes: number of classes
        img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        include_top: flag to include the final Dense layer
        init_filters: initial number of filters. Default -1 indicates initial number of filters is 128.
        num_reduction_cells: number of 2x2 strided reductions cells
        repeat_val: number of normal NAS convolutional cells surrounding the reduction cells        
        dropout_rate: dropout rate
        weight_decay: weight decay
        activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'.
                Note that if sigmoid is used, classes must be 1.
                
    Returns: keras tensor with nb_layers of conv_block appended
    '''
    
    # Get channel axis
    concat_axis = 1 if K.image_data_format() == 'channels_first' else -1
    
    
    # Create drop table
    total_elements_per_NAS_cell = COMBINATIONS_PER_LAYER * ELEMENTS_PER_COMBINATION
    total_cells = num_reduction_cells + ((num_reduction_cells + 1) * repeat_val)
    total_elements = total_cells * total_elements_per_NAS_cell
    
    if use_droppath:
        drop_table = []
        
        for _ in range(total_elements):
            death_rate = K.variable(DEFAULT_DEATH_RATE)
            gate = K.variable(1, dtype='uint16')    
            drop_table.append({"death_rate": death_rate, "gate": gate})
            
    else:
        drop_table = [None] * total_elements    

        
    # Begin the process of Normal NAS Cell and Reduction NAS cell sequences        
    dt_start = 0
    dt_end = total_elements_per_NAS_cell    

    cur_hid = (img_input, True)
    prev_hid = (img_input, True)
    
    num_channels = init_filters
    for i in range(num_reduction_cells + 1):
        # Iterate N times through normal cell
        for _ in range(repeat_val):
            cur_spatial, cur_channels = layer_into_spatial_and_channels(cur_hid[0])
            prev_hid = make_prev_match_cur_layer(prev_hid, cur_spatial, cur_channels)

            tmp_prev_hid = cur_hid            
            cur_hid = __normal_nas_cell(prev_hid, cur_hid, num_channels, 
                                                drop_table[dt_start:dt_end], weight_decay)
            prev_hid = tmp_prev_hid
            
            dt_start += total_elements_per_NAS_cell
            dt_end += total_elements_per_NAS_cell

        # Double number of channels and pass through reduction cell
        num_channels = int(num_channels * 2.5)
        if i < num_reduction_cells:
            cur_spatial, cur_channels = layer_into_spatial_and_channels(cur_hid[0])
            prev_hid = make_prev_match_cur_layer(prev_hid, cur_spatial, cur_channels)

            tmp_prev_hid = cur_hid            
            cur_hid = __reduction_nas_cell(prev_hid, cur_hid, num_channels, 
                                                drop_table[dt_start:dt_end], weight_decay)
            prev_hid = tmp_prev_hid
            
            dt_start += total_elements_per_NAS_cell
            dt_end += total_elements_per_NAS_cell
            
            
    # Average pool, apply dropout (if desired), and apply final FC layer
    x = cur_hid[0]
    
    x = Activation('relu')(x)    
    x = GlobalAveragePooling2D(name="final_embeddings")(x)
    x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay),
                               beta_regularizer=l2(weight_decay))(x)
    
    if include_top:
        if dropout_rate:
            x = Dropout(dropout_rate, name="final_dropout")(x)
            
        x = Dense(nb_classes, activation=activation, kernel_regularizer=l2(weight_decay), 
                        bias_regularizer=l2(weight_decay))(x)

                        
    # Return classification logits and drop table                 
    return x, drop_table
Пример #20
0
def ResNet(input_shape=None,
           classes=10,
           block='bottleneck',
           residual_unit='v2',
           repetitions=None,
           initial_filters=64,
           activation='softmax',
           include_top=True,
           input_tensor=None,
           dropout=None,
           transition_dilation_rate=(1, 1),
           initial_strides=(2, 2),
           initial_kernel_size=(7, 7),
           initial_pooling='max',
           final_pooling=None,
           top='classification'):
    """Builds a custom ResNet like architecture. Defaults to ResNet50 v2.

    Args:
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` dim ordering)
            or `(3, 224, 224)` (with `channels_first` dim ordering).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 8.
            E.g. `(224, 224, 3)` would be one valid value.
        classes: The number of outputs at final softmax layer
        block: The block function to use. This is either `'basic'` or `'bottleneck'`.
            The original paper used `basic` for layers < 50.
        repetitions: Number of repetitions of various block units.
            At each block unit, the number of filters are doubled and the input size is halved.
            Default of None implies the ResNet50v2 values of [3, 4, 6, 3].
        transition_dilation_rate: Used for pixel-wise prediction tasks such as image segmentation.
        residual_unit: the basic residual unit, 'v1' for conv bn relu, 'v2' for bn relu conv.
            See [Identity Mappings in Deep Residual Networks](https://arxiv.org/abs/1603.05027)
            for details.
        dropout: None for no dropout, otherwise rate of dropout from 0 to 1.
            Based on [Wide Residual Networks.(https://arxiv.org/pdf/1605.07146) paper.
        transition_dilation_rate: Dilation rate for transition layers. For semantic
            segmentation of images use a dilation rate of (2, 2).
        initial_strides: Stride of the very first residual unit and MaxPooling2D call,
            with default (2, 2), set to (1, 1) for small images like cifar.
        initial_kernel_size: kernel size of the very first convolution, (7, 7) for imagenet
            and (3, 3) for small image datasets like tiny imagenet and cifar.
            See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
        initial_pooling: Determine if there will be an initial pooling layer,
            'max' for imagenet and None for small image datasets.
            See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
        final_pooling: Optional pooling mode for feature extraction at the final model layer
            when `include_top` is `False`.
            - `None` means that the output of the model
                will be the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a
                2D tensor.
            - `max` means that global max pooling will
                be applied.
        top: Defines final layers to evaluate based on a specific problem type. Options are
            'classification' for ImageNet style problems, 'segmentation' for problems like
            the Pascal VOC dataset, and None to exclude these layers entirely.

    Returns:
        The keras `Model`.
    """
    if activation not in ['softmax', 'sigmoid', None]:
        raise ValueError(
            'activation must be one of "softmax", "sigmoid", or None')
    if activation == 'sigmoid' and classes != 1:
        raise ValueError(
            'sigmoid activation can only be used when classes = 1')
    if repetitions is None:
        repetitions = [3, 4, 6, 3]
    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=32,
                                      min_size=8,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top)
    _handle_dim_ordering()
    if len(input_shape) != 3:
        raise Exception(
            "Input shape should be a tuple (nb_channels, nb_rows, nb_cols)")

    if block == 'basic':
        block_fn = basic_block
    elif block == 'bottleneck':
        block_fn = bottleneck
    elif isinstance(block, six.string_types):
        block_fn = _string_to_function(block)
    else:
        block_fn = block

    if residual_unit == 'v2':
        residual_unit = _bn_relu_conv
    elif residual_unit == 'v1':
        residual_unit = _conv_bn_relu
    elif isinstance(residual_unit, six.string_types):
        residual_unit = _string_to_function(residual_unit)
    else:
        residual_unit = residual_unit

    # Permute dimension order if necessary
    if K.image_data_format() == 'channels_first':
        input_shape = (input_shape[1], input_shape[2], input_shape[0])
    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=32,
                                      min_size=8,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top)

    img_input = Input(shape=input_shape, tensor=input_tensor)
    x = _conv_bn_relu(filters=initial_filters,
                      kernel_size=initial_kernel_size,
                      strides=initial_strides)(img_input)
    if initial_pooling == 'max':
        x = MaxPooling2D(pool_size=(3, 3),
                         strides=initial_strides,
                         padding="same")(x)

    block = x
    filters = initial_filters
    for i, r in enumerate(repetitions):
        transition_dilation_rates = [transition_dilation_rate] * r
        transition_strides = [(1, 1)] * r
        if transition_dilation_rate == (1, 1):
            transition_strides[0] = (2, 2)
        block = _residual_block(
            block_fn,
            filters=filters,
            stage=i,
            blocks=r,
            is_first_layer=(i == 0),
            dropout=dropout,
            transition_dilation_rates=transition_dilation_rates,
            transition_strides=transition_strides,
            residual_unit=residual_unit)(block)
        filters *= 2

    # Last activation
    x = _bn_relu(block)

    # Classifier block
    if include_top and top is 'classification':
        x = GlobalAveragePooling2D()(x)
        x = Dense(units=classes,
                  activation=activation,
                  kernel_initializer="he_normal")(x)
    elif include_top and top is 'segmentation':
        x = Conv2D(classes, (1, 1), activation='linear', padding='same')(x)

        if K.image_data_format() == 'channels_first':
            channel, row, col = input_shape
        else:
            row, col, channel = input_shape

        x = Reshape((row * col, classes))(x)
        x = Activation(activation)(x)
        x = Reshape((row, col, classes))(x)
    elif final_pooling == 'avg':
        x = GlobalAveragePooling2D()(x)
    elif final_pooling == 'max':
        x = GlobalMaxPooling2D()(x)

    model = Model(inputs=img_input, outputs=x)
    return model
Пример #21
0
from keras.datasets import cifar10
from keras.utils import np_utils
from keras.layers.normalization import BatchNormalization

input_1 = Input(shape=(32, 32, 3), name="head")

x1 = Conv2D(16, (3, 3), padding='same', name='2-1')(input_1)
x2 = Conv2D(16, (3, 3), padding='same', name='2-2')(input_1)
x3 = Conv2D(16, (3, 3), padding='same', name='2-3')(input_1)

x4 = Conv2D(10, (3, 3), padding='same', name='3-1')(x1)
x5 = Concatenate(axis=3, name="3-2")([x1, x2])
x6 = Concatenate(axis=3, name="3-3")([x2, x3])
x7 = Conv2D(10, (3, 3), padding='same', name='3-4')(x3)

out1 = GlobalAveragePooling2D(name="4-1")(x4)
out1 = Activation('softmax', name='r_hand')(out1)

x10 = Concatenate(axis=3, name="4-2")([x5, x6])

out4 = GlobalAveragePooling2D(name="4-3")(x7)
out4 = Activation('softmax', name='l_hand')(out4)

x11 = MaxPooling2D(pool_size=(2, 2), name="5-1")(x10)

out2 = Flatten(name="6-1")(x11)
out2 = Dense(10, activation='relu', name="7-1")(out2)
out2 = Activation('softmax', name="r_foot")(out2)

out3 = Flatten(name="6-2")(x11)
out3 = Dense(10, activation='relu', name="7-2")(out3)
Пример #22
0
def get_squeezenet(nb_classes, dim_ordering='tf'):
    if dim_ordering is 'th':
        input_img = Input(shape=(3, 227, 227))
    elif dim_ordering is 'tf':
        input_img = Input(shape=(227, 227, 3))
    else:
        raise NotImplementedError("Theano and Tensorflow are only available")
    x = Convolution2D(64,
                      3,
                      3,
                      subsample=(2, 2),
                      border_mode='valid',
                      name='conv1')(input_img)
    x = Activation('relu', name='relu_conv1')(x)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x)

    x = fire_module(x,
                    fire_id=2,
                    squeeze=16,
                    expand=64,
                    dim_ordering=dim_ordering)
    x = fire_module(x,
                    fire_id=3,
                    squeeze=16,
                    expand=64,
                    dim_ordering=dim_ordering)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x)

    x = fire_module(x,
                    fire_id=4,
                    squeeze=32,
                    expand=128,
                    dim_ordering=dim_ordering)
    x = fire_module(x,
                    fire_id=5,
                    squeeze=32,
                    expand=128,
                    dim_ordering=dim_ordering)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x)

    x = fire_module(x,
                    fire_id=6,
                    squeeze=48,
                    expand=192,
                    dim_ordering=dim_ordering)
    x = fire_module(x,
                    fire_id=7,
                    squeeze=48,
                    expand=192,
                    dim_ordering=dim_ordering)
    x = fire_module(x,
                    fire_id=8,
                    squeeze=64,
                    expand=256,
                    dim_ordering=dim_ordering)
    x = fire_module(x,
                    fire_id=9,
                    squeeze=64,
                    expand=256,
                    dim_ordering=dim_ordering)
    x = Dropout(0.5, name='drop9')(x)

    x = Convolution2D(nb_classes, 1, 1, border_mode='valid', name='conv10')(x)
    x = Activation('relu', name='relu_conv10')(x)
    x = GlobalAveragePooling2D()(x)
    out = Activation('softmax', name='loss')(x)
    model = Model(input=input_img, output=[out])
    return model
def densenet121_model(img_rows,
                      img_cols,
                      color_type=1,
                      nb_dense_block=4,
                      growth_rate=32,
                      nb_filter=64,
                      reduction=0.5,
                      dropout_rate=0.0,
                      weight_decay=1e-4,
                      num_classes=None):
    '''
    DenseNet 121 Model for Keras
    Model Schema is based on
    https://github.com/flyyufelix/DenseNet-Keras
    ImageNet Pretrained Weights
    Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfMlRYb3YzV210VzQ
    TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfSTA4SHJVOHNuTXc
    # Arguments
        nb_dense_block: number of dense blocks to add to end
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters
        reduction: reduction factor of transition blocks.
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        classes: optional number of classes to classify images
        weights_path: path to pre-trained weights
    # Returns
        A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 24, 16]  # For DenseNet-121

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter,
                      7,
                      7,
                      subsample=(2, 2),
                      name='conv1',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    x_fc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_fc = Dense(1000, name='fc6')(x_fc)
    x_fc = Activation('softmax', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')

    if K.image_dim_ordering() == 'th':
        # Use pre-trained weights for Theano backend
        weights_path = 'imagenet_models/densenet121_weights_th.h5'
    else:
        # Use pre-trained weights for Tensorflow backend
        weights_path = 'imagenet_models/densenet121_weights_tf.h5'

    model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_newfc = Dense(num_classes, name='fc6')(x_newfc)
    x_newfc = Activation('softmax', name='prob')(x_newfc)

    model = Model(img_input, x_newfc)

    # Learning rate is changed to 0.001
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
Пример #24
0
def cnn_model(model_name, img_size):
    """
    Model definition using Xception net architecture
    """
    input_size = (img_size, img_size, 3)

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

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

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

    optimizer = Nadam(lr=0.002,
                      beta_1=0.9,
                      beta_2=0.999,
                      epsilon=1e-08,
                      schedule_decay=0.004)
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizer,
                  metrics=["accuracy"])
    return model
Пример #25
0
def __create_res_next_imagenet(nb_classes,
                               img_input,
                               include_top,
                               depth,
                               cardinality=32,
                               width=4,
                               weight_decay=5e-4,
                               pooling=None,
                               attention_module=None,
                               activation='softmax'):
    ''' Creates a ResNeXt model with specified parameters
    Args:
        nb_classes: Number of output classes
        img_input: Input tensor or layer
        include_top: Flag to include the last dense layer
        depth: Depth of the network. List of integers.
               Increasing cardinality improves classification accuracy,
        width: Width of the network.
        weight_decay: weight_decay (l2 norm)
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
    Returns: a Keras Model
    '''

    if type(depth) is list or type(depth) is tuple:
        # If a list is provided, defer to user how many blocks are present
        N = list(depth)
    else:
        # Otherwise, default to 3 blocks each of default number of group convolution blocks
        N = [(depth - 2) // 9 for _ in range(3)]

    filters = cardinality * width
    filters_list = []

    for i in range(len(N)):
        filters_list.append(filters)
        filters *= 2  # double the size of the filters

    x = __initial_conv_block_inception(img_input, weight_decay)

    # block 1 (no pooling)
    for i in range(N[0]):
        x = __bottleneck_block(x,
                               filters_list[0],
                               cardinality,
                               strides=1,
                               weight_decay=weight_decay,
                               attention_module=attention_module)

    N = N[1:]  # remove the first block from block definition list
    filters_list = filters_list[
        1:]  # remove the first filter from the filter list

    # block 2 to N
    for block_idx, n_i in enumerate(N):
        for i in range(n_i):
            if i == 0:
                x = __bottleneck_block(x,
                                       filters_list[block_idx],
                                       cardinality,
                                       strides=2,
                                       weight_decay=weight_decay,
                                       attention_module=attention_module)
            else:
                x = __bottleneck_block(x,
                                       filters_list[block_idx],
                                       cardinality,
                                       strides=1,
                                       weight_decay=weight_decay,
                                       attention_module=attention_module)

    if include_top:
        x = GlobalAveragePooling2D()(x)
        x = Dense(nb_classes,
                  use_bias=False,
                  kernel_regularizer=l2(weight_decay),
                  kernel_initializer='he_normal',
                  activation=activation)(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    return x
def discriminator(img_dim, bn_mode, model_name="discriminator"):
    """DCGAN discriminator

    Args:
        img_dim: dimension of the image output
        bn_mode: keras batchnorm mode
        model_name: model name (default: {"generator_deconv"})

    Returns:
        keras model
    """

    if K.image_dim_ordering() == "th":
        bn_axis = 1
        min_s = min(img_dim[1:])
    else:
        bn_axis = -1
        min_s = min(img_dim[:-1])

    disc_input = Input(shape=img_dim, name="discriminator_input")

    # Get the list of number of conv filters
    # (first layer starts with 64), filters are subsequently doubled
    nb_conv = int(np.floor(np.log(min_s // 4) / np.log(2)))
    list_f = [64 * min(8, (2**i)) for i in range(nb_conv)]

    # First conv with 2x2 strides
    x = Conv2D(list_f[0], (3, 3),
               strides=(2, 2),
               name="disc_conv2d_1",
               padding="same",
               use_bias=False,
               kernel_initializer=RandomNormal(stddev=0.02))(disc_input)
    x = BatchNormalization(axis=bn_axis)(x)
    x = LeakyReLU(0.2)(x)

    # Conv blocks: Conv2D(2x2 strides)->BN->LReLU
    for i, f in enumerate(list_f[1:]):
        name = "disc_conv2d_%s" % (i + 2)
        x = Conv2D(f, (3, 3),
                   strides=(2, 2),
                   name=name,
                   padding="same",
                   use_bias=False,
                   kernel_initializer=RandomNormal(stddev=0.02))(x)
        x = BatchNormalization(axis=bn_axis)(x)
        x = LeakyReLU(0.2)(x)

    # Last convolution
    x = Conv2D(1, (3, 3),
               name="last_conv",
               padding="same",
               use_bias=False,
               kernel_initializer=RandomNormal(stddev=0.02))(x)
    # Average pooling
    x = GlobalAveragePooling2D()(x)

    discriminator_model = Model(inputs=[disc_input],
                                outputs=[x],
                                name=model_name)
    visualize_model(discriminator_model)

    return discriminator_model
Пример #27
0
def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1,
                       nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1e-4,
                       subsample_initial_block=False, activation='softmax'):
    ''' Build the DenseNet model
    Args:
        nb_classes: number of classes
        img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        include_top: flag to include the final Dense layer
        depth: number or layers
        nb_dense_block: number of dense blocks to add to end (generally = 3)
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate
        nb_layers_per_block: number of layers in each dense block.
                Can be a -1, positive integer or a list.
                If -1, calculates nb_layer_per_block from the depth of the network.
                If positive integer, a set number of layers per dense block.
                If list, nb_layer is used as provided. Note that list size must
                be (nb_dense_block + 1)
        bottleneck: add bottleneck blocks
        reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression
        dropout_rate: dropout rate
        weight_decay: weight decay rate
        subsample_initial_block: Set to True to subsample the initial convolution and
                add a MaxPool2D before the dense blocks are added.
        subsample_initial:
        activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'.
                Note that if sigmoid is used, classes must be 1.
    Returns: keras tensor with nb_layers of conv_block appended
    '''

    concat_axis = 1 if K.image_data_format() == 'channels_first' else -1

    if reduction != 0.0:
        assert reduction <= 1.0 and reduction > 0.0, 'reduction value must lie between 0.0 and 1.0'

    # layers in each dense block
    if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
        nb_layers = list(nb_layers_per_block)  # Convert tuple to list

        assert len(nb_layers) == (nb_dense_block), 'If list, nb_layer is used as provided. ' \
                                                   'Note that list size must be (nb_dense_block)'
        final_nb_layer = nb_layers[-1]
        nb_layers = nb_layers[:-1]
    else:
        if nb_layers_per_block == -1:
            assert (depth - 4) % 3 == 0, 'Depth must be 3 N + 4 if nb_layers_per_block == -1'
            count = int((depth - 4) / 3)
            nb_layers = [count for _ in range(nb_dense_block)]
            final_nb_layer = count
        else:
            final_nb_layer = nb_layers_per_block
            nb_layers = [nb_layers_per_block] * nb_dense_block

    # compute initial nb_filter if -1, else accept users initial nb_filter
    if nb_filter <= 0:
        nb_filter = 2 * growth_rate

    # compute compression factor
    compression = 1.0 - reduction

    # Initial convolution
    if subsample_initial_block:
        initial_kernel = (7, 7)
        initial_strides = (2, 2)
    else:
        initial_kernel = (3, 3)
        initial_strides = (1, 1)

    x = Conv2D(nb_filter, initial_kernel, kernel_initializer='he_normal', padding='same',
               strides=initial_strides, use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)

    if subsample_initial_block:
        x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck,
                                     dropout_rate=dropout_rate, weight_decay=weight_decay)
        # add transition_block
        x = __transition_block(x, nb_filter, compression=compression, weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck,
                                 dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)

    if include_top:
        x = Dense(nb_classes, activation=activation)(x)

    return x
Пример #28
0
def DenseNet(nb_classes,
             img_dim,
             depth,
             nb_dense_block,
             growth_rate,
             nb_filter,
             dropout_rate=None,
             weight_decay=1E-4):
    """ Build the DenseNet model
    :param nb_classes: int -- number of classes
    :param img_dim: tuple -- (channels, rows, columns)
    :param depth: int -- how many layers
    :param nb_dense_block: int -- number of dense blocks to add to end
    :param growth_rate: int -- number of filters to add
    :param nb_filter: int -- number of filters
    :param dropout_rate: float -- dropout rate
    :param weight_decay: float -- weight decay
    :returns: keras model with nb_layers of conv_factory appended
    :rtype: keras model
    """

    model_input = Input(shape=img_dim)

    assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"

    # layers in each dense block
    nb_layers = int((depth - 4) / 3)

    # Initial convolution
    x = Convolution2D(nb_filter,
                      3,
                      3,
                      init="he_uniform",
                      border_mode="same",
                      name="initial_conv2D",
                      bias=False,
                      W_regularizer=l2(weight_decay))(model_input)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = denseblock(x,
                                  nb_layers,
                                  nb_filter,
                                  growth_rate,
                                  dropout_rate=dropout_rate,
                                  weight_decay=weight_decay)
        # add transition
        x = transition(x,
                       nb_filter,
                       dropout_rate=dropout_rate,
                       weight_decay=weight_decay)

    # The last denseblock does not have a transition
    x, nb_filter = denseblock(x,
                              nb_layers,
                              nb_filter,
                              growth_rate,
                              dropout_rate=dropout_rate,
                              weight_decay=weight_decay)

    x = BatchNormalization(mode=0,
                           axis=1,
                           gamma_regularizer=l2(weight_decay),
                           beta_regularizer=l2(weight_decay))(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D(dim_ordering="th")(x)
    x = Dense(nb_classes,
              activation='softmax',
              W_regularizer=l2(weight_decay),
              b_regularizer=l2(weight_decay))(x)

    densenet = Model(input=[model_input], output=[x], name="DenseNet")
    # optimizer=SGD
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    densenet.compile(loss='categorical_crossentropy',
                     optimizer=sgd,
                     metrics=['accuracy', precision, recall, f1score])

    return densenet
Пример #29
0
def X4(inputa,
       inputb,
       inputc,
       demographics=None,
       dropout=0.8,
       num_cls=1000,
       is_train=True,
       scope='X4',
       supermd=False):
    with tf.variable_scope(scope, 'X4', [inputa, inputb, inputc]):
        xa, auxa = Branch(inputa,
                          dropout_keep_prob=dropout,
                          num_classes=num_cls,
                          is_training=is_train)
        xb, auxb = Branch(inputb,
                          dropout_keep_prob=dropout,
                          num_classes=num_cls,
                          is_training=is_train)
        xc, auxc = Branch(inputc,
                          dropout_keep_prob=dropout,
                          num_classes=num_cls,
                          is_training=is_train)

        x = concatenate([xa, xb, xc], axis=3)  # Output: 8 * 8 * 2688

        x = Conv2D(2688, (1, 1),
                   kernel_regularizer=l2(0.0002),
                   activation="relu",
                   padding="same")(x)

        net = x

        loss2_classifier = tf.add(auxa, tf.add(auxb, auxc))

        # Average Pooling
        x = GlobalAveragePooling2D(name='avg_pool')(x)  # Output: 2688

        pool5_drop_10x10_s1 = Dropout(dropout)(x, training=is_train)

        if supermd:
            demographics = Dense(2,
                                 name='demographic_fc1',
                                 activation="relu",
                                 kernel_regularizer=l2(0.0002))(demographics)
            merged = concatenate([pool5_drop_10x10_s1, demographics])
        else:
            merged = pool5_drop_10x10_s1

        loss3_classifier_w = Dense(num_cls,
                                   name='loss3/classifier',
                                   kernel_regularizer=l2(0.0002))

        loss3_classifier = loss3_classifier_w(merged)

        w_variables = loss3_classifier_w.get_weights()
        w_variables = w_variables[0]

        logits = tf.cond(
            tf.equal(is_train, tf.constant(True)),
            lambda: tf.add(loss3_classifier,
                           tf.scalar_mul(tf.constant(0.1), loss2_classifier)),
            lambda: loss3_classifier)

        return logits, net, tf.convert_to_tensor(w_variables)
Пример #30
0
def fit(image_dir: str = IMAGE_DIR, dump: bool = True, **kwargs):
    """ Read and resize images
    Save all the data in:
        TRAIN_X - pixels
        TRAIN_Y - labels
    """
    logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S")
    tensorboard_callback = TensorBoard(log_dir=logdir)

    model_checkpoint = ModelCheckpoint(
        str(MODELS_DIR /
            'weights-improvement-{epoch:02d}-{val_accuracy:.2f}.hdf5'),
        monitor='val_loss',
        verbose=0,
        save_best_only=False,
        save_weights_only=False,
        mode='auto',
        period=1)

    reduce_lron = ReduceLROnPlateau(monitor='val_loss',
                                    factor=0.1,
                                    patience=3,
                                    verbose=1,
                                    mode='auto')

    base_model = InceptionV3(include_top=False,
                             weights='imagenet',
                             input_shape=(WIDHT, HEIGHT, 3))
    for layer in base_model.layers:
        layer.trainable = False

    model = Sequential()
    model.add(base_model)
    model.add(GlobalAveragePooling2D())
    model.add(Dense(1024, activation='relu', kernel_regularizer=l2(0.0001)))
    model.add(Dense(LABEL_SIZE, activation='softmax'))
    model.compile(
        loss='categorical_crossentropy',
        optimizer='rmsprop',
        metrics=['accuracy'],
    )

    train_generator = augs_gen.flow_from_directory(
        directory=IMAGE_DIR,
        target_size=WH,
        batch_size=BATCH_SIZE,
        seed=1,
        shuffle=True,
        subset='training',
    )
    test_generator = augs_gen.flow_from_directory(
        directory=IMAGE_DIR,
        target_size=WH,
        batch_size=BATCH_SIZE,
        seed=1,
        shuffle=True,
        subset='validation',
    )
    labels = (train_generator.class_indices)
    labels = dict((v, k) for k, v in labels.items())

    with open(DATA_DIR / 'generator_labels.dump', 'wb') as file:
        pickle.dump(labels, file)

    with graph.as_default():
        model.fit_generator(
            train_generator,
            validation_data=test_generator,
            steps_per_epoch=train_generator.samples // BATCH_SIZE,
            validation_steps=test_generator.samples // BATCH_SIZE,
            epochs=EPOCHS,
            verbose=1,
            callbacks=[tensorboard_callback, model_checkpoint, reduce_lron],
        )
    print('Prepare to write data on the disk')

    model.save(f'{model_name}.dump')