Exemplo n.º 1
0
def resnet18_2d(image_rows=256,
                image_cols=256,
                input_channels=3,
                train_encoder=True):

    # Block 1
    # get parameters for model layers
    no_scale_bn_params = get_bn_params(train_encoder, scale=False)
    bn_params = get_bn_params(train_encoder)
    conv_params = get_conv_params(train_encoder)
    init_filters = 64

    # INPUT
    inputs = layers.Input((image_rows, image_cols, input_channels))
    # resnet bottom
    x = layers.BatchNormalization(name='bn_data', **no_scale_bn_params)(inputs)
    x = layers.ZeroPadding2D(padding=(3, 3))(x)
    x = layers.Conv2D(init_filters, (7, 7),
                      strides=(2, 2),
                      name='conv0',
                      **conv_params)(x)
    x = layers.BatchNormalization(name='bn0', **bn_params)(x)
    x = layers.Activation('relu', name='relu0')(x)
    skip_connection_1 = x
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.MaxPooling2D((3, 3),
                            strides=(2, 2),
                            padding='valid',
                            name='pooling0')(x)

    # Stage 1, Unit 1 - Settings
    stage = 0
    block = 0
    strides = (1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    # defining shortcut connection
    shortcut = layers.Conv2D(filters, (1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 1, Unit 2 - Settings
    stage = 0
    block = 1
    strides = (1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 2, Unit 1 - Settings
    stage = 1
    block = 0
    strides = (2, 2)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_2 = x
    # defining shortcut connection
    shortcut = layers.Conv2D(filters, (1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1_convpool',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 2, Unit 2 - Settings
    stage = 1
    block = 1
    strides = (1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 3, Unit 1 - Settings
    stage = 2
    block = 0
    strides = (2, 2)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_3 = x
    # defining shortcut connection
    shortcut = layers.Conv2D(filters, (1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1_convpool',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 3, Unit 2 - Settings
    stage = 2
    block = 1
    strides = (1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 4, Unit 1 - Settings
    stage = 3
    block = 0
    strides = (2, 2)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_4 = x
    # defining shortcut connection
    shortcut = layers.Conv2D(filters, (1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1_convpool',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 4, Unit 2 - Settings
    stage = 3
    block = 1
    strides = (1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)
    x = layers.Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Resnet OUTPUT

    x = layers.BatchNormalization(name='bn1', **bn_params)(x)
    x = layers.Activation('relu', name='relu1')(x)

    model = Model(inputs=[inputs], outputs=[x], name='resnet18')

    model.summary()

    plot_model(model, to_file='model.png')

    weights_path = utils.get_file('resnet18_imagenet_1000_no_top.h5.h5',
                                  WEIGHTS_PATH_NO_TOP,
                                  cache_subdir='models')

    model.load_weights(weights_path)

    return model
Exemplo n.º 2
0
'''for x in range(0,len(itenes)):
   itenes[x] = Image.open(itenes[x])
   itenes[x] = itenes[x].resize(size=input_size)
   itenes[x] = np.asarray(itenes[x])
itenes = np.asarray(itenes)
y = itenes.shape
if(len(y)==3):
   itenes = np.reshape(itenes, (y[0],y[1],y[2],1))
generator.fit(itenes)'''
traingen = generator.flow_from_directory(path,
                                         target_size=input_size,
                                         batch_size=batch_size,
                                         shuffle=True)
#seção do modelo da arquitetura
model = models.Sequential()
model.add(layers.ZeroPadding2D(input_shape=input_shape, padding=1))
model.add(
    layers.Conv2D(filters=64,
                  kernel_size=3,
                  strides=1,
                  dilation_rate=1,
                  activation='relu'))
model.add(layers.ZeroPadding2D(1))
model.add(
    layers.Conv2D(filters=64,
                  kernel_size=3,
                  strides=1,
                  dilation_rate=1,
                  activation='relu'))
model.add(layers.MaxPooling2D(pool_size=2, strides=2))
Exemplo n.º 3
0
def MobileNetV2(input_shape=None,
                alpha=1.0,
                depth_multiplier=1,
                include_top=True,
                weights='imagenet',
                input_tensor=None,
                pooling=None,
                classes=1000,
                leaky_relu=None,
                **kwargs):
    """Instantiates the MobileNetV2 architecture.

    This is a simple modification of MobileNetV2 in keras_applications. I add
    just one parameter: leaky_relu. If None, the original activation function
    is used. Otherwise, Leaky_ReLU with alpha value just specified by leaky_relu
    is used to substitute all activate functions.

    :param input_shape: Optional shape tuple, to be specified if you would
            like to use a model with an input img resolution that is not
            (224, 224, 3).
            It should have exactly 3 inputs channels (224, 224, 3).
            You can also omit this option if you would like
            to infer input_shape from an input_tensor.
            If you choose to include both input_tensor and input_shape then
            input_shape will be used if they match, if the shapes
            do not match then we will throw an error.
            E.g. `(160, 160, 3)` would be one valid value.
    :param alpha: Controls the width of the 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.
    :param depth_multiplier: Depth multiplier for depthwise convolution
            (also called the resolution multiplier)
    :param include_top: Whether to include the fully-connected
            layer at the top of the network.
    :param weights: 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.
    :param input_tensor: Optional Keras tensor (i.e. output of
            `layers.Input()`)
            to use as image input for the model.
    :param 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.
    :param classes: Optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    :param leaky_relu:
    :param kwargs:
    :return: A Keras model instance.
    :raises:
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape or invalid depth_multiplier, alpha,
            rows when weights='imagenet'
    """

    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError(
            'If using `weights` as `"imagenet"` with `include_top`'
            'as true, `classes` should be 1000')

    # Determine proper input shape and default size.
    # If both input_shape and input_tensor are used, they should match
    if input_shape is not None and input_tensor is not None:
        try:
            is_input_t_tensor = backend.is_keras_tensor(input_tensor)
        except ValueError:
            try:
                is_input_t_tensor = backend.is_keras_tensor(
                    keras_utils.get_source_inputs(input_tensor))
            except ValueError:
                raise ValueError('input_tensor: ', input_tensor,
                                 'is not type input_tensor')
        if is_input_t_tensor:
            if backend.image_data_format == 'channels_first':
                if backend.int_shape(input_tensor)[1] != input_shape[1]:
                    raise ValueError(
                        'input_shape: ', input_shape, 'and input_tensor: ',
                        input_tensor,
                        'do not meet the same shape requirements')
            else:
                if backend.int_shape(input_tensor)[2] != input_shape[1]:
                    raise ValueError(
                        'input_shape: ', input_shape, 'and input_tensor: ',
                        input_tensor,
                        'do not meet the same shape requirements')
        else:
            raise ValueError('input_tensor specified: ', input_tensor,
                             'is not a keras tensor')

    # If input_shape is None, infer shape from input_tensor
    if input_shape is None and input_tensor is not None:

        try:
            backend.is_keras_tensor(input_tensor)
        except ValueError:
            raise ValueError('input_tensor: ', input_tensor, 'is type: ',
                             type(input_tensor), 'which is not a valid type')

        if input_shape is None and not backend.is_keras_tensor(input_tensor):
            default_size = 224
        elif input_shape is None and backend.is_keras_tensor(input_tensor):
            if backend.image_data_format() == 'channels_first':
                rows = backend.int_shape(input_tensor)[2]
                cols = backend.int_shape(input_tensor)[3]
            else:
                rows = backend.int_shape(input_tensor)[1]
                cols = backend.int_shape(input_tensor)[2]

            if rows == cols and rows in [96, 128, 160, 192, 224]:
                default_size = rows
            else:
                default_size = 224

    # If input_shape is None and no input_tensor
    elif input_shape is None:
        default_size = 224

    # If input_shape is not None, assume default size
    else:
        if backend.image_data_format() == 'channels_first':
            rows = input_shape[1]
            cols = input_shape[2]
        else:
            rows = input_shape[0]
            cols = input_shape[1]

        if rows == cols and rows in [96, 128, 160, 192, 224]:
            default_size = rows
        else:
            default_size = 224

    input_shape = _obtain_input_shape(input_shape,
                                      default_size=default_size,
                                      min_size=32,
                                      data_format=backend.image_data_format(),
                                      require_flatten=include_top,
                                      weights=weights)

    if backend.image_data_format() == 'channels_last':
        row_axis, col_axis = (0, 1)
    else:
        row_axis, col_axis = (1, 2)
    rows = input_shape[row_axis]
    cols = input_shape[col_axis]

    if weights == 'imagenet':
        if depth_multiplier != 1:
            raise ValueError('If imagenet weights are being loaded, '
                             'depth multiplier must be 1')

        if alpha not in [0.35, 0.50, 0.75, 1.0, 1.3, 1.4]:
            raise ValueError('If imagenet weights are being loaded, '
                             'alpha can be one of `0.35`, `0.50`, `0.75`, '
                             '`1.0`, `1.3` or `1.4` only.')

        if rows != cols or rows not in [96, 128, 160, 192, 224]:
            if rows is None:
                rows = 224
                warnings.warn('MobileNet shape is undefined.'
                              ' Weights for input shape'
                              '(224, 224) will be loaded.')
            else:
                raise ValueError('If imagenet weights are being loaded, '
                                 'input must have a static square shape'
                                 '(one of (96, 96), (128, 128), (160, 160),'
                                 '(192, 192), or (224, 224)).'
                                 'Input shape provided = %s' % (input_shape, ))

    if backend.image_data_format() != 'channels_last':
        warnings.warn('The MobileNet family of models is only available '
                      'for the input data format "channels_last" '
                      '(width, height, channels). '
                      'However your settings specify the default '
                      'data format "channels_first" (channels, width, height).'
                      ' You should set `image_data_format="channels_last"` '
                      'in your Keras config located at ~/.keras/keras.json. '
                      'The model being returned right now will expect inputs '
                      'to follow the "channels_last" data format.')
        backend.set_image_data_format('channels_last')
        old_data_format = 'channels_first'
    else:
        old_data_format = None

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

    first_block_filters = _make_divisible(32 * alpha, 8)
    x = layers.ZeroPadding2D(padding=correct_pad(backend, img_input, 3),
                             name='Conv1_pad')(img_input)
    x = layers.Conv2D(first_block_filters,
                      kernel_size=3,
                      strides=(2, 2),
                      padding='valid',
                      use_bias=False,
                      name='Conv1')(x)
    x = layers.BatchNormalization(epsilon=1e-3,
                                  momentum=0.999,
                                  name='bn_Conv1')(x)
    x = _mobilenetv2_relu_activate(name='Conv1_relu', leaky_relu=leaky_relu)(x)

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

    x = _inverted_res_block(x,
                            filters=24,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=1,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=24,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=2,
                            leaky_relu=leaky_relu)

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

    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=6,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=7,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=8,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=9,
                            leaky_relu=leaky_relu)

    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=10,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=11,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=12,
                            leaky_relu=leaky_relu)

    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=13,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=14,
                            leaky_relu=leaky_relu)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=15,
                            leaky_relu=leaky_relu)

    x = _inverted_res_block(x,
                            filters=320,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=16,
                            leaky_relu=leaky_relu)

    # no alpha applied to last conv as stated in the paper:
    # if the width multiplier is greater than 1 we
    # increase the number of output channels
    if alpha > 1.0:
        last_block_filters = _make_divisible(1280 * alpha, 8)
    else:
        last_block_filters = 1280

    x = layers.Conv2D(last_block_filters,
                      kernel_size=1,
                      use_bias=False,
                      name='Conv_1')(x)
    x = layers.BatchNormalization(epsilon=1e-3,
                                  momentum=0.999,
                                  name='Conv_1_bn')(x)
    x = _mobilenetv2_relu_activate(name='out_relu', leaky_relu=leaky_relu)(x)

    if include_top:
        x = layers.GlobalAveragePooling2D()(x)
        x = layers.Dense(classes,
                         activation='softmax',
                         use_bias=True,
                         name='Logits')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)

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

    # Create model.
    model = models.Model(inputs,
                         x,
                         name='mobilenetv2_%0.2f_%s' % (alpha, rows))

    # Load weights.
    if weights == 'imagenet':
        if backend.image_data_format() == 'channels_first':
            raise ValueError('Weights for "channels_first" format '
                             'are not available.')

        if include_top:
            model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                          str(alpha) + '_' + str(rows) + '.h5')
            weigh_path = MOBILENETV2_BASE_WEIGHT_PATH + model_name
            weights_path = keras_utils.get_file(model_name,
                                                weigh_path,
                                                cache_subdir='models')
        else:
            model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                          str(alpha) + '_' + str(rows) + '_no_top' + '.h5')
            weigh_path = MOBILENETV2_BASE_WEIGHT_PATH + model_name
            weights_path = keras_utils.get_file(model_name,
                                                weigh_path,
                                                cache_subdir='models')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    if old_data_format:
        backend.set_image_data_format(old_data_format)
    return model
Exemplo n.º 4
0
def _reduction_a_cell(ip, p, filters, block_id=None):
    '''Adds a Reduction cell for NASNet-A (Fig. 4 in the paper).

    # Arguments
        ip: Input tensor `x`
        p: Input tensor `p`
        filters: Number of output filters
        block_id: String block_id

    # Returns
        A Keras tensor
    '''
    channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1

    with backend.name_scope('reduction_A_block_%s' % block_id):
        p = _adjust_block(p, ip, filters, block_id)

        h = layers.Activation('relu')(ip)
        h = layers.Conv2D(filters, (1, 1),
                          strides=(1, 1),
                          padding='same',
                          name='reduction_conv_1_%s' % block_id,
                          use_bias=False,
                          kernel_initializer='he_normal')(h)
        h = layers.BatchNormalization(axis=channel_dim,
                                      momentum=0.9997,
                                      epsilon=1e-3,
                                      name='reduction_bn_1_%s' % block_id)(h)
        h3 = layers.ZeroPadding2D(padding=correct_pad(backend, h, 3),
                                  name='reduction_pad_1_%s' % block_id)(h)

        with backend.name_scope('block_1'):
            x1_1 = _separable_conv_block(h,
                                         filters, (5, 5),
                                         strides=(2, 2),
                                         block_id='reduction_left1_%s' %
                                         block_id)
            x1_2 = _separable_conv_block(p,
                                         filters, (7, 7),
                                         strides=(2, 2),
                                         block_id='reduction_right1_%s' %
                                         block_id)
            x1 = layers.add([x1_1, x1_2], name='reduction_add_1_%s' % block_id)

        with backend.name_scope('block_2'):
            x2_1 = layers.MaxPooling2D(
                (3, 3),
                strides=(2, 2),
                padding='valid',
                name='reduction_left2_%s' % block_id)(h3)
            x2_2 = _separable_conv_block(p,
                                         filters, (7, 7),
                                         strides=(2, 2),
                                         block_id='reduction_right2_%s' %
                                         block_id)
            x2 = layers.add([x2_1, x2_2], name='reduction_add_2_%s' % block_id)

        with backend.name_scope('block_3'):
            x3_1 = layers.AveragePooling2D(
                (3, 3),
                strides=(2, 2),
                padding='valid',
                name='reduction_left3_%s' % block_id)(h3)
            x3_2 = _separable_conv_block(p,
                                         filters, (5, 5),
                                         strides=(2, 2),
                                         block_id='reduction_right3_%s' %
                                         block_id)
            x3 = layers.add([x3_1, x3_2], name='reduction_add3_%s' % block_id)

        with backend.name_scope('block_4'):
            x4 = layers.AveragePooling2D(
                (3, 3),
                strides=(1, 1),
                padding='same',
                name='reduction_left4_%s' % block_id)(x1)
            x4 = layers.add([x2, x4])

        with backend.name_scope('block_5'):
            x5_1 = _separable_conv_block(x1,
                                         filters, (3, 3),
                                         block_id='reduction_left4_%s' %
                                         block_id)
            x5_2 = layers.MaxPooling2D(
                (3, 3),
                strides=(2, 2),
                padding='valid',
                name='reduction_right5_%s' % block_id)(h3)
            x5 = layers.add([x5_1, x5_2], name='reduction_add4_%s' % block_id)

        x = layers.concatenate([x2, x3, x4, x5],
                               axis=channel_dim,
                               name='reduction_concat_%s' % block_id)
        return x, ip
Exemplo n.º 5
0
def ResNet(stack_fn,
           preact,
           use_bias,
           model_name='resnet',
           include_top=True,
           weights=None,
           input_tensor=None,
           input_shape=None,
           pooling=None,
           nclass=1000,
           **kwargs):
    """Instantiates the ResNet, ResNetV2, and ResNeXt architecture.
    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.
    # Arguments
        stack_fn: a function that returns output tensor for the
            stacked residual blocks.
        preact: whether to use pre-activation or not
            (True for ResNetV2, False for ResNet and ResNeXt).
        use_bias: whether to use biases for convolutional layers or not
            (True for ResNet and ResNetV2, False for ResNeXt).
        model_name: string, model name.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor
            (i.e. output of `layers.Input()`)
            to use as image input for the model.
        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` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels.
        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.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """

    # Determine proper input shape
#     input_shape = input_shape

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

    bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1

    x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input)
    x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x)

    if preact is False:
        x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                                      name='conv1_bn')(x)
        x = layers.Activation('relu', name='conv1_relu')(x)

    x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x)
    x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x)

    x = stack_fn(x)

    if preact is True:
        x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                                      name='post_bn')(x)
        x = layers.Activation('relu', name='post_relu')(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(nclass, activation='softmax', name='probs')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='max_pool')(x)

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

    # Create model.
    model = models.Model(inputs, x, name=model_name)

    # Load weights.
    if weights is not None:
        model.load_weights(weights)

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam', metrics=['accuracy'])
    return model
