UNet以ResNet34为backbone in keras
backbone
Resnet34网络结构图:
其中在网络搭建的过程中分为4个stage,蓝色箭头是在Unet中要进行合并的层。注意:前向的运算encoder过程一共经过了5次降采样,包括刚开始的7∗77*77∗7卷积 stride,所以decoder过程要有5次上采样的过程,但是跨层连接(encoder 与 decoder之间)只有4次,如下图所示,以输入图像大小224x224为例:
Resnet34代码搭建(keras)
卷积block搭建
有两种形式:
A: 单纯的shortcut
B: 虚线的shortcut是对特征图的维度做了调整(1∗11*11∗1卷积)
def basic_identity_block(filters, stage, block):"""The identity block is the block that has no conv layer at shortcut.# Argumentskernel_size: default 3, the kernel size ofmiddle conv layer at main pathfilters: list of integers, the filters of 3 conv layer at main pathstage: integer, current stage label, used for generating layer namesblock: 'a','b'..., current block label, used for generating layer names# ReturnsOutput tensor for the block."""def layer(input_tensor):conv_params = get_conv_params()bn_params = get_bn_params()conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)x = BatchNormalization(name=bn_name + '1', **bn_params)(input_tensor)x = Activation('relu', name=relu_name + '1')(x)x = ZeroPadding2D(padding=(1, 1))(x)x = Conv2D(filters, (3, 3), name=conv_name + '1', **conv_params)(x)x = BatchNormalization(name=bn_name + '2', **bn_params)(x)x = Activation('relu', name=relu_name + '2')(x)x = ZeroPadding2D(padding=(1, 1))(x)x = Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)x = Add()([x, input_tensor])return xreturn layerdef basic_conv_block(filters, stage, block, strides=(2, 2)):"""The identity block is the block that has no conv layer at shortcut.# Argumentsinput_tensor: input tensorkernel_size: default 3, the kernel size ofmiddle conv layer at main pathfilters: list of integers, the filters of 3 conv layer at main pathstage: integer, current stage label, used for generating layer namesblock: 'a','b'..., current block label, used for generating layer names# ReturnsOutput tensor for the block."""def layer(input_tensor):conv_params = get_conv_params()bn_params = get_bn_params()conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)x = BatchNormalization(name=bn_name + '1', **bn_params)(input_tensor)x = Activation('relu', name=relu_name + '1')(x)shortcut = xx = ZeroPadding2D(padding=(1, 1))(x)x = Conv2D(filters, (3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)x = BatchNormalization(name=bn_name + '2', **bn_params)(x)x = Activation('relu', name=relu_name + '2')(x)x = ZeroPadding2D(padding=(1, 1))(x)x = Conv2D(filters, (3, 3), name=conv_name + '2', **conv_params)(x)shortcut = Conv2D(filters, (1, 1), name=sc_name, strides=strides, **conv_params)(shortcut)x = Add()([x, shortcut])return xreturn layer
Resnet34网络搭建
网络结构即如上图所示。
def build_resnet(repetitions=(2, 2, 2, 2),include_top=True,input_tensor=None,input_shape=None,classes=1000,block_type='usual'):# Determine proper input shapeinput_shape = _obtain_input_shape(input_shape,default_size=224,min_size=197,data_format='channels_last',require_flatten=include_top)if input_tensor is None:img_input = Input(shape=input_shape, name='data')else:if not K.is_keras_tensor(input_tensor):img_input = Input(tensor=input_tensor, shape=input_shape)else:img_input = input_tensor# get parameters for model layersno_scale_bn_params = get_bn_params(scale=False)bn_params = get_bn_params()conv_params = get_conv_params()init_filters = 64if block_type == 'basic':conv_block = basic_conv_blockidentity_block = basic_identity_blockelse:conv_block = usual_conv_blockidentity_block = usual_identity_block# renet bottomx = BatchNormalization(name='bn_data', **no_scale_bn_params)(img_input)x = ZeroPadding2D(padding=(3, 3))(x)x = Conv2D(init_filters, (7, 7), strides=(2, 2), name='conv0', **conv_params)(x)x = BatchNormalization(name='bn0', **bn_params)(x)x = Activation('relu', name='relu0')(x)x = ZeroPadding2D(padding=(1, 1))(x)x = MaxPooling2D((3, 3), strides=(2, 2), padding='valid', name='pooling0')(x)# resnet body repetitions = (3,4,6,3)for stage, rep in enumerate(repetitions):for block in range(rep):# print(block)filters = init_filters * (2**stage) # first block of first stage without strides because we have maxpooling beforeif block == 0 and stage == 0:# x = conv_block(filters, stage, block, strides=(1, 1))(x)x = identity_block(filters, stage, block)(x)continue elif block == 0:x = conv_block(filters, stage, block, strides=(2, 2))(x) else:x = identity_block(filters, stage, block)(x) x = BatchNormalization(name='bn1', **bn_params)(x)x = Activation('relu', name='relu1')(x)# resnet topif include_top:x = GlobalAveragePooling2D(name='pool1')(x)x = Dense(classes, name='fc1')(x)x = Activation('softmax', name='softmax')(x)# Ensure that the model takes into account any potential predecessors of `input_tensor`.if input_tensor is not None:inputs = get_source_inputs(input_tensor)else:inputs = img_input # Create model.model = Model(inputs, x)return model
def ResNet34(input_shape, input_tensor=None, weights=None, classes=1000, include_top=True):model = build_resnet(input_tensor=input_tensor,input_shape=input_shape,repetitions=(3, 4, 6, 3),classes=classes,include_top=include_top,block_type='basic')model.name = 'resnet34'if weights:load_model_weights(weights_collection, model, weights, classes, include_top)return model
decoder过程
def build_unet(backbone, classes, skip_connection_layers,decoder_filters=(256,128,64,32,16),upsample_rates=(2,2,2,2,2),n_upsample_blocks=5,block_type='upsampling',activation='sigmoid',use_batchnorm=True):input = backbone.inputx = backbone.outputif block_type == 'transpose':up_block = Transpose2D_blockelse:up_block = Upsample2D_block# convert layer names to indicesskip_connection_idx = ([get_layer_number(backbone, l) if isinstance(l, str) else lfor l in skip_connection_layers])# print(skip_connection_idx) [128, 73, 36, 5]for i in range(n_upsample_blocks):# print(i)# check if there is a skip connectionskip_connection = Noneif i < len(skip_connection_idx):skip_connection = backbone.layers[skip_connection_idx[i]].output# print(backbone.layers[skip_connection_idx[i]])# <keras.layers.core.Activation object at 0x00000164CC562A20>upsample_rate = to_tuple(upsample_rates[i])x = up_block(decoder_filters[i], i, upsample_rate=upsample_rate,skip=skip_connection, use_batchnorm=use_batchnorm)(x)x = Conv2D(classes, (3,3), padding='same', name='final_conv')(x)x = Activation(activation, name=activation)(x)model = Model(input, x)return model
参考:
- https://www.kaggle.com/meaninglesslives/unet-resnet34-in-keras
- https://github.com/qubvel/segmentation_models/blob/master/segmentation_models/unet/model.py
UNet以ResNet34为backbone in keras相关推荐
- 医学图像分割综述:U-Net系列
文章目录 Medical Image Segmentation Review:The Success of U-Net 摘要 引言 分类法 2D Unet 3D U-Net U-Ne的临床意义和疗效 ...
- Github语义分割框架(包含Unet,Unet++,MAnet等)
参考链接1:GitHub链接:GitHub - qubvel/segmentation_models.pytorch: Segmentation models with pretrained back ...
- 本科生晋升GM记录: Kaggle比赛进阶技巧分享
关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 作者:Gary 知乎链接:https://zhuanlan.zhihu.com/p ...
- 深度学习系列7:fastai
参考:https://zhuanlan.zhihu.com/p/89385842.https://www.zhihu.com/people/xie-dai-cheng FBI warning:长文多图 ...
- 一文详解AI模型部署及工业落地方式
点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 Hello大家好,我是老潘,好久不见各位~ 最近在复盘今年上半年做的一些事情,不管是训练模型.部署模型 ...
- 【天池赛事】零基础入门语义分割-地表建筑物识别 Task3:网络模型结构发展
[天池赛事]零基础入门语义分割-地表建筑物识别 Task1:赛题理解与 baseline(3 天) – 学习主题:理解赛题内容解题流程 – 学习内容:赛题理解.数据读取.比赛 baseline 构建 ...
- Understanding Clouds from Satellite Images的kernel调研+肉眼识别每种云朵示例
pupil1视角,凡是看过的全部进行upvote作为标记 kernel 注释 框架 输出模型格式 EDA: Find Me In The Clouds 非常赞的可视化(博文下方列出其主要内容) ...
- 使用 BEV 投影的高效城市规模点云分割
使用 BEV 投影的高效城市规模点云分割 论文 Efficient Urban-scale Point Clouds Segmentation with BEV Projection 地址 https ...
- 目标检测论文核心思想,18-19-20年论文梗概
论文太多来不及看,有些论文用了很大篇幅印证了某种模块有用或者没用,对于不研究这部分的人来说了解一下结论,这个模块有什么优缺点就够了.因而记录一下2018-2019-2020这几年泛读的论文梗概: 文章 ...
- TensorFlow+Pytorch识别阿猫阿狗
文章目录 猫狗大战 前言 1. TensorFlow版 1.1 获取数据集 1.2 载入划分训练集,并且构造数据生成器 1.3 模型构建与训练 2. Pytorch版 2.1 载入数据 2.2 模型构 ...
最新文章
- 漫水填充算法 - cvFloodFill() 实现
- 六、配置Oracle数据库的网络环境
- 【Python】透视表、统计表、汇总表、报表
- buu [BJDCTF 2nd]rsa0
- 我脑洞大开,让机器人来给我剪头发
- net如何判断浏览器的类别
- Go语言内置的基础类型
- GitHub 闯入印度!
- 《推荐系统实践》算法纯享(附代码链接)(六)—— 借助社交网络推荐篇
- CentOS7.5搭建ELK6.2.4集群及插件安装
- 计算机毕业设计java+jsp幼儿园信息网站(源码+系统+mysql数据库+Lw文档)
- html5化妆品网站源码,织梦响应式化妆美妆品类展示网站模板dedecms移动手机端HTML5自适应整站源码...
- android button设置边框背景颜色
- eclipse osgi_Eclipse通过提议的OSGi容器Kura支持M2M产品组合
- linux系统盘ssd,Linux系统下如何使用SSD固态硬盘
- 兆芯国产电脑如何安装Windows+麒麟kylin双系统
- 在ubuntu 上搭建Nginx-RTMP 直播服务器
- 使用git将代码提交到github
- StringIO cStringIO
- 颜色混合BlendFunc用法实例总结
热门文章
- atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException
- layout_gravity 属性和 gravity属性的区别
- idea中git回退远程仓库版本
- Android SQLite数据库 SQLiteOpenHelper的操作使用
- Bootstrap 更改Navbar默认样式
- ArcEngine 渲染的使用【转载】
- openmv探索_4_AprilTag标记追踪
- 一张图看懂AI、机器学习和深度学习的区别
- 上海人工智能实验室招聘NLP研究员和工程师啦,是事业单位呦~
- 珍惜平时一点一滴,这几个值得跟进学习的阿里、滴滴、微软超级牛人的公众号!...