Beispiel #1
0
def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(2, 2),
               trainable=True):

    nb_filter1, nb_filter2, nb_filter3 = filters
    if K.image_data_format() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Convolution2D(nb_filter1, (1, 1),
                      strides=strides,
                      name=conv_name_base + '2a',
                      trainable=trainable)(input_tensor)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = Convolution2D(nb_filter2, (kernel_size, kernel_size),
                      padding='same',
                      name=conv_name_base + '2b',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = Convolution2D(nb_filter3, (1, 1),
                      name=conv_name_base + '2c',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)

    shortcut = Convolution2D(nb_filter3, (1, 1),
                             strides=strides,
                             name=conv_name_base + '1',
                             trainable=trainable)(input_tensor)
    shortcut = FixedBatchNormalization(axis=bn_axis,
                                       name=bn_name_base + '1')(shortcut)

    x = Add()([x, shortcut])
    x = Activation('relu')(x)
    return x
Beispiel #2
0
def identity_block_td(input_tensor,
                      kernel_size,
                      filters,
                      stage,
                      block,
                      trainable=True):

    # identity block time distributed

    nb_filter1, nb_filter2, nb_filter3 = filters
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = TimeDistributed(Convolution2D(nb_filter1, (1, 1),
                                      trainable=trainable,
                                      kernel_initializer='normal'),
                        name=conv_name_base + '2a')(input_tensor)
    x = TimeDistributed(FixedBatchNormalization(axis=bn_axis),
                        name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = TimeDistributed(Convolution2D(nb_filter2, (kernel_size, kernel_size),
                                      trainable=trainable,
                                      kernel_initializer='normal',
                                      padding='same'),
                        name=conv_name_base + '2b')(x)
    x = TimeDistributed(FixedBatchNormalization(axis=bn_axis),
                        name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = TimeDistributed(Convolution2D(nb_filter3, (1, 1),
                                      trainable=trainable,
                                      kernel_initializer='normal'),
                        name=conv_name_base + '2c')(x)
    x = TimeDistributed(FixedBatchNormalization(axis=bn_axis),
                        name=bn_name_base + '2c')(x)

    x = Add()([x, input_tensor])
    x = Activation('relu')(x)

    return x
Beispiel #3
0
def _depthwise_conv_block_td(inputs, pointwise_conv_filters, alpha, input_shape,
                          depth_multiplier=1, strides=(1, 1), block_id=1, trainable=True):
    """Adds a depthwise convolution block.
    """
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    pointwise_conv_filters = int(pointwise_conv_filters * alpha)

    x = TimeDistributed(DepthwiseConv2D((3, 3), padding='same', depth_multiplier=depth_multiplier, strides=strides,
                                        use_bias=False, trainable=trainable),
                        input_shape=input_shape, name='conv_dw_%d' % block_id)(inputs)
    x = TimeDistributed(FixedBatchNormalization(axis=channel_axis), name='conv_dw_%d_bn' % block_id)(x)

    x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)

    x = TimeDistributed(Conv2D(pointwise_conv_filters, (1, 1), padding='same', use_bias=False, strides=(1, 1),
                               trainable=trainable), name='conv_pw_%d' % block_id)(x)
    x = TimeDistributed(FixedBatchNormalization(axis=channel_axis), name='conv_pw_%d_bn' % block_id)(x)
    return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)
Beispiel #4
0
def identity_block(input_tensor,
                   kernel_size,
                   filters,
                   stage,
                   block,
                   trainable=True):

    nb_filter1, nb_filter2, nb_filter3 = filters

    bn_axis = 3

    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(nb_filter1, (1, 1),
               name=conv_name_base + '2a',
               trainable=trainable,
               kernel_regularizer=WEIGHT_RGLZ,
               bias_regularizer=BIAS_RGLZ)(input_tensor)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = Conv2D(nb_filter2, (kernel_size, kernel_size),
               padding='same',
               name=conv_name_base + '2b',
               trainable=trainable,
               kernel_regularizer=WEIGHT_RGLZ,
               bias_regularizer=BIAS_RGLZ)(x)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = Conv2D(nb_filter3, (1, 1),
               name=conv_name_base + '2c',
               trainable=trainable,
               kernel_regularizer=WEIGHT_RGLZ,
               bias_regularizer=BIAS_RGLZ)(x)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)

    x = Add()([x, input_tensor])
    x = Activation('relu')(x)
    return x