Exemplo n.º 6
0
def resnet50(num_classes, size, compiled=True):
    img_input = layers.Input(shape=(size[1], size[0], 3))

    if backend.image_data_format() == 'channels_first':
        x = layers.Lambda(
            lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)),
            name='transpose')(img_input)
        bn_axis = 1
    else:  # channels_last
        x = img_input
        bn_axis = 3

    # Conv1 (7x7,64,stride=2)
    x = layers.ZeroPadding2D(padding=(3, 3))(x)

    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      use_bias=False,
                      kernel_initializer='he_normal',
                      kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY))(x)
    x = layers.BatchNormalization(axis=bn_axis,
                                  momentum=BATCH_NORM_DECAY,
                                  epsilon=BATCH_NORM_EPSILON)(x)
    x = layers.Activation('relu')(x)
    x = layers.ZeroPadding2D(padding=(1, 1))(x)

    # 3x3 max pool,stride=2
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    # Conv2_x

    # 1x1, 64
    # 3x3, 64
    # 1x1, 256

    x = conv_block(x, 3, [64, 64, 256], strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256])
    x = identity_block(x, 3, [64, 64, 256])

    # Conv3_x
    #
    # 1x1, 128
    # 3x3, 128
    # 1x1, 512

    x = conv_block(x, 3, [128, 128, 512])
    x = identity_block(x, 3, [128, 128, 512])
    x = identity_block(x, 3, [128, 128, 512])
    x = identity_block(x, 3, [128, 128, 512])

    # Conv4_x
    # 1x1, 256
    # 3x3, 256
    # 1x1, 1024
    x = conv_block(x, 3, [256, 256, 1024])
    x = identity_block(x, 3, [256, 256, 1024])
    x = identity_block(x, 3, [256, 256, 1024])
    x = identity_block(x, 3, [256, 256, 1024])
    x = identity_block(x, 3, [256, 256, 1024])
    x = identity_block(x, 3, [256, 256, 1024])

    # 1x1, 512
    # 3x3, 512
    # 1x1, 2048
    x = conv_block(x, 3, [512, 512, 2048])
    x = identity_block(x, 3, [512, 512, 2048])
    x = identity_block(x, 3, [512, 512, 2048])

    # average pool, 1000-d fc, sigmoid
    x = layers.GlobalAveragePooling2D()(x)
    if (compiled):
        x = layers.Dense(num_classes,
                         activation='sigmoid',
                         kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
                         bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY))(x)
    else:
        x = layers.BatchNormalization()(x)

    model = models.Model(img_input, x, name='resnet50')

    if (compiled):
        top3_acc = functools.partial(metric.top_categorical_accuracy,
                                     num_classes=num_classes)
        top3_acc.__name__ = 'top3_accuracy'
        opt = Adam(lr=0.001)
        model.compile(optimizer=opt,
                      loss=keras.losses.binary_crossentropy,
                      metrics=[
                          top3_acc, metrics.categorical_accuracy,
                          metrics.binary_accuracy
                      ])

    return model
