Maskrcnn中resnet50改为resnet34「建议收藏」

Maskrcnn中resnet50改为resnet34「建议收藏」因需要训练的数据集并不复杂,resnet50的结构有点冗余,于是就把maskrcnn的backbone从resnet50改为resnet34结构。找到model文件,将resnet50部分代码做一定的修改,就可以得到resnet34的相关代码下面是相关代码:##con_block修改为conv_block0并添加到model文件中defconv_block0(input_tensor…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

找到很多关于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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185483.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • excel截取字符串函数_截取函数 excel

    excel截取字符串函数_截取函数 excelExcel中共提供了三种函数来对字符串进行截取操作left(text,num):用于对一个文本字符串,从左向右提取指定个数的字符right(text,num):用于对一个文本字符串,从右向左提取指定个数的字符MID(text,start_num,num_chars):从一个文本字符串的指定位置开始,截取指定个数的字符…

    2022年10月22日
    0
  • timestampdiff的一个BUG

    timestampdiff的一个BUG发现一个timestampdiff无法正确判断列值的BUG,看下面例子:–测试表如下:mysql>select*fromtest;+—-+———————+|id|col2|+—-+———————+|1|2019-03-2000:00:00||2|2…

    2022年5月2日
    38
  • pandas中的 fillna使用(pandas.DataFrame.fillna)「建议收藏」

    pandas中的 fillna使用(pandas.DataFrame.fillna)「建议收藏」api参考:fillna:使用指定的方法填充NA/NaN值。>>>df=pd.DataFrame([[np.nan,2,np.nan,0],[3,4,np.nan,1],[np.nan,np.nan,np.nan,5],[np.nan,3,np.nan,4]],columns=list.

    2022年8月12日
    3
  • python进阶(6)深拷贝和浅拷贝「建议收藏」

    python进阶(6)深拷贝和浅拷贝「建议收藏」深拷贝和浅拷贝不管对于浅拷贝、还是深拷贝,针对不可变对象str、int、tuple(有点特殊)、boolean,它的内存地址是不变的,拷贝的仅仅是值importcopya=1b=co

    2022年7月28日
    1
  • strtok独到深刻的讲解「建议收藏」

    strtok独到深刻的讲解「建议收藏」strtok函数的使用是一个老生常谈的问题了。该函数的作用很大,争议也很大。以下的表述可能与一些资料有区别或者说与你原来的认识有差异,因此,我尽量以实验为证。交代一下实验环境是必要的,winxp+vc6.0,一个极端平民化的实验环境。本文中使用的源代码大部分来自于网络,我稍加修改作为例证。当然,本人水平有限,有不妥之处在所难免,各位见谅的同时不妨多做实验,以实验为证。strtok的

    2022年7月14日
    15
  • Java中的锁

    Java中的锁在学习或者使用Java的过程中进程会遇到各种各样的锁的概念:公平锁、非公平锁、自旋锁、可重入锁、偏向锁、轻量级锁、重量级锁、读写锁、互斥锁等待。这里整理了Java中的各种锁,若有不足之处希望大家在下方留言探讨。WARNING:本文适合有一定JAVA基础的同学阅读。公平锁和非公平锁公平锁是指多个线程在等待同一个锁时,必须按照申请锁的先后顺序来一次获得锁。公平锁的好处是等待锁的线程…

    2022年7月18日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号