Beispiel #5
0
def res50ori(input_tensor=None,nb_classes=200, trainable=True):

    # 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

    x = ZeroPadding2D((3, 3))(img_input)

    x = Convolution2D(64, (7, 7), strides=(2, 2), name='conv1', trainable = trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

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

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

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', trainable = trainable)
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', trainable = trainable)
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', trainable = trainable)
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', trainable = trainable)
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', trainable = trainable)
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', trainable = trainable)
    # 看一下网络输出的形状.网络输出的形状是(1, 38, 56, 1024)

    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 = AveragePooling2D((7, 7), name='avg_pool')(x)
    x = Flatten()(x)
    x = Dense(nb_classes, activation='softmax', name='fc200')(x)
    return x
Beispiel #6
0
def identity_block(input_tensor,
                   kernel_size,
                   filters,
                   stage,
                   block,
                   trainable=True):

    nb_filter1, nb_filter2, nb_filter3 = filters

    # if K.image_dim_ordering() == 'tf':
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Convolution2D(nb_filter1, (1, 1),
                      name=conv_name_base + '2a',
                      trainable=trainable)(input_tensor)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = Convolution2D(nb_filter2, (kernel_size, kernel_size),
                      padding='same',
                      name=conv_name_base + '2b',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = Convolution2D(nb_filter3, (1, 1),
                      name=conv_name_base + '2c',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)

    x = Add()([x, input_tensor])
    x = Activation('relu')(x)
    return x
Beispiel #7
0
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

    print(img_input.shape)

    x = ZeroPadding2D((3, 3))(img_input)

    x = Convolution2D(64, (7, 7), strides=(2, 2), name='conv1', trainable = trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

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

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

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

    print("hello")
    print(x.shape)

    return x
Beispiel #8
0
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), trainable=True):
    '''conv_block is the block that has a conv layer at shortcut
    # Arguments
            input_tensor: input tensor
            kernel_size: defualt 3, the kernel size of middle conv layer at main path
            filters: list of integers, the nb_filters of 3 conv layer at main path
            stage: integer, current stage label, used for generating layer names
            block: 'a','b'..., current block label, used for generating layer names
    Note that from stage 3, the first conv layer at main path is with strides=(2,2)
    And the shortcut should have strides=(2,2) as well
    '''
    nb_filter1, nb_filter2, nb_filter3 = filters
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Convolution2D(nb_filter1, (1, 1), strides=strides, name=conv_name_base + '2a', trainable=trainable)(input_tensor)
    x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = Convolution2D(nb_filter2, (kernel_size, kernel_size), padding='same', name=conv_name_base + '2b', trainable=trainable)(x)
    x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = Convolution2D(nb_filter3, (1, 1), name=conv_name_base + '2c', trainable=trainable)(x)
    x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2c')(x)

    shortcut = Convolution2D(nb_filter3, (1, 1), strides=strides, name=conv_name_base + '1', trainable=trainable)(input_tensor)
    shortcut = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '1')(shortcut)

    x = merge([x, shortcut], mode='sum')
    x = Activation('relu')(x)
    return x
    def conv_block_td(self, input_tensor, kernel_size, filters, stage, block, input_shape, strides=(2, 2),
                      trainable=True):
        # conv block time distributed

        nb_filter1, nb_filter2, nb_filter3 = filters
        if K.image_dim_ordering() == 'tf':
            bn_axis = 3
        else:
            bn_axis = 1

        conv_name_base = 'res' + str(stage) + block + '_branch'
        bn_name_base = 'bn' + str(stage) + block + '_branch'

        x = TimeDistributed(
                Convolution2D(nb_filter1, (1, 1), strides=strides, trainable=trainable, kernel_initializer='normal'),
                input_shape=input_shape, name=conv_name_base + '2a')(input_tensor)
        x = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '2a')(x)
        x = Activation('relu')(x)

        x = TimeDistributed(Convolution2D(nb_filter2, (kernel_size, kernel_size), padding='same', trainable=trainable,
                                          kernel_initializer='normal'), name=conv_name_base + '2b')(x)
        x = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '2b')(x)
        x = Activation('relu')(x)

        x = TimeDistributed(Convolution2D(nb_filter3, (1, 1), kernel_initializer='normal'), name=conv_name_base + '2c',
                            trainable=trainable)(x)
        x = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '2c')(x)

        shortcut = TimeDistributed(
                Convolution2D(nb_filter3, (1, 1), strides=strides, trainable=trainable, kernel_initializer='normal'),
                name=conv_name_base + '1')(input_tensor)
        shortcut = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '1')(shortcut)

        x = Add()([x, shortcut])
        x = Activation('relu')(x)
        return x