Exemplo n.º 7
0
def conv2d(x,
           in_channels,
           out_channels,
           kernel_size,
           strides=1,
           padding=0,
           dilation=1,
           groups=1,
           use_bias=True,
           name="conv2d"):
    """
    Convolution 2D layer wrapper.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    kernel_size : int or tuple/list of 2 int
        Convolution window size.
    strides : int or tuple/list of 2 int
        Strides of the convolution.
    padding : int or tuple/list of 2 int
        Padding value for convolution layer.
    dilation : int or tuple/list of 2 int, default 1
        Dilation value for convolution layer.
    groups : int, default 1
        Number of groups.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    name : str, default 'conv2d'
        Layer name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    if isinstance(kernel_size, int):
        kernel_size = (kernel_size, kernel_size)
    if isinstance(strides, int):
        strides = (strides, strides)
    if isinstance(padding, int):
        padding = (padding, padding)
    if isinstance(dilation, int):
        dilation = (dilation, dilation)

    extra_pad = False
    if K.backend() == "tensorflow":
        if (padding[0] > 0) or (padding[1] > 0):
            import tensorflow as tf
            x = nn.Lambda(
                (lambda z: tf.pad(z, [[0, 0], [0, 0], list(padding), list(padding)]))
                if is_channels_first() else
                (lambda z: tf.pad(z, [[0, 0], list(padding), list(padding), [0, 0]])))(x)
            if not ((padding[0] == padding[1]) and (kernel_size[0] == kernel_size[1]) and
                    (kernel_size[0] // 2 == padding[0])):
                extra_pad = True
        padding_ke = "valid"
    else:
        if (padding[0] == padding[1]) and (padding[0] == 0):
            padding_ke = "valid"
        elif (padding[0] == padding[1]) and (kernel_size[0] == kernel_size[1]) and (kernel_size[0] // 2 == padding[0]):
            padding_ke = "same"
        else:
            x = nn.ZeroPadding2D(
                padding=padding,
                name=name + "/pad")(x)
            padding_ke = "valid"
            extra_pad = True

    if groups == 1:
        if extra_pad:
            name = name + "/conv"
        x = nn.Conv2D(
            filters=out_channels,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding_ke,
            dilation_rate=dilation,
            use_bias=use_bias,
            name=name)(x)
    elif (groups == out_channels) and (out_channels == in_channels):
        assert (dilation[0] == 1) and (dilation[1] == 1)
        if extra_pad:
            name = name + "/conv"
        x = nn.DepthwiseConv2D(
            kernel_size=kernel_size,
            strides=strides,
            padding=padding_ke,
            use_bias=use_bias,
            name=name)(x)
    else:
        assert (in_channels % groups == 0)
        assert (out_channels % groups == 0)
        none_batch = (x._keras_shape[0] is None)
        in_group_channels = in_channels // groups
        out_group_channels = out_channels // groups
        group_list = []
        for gi in range(groups):
            xi = nn.Lambda(
                (lambda z: z[:, gi * in_group_channels:(gi + 1) * in_group_channels, :, :])
                if is_channels_first() else
                (lambda z: z[:, :, :, gi * in_group_channels:(gi + 1) * in_group_channels]))(x)
            xi = nn.Conv2D(
                filters=out_group_channels,
                kernel_size=kernel_size,
                strides=strides,
                padding=padding_ke,
                dilation_rate=dilation,
                use_bias=use_bias,
                name=name + "/convgroup{}".format(gi + 1))(xi)
            group_list.append(xi)
        x = nn.concatenate(group_list, axis=get_channel_axis(), name=name + "/concat")
        if none_batch and (x._keras_shape[0] is not None):
            x._keras_shape = (None, ) + x._keras_shape[1:]

    return x
Exemplo n.º 8
0
def YOLOv4(inputs,
           num_classes,
           num_anchors,
           initial_filters=32,
           fast=False,
           anchors=None,
           conf_thresh=0.05,
           nms_thresh=0.45,
           keep_top_k=100,
           nms_top_k=100):
    i32 = initial_filters
    i64 = i32 * 2
    i128 = i32 * 4
    i256 = i32 * 8
    i512 = i32 * 16
    i1024 = i32 * 32

    if fast:
        # x = PreLayer()(inputs)
        x = inputs
    else:
        x = inputs

    # cspdarknet53部分
    x = conv2d_unit(x, i32, 3, strides=1, padding='same')

    # ============================= s2 =============================
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(x)
    x = conv2d_unit(x, i64, 3, strides=2)
    s2 = conv2d_unit(x, i64, 1, strides=1)
    x = conv2d_unit(x, i64, 1, strides=1)
    x = stack_residual_block(x, i32, i64, n=1)
    x = conv2d_unit(x, i64, 1, strides=1)
    x = layers.Concatenate()([x, s2])
    s2 = conv2d_unit(x, i64, 1, strides=1)

    # ============================= s4 =============================
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(s2)
    x = conv2d_unit(x, i128, 3, strides=2)
    s4 = conv2d_unit(x, i64, 1, strides=1)
    x = conv2d_unit(x, i64, 1, strides=1)
    x = stack_residual_block(x, i64, i64, n=2)
    x = conv2d_unit(x, i64, 1, strides=1)
    x = layers.Concatenate()([x, s4])
    s4 = conv2d_unit(x, i128, 1, strides=1)

    # ============================= s8 =============================
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(s4)
    x = conv2d_unit(x, i256, 3, strides=2)
    s8 = conv2d_unit(x, i128, 1, strides=1)
    x = conv2d_unit(x, i128, 1, strides=1)
    x = stack_residual_block(x, i128, i128, n=8)
    x = conv2d_unit(x, i128, 1, strides=1)
    x = layers.Concatenate()([x, s8])
    s8 = conv2d_unit(x, i256, 1, strides=1)

    # ============================= s16 =============================
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(s8)
    x = conv2d_unit(x, i512, 3, strides=2)
    s16 = conv2d_unit(x, i256, 1, strides=1)
    x = conv2d_unit(x, i256, 1, strides=1)
    x = stack_residual_block(x, i256, i256, n=8)
    x = conv2d_unit(x, i256, 1, strides=1)
    x = layers.Concatenate()([x, s16])
    s16 = conv2d_unit(x, i512, 1, strides=1)

    # ============================= s32 =============================
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(s16)
    x = conv2d_unit(x, i1024, 3, strides=2)
    s32 = conv2d_unit(x, i512, 1, strides=1)
    x = conv2d_unit(x, i512, 1, strides=1)
    x = stack_residual_block(x, i512, i512, n=4)
    x = conv2d_unit(x, i512, 1, strides=1)
    x = layers.Concatenate()([x, s32])
    s32 = conv2d_unit(x, i1024, 1, strides=1)
    # cspdarknet53部分结束

    # fpn部分
    x = conv2d_unit(s32, i512, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i1024, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i512, 1, strides=1, act='leaky')
    x = spp(x)

    x = conv2d_unit(x, i512, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i1024, 3, strides=1, padding='same', act='leaky')
    fpn_s32 = conv2d_unit(x, i512, 1, strides=1, act='leaky')

    # pan01
    x = conv2d_unit(fpn_s32, i256, 1, strides=1, act='leaky')
    x = layers.UpSampling2D(2)(x)
    s16 = conv2d_unit(s16, i256, 1, strides=1, act='leaky')
    x = layers.Concatenate()([s16, x])
    x = conv2d_unit(x, i256, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i512, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i256, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i512, 3, strides=1, padding='same', act='leaky')
    fpn_s16 = conv2d_unit(x, i256, 1, strides=1, act='leaky')
    # pan01结束

    # pan02
    x = conv2d_unit(fpn_s16, i128, 1, strides=1, act='leaky')
    x = layers.UpSampling2D(2)(x)
    s8 = conv2d_unit(s8, i128, 1, strides=1, act='leaky')
    x = layers.Concatenate()([s8, x])
    x = conv2d_unit(x, i128, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i256, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i128, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i256, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i128, 1, strides=1, act='leaky')
    # pan02结束

    # output_s, 不用concat()
    output_s = conv2d_unit(x, i256, 3, strides=1, padding='same', act='leaky')
    output_s = conv2d_unit(output_s,
                           num_anchors * (num_classes + 5),
                           1,
                           strides=1,
                           bn=0,
                           act=None)

    # output_m, 需要concat()
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(x)
    x = conv2d_unit(x, i256, 3, strides=2, act='leaky')
    x = layers.Concatenate()([x, fpn_s16])
    x = conv2d_unit(x, i256, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i512, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i256, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i512, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i256, 1, strides=1, act='leaky')
    output_m = conv2d_unit(x, i512, 3, strides=1, padding='same', act='leaky')
    output_m = conv2d_unit(output_m,
                           num_anchors * (num_classes + 5),
                           1,
                           strides=1,
                           bn=0,
                           act=None)

    # output_l, 需要concat()
    x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(x)
    x = conv2d_unit(x, i512, 3, strides=2, act='leaky')
    x = layers.Concatenate()([x, fpn_s32])
    x = conv2d_unit(x, i512, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i1024, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i512, 1, strides=1, act='leaky')
    x = conv2d_unit(x, i1024, 3, strides=1, padding='same', act='leaky')
    x = conv2d_unit(x, i512, 1, strides=1, act='leaky')
    output_l = conv2d_unit(x, i1024, 3, strides=1, padding='same', act='leaky')
    output_l = conv2d_unit(output_l,
                           num_anchors * (num_classes + 5),
                           1,
                           strides=1,
                           bn=0,
                           act=None)

    # 用张量操作实现后处理
    if fast:

        def output_layer(args):
            output_s, output_m, output_l = args

            # 先对坐标解码
            pred_xywh_s, pred_conf_s, pred_prob_s = decode(
                output_s, anchors[0], 8, num_classes)
            pred_xywh_m, pred_conf_m, pred_prob_m = decode(
                output_m, anchors[1], 16, num_classes)
            pred_xywh_l, pred_conf_l, pred_prob_l = decode(
                output_l, anchors[2], 32, num_classes)
            # 获取分数
            pred_score_s = pred_conf_s * pred_prob_s
            pred_score_m = pred_conf_m * pred_prob_m
            pred_score_l = pred_conf_l * pred_prob_l
            # 所有输出层的预测框集合后再执行nms
            all_pred_boxes = tf.concat([pred_xywh_s, pred_xywh_m, pred_xywh_l],
                                       axis=1)  # [batch_size, -1, 4]
            all_pred_scores = tf.concat(
                [pred_score_s, pred_score_m, pred_score_l],
                axis=1)  # [batch_size, -1, 80]

            # 用fastnms
            output = fastnms(all_pred_boxes, all_pred_scores, conf_thresh,
                             nms_thresh, keep_top_k, nms_top_k)

            return output

        output = layers.Lambda(output_layer)([output_s, output_m, output_l])
        model_body = keras.models.Model(inputs=inputs, outputs=output)
    else:
        model_body = keras.models.Model(inputs=inputs,
                                        outputs=[output_l, output_m, output_s])
    return model_body
Exemplo n.º 9
0
def ResNet(input_shape, **kwargs):
    """Instantiates the ResNet architecture.

    # Arguments
        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` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.

    # Returns
        A Keras model instance.

    # ResNet configuration space:
        kernel_size: 3,5
        stage2_block: [1,3]
        stage3_block: [1,11]
        stage4_block: [1,47]
        stage5_block: [1,4]
    """

    kwargs = {k: kwargs[k]
              for k in kwargs if kwargs[k]}  # Remove None value in args

    kernel_size = kwargs['res_kernel_size']
    stages = 4

    img_input = layers.Input(shape=input_shape)
    if backend.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1
    filters = 64

    # stage 1
    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    x = layers.Conv2D(filters, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    # stage 2-5
    for stage in range(2, stages + 2):
        if stage == 2:
            x = conv_block(x,
                           kernel_size, [filters, filters, filters * 4],
                           stage=stage,
                           block='_0_',
                           strides=(1, 1))
        else:
            x = conv_block(x,
                           kernel_size, [filters, filters, filters * 4],
                           stage=stage,
                           block='_0_')
        for i in range(kwargs['res_stage' + str(stage) + '_block']):
            x = identity_block(x,
                               3, [filters, filters, filters * 4],
                               stage=stage,
                               block="_" + str(i + 1) + "_")
        filters *= 2

    x = layers.GlobalAveragePooling2D()(x)
    # Create model.
    model = Model(img_input, x, name='resnet')
    return model
Exemplo n.º 10
0
def create_CNN(window_shape):
    K.set_image_dim_ordering('tf')
    # sgd = optimizers.SGD()
    adam = optimizers.Adam(lr=0.0000001)  #  , decay=0.000001)
    cnn_model = Sequential()
    print(window_shape)
    cnn_model.add(
        layers.ZeroPadding2D(padding=2,
                             input_shape=window_shape,
                             data_format='channels_last'))
    cnn_model.add(layers.Conv2D(18, (5, 5), strides=(1, 1), activation='relu')
                  )  #, kernel_regularizer=regularizers.l2()))  # 'relu'))
    cnn_model.add(layers.BatchNormalization())
    cnn_model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='valid'))
    cnn_model.add(layers.ZeroPadding2D(padding=(1, 1), data_format=None))
    cnn_model.add(
        layers.Conv2D(36, (3, 3), strides=(1, 1), activation='relu')
    )  #,kernel_regularizer=regularizers.l2()))  # 'relu')) #4 previously
    cnn_model.add(layers.BatchNormalization())
    cnn_model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='valid'))
    cnn_model.add(layers.Flatten())
    cnn_model.add(
        layers.Dense(20, activation='relu')
    )  #, kernel_regularizer=regularizers.l2()))  # 'sigmoid'))  # layers.Dropout(rate, noise_shape=None, seed=None)
    cnn_model.add(layers.Dropout(0.5))
    # cnn_model.add(layers.Flatten())
    cnn_model.add(
        layers.Dense(1, activation='sigmoid')
    )  #,kernel_regularizer=regularizers.l2()))  # layers.Dropout(rate, noise_shape=None, seed=None)
    # cnn_model.add(layers.Dropout(0.1))
    # cnn_model.add(layers.Activation('softmax'))
    cnn_model.summary()
    # cnn_model.add(layers.Dense())
    cnn_model.compile(optimizer=adam, loss='binary_crossentropy')
    # K.set_image_dim_ordering('tf')
    # # sgd = optimizers.SGD()
    # adam = optimizers.Adam(lr=0.0001)
    # cnn_model = Sequential()
    # print(window_shape)
    # cnn_model.add(layers.ZeroPadding2D(padding=2, input_shape=window_shape, data_format='channels_last'))
    # cnn_model.add(
    #     layers.Conv2D(12, (5, 5), strides=(1, 1), activation=None, kernel_regularizer=regularizers.l2()))  # 'relu'))
    # cnn_model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='valid'))
    # cnn_model.add(layers.ZeroPadding2D(padding=(1, 1), data_format=None))
    # cnn_model.add(layers.Conv2D(8, (3, 3), strides=(1, 1), activation=None,
    #                             kernel_regularizer=regularizers.l2()))  # 'relu')) #4 previously
    # cnn_model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='valid'))
    # cnn_model.add(layers.Flatten())
    # cnn_model.add(layers.Dense(20, activation=None,
    #                            kernel_regularizer=regularizers.l2()))  # 'sigmoid'))  # layers.Dropout(rate, noise_shape=None, seed=None)
    # # cnn_model.add(layers.Dropout(0.2))
    # # cnn_model.add(layers.Flatten())
    # cnn_model.add(layers.Dense(2, activation='softmax', kernel_regularizer=regularizers.l2()))
    # cnn_model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])

    # K.set_image_dim_ordering('tf')
    # sgd = optimizers.SGD()
    # cnn_model = Sequential()
    # # sigmoid part may need to be of form, activation='tanh'  activation = layers.sigmoid  input_shape=window_shape,
    # print(window_shape)
    # cnn_model.add(layers.ZeroPadding2D(padding=2, input_shape=window_shape, data_format='channels_last'))
    # # cnn_model.add(layers.ZeroPadding2D(padding=2, input_shape=(window_shape[2], window_shape[0], window_shape[1]), data_format='channels_first'))
    # cnn_model.add(layers.Conv2D(12, (5, 5), strides=(1, 1), activation ='relu'))
    # cnn_model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='valid'))
    # cnn_model.add(layers.ZeroPadding2D(padding=(1, 1), data_format=None))
    # cnn_model.add(layers.Conv2D(4, (3, 3), strides=(1, 1), activation='relu'))
    # cnn_model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='valid'))
    # cnn_model.add(layers.Flatten())
    # cnn_model.add(layers.Dense(20, activation='sigmoid'))  # layers.Dropout(rate, noise_shape=None, seed=None)
    # # cnn_model.add(layers.Dropout(0.1))
    # # cnn_model.add(layers.Flatten())
    # cnn_model.add(layers.Dense(2, activation='softmax'))  # layers.Dropout(rate, noise_shape=None, seed=None)
    # # cnn_model.add(layers.Dropout(0.1))
    # # cnn_model.add(layers.Activation('softmax'))
    #
    # ## Summary Line, Suppressed
    # # cnn_model.summary()
    #
    #
    # # cnn_model.add(layers.Dense())
    # cnn_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
    cnn_model.load_weights('Keras_Models_Saved\Model_12')
    # cnn_model.load_weights('Keras_Models_Saved\Model_112917_2220_binary')
    # cnn_model.load_weights('Keras_Models_Saved\Model_112717_test7')
    # utils.plot_model(cnn_model, to_file='model_1107.png', show_shapes=True, show_layer_names=True, rankdir='LR')
    print(cnn_model.get_weights())
    return cnn_model
Exemplo n.º 11
0
def cnn_model(input_shape, num_classes=1284):
    """Generate CNN with backdoor."""
    input = layers.Input(shape=input_shape)

    # Border extraction
    c_w1 = shift_kernel((2, 2), 3)
    c_w2 = shift_kernel((-4, -4), 3)
    c_w3 = shift_kernel((2, 2), 3)
    c_wp = subtractive_pointwise_kernel(6, preserve_org=True)

    # First method of extracting border, it is bit slower than next one
    #     l1 = layers.Conv2D(
    #             3,
    #             c_w1.shape[:2],
    #             padding="same",
    #             use_bias=False,
    #             weights=[c_w1],
    #             trainable=False)(input)
    #     l2 = layers.Conv2D(
    #             3,
    #             c_w1.shape[:2],
    #             padding="same",
    #             use_bias=False,
    #             weights=[c_w2],
    #             trainable=False)(input)
    #     l3 = layers.Conv2D(
    #             3,
    #             c_w1.shape[:2],
    #             padding="same",
    #             use_bias=False,
    #             weights=[c_w3],
    #             trainable=False)(input)
    #     concat = layers.Concatenate()([intput, l3])
    #     border_plus_org_input = layers.Conv2D(
    #             6,
    #             c_w1.shape[:2],
    #             padding="same",
    #             use_bias=False,
    #             weights=[c_wp],
    #             trainable=False)(input)

    # Second method of extraction
    c_weights_1 = shift_kernel((0, 0), 3, kernel_size=5) * -1
    l1 = layers.Conv2D(3,
                       c_weights_1.shape[:2],
                       padding="valid",
                       use_bias=False,
                       weights=[c_weights_1],
                       trainable=False,
                       name="train")(input)
    l1_pad = layers.ZeroPadding2D(padding=(2, 2))(l1)
    border = layers.Add()([input, l1_pad])
    concat = layers.Concatenate()([input, border])

    # Rest of the CNN
    c_layer1_5 = layers.Conv2D(12, (5, 5), padding="same",
                               activation="relu")(concat)
    c_layer1_3 = layers.Conv2D(12, (3, 3), padding="same",
                               activation="relu")(concat)
    c_layer1_1 = layers.Conv2D(12, (1, 1), padding="same",
                               activation="relu")(concat)
    concat_1 = layers.Concatenate()(
        [c_layer1_5, c_layer1_3, c_layer1_1, border])
    max_pool1 = layers.Conv2D(36, (3, 3),
                              strides=2,
                              padding="same",
                              activation="relu")(concat_1)

    c_layer2_5 = layers.Conv2D(64, (5, 5), padding="same",
                               activation="relu")(max_pool1)
    max_pool2 = layers.MaxPooling2D(pool_size=2, strides=2)(c_layer2_5)

    c_layer3_5 = layers.Conv2D(128, (5, 5),
                               strides=2,
                               padding="same",
                               activation="relu")(max_pool2)
    flatten = layers.Flatten()(c_layer3_5)

    dense = layers.Dense(2048, activation='relu')(flatten)
    dropout_2 = layers.Dropout(0.5)(dense)
    output = layers.Dense(num_classes, activation='softmax')(dropout_2)

    model = Model(inputs=input, outputs=output)
    return model
def EfficientNet(width_coefficient,
                 depth_coefficient,
                 default_size,
                 dropout_rate=0.2,
                 drop_connect_rate=0.2,
                 depth_divisor=8,
                 activation_fn=tf.nn.swish,
                 blocks_args=DEFAULT_BLOCKS_ARGS,
                 model_name='efficientnet',
                 weights='imagenet',
                 input_tensor=None,
                 input_shape=None,
                 pooling=None,
                 classes=1000,
                 **kwargs):

    img_input = layers.Input(tensor=input_tensor, shape=input_shape)

    #-------------------------------------------------#
    #   该函数的目的是保证filter的大小可以被8整除
    #-------------------------------------------------#
    def round_filters(filters, divisor=depth_divisor):
        """Round number of filters based on depth multiplier."""
        filters *= width_coefficient
        new_filters = max(divisor,
                          int(filters + divisor / 2) // divisor * divisor)
        # Make sure that round down does not go down by more than 10%.
        if new_filters < 0.9 * filters:
            new_filters += divisor
        return int(new_filters)

    #-------------------------------------------------#
    #   计算模块的重复次数
    #-------------------------------------------------#
    def round_repeats(repeats):
        return int(math.ceil(depth_coefficient * repeats))

    #-------------------------------------------------#
    #   创建stem部分
    #-------------------------------------------------#
    x = img_input
    x = layers.ZeroPadding2D(padding=correct_pad(x, 3),
                             name='stem_conv_pad')(x)
    x = layers.Conv2D(round_filters(32),
                      3,
                      strides=2,
                      padding='valid',
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name='stem_conv')(x)
    x = layers.BatchNormalization(name='stem_bn')(x)
    x = layers.Activation(activation_fn, name='stem_activation')(x)

    blocks_args = deepcopy(blocks_args)
    #-------------------------------------------------#
    #   计算总的efficient_block的数量
    #-------------------------------------------------#
    b = 0
    blocks = float(sum(args['repeats'] for args in blocks_args))
    #------------------------------------------------------------------------------#
    #   对结构块参数进行循环、一共进行7个大的结构块。
    #   每个大结构块下会重复小的efficient_block
    #------------------------------------------------------------------------------#
    for (i, args) in enumerate(blocks_args):
        assert args['repeats'] > 0
        #-------------------------------------------------#
        #   对使用到的参数进行更新
        #-------------------------------------------------#
        args['filters_in'] = round_filters(args['filters_in'])
        args['filters_out'] = round_filters(args['filters_out'])

        for j in range(round_repeats(args.pop('repeats'))):
            if j > 0:
                args['strides'] = 1
                args['filters_in'] = args['filters_out']
            x = block(x,
                      activation_fn,
                      drop_connect_rate * b / blocks,
                      name='block{}{}_'.format(i + 1, chr(j + 97)),
                      **args)
            b += 1

    #-------------------------------------------------#
    #   1x1卷积调整通道数
    #-------------------------------------------------#
    x = layers.Conv2D(round_filters(1280),
                      1,
                      padding='same',
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name='top_conv')(x)
    x = layers.BatchNormalization(name='top_bn')(x)
    x = layers.Activation(activation_fn, name='top_activation')(x)

    #-------------------------------------------------#
    #   利用GlobalAveragePooling2D代替全连接层
    #-------------------------------------------------#
    x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
    if dropout_rate > 0:
        x = layers.Dropout(dropout_rate, name='top_dropout')(x)

    x = layers.Dense(classes,
                     activation='softmax',
                     kernel_initializer=DENSE_KERNEL_INITIALIZER,
                     name='probs')(x)

    inputs = img_input
    model = Model(inputs, x, name=model_name)

    #-------------------------------------------------#
    #   载入权值
    #-------------------------------------------------#
    if weights == 'imagenet':
        file_suff = '_weights_tf_dim_ordering_tf_kernels_autoaugment.h5'
        file_hash = WEIGHTS_HASHES[model_name[-2:]][0]
        file_name = model_name + file_suff
        weights_path = get_file(file_name,
                                BASE_WEIGHTS_PATH + file_name,
                                cache_subdir='models',
                                file_hash=file_hash)
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model
def block(inputs,
          activation_fn=get_swish,
          drop_rate=0.,
          name='',
          filters_in=32,
          filters_out=16,
          kernel_size=3,
          strides=1,
          expand_ratio=1,
          se_ratio=0.,
          id_skip=True):

    filters = filters_in * expand_ratio
    #-------------------------------------------------#
    #   利用Inverted residuals
    #   part1 利用1x1卷积进行通道数上升
    #-------------------------------------------------#
    if expand_ratio != 1:
        x = layers.Conv2D(filters,
                          1,
                          padding='same',
                          use_bias=False,
                          kernel_initializer=CONV_KERNEL_INITIALIZER,
                          name=name + 'expand_conv')(inputs)
        x = layers.BatchNormalization(name=name + 'expand_bn')(x)
        x = layers.Activation(activation_fn,
                              name=name + 'expand_activation')(x)
    else:
        x = inputs

    #------------------------------------------------------#
    #   如果步长为2x2的话,利用深度可分离卷积进行高宽压缩
    #   part2 利用3x3卷积对每一个channel进行卷积
    #------------------------------------------------------#
    if strides == 2:
        x = layers.ZeroPadding2D(padding=correct_pad(x, kernel_size),
                                 name=name + 'dwconv_pad')(x)
        conv_pad = 'valid'
    else:
        conv_pad = 'same'
    x = layers.DepthwiseConv2D(kernel_size,
                               strides=strides,
                               padding=conv_pad,
                               use_bias=False,
                               depthwise_initializer=CONV_KERNEL_INITIALIZER,
                               name=name + 'dwconv')(x)
    x = layers.BatchNormalization(name=name + 'bn')(x)
    x = layers.Activation(activation_fn, name=name + 'activation')(x)

    #------------------------------------------------------#
    #   完成深度可分离卷积后
    #   对深度可分离卷积的结果施加注意力机制
    #------------------------------------------------------#
    if 0 < se_ratio <= 1:
        filters_se = max(1, int(filters_in * se_ratio))
        se = layers.GlobalAveragePooling2D(name=name + 'se_squeeze')(x)
        se = layers.Reshape((1, 1, filters), name=name + 'se_reshape')(se)
        #------------------------------------------------------#
        #   通道先压缩后上升,最后利用sigmoid将值固定到0-1之间
        #------------------------------------------------------#
        se = layers.Conv2D(filters_se,
                           1,
                           padding='same',
                           activation=activation_fn,
                           kernel_initializer=CONV_KERNEL_INITIALIZER,
                           name=name + 'se_reduce')(se)
        se = layers.Conv2D(filters,
                           1,
                           padding='same',
                           activation='sigmoid',
                           kernel_initializer=CONV_KERNEL_INITIALIZER,
                           name=name + 'se_expand')(se)
        x = layers.multiply([x, se], name=name + 'se_excite')

    #------------------------------------------------------#
    #   part3 利用1x1卷积进行通道下降
    #------------------------------------------------------#
    x = layers.Conv2D(filters_out,
                      1,
                      padding='same',
                      use_bias=False,
                      kernel_initializer=CONV_KERNEL_INITIALIZER,
                      name=name + 'project_conv')(x)
    x = layers.BatchNormalization(name=name + 'project_bn')(x)

    #------------------------------------------------------#
    #   part4 如果满足残差条件,那么就增加残差边
    #------------------------------------------------------#
    if (id_skip is True and strides == 1 and filters_in == filters_out):
        if drop_rate > 0:
            x = layers.Dropout(drop_rate,
                               noise_shape=(None, 1, 1, 1),
                               name=name + 'drop')(x)
        x = layers.add([x, inputs], name=name + 'add')

    return x
Exemplo n.º 14
0
c7 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(u7)
c7 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c7)

u8 = upsample(16, (2, 2), strides=(2, 2), padding='same')(c7)
u8 = layers.concatenate([u8, c2])
c8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(u8)
c8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c8)

u9 = upsample(8, (2, 2), strides=(2, 2), padding='same')(c8)
u9 = layers.concatenate([u9, c1], axis=3)
c9 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(u9)
c9 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(c9)

d = layers.Conv2D(1, (1, 1), activation='sigmoid')(c9)
d = layers.Cropping2D((EDGE_CROP, EDGE_CROP))(d)
d = layers.ZeroPadding2D((EDGE_CROP, EDGE_CROP))(d)
if NET_SCALING is not None:
    d = layers.UpSampling2D(NET_SCALING)(d)

seg_model = models.Model(inputs=[input_img], outputs=[d])
seg_model.summary()

# In[ ]:

import keras.backend as K
from keras.optimizers import Adam
from keras.losses import binary_crossentropy


def dice_coef(y_true, y_pred, smooth=1):
    intersection = K.sum(y_true * y_pred, axis=[1, 2, 3])
def mobilenet_v2_ssdlite(input_image, sub):

    alpha = 1.0

    first_block_filters = _make_divisible(32 * alpha, 8)

    # stage subtraction feature map extraction
    # 300*300*3 -> 150*150*16
    sub = KL.ZeroPadding2D(padding=correct_pad(K, sub, 3),
                         name='sub_stage1_block1_pad')(sub)
    sub = KL.Conv2D(first_block_filters,
                  kernel_size=3, strides=(2, 2),
                  padding='valid', use_bias=False,
                  name='sub_stage1_block1_conv')(sub)
    sub = KL.BatchNormalization(
        epsilon=1e-3, momentum=0.999, name='sub_stage1_block1_bn')(sub)
    sub = KL.ReLU(6., name='sub_stage1_block1_relu')(sub)


    sub = dw_sub_block(sub, filters=24, alpha=alpha, stride=2, stage=2, block_id=1)
    # 150*150*16 -> 75*75*24
    # 75*75*24 -> 38*38*32
    #sub = dw_sub_block(sub, filters=32, alpha=alpha, stride=2, stage=3, block_id=1)
    #38*38*32 -> 19*19*32
    #sub = dw_sub_block(sub, filters=32, alpha=alpha, stride=2, stage=4, block_id=1)


    # stage1
    x = KL.ZeroPadding2D(padding=correct_pad(K, input_image, 3),
                         name='bbn_stage1_block1_pad')(input_image)
    x = KL.Conv2D(first_block_filters,
                  kernel_size=3, strides=(2, 2),
                  padding='valid', use_bias=False,
                  name='bbn_stage1_block1_conv')(x)
    x = KL.BatchNormalization(
        epsilon=1e-3, momentum=0.999, name='bbn_stage1_block1_bn')(x)
    x = KL.ReLU(6., name='bbn_stage1_block1_relu')(x)
    x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1,
                                 expansion=1, stage=1, block_id=2, expand=False)

    # stage2
    x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2,
                            expansion=6, stage=2, block_id=1)
    x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1,
                                 expansion=6, stage=2, block_id=2)

    # stage3
    x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2,
                            expansion=6, stage=3, block_id=1)

    # concatenate sub here  
    sub = dw_sub_block(sub, filters=32, alpha=alpha, stride=2, stage=3, block_id=1)
    x = KL.Add(name='38_38_32stage3_add')([x, sub])#KL.Add(name=name + '_add')([inputs, x])
                        
    x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1,
                            expansion=6, stage=3, block_id=2)
    x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1,
                                 expansion=6, stage=3, block_id=3)

    # stage4 
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2,
                            expansion=6, stage=4, block_id=1)
    # concatenate sub here
    sub = dw_sub_block(sub, filters=64, alpha=alpha, stride=2, stage=4, block_id=1)
    x = KL.Add(name='19_19_64stage4_add')([x, sub])

    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1,
                            expansion=6, stage=4, block_id=2)
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1,
                            expansion=6, stage=4, block_id=3)
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1,
                            expansion=6, stage=4, block_id=4)

    x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1,
                            expansion=6, stage=4, block_id=5)
    x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1,
                            expansion=6, stage=4, block_id=6)
    x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1,
                                 expansion=6, stage=4, block_id=7)
    
    # stage5
    x, link1 = _inverted_res_block(x, filters=160, alpha=alpha, stride=2,
                            expansion=6, stage=5, block_id=1, output2=True)
    # concatenate sub here                        
    sub = dw_sub_b lock(sub, filters=160, alpha=alpha, stride=2, stage=5, block_id=1)
    x = KL.Add(name='10_10_160stage5_add')([x, sub])

    x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1,
                            expansion=6, stage=5, block_id=2)
    x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1,
                            expansion=6, stage=5, block_id=3)

    x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1,
                                 expansion=6, stage=5, block_id=4)

    x = KL.Conv2D(1280, kernel_size=1, padding='same', use_bias=False, activation=None, name='ssd_2_conv')(x)
    x = KL.BatchNormalization(epsilon=1e-3, momentum=0.999, name='ssd_2_conv_bn')(x)
    link2 = x = KL.ReLU(6., name='ssd_2_conv_relu')(x)

    link3 = x = _followed_down_sample_block(x, 256, 512, 3)

    link4 = x = _followed_down_sample_block(x, 128, 256, 4)

    link5 = x = _followed_down_sample_block(x, 128, 256, 5)

    link6 = x = _followed_down_sample_block(x, 64, 128, 6)

    links = [link1, link2, link3, link4, link5, link6]# return 6 feature maps with different scales 

    return links
Exemplo n.º 16
0
def extend_resnet(input_image, architecture="resnet101", train_bn=True):
    assert architecture in ["resnet50", "resnet101"]
    # Stage 1
    x = KL.ZeroPadding2D((3, 3))(input_image)
    x = KL.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=True)(x)
    x = BatchNorm(name='bn_conv1')(x, training=train_bn)
    x = KL.Activation('relu')(x)
    x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)

    # Stage 2
    x = conv_block(x,
                   3, [64, 64, 256],
                   stage=2,
                   block='a',
                   strides=(1, 1),
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=2,
                       block='b',
                       train_bn=train_bn)
    conv3_3 = x = identity_block(x,
                                 3, [64, 64, 256],
                                 stage=2,
                                 block='c',
                                 train_bn=train_bn)

    # Stage 3
    x = conv_block(x,
                   3, [128, 128, 512],
                   stage=3,
                   block='a',
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=3,
                       block='b',
                       train_bn=train_bn)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=3,
                       block='c',
                       train_bn=train_bn)
    conv4_3 = x = identity_block(x,
                                 3, [128, 128, 512],
                                 stage=3,
                                 block='d',
                                 train_bn=train_bn)

    # Stage 4
    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=4,
                   block='a',
                   train_bn=train_bn)
    block_count = {"resnet50": 5, "resnet101": 22}[architecture]
    for i in range(block_count):
        x = identity_block(x,
                           3, [256, 256, 1024],
                           stage=4,
                           block=chr(98 + i),
                           train_bn=train_bn)
    conv5_3 = x

    # Stage 5
    x = conv_block(x,
                   3, [512, 512, 2048],
                   stage=5,
                   block='a',
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [512, 512, 2048],
                       stage=5,
                       block='b',
                       train_bn=train_bn)
    conv_fc7 = x = identity_block(x,
                                  3, [512, 512, 2048],
                                  stage=5,
                                  block='c',
                                  train_bn=train_bn)

    # Stage 6(Extended)
    x = KL.Conv2D(512, (1, 1), name='stage6_conv1', padding='same',
                  strides=1)(x)
    x = BatchNorm(name='stage6_bn1', )(x, training=train_bn)
    x = KL.Activation('relu')(x)
    x = KL.Conv2D(512, (3, 3), name='stage6_conv2', padding='same',
                  strides=2)(x)
    x = BatchNorm(name='stage6_bn2', )(x, training=train_bn)
    conv6_2 = x = KL.Activation('relu')(x)

    # Stage 7(Extended)
    x = KL.Conv2D(128, (1, 1), name='stage7_conv1', padding='same',
                  strides=1)(x)
    x = BatchNorm(name='stage7_bn1', )(x, training=train_bn)
    x = KL.Activation('relu')(x)
    x = KL.Conv2D(256, (3, 3), name='stage7_conv2', padding='same',
                  strides=2)(x)
    x = BatchNorm(name='stage7_bn2', )(x, training=train_bn)
    conv7_2 = x = KL.Activation('relu')(x)

    return conv3_3, conv4_3, conv5_3, conv_fc7, conv6_2, conv7_2
Exemplo n.º 17
0
def ResNet50(classes: int = 1000, input_shape: Tuple[int] = (224, 224)):
    """ Instantiates the ResNet50 architecture.

    # Arguments
        - classes: The number of classes to predict.
        - input_shape: The size of the inputs (x, y).

    # Returns:
        A Keras model instance.
    """
    img_input = layers.Input(shape=(*input_shape, 3))

    if backend.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    # 1
    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      kernel_regularizer=l2(0.00005),
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis,
                                  momentum=0.9,
                                  epsilon=1e-5,
                                  name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    # 2
    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    # 3
    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    # 4
    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    # 5
    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
    x = layers.Dense(classes,
                     activation='softmax',
                     kernel_regularizer=l2(0.00005),
                     name='fc1000')(x)

    inputs = img_input

    # Create model.
    model = models.Model(inputs, x, name='resnet50')

    return model
Exemplo n.º 18
0
    def create_model(self, img_shape, num_class):
        concat_axis = 3

        shape = img_shape + [1] if len(img_shape) == 1 else img_shape
        inputs = layers.Input(shape=shape)

        conv1 = layers.Conv2D(32, (3, 3),
                              activation='relu',
                              padding='same',
                              name='conv1_1')(inputs)
        conv1 = layers.Conv2D(32, (3, 3), activation='relu',
                              padding='same')(conv1)
        pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)
        conv2 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(pool1)
        conv2 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(conv2)
        pool2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2)

        conv3 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(pool2)
        conv3 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(conv3)
        pool3 = layers.MaxPooling2D(pool_size=(2, 2))(conv3)

        conv4 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(pool3)
        conv4 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(conv4)
        pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

        conv5 = layers.Conv2D(512, (3, 3), activation='relu',
                              padding='same')(pool4)
        conv5 = layers.Conv2D(512, (3, 3), activation='relu',
                              padding='same')(conv5)

        up_conv5 = layers.UpSampling2D(size=(2, 2))(conv5)
        ch, cw = self.get_crop_shape(conv4, up_conv5)
        crop_conv4 = layers.Cropping2D(cropping=(ch, cw))(conv4)
        up6 = layers.concatenate([up_conv5, crop_conv4], axis=concat_axis)
        conv6 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(up6)
        conv6 = layers.Conv2D(256, (3, 3), activation='relu',
                              padding='same')(conv6)

        up_conv6 = layers.UpSampling2D(size=(2, 2))(conv6)
        ch, cw = self.get_crop_shape(conv3, up_conv6)
        crop_conv3 = layers.Cropping2D(cropping=(ch, cw))(conv3)
        up7 = layers.concatenate([up_conv6, crop_conv3], axis=concat_axis)
        conv7 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(up7)
        conv7 = layers.Conv2D(128, (3, 3), activation='relu',
                              padding='same')(conv7)

        up_conv7 = layers.UpSampling2D(size=(2, 2))(conv7)
        ch, cw = self.get_crop_shape(conv2, up_conv7)
        crop_conv2 = layers.Cropping2D(cropping=(ch, cw))(conv2)
        up8 = layers.concatenate([up_conv7, crop_conv2], axis=concat_axis)
        conv8 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(up8)
        conv8 = layers.Conv2D(64, (3, 3), activation='relu',
                              padding='same')(conv8)

        up_conv8 = layers.UpSampling2D(size=(2, 2))(conv8)
        ch, cw = self.get_crop_shape(conv1, up_conv8)
        crop_conv1 = layers.Cropping2D(cropping=(ch, cw))(conv1)
        up9 = layers.concatenate([up_conv8, crop_conv1], axis=concat_axis)
        conv9 = layers.Conv2D(32, (3, 3), activation='relu',
                              padding='same')(up9)
        conv9 = layers.Conv2D(32, (3, 3), activation='relu',
                              padding='same')(conv9)

        ch, cw = self.get_crop_shape(inputs, conv9)
        conv9 = layers.ZeroPadding2D(padding=((ch[0], ch[1]), (cw[0],
                                                               cw[1])))(conv9)
        # conv10 = layers.Conv2D(num_class, (1, 1))(conv9)
        conv10 = layers.Conv2D(1, (1, 1), activation='sigmoid')(conv9)

        model = models.Model(inputs=inputs, outputs=conv10)

        return model
def _depthwise_conv_block(inputs,
                          pointwise_conv_filters,
                          alpha,
                          depth_multiplier=1,
                          strides=(1, 1),
                          block_id=1):
    """Adds a depthwise convolution block.
    A depthwise convolution block consists of a depthwise conv,
    batch normalization, relu6, pointwise convolution,
    batch normalization and relu6 activation.
    # Arguments
        inputs: Input tensor of shape `(rows, cols, channels)`
            (with `channels_last` data format) or
            (channels, rows, cols) (with `channels_first` data format).
        pointwise_conv_filters: Integer, the dimensionality of the output space
            (i.e. the number of output filters in the pointwise convolution).
        alpha: controls the width of the network.
            - 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.
        depth_multiplier: The number of depthwise convolution output channels
            for each input channel.
            The total number of depthwise convolution output
            channels will be equal to `filters_in * depth_multiplier`.
        strides: An integer or tuple/list of 2 integers,
            specifying the strides of the convolution
            along the width and height.
            Can be a single integer to specify the same value for
            all spatial dimensions.
            Specifying any stride value != 1 is incompatible with specifying
            any `dilation_rate` value != 1.
        block_id: Integer, a unique identification designating
            the block number.
    # Input shape
        4D tensor with shape:
        `(batch, channels, rows, cols)` if data_format='channels_first'
        or 4D tensor with shape:
        `(batch, rows, cols, channels)` if data_format='channels_last'.
    # Output shape
        4D tensor with shape:
        `(batch, filters, new_rows, new_cols)`
        if data_format='channels_first'
        or 4D tensor with shape:
        `(batch, new_rows, new_cols, filters)`
        if data_format='channels_last'.
        `rows` and `cols` values might have changed due to stride.
    # Returns
        Output tensor of block.
    """
    channel_axis = 3
    pointwise_conv_filters = int(pointwise_conv_filters * alpha)

    if strides == (1, 1):
        x = inputs
    else:
        x = layers.ZeroPadding2D(((0, 1), (0, 1)),
                                 name='conv_pad_%d' % block_id)(inputs)
    x = layers.DepthwiseConv2D(
        (3, 3),
        padding='same' if strides == (1, 1) else 'valid',
        depth_multiplier=depth_multiplier,
        strides=strides,
        use_bias=False,
        name='conv_dw_%d' % block_id)(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='conv_dw_%d_bn' % block_id)(x)
    x = layers.ReLU(6., name='conv_dw_%d_relu' % block_id)(x)

    x = layers.Conv2D(pointwise_conv_filters, (1, 1),
                      padding='same',
                      use_bias=False,
                      strides=(1, 1),
                      name='conv_pw_%d' % block_id)(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='conv_pw_%d_bn' % block_id)(x)
    return layers.ReLU(6., name='conv_pw_%d_relu' % block_id)(x)
Exemplo n.º 20
0
    def build(self,
              input_shape=None,
              data_format='channels_last',
              pooling=None,
              repetitions=None,
              bottleneck=True,
              model_name='resnet50'):

        assert len(repetitions) == 4

        img_input = layers.Input(shape=input_shape)

        if data_format == 'channels_first':
            bn_axis = 1
        else:
            bn_axis = 3

        # stage1
        x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
        x = layers.Conv2D(
            64, (7, 7),
            strides=(2, 2),
            padding='valid',
            kernel_initializer='he_normal',
            name='conv1_4' if input_shape[2] == 4 else 'conv1')(x)
        x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
        x = layers.Activation('relu')(x)
        x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
        x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

        block_names = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
        base_num1 = 64
        base_num2 = 256

        # loop for stages from stage2 to ...
        for i, num in enumerate(repetitions):
            stage = i + 2
            # fisrt block in i-th stage
            x = conv_block(x,
                           3, [base_num1, base_num1, base_num2],
                           stage=stage,
                           block='a',
                           bn_axis=bn_axis,
                           strides=(1, 1) if i == 0 else (2, 2),
                           bottleneck=bottleneck)
            # loop for rest block in i-th stage
            for ii in range(1, num):
                #block_name = block_names[ii+1]
                x = identity_block(
                    x,
                    3,
                    [base_num1, base_num1, base_num2],
                    stage=stage,
                    block='b' + str(ii) if
                    (repetitions[2] > 6 and stage in [3, 4]) else block_names[
                        ii],  #res50(包括50)以下按照keras内置的命名格式,res50以上则按照权值文件的命名格式,参考:https://github.com/GKalliatakis/Keras-Application-Zoo/blob/master/resnet101.py
                    bn_axis=bn_axis,
                    bottleneck=bottleneck)
            base_num1 = 2 * base_num1
            base_num2 = 2 * base_num2

        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)
        elif pooling:
            raise ('no such pooling type!!')

        # create model
        model = models.Model(img_input, x, name=model_name)

        return model
def nn_base(input_tensor=None, trainable=False):

    #Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (3, None, None)
    else:
        input_shape = (None, None, 3)

    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 K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    # for testing..
    alpha = 1
    depth_multiplier = 1

    # need this layer to pass the input image size
    x = layers.ZeroPadding2D((3, 3))(img_input)

    #Input: 320x320
    #Block 1, Output = 160x160
    x = _conv_block(img_input, 32, alpha, strides=(2, 2))

    #Block 2, Output 160x160 (Stride 1)
    x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)

    #Block 3, Output = 80x80
    x = _depthwise_conv_block(x,
                              128,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=2)
    x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)

    #Block 4, Output = 40x40
    x = _depthwise_conv_block(x,
                              256,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=4)
    x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)

    #Block 5 is a 5x stride1 dw-separable, Output = 20x20
    x = _depthwise_conv_block(x,
                              512,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=6)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)

    return x
    def unet(self, img_shape, num_class=2):
        '''Create non-compiled UNet model

        # arguments
            img_shape: shape of input image
            num_class: default value is 2 as we classify pixel in two classes
        '''
        concat_axis = -1
        inputs = layers.Input(shape = img_shape)

        conv1 = layers.Conv2D(32, (3, 3), activation='relu', padding='same', name='conv1_1')(inputs)
        conv1 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
        pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)
        conv2 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
        conv2 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
        pool2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2)

        conv3 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
        conv3 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
        pool3 = layers.MaxPooling2D(pool_size=(2, 2))(conv3)

        conv4 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
        conv4 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
        pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

        conv5 = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
        conv5 = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)

        up_conv5 = layers.UpSampling2D(size=(2, 2))(conv5)
        ch, cw = get_crop_shape(conv4, up_conv5)
        crop_conv4 = layers.Cropping2D(cropping=(ch,cw))(conv4)
        up6 = layers.concatenate([up_conv5, crop_conv4], axis=concat_axis)
        conv6 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
        conv6 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)

        up_conv6 = layers.UpSampling2D(size=(2, 2))(conv6)
        ch, cw = get_crop_shape(conv3, up_conv6)
        crop_conv3 = layers.Cropping2D(cropping=(ch,cw))(conv3)
        up7 = layers.concatenate([up_conv6, crop_conv3], axis=concat_axis)
        conv7 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
        conv7 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)

        up_conv7 = layers.UpSampling2D(size=(2, 2))(conv7)
        ch, cw = get_crop_shape(conv2, up_conv7)
        crop_conv2 = layers.Cropping2D(cropping=(ch,cw))(conv2)
        up8 = layers.concatenate([up_conv7, crop_conv2], axis=concat_axis)
        conv8 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
        conv8 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)

        up_conv8 = layers.UpSampling2D(size=(2, 2))(conv8)
        ch, cw = get_crop_shape(conv1, up_conv8)
        crop_conv1 = layers.Cropping2D(cropping=(ch,cw))(conv1)
        up9 = layers.concatenate([up_conv8, crop_conv1], axis=concat_axis)
        conv9 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
        conv9 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)

        ch, cw = get_crop_shape(inputs, conv9)
        conv9 = layers.ZeroPadding2D(padding=((ch[0], ch[1]), (cw[0], cw[1])))(conv9)
        conv10 = layers.Conv2D(num_class, (1, 1))(conv9)

        model = Model(inputs=inputs, outputs=conv10)

        return model
Exemplo n.º 23
0
def YnetResNet(netpram,include_top=True, weights=None, input_tensor=None,  input_shape=None,pooling=None, classes=1000):


    include_top=False
    pooling='max'
    classes=1000
    if  netpram.task=='all':
        num_classes=11
    elif  netpram.task=='binary':
        num_classes=1
    elif  netpram.task=='parts':
        num_classes=3
    elif  netpram.task=='instrument':
        num_classes=7 
    #img_input=Input((224, 224, 3))
    inputs_L   = Input((224, 224, 3))
    inputs_R   = Input((224, 224, 3))
    
   
    inputs=[inputs_L,inputs_R]
    bn_axis = 3


    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(inputs_L)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x_a = layers.Activation('relu')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x_a)

    x_b = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x_b, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x_c = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x_c, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x_d = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x_d, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x_e = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x_e, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    if include_top:
        x = layers.AveragePooling2D((7, 7), name='avg_pool')(x)
        x = layers.Flatten()(x)
        x = layers.Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)
        else:
            warnings.warn('The output shape of `ResNet50(include_top=False)` '
                          'has been changed since Keras 2.2.0.')

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.

    inputs = inputs_L
    # Create model.
    modelen1 = Model(inputs, x, name='resnet50')
    # Load weights.
    weights_path = utils.get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='a268eb855778b3df3c7506639542a6af')
    modelen1.load_weights(weights_path)
 #   for layer in modelen1.layers:
 #       layer.trainable=False
  
   # Loaded enoder one- untrainable
    
    x = layers.ZeroPadding2D(padding=(3, 3), name='enc2_conv1_pad')(inputs_R)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      name='enc2_conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='enc2_bn_conv1')(x)
    x_a_e2 = layers.Activation('relu')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x_a_e2)

    x_b_e2 = conv_block(x, 3, [64, 64, 256], stage=2, block='a_enc2', strides=(1, 1))
    x = identity_block(x_b_e2, 3, [64, 64, 256], stage=2, block='b_enc2')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c_enc2')

    x_c_e2 = conv_block(x, 3, [128, 128, 512], stage=3, block='a_enc2')
    x = identity_block(x_c_e2, 3, [128, 128, 512], stage=3, block='b_enc2')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c_enc2')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d_enc2')

    x_d_e2 = conv_block(x, 3, [256, 256, 1024], stage=4, block='a_enc2')
    x = identity_block(x_d_e2, 3, [256, 256, 1024], stage=4, block='b_enc2')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c_enc2')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d_enc2')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e_enc2')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f_enc2')

    x_e_e2 = conv_block(x, 3, [512, 512, 2048], stage=5, block='a_enc2')
    x = identity_block(x_e_e2, 3, [512, 512, 2048], stage=5, block='b_enc2_enc2')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c_enc2')

    if include_top:
        x = layers.AveragePooling2D((7, 7), name='avg_pool_enc2')(x)
        x = layers.Flatten()(x)
        x = layers.Dense(classes, activation='softmax', name='fc1000_enc2')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)
        else:
            warnings.warn('The output shape of `ResNet50(include_top=False)` '
                          'has been changed since Keras 2.2.0.')

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.

    inputs = inputs_R
    # Create model.
    modelen2 = Model(inputs, x, name='resnet50')

    # Load weights.
    weights_path = utils.get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='a268eb855778b3df3c7506639542a6af')
    modelen2.load_weights(weights_path)
        
     #Loaded encoder 2-trainable   
        
        
        
    
    
    #center = add([xold, ynew])
    #center = concatenate([xold, ynew], axis=3)
    center=conv_block_decoder(add([x_e,x_e_e2]), 1024, 7, 'center', strides=(2, 2))
    center=conv_block_decoder(center, 1024, 7, 'center_2', strides=(2, 2))
    # 32
    up4 = UpSampling2D((2, 2))(center)
    #upc=concatenate([x_d,x_d_e2], axis=3)
    up4 = concatenate([add([x_d,x_d_e2]), up4], axis=3)
    up4=conv_block_decoder(up4, 512, 32, 'deconv1', strides=(2, 2))
  #  up4=conv_block_decoder(up4, 512, 32, 'deconv2', strides=(2, 2))
    up4=conv_block_decoder(up4, 512, 32, 'deconv3', strides=(2, 2))
    
    # 64

    up3 = UpSampling2D((2, 2))(up4) 
   # upc=concatenate([down2T,down2B], axis=3)
    #upc=concatenate([x_c,x_c_e2], axis=3)
    up3 = concatenate([add([x_c,x_c_e2]), up3], axis=3)
    up3=conv_block_decoder(up3, 512, 64, 'deconv1', strides=(2, 2))
    up3=conv_block_decoder(up3, 512, 64, 'deconv2', strides=(2, 2))
    up3=conv_block_decoder(up3, 512, 64, 'deconv3', strides=(2, 2))
    # 128

    up2 = UpSampling2D((2, 2))(up3) 
    #upc = concatenate([down1T,down1B], axis=3)
    
    x_b = layers.ZeroPadding2D(padding=[1, 1], name='convdec_pad')(x_b)
    x_b = layers.Cropping2D(cropping=((1, 0), (1, 0)), data_format=None)(x_b)
    
    x_b_e2 = layers.ZeroPadding2D(padding=[1, 1], name='convdec_pad2')(x_b_e2)
    x_b_e2 = layers.Cropping2D(cropping=((1, 0), (1, 0)), data_format=None)(x_b_e2)
    up2 = concatenate([add([x_b,x_b_e2]), up2], axis=3)
    up2=conv_block_decoder(up2, 256, 128, 'deconv1', strides=(2, 2))
  #  up2=conv_block_decoder(up2, 256, 128, 'deconv2', strides=(2, 2))
    up2=conv_block_decoder(up2, 256, 128, 'deconv3', strides=(2, 2))
    # 256

    up1 = UpSampling2D((2, 2))(up2)  
    #upc=concatenate([down0T,down0B], axis=3)
    up1 = concatenate([add([x_a,x_a_e2]), up1], axis=3)
    up1=conv_block_decoder(up1, 128, 256, 'deconv1', strides=(2, 2))
    up1=conv_block_decoder(up1, 128, 256, 'deconv2', strides=(2, 2))
    up1=conv_block_decoder(up1, 128, 256, 'deconv3', strides=(2, 2))


    # 512

    up0a = UpSampling2D((2, 2))(up1)
    #upc= concatenate([down0aT,down0aB],axis=3)
  #  up0a = concatenate([img_input, up0a], axis=3)    
    up0a=conv_block_decoder(up0a, 64, 512, 'deconv1', strides=(2, 2))
    up0a=conv_block_decoder(up0a, 64, 512, 'deconv2', strides=(2, 2))
    up0a=conv_block_decoder(up0a, 64, 512, 'deconv3', strides=(2, 2))
  
    
    

    classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0a)
        

    model = Model(inputs=[inputs_L,inputs_R], outputs=classify,name='RESNetU')
   # optimizerc =CustomRMSprop(lr=0.00001,multipliers = LR_mult_dict)
    #lr=0.00001, F1=79.8
    model.compile(optimizer=RMSprop(lr=0.0001), loss=bce_dice_loss, metrics=[dice_coeff])
   # '''
    return model
Exemplo n.º 24
0
def _adjust_block(p, ip, filters, block_id=None):
    '''Adjusts the input `previous path` to match the shape of the `input`.

    Used in situations where the output number of filters needs to be changed.

    # Arguments
        p: Input tensor which needs to be modified
        ip: Input tensor whose shape needs to be matched
        filters: Number of output filters to be matched
        block_id: String block_id

    # Returns
        Adjusted Keras tensor
    '''
    channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1
    img_dim = 2 if backend.image_data_format() == 'channels_first' else -2

    ip_shape = backend.int_shape(ip)

    if p is not None:
        p_shape = backend.int_shape(p)

    with backend.name_scope('adjust_block'):
        if p is None:
            p = ip

        elif p_shape[img_dim] != ip_shape[img_dim]:
            with backend.name_scope('adjust_reduction_block_%s' % block_id):
                p = layers.Activation('relu',
                                      name='adjust_relu_1_%s' % block_id)(p)
                p1 = layers.AveragePooling2D(
                    (1, 1),
                    strides=(2, 2),
                    padding='valid',
                    name='adjust_avg_pool_1_%s' % block_id)(p)
                p1 = layers.Conv2D(filters // 2, (1, 1),
                                   padding='same',
                                   use_bias=False,
                                   name='adjust_conv_1_%s' % block_id,
                                   kernel_initializer='he_normal')(p1)

                p2 = layers.ZeroPadding2D(padding=((0, 1), (0, 1)))(p)
                p2 = layers.Cropping2D(cropping=((1, 0), (1, 0)))(p2)
                p2 = layers.AveragePooling2D(
                    (1, 1),
                    strides=(2, 2),
                    padding='valid',
                    name='adjust_avg_pool_2_%s' % block_id)(p2)
                p2 = layers.Conv2D(filters // 2, (1, 1),
                                   padding='same',
                                   use_bias=False,
                                   name='adjust_conv_2_%s' % block_id,
                                   kernel_initializer='he_normal')(p2)

                p = layers.concatenate([p1, p2], axis=channel_dim)
                p = layers.BatchNormalization(axis=channel_dim,
                                              momentum=0.9997,
                                              epsilon=1e-3,
                                              name='adjust_bn_%s' %
                                              block_id)(p)

        elif p_shape[channel_dim] != filters:
            with backend.name_scope('adjust_projection_block_%s' % block_id):
                p = layers.Activation('relu')(p)
                p = layers.Conv2D(filters, (1, 1),
                                  strides=(1, 1),
                                  padding='same',
                                  name='adjust_conv_projection_%s' % block_id,
                                  use_bias=False,
                                  kernel_initializer='he_normal')(p)
                p = layers.BatchNormalization(axis=channel_dim,
                                              momentum=0.9997,
                                              epsilon=1e-3,
                                              name='adjust_bn_%s' %
                                              block_id)(p)
    return p
# In[15]:

network = models.Sequential()
network.add(
    layers.Conv2D(32, (3, 3),
                  activation='relu',
                  padding='same',
                  input_shape=input_shape))
network.add(layers.MaxPooling2D((2, 2), padding='same'))
network.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
network.add(layers.MaxPooling2D((2, 2), padding='valid'))
network.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
network.add(layers.UpSampling2D((2, 2)))
network.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
network.add(layers.UpSampling2D((2, 2)))
network.add(layers.ZeroPadding2D(padding=(1, 0), data_format=None))
network.add(layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same'))

# In[16]:

network.summary()

# In[17]:

network2 = keras.utils.multi_gpu_model(network, gpus=2)  #model for 2 GPUs

# # Compiling Model

# In[18]:

network.compile(optimizer='adadelta', loss='binary_crossentropy')
Exemplo n.º 26
0
        initial_filters = 8

        i32 = initial_filters
        i64 = i32 * 2
        i128 = i32 * 4
        i256 = i32 * 8
        i512 = i32 * 16
        i1024 = i32 * 32

        # 多尺度训练
        inputs = layers.Input(shape=(None, None, 3))
        ''' darknet53部分,所有卷积层都没有偏移use_bias=False '''
        x = conv2d_unit(inputs, i32, (3, 3))

        x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(x)
        x = conv2d_unit(x, i64, (3, 3), strides=2, padding='valid')
        x = stack_residual_block(x, i32, n=1)

        x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(x)
        x = conv2d_unit(x, i128, (3, 3), strides=2, padding='valid')
        x = stack_residual_block(x, i64, n=2)

        x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(x)
        x = conv2d_unit(x, i256, (3, 3), strides=2, padding='valid')
        act11 = stack_residual_block(x, i128, n=8)

        x = layers.ZeroPadding2D(padding=((1, 0), (1, 0)))(act11)
        x = conv2d_unit(x, i512, (3, 3), strides=2, padding='valid')
        act19 = stack_residual_block(x, i256, n=8)
Exemplo n.º 27
0
def resnet_graph(input_image, architecture, stage5=False, train_bn=True):
    """Build a ResNet graph.
        architecture: Can be resnet50 or resnet101
        stage5: Boolean. If False, stage5 of the network is not created
        train_bn: Boolean. Train or freeze Batch Norm layers
    """
    assert architecture in ["resnet50", "resnet101"]
    # Stage 1
    x = KL.ZeroPadding2D((3, 3))(input_image)
    x = KL.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=True)(x)
    x = BatchNorm(name='bn_conv1')(x, training=train_bn)
    x = KL.Activation('relu')(x)
    C1 = x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
    # Stage 2
    x = conv_block(x,
                   3, [64, 64, 256],
                   stage=2,
                   block='a',
                   strides=(1, 1),
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=2,
                       block='b',
                       train_bn=train_bn)
    C2 = x = identity_block(x,
                            3, [64, 64, 256],
                            stage=2,
                            block='c',
                            train_bn=train_bn)
    # Stage 3
    x = conv_block(x,
                   3, [128, 128, 512],
                   stage=3,
                   block='a',
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=3,
                       block='b',
                       train_bn=train_bn)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=3,
                       block='c',
                       train_bn=train_bn)
    C3 = x = identity_block(x,
                            3, [128, 128, 512],
                            stage=3,
                            block='d',
                            train_bn=train_bn)
    # Stage 4
    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=4,
                   block='a',
                   train_bn=train_bn)
    block_count = {"resnet50": 5, "resnet101": 22}[architecture]
    for i in range(block_count):
        x = identity_block(x,
                           3, [256, 256, 1024],
                           stage=4,
                           block=chr(98 + i),
                           train_bn=train_bn)
    C4 = x
    # Stage 5
    if stage5:
        x = conv_block(x,
                       3, [512, 512, 2048],
                       stage=5,
                       block='a',
                       train_bn=train_bn)
        x = identity_block(x,
                           3, [512, 512, 2048],
                           stage=5,
                           block='b',
                           train_bn=train_bn)
        C5 = x = identity_block(x,
                                3, [512, 512, 2048],
                                stage=5,
                                block='c',
                                train_bn=train_bn)
    else:
        C5 = None
    return [C1, C2, C3, C4, C5]