def AttentionResNetCifar10(shape=(32, 32, 3), n_channels=32, n_classes=10):
    """
    Attention-56 ResNet for Cifar10 Dataset
    https://arxiv.org/abs/1704.06904
    """
    input_ = Input(shape=shape)
    x = Conv2D(n_channels, (5, 5), padding='same')(input_)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)  # 16x16

    x = residual_block(x, input_channels=32, output_channels=128)
    x = attention_block(x, encoder_depth=2)

    x = residual_block(x, input_channels=128, output_channels=256, stride=2)  # 8x8
    x = attention_block(x, encoder_depth=1)

    x = residual_block(x, input_channels=256, output_channels=512, stride=2)  # 4x4
    x = attention_block(x, encoder_depth=1)

    x = residual_block(x, input_channels=512, output_channels=1024)
    x = residual_block(x, input_channels=1024, output_channels=1024)
    x = residual_block(x, input_channels=1024, output_channels=1024)

    x = AveragePooling2D(pool_size=(4, 4), strides=(1, 1))(x)  # 1x1
    x = Flatten()(x)
    output = Dense(n_classes, activation='softmax')(x)

    model = Model(input_, output)
    return model
Beispiel #2
0
def AttentionResNetCifar10(shape=(32, 32, 3), n_channels=32, n_classes=10):
    input_ = Input(shape=shape)
    x = Conv2D(n_channels, (5, 5), padding='same')(input_)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPool2D(pool_size=(2, 2))(x)  # 16x16

    x = residual_block(x, input_channels=32, output_channels=128)
    x = attention_block(x, encoder_depth=2)
    x = Conv2D(x.get_shape()[-1].value, (1, 1),name='residual_attention_stage1')(x)

    x = residual_block(x, input_channels=128, output_channels=256, stride=2)  # 8x8
    x = attention_block(x, encoder_depth=1)
    x = Conv2D(x.get_shape()[-1].value, (1, 1),name='residual_attention_stage2')(x)

    x = residual_block(x, input_channels=256, output_channels=512, stride=2)  # 4x4
    x = attention_block(x, encoder_depth=1) #第三个Attention Module在论文中是不需要要skip_connections的,所以encoder_depth在此处应该为0,不过代码为了方便还是设为1
    x = Conv2D(x.get_shape()[-1].value, (1, 1),name='residual_attention_stage3')(x)

    x = residual_block(x, input_channels=512, output_channels=1024)
    x = residual_block(x, input_channels=1024, output_channels=1024)
    x = residual_block(x, input_channels=1024, output_channels=1024)

    x = AveragePooling2D(pool_size=(4, 4), strides=(1, 1))(x)  # 1x1
    x = Flatten()(x)
    output = Dense(n_classes, activation='softmax')(x)

    model = Model(input_, output)
    
    return model
def AttentionResNet18(shape=(224, 224, 3), n_channels=64, n_classes=5, dropout=0, regularization=0.01):
    """
    Attention ResNet-18 with an attention-layer, variable imput size 
    """

    regularizer = l2(regularization)

    input_ = Input(shape=shape)
    x = Conv2D(n_channels, (7, 7), strides=(2, 2),
               padding='same')(input_)  # 112x112
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D(pool_size=(3, 3), strides=( 2, 2), padding='same')(x)  # 56x56

    x = residual_block(x, output_channels=n_channels * 16, stride=4)  # 14x14

    # ATTENTION LAYER
    x = attention_block(x, encoder_depth=1)  # bottleneck 7x7

    x = residual_block(x, output_channels=n_channels * 32, stride=2)  # 7x7

    pool_size = (x.get_shape()[1], x.get_shape()[2])
    x = AveragePooling2D(pool_size=pool_size, strides=(1, 1))(x)
    x = Flatten()(x)
    if dropout:
        x = Dropout(dropout)(x)
    output = Dense(n_classes, kernel_regularizer=regularizer,
                   activation='softmax')(x)

    model = Model(input_, output)
    return model
