ResNet34_keras dropout

ResNet34_keras dropout参考:https://www.kaggle.com/meaninglesslives/unet-resnet34-in-keras

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

Jetbrains全系列IDE稳定放心使用

backbone

Resnet34网络结构图:

其中在网络搭建的过程中分为4个stage,蓝色箭头是在Unet中要进行合并的层。注意:前向的运算encoder过程一共经过了5次降采样,包括刚开始的 7 ∗ 7 7*7 77卷积 stride,所以decoder过程要有5次上采样的过程,但是跨层连接(encoder 与 decoder之间)只有4次,如下图所示,以输入图像大小224×224为例:
在这里插入图片描述
在这里插入图片描述

Resnet34代码搭建(keras)

卷积block搭建

有两种形式:
在这里插入图片描述
A: 单纯的shortcut
B: 虚线的shortcut是对特征图的维度做了调整( 1 ∗ 1 1*1 11卷积)

def basic_identity_block(filters, stage, block):
"""The identity block is the block that has no conv layer at shortcut. # Arguments kernel_size: default 3, the kernel size of middle conv layer at main path filters: list of integers, the 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 # Returns Output 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 x
return layer
def basic_conv_block(filters, stage, block, strides=(2, 2)):
"""The identity block is the block that has no conv layer at shortcut. # Arguments input_tensor: input tensor kernel_size: default 3, the kernel size of middle conv layer at main path filters: list of integers, the 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 # Returns Output 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 = x
x = 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 x
return 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 shape
input_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 layers
no_scale_bn_params = get_bn_params(scale=False)
bn_params = get_bn_params()
conv_params = get_conv_params()
init_filters = 64
if block_type == 'basic':
conv_block = basic_conv_block
identity_block = basic_identity_block
else:
conv_block = usual_conv_block
identity_block = usual_identity_block
# renet bottom
x = 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 before
if 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 top
if 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.input
x = backbone.output
if block_type == 'transpose':
up_block = Transpose2D_block
else:
up_block = Upsample2D_block
# convert layer names to indices
skip_connection_idx = ([get_layer_number(backbone, l) if isinstance(l, str) else l
for 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 connection
skip_connection = None
if 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

参考:

  1. https://www.kaggle.com/meaninglesslives/unet-resnet34-in-keras
  2. https://github.com/qubvel/segmentation_models/blob/master/segmentation_models/unet/model.py
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185407.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Awvs详细使用教程「建议收藏」

    Awvs详细使用教程「建议收藏」Awvs的是一款非常好用的web漏洞扫描工具,他的扫描速度比较快,可以自己选择扫描速度,比较灵活。Awvs分为老的版本和新版本,下面我介绍的是新版本的功能和用法。功能介绍如下:仪表盘(监视器)功能,添加目标功能,漏洞排序功能,扫描功能,发现功能,用户功能,扫描配置功能,网络扫描功能,追踪器功能,防火墙设置,邮件设置,引擎,时间排除功能,代理功能,常规设置主要使用的功能是前面的6个,后面的根据个人的需要进行配置详细介绍如下:Dashboard功能:翻译意思仪表盘(监视器),可以对扫描对扫描

    2022年10月22日
  • reaver使用方法

    reaver使用方法闲话少叙使用方法:airmon-ngstartwlan0//启动mon0监控reaver-imon0-bMAC-a-S-vv//普通用法如果,90.9%进程后死机或停机,请记下PIN前四位数,用指令:reaver-imon0-bMAC-a-vv-pXXXX(PIN前四位数)其他命令airodump-ngmon0用来扫描周

  • 使用Fiddler进行Mock测试

    使用Fiddler进行Mock测试目录1、接口抓包2、复制该接口数据到本地3、修改你要mock的数据4、替换json文件1)在websession面板中找到对应的请求,然后将其拖到AutoResponder面板中。2)在RuleEditor中单击“Findafile…”,选择本地json文件的路径。5、激活规则6、save,刷新页面1、接口抓包找到要mock的接口,打开fiddler抓包以某某接口为例,找到下面的接口http://XXX/SYSTEMS2、复制该接口数据到本..

  • 群晖ds3617xs_23739虚拟机安装与半洗白教程

    群晖ds3617xs_23739虚拟机安装与半洗白教程需要安装vmware15及以上的版本1.引导镜像img转vm格式打开StarWind下一步选择synoboot.img下一步选择VMWarepre-allocatedimage下一步选择IDE下一步finish2.创建VMware虚拟机硬件兼容性Workstation15.xLinux其他Linux3.x内核64位硬盘1选择刚刚转换好的synoboot.vmdk保持现有格式SATA通道硬盘2添…

  • 超级简单的matplotlib安装教程

    超级简单的matplotlib安装教程网上许多matplotlib的安装教程都是比较复杂,需要配置许多环境,对于电脑基础不好的人来说可是一件头疼的事情,今天我介绍一个简单的安装方法。1.Win+R输入cmd进入到CMD窗口下,执行python-mpipinstall-Upipsetuptools进行升级。2.输入python-mpipinstallmatplotlib进行自动的安装,系统会自动下载安装包…

  • Week04-面向对象设计与继承

    Week04-面向对象设计与继承

发表回复

您的电子邮箱地址不会被公开。

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