Beispiel #10
0
def view_layers(x, input_shape, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround
    # (hence a smaller stride in the region that follows the ROI pool)
    if K.backend() == 'tensorflow':
        x = conv_block_td(x, 3, [512, 512, 2048], stage='_view_', block='a', input_shape=input_shape, strides=(2, 2), trainable=trainable)
    elif K.backend() == 'theano':
        x = conv_block_td(x, 3, [512, 512, 2048], stage='_view_', block='a', input_shape=input_shape, strides=(1, 1), trainable=trainable)

    x = identity_block_td(x, 3, [512, 512, 2048], stage='_view_', block='b', trainable=trainable)
    x = identity_block_td(x, 3, [512, 512, 2048], stage='_view_', block='c', trainable=trainable)
    x1 = TimeDistributed(AveragePooling2D((7, 7)), name='avg_pool_view')(x)
    x2 = TimeDistributed(Convolution2D(512, (1, 1), trainable=True, kernel_initializer='normal',padding='same'), name='bla_bla')(x)
    x2 = TimeDistributed(FixedBatchNormalization(axis=3), name='bka' + '2b')(x2)
    x2= Activation('relu')(x2)
    return x1,x2
def _conv_block(inputs,
                filters,
                alpha,
                kernel=(3, 3),
                strides=(1, 1),
                trainable=True):

    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    filters = int(filters * alpha)
    x = Conv2D(filters,
               kernel,
               padding='same',
               use_bias=False,
               strides=strides,
               name='conv1',
               trainable=trainable)(inputs)
    x = FixedBatchNormalization(axis=channel_axis, name='conv1_bn')(x)
    return Activation('relu6', name='conv1_relu')(x)
    def nn_base(self, input_tensor=None, trainable=False):
        # Determine proper input shape
        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

        bn_axis = 3

        x = ZeroPadding2D((3, 3))(img_input)

        x = Convolution2D(64, (7, 7),
                          strides=(2, 2),
                          name='conv1',
                          trainable=trainable)(x)
        x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2))(x)

        x = self.conv_block(x,
                            3, [64, 64, 128],
                            stage=2,
                            block='a',
                            strides=(1, 1),
                            trainable=trainable)
        x = self.conv_block(x,
                            3, [128, 128, 256],
                            stage=3,
                            block='a',
                            trainable=trainable)
        x = self.conv_block(x,
                            3, [256, 256, 512],
                            stage=4,
                            block='a',
                            trainable=trainable)

        return x