def AttentionResNet56(shape=(224, 224, 3),
                      n_channels=64,
                      n_classes=10,
                      dropout=0,
                      regularization=0.01):
    """
    Attention-56 ResNet
    https://arxiv.org/abs/1704.06904
    """

    regularizer = l2(regularization)

    input_ = Input(shape=shape)
    x = Conv2D(n_channels, (7, 7), strides=(2, 2),
               padding='same')(input_)  # 112x112
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2),
                     padding='same')(x)  # 56x56

    x = residual_block(x, output_channels=n_channels * 4)  # 56x56
    x = attention_block(x, encoder_depth=3)  # bottleneck 7x7

    x = residual_block(x, output_channels=n_channels * 8, stride=2)  # 28x28
    x = attention_block(x, encoder_depth=2)  # bottleneck 7x7

    x = residual_block(x, output_channels=n_channels * 16, stride=2)  # 14x14
    x = attention_block(x, encoder_depth=1)  # bottleneck 7x7

    x = residual_block(x, output_channels=n_channels * 32, stride=2)  # 7x7
    x = residual_block(x, output_channels=n_channels * 32)
    x = residual_block(x, output_channels=n_channels * 32)

    pool_size = (x.get_shape()[1].value, x.get_shape()[2].value)
    x = AveragePooling2D(pool_size=pool_size, strides=(1, 1))(x)
    x = Flatten()(x)
    if dropout:
        x = Dropout(dropout)(x)
    output = Dense(n_classes,
                   kernel_regularizer=regularizer,
                   activation='softmax')(x)

    model = Model(input_, output)
    return model
Beispiel #5
0
def attention(inputs,
              skip,
              depth,
              depth_bottleneck,
              scale=2,
              stride=1,
              rate=1,
              outputs_collections=None,
              scope=None):
    assert stride == 1
    if args.resize == 'bilinear':  #双线性插值
        resize_method = tf.image.ResizeMethod.BILINEAR
    if args.resize == 'nearest':  #最近邻插值
        resize_method = tf.image.ResizeMethod.NEAREST_NEIGHBOR
    with tf.variable_scope(scope, 'bottleneck_skip', [inputs, skip]) as sc:
        depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
        #res_inpt = tf.image.resize_images(inputs, tf.shape(skip)[1:3], method=resize_method)
        skip = attention_block(skip)
        res_inpt = slim.conv2d(inputs,
                               depth * scale * scale, [1, 1],
                               stride=1,
                               padding='SAME',
                               activation_fn=None,
                               scope=scope)

        res_inpt = tf.depth_to_space(res_inpt, scale)
        res_inpt = tf.image.resize_images(res_inpt,
                                          tf.shape(skip)[1:3],
                                          method=resize_method)
        if depth != depth_in:  #
            shortcut = slim.conv2d(
                res_inpt,
                depth, [1, 1],
                stride=stride,
                activation_fn=None,
                scope='shortcut')  #第一次resskip时,2048 != 512,要把shortcut的深度变为512
        else:
            shortcut = res_inpt

        # print("Live from skip bottleneck block! We got %s as input and %s as skip connection" % (inputs.get_shape(), skip.get_shape()))
        concat = tf.concat([res_inpt, skip], 3)
        residual = slim.conv2d(concat,
                               depth_bottleneck, [1, 1],
                               stride=stride,
                               scope='conv1')
        residual = resnet_utils.conv2d_same(residual,
                                            depth_bottleneck,
                                            3,
                                            1,
                                            rate=rate,
                                            scope='conv2')
        residual = slim.conv2d(residual,
                               depth, [1, 1],
                               stride=1,
                               activation_fn=None,
                               scope='conv3')

        output = tf.nn.relu(shortcut + residual)
        # print("So far in the end of bottleneck skip we have %s" % (output.get_shape()))

        return slim.utils.collect_named_outputs(outputs_collections, sc.name,
                                                output)