Maskrcnn中resnet50改为resnet34

2 篇文章 0 订阅
1 篇文章 0 订阅

找到很多关于maskrcnn具体用法的代码,但是全是基于resnet50/101的,因需要训练的数据集并不复杂,resnet50的结构有点冗余,于是就把maskrcnn的backbone从resnet50改为resnet34结构。
找到model文件,将resnet50(侵删)部分代码做一定的修改,就可以得到resnet34的相关代码
下面是相关代码:


## con_block修改为conv_block0并添加到model文件中
def conv_block0(input_tensor, kernel_size, filters, stage, block,
               strides, use_bias=True, train_bn=True):
    nb_filter1, nb_filter2 = filters
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = KL.Conv2D(nb_filter1, (kernel_size, kernel_size),padding='same',strides=strides,
                  name=conv_name_base + '2a', use_bias=use_bias)(input_tensor)
    x = BatchNorm(name=bn_name_base + '2a')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size),padding='same',
                  name=conv_name_base + '2b', use_bias=use_bias)(x)
    x = BatchNorm(name=bn_name_base + '2b')(x, training=train_bn)

    shortcut = KL.Conv2D(nb_filter2, (1, 1), strides=strides, padding='same',
                         name=conv_name_base + '1', use_bias=use_bias)(input_tensor)
    shortcut = BatchNorm(name=bn_name_base + '1')(shortcut, training=train_bn)

    x = KL.Add()([x,shortcut ])
    x = KL.Activation('relu', name='res' + str(stage) + block + '_out')(x)

    return x
    
## identity_block修改为identity_block0,并添加
def identity_block0(input_tensor, kernel_size, filters,  stage, block,
                   use_bias=True, train_bn=True):
 
    nb_filter1, nb_filter2 = filters
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'


    x = KL.Conv2D(nb_filter1, (kernel_size, kernel_size),name=conv_name_base + '2a',
                  padding='same',

                  use_bias=use_bias)(input_tensor)
    x = BatchNorm(name=bn_name_base + '2a')(x, training=train_bn)
    x = KL.Activation('relu')(x)


    x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size), name=conv_name_base + '2b',padding='same',

                  use_bias=use_bias)(x)
    x = BatchNorm(name=bn_name_base + '2b')(x, training=train_bn)
    x = KL.Activation('relu', name='res' + str(stage) + block + '_out')(x)

    x = KL.Add()([x, input_tensor])

    return x

# 将resnet_graph改为

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 ["resnet34", "resnet50", "resnet101"]
    block_identify = {"resnet34": 0, "resnet50": 1, "resnet101": 1}[architecture]
    # 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
    if block_identify == 0:
        x = conv_block0(x, 3, [64,64], stage=2, block='a',strides=(1, 1),train_bn=train_bn)
        x = identity_block0(x, 3, [64,64], stage=2, block='b', train_bn=train_bn)
        C2 = x = identity_block0(x, 3, [64,64], stage=2, block='c',train_bn=train_bn)

    else:
        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
    if block_identify == 0:
        x = conv_block0(x, 3, [128,128], stage=3, block='a', strides=(2, 2),train_bn=train_bn)
        x = identity_block0(x, 3, [128,128], stage=3, block='b', train_bn=train_bn)
        x = identity_block0(x, 3, [128,128], stage=3, block='c', train_bn=train_bn)
        C3 = x = identity_block0(x, 3, [128,128], stage=3, block='d', train_bn=train_bn)

    else:
        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
    block_count = {"resnet34": 5, "resnet50": 5, "resnet101": 22}[architecture]
    if block_identify == 0:
        x = conv_block0(x, 3, [256,256], stage=4, block='a', strides=(2, 2),train_bn=train_bn)
        for i in range(block_count):
            x = identity_block0(x, 3, [256,256], stage=4, block=chr(98 + i), train_bn=train_bn)
        C4 = x


    else:
        x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', train_bn=train_bn)
        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:
        if block_identify == 0:
            x = conv_block0(x, 3, [512,512], stage=5, block='a', strides=(2, 2),train_bn=train_bn)
            x = identity_block0(x, 3, [512,512], stage=5, block='b', train_bn=train_bn)
            C5 = x = identity_block0(x, 3, [512,512], stage=5, block='c', train_bn=train_bn)

        else:
            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]




注:
1.初始化权重时我使用的是
https://github.com/qubvel/classification_models/releases/download/0.0.1/resnet34_imagenet_1000.h5
2.compute_backbone_shapes中也要加入resnet34

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值