Beispiel #13
0
def nn_base(input_tensor_1=None, input_tensor_2=None, trainable=False):

    #print len(input_tensor)
    #print "length of input_tensor"
    # Determine proper input shape

    # Determine proper input shape
    with tf.device('/gpu:0'):
        if K.image_dim_ordering() == 'th':
            input_shape = (3, None, None)
        else:
            input_shape = (None, None, 3)

        if input_tensor_1 is None and input_tensor_2 is None:
            rgb_input = Input(shape=input_shape)
            thermal_input = Input(shape=input_shape)
        else:
            if not K.is_keras_tensor(input_tensor_1):
                rgb_input = Input(tensor=input_tensor_1, shape=input_shape)
                thermal_input = Input(tensor=input_tensor_2, shape=input_shape)
            else:
                rgb_input = input_tensor_1
                thermal_input = input_tensor_2

        if K.image_dim_ordering() == 'tf':
            bn_axis = 3
        else:
            bn_axis = 1

        x = ZeroPadding2D((3, 3), name='rgb_zero_padding')(rgb_input)
        x = Convolution2D(64, (7, 7),
                          strides=(2, 2),
                          kernel_initializer='he_normal',
                          name='rgbconv1',
                          trainable=trainable)(x)
        x = FixedBatchNormalization(axis=bn_axis, name='bn_rgbconv1')(x)
        x = Activation('relu')(x)

        x = Convolution2D(64, (3, 3),
                          padding='same',
                          kernel_initializer='he_normal',
                          name='rgbconv2',
                          trainable=trainable)(x)
        x = FixedBatchNormalization(axis=bn_axis, name='bn_rgbconv2')(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2))(x)

        y = ZeroPadding2D((3, 3), name='therm_zero_padding')(thermal_input)
        y = Convolution2D(64, (7, 7),
                          strides=(2, 2),
                          kernel_initializer='he_normal',
                          name='thermconv1',
                          trainable=trainable)(y)
        y = FixedBatchNormalization(axis=bn_axis, name='bn_thermconv1')(y)
        y = Activation('relu')(y)
        y = Convolution2D(64, (3, 3),
                          padding='same',
                          kernel_initializer='he_normal',
                          name='thermconv2',
                          trainable=trainable)(y)
        y = FixedBatchNormalization(axis=bn_axis, name='bn_thermconv2')(y)
        y = Activation('relu')(y)

        y = MaxPooling2D((3, 3), strides=(2, 2))(y)

        z = Add()([x, y])

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

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

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

        return x
Beispiel #14
0
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

    # personal_explanation:
    #    fill the bounding of 2-d input(like image) with zero, to control the size of output feature map
    # official explanation:
    #    This layer can add rows and columns of zeros at the top, bottom, left and right side of an image tensor.
    x = ZeroPadding2D(
        (3, 3))(img_input)  # equals to 'ZeroPadding2D(padding=3)(img_input)'

    x = Convolution2D(64, (7, 7),
                      strides=(2, 2),
                      name='conv1',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

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

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

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

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

    return x
Beispiel #15
0
def nn_base(input_tensor=None, trainable=False):
    '''
    输入图片矩阵
    经过残差神经网络
    输出feature map
    shape of feature map is (None, None, 1024)
    '''
    # Determine proper input shape
    #  import ipdb; ipdb.set_trace()
    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

    # padding 的数目由下一层的kernal_size决定的。(kernal_size-1)/2
    # kernal_size只能是奇数
    x = ZeroPadding2D((3, 3))(img_input)

    x = Convolution2D(64, (7, 7),
                      strides=(2, 2),
                      name='conv1',
                      trainable=trainable)(x)

    # print('+ conv 2d: ', x)
    print(
        'NOTE: this code only support to keras 2.0.3, newest version this line will got errors. see trace back.'
    )
    x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

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

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

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

    return x
Beispiel #16
0
def nn_base(input_tensor=None, trainable=False):

    # Determine proper input shape
    input_shape = (None, None, 3)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if hasattr(input_tensor, '_keras_history'):
            #            img_input = Input(tensor=input_tensor, shape=input_shape)
            img_input = input_tensor
        else:
            img_input = input_tensor

    bn_axis = 3

    x = ZeroPadding2D((3, 3))(img_input)

    x = Convolution2D(64, (7, 7),
                      strides=(2, 2),
                      name='conv1',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

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

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

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

    return x
Beispiel #17
0
def nn_base(input_tensor=None, trainable=False):
    # Determine proper input shape
    if K.image_data_format() == 'channels_first':
        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_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D((3, 3))(img_input)

    x = Convolution2D(64, (7, 7),
                      strides=(2, 2),
                      name='conv1',
                      trainable=trainable)(x)
    x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

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

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

    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=4,
                   block='a',
                   trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='b',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='c',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='d',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='e',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='f',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='g',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='h',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='i',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='j',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='k',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='l',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='m',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='n',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='o',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='p',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='q',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='r',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='s',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='t',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='u',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='v',
                       trainable=trainable)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=4,
                       block='w',
                       trainable=trainable)

    return x