bottleneck resnet网络_深度学习|图像分类:ResNet(二)
接着上一讲,我们来看下ResNet的代码,代码的github链接为:ry/tensorflow-resnet,代码基于tensorflow编写。
网络结构的代码如下:
import skimage.io # bug. need to import this before tensorflow
import skimage.transform # bug. need to import this before tensorflow
import tensorflow as tf
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.training import moving_averagesfrom config import Configimport datetime
import numpy as np
import os
import timeMOVING_AVERAGE_DECAY = 0.9997
BN_DECAY = MOVING_AVERAGE_DECAY
BN_EPSILON = 0.001
CONV_WEIGHT_DECAY = 0.00004
CONV_WEIGHT_STDDEV = 0.1
FC_WEIGHT_DECAY = 0.00004
FC_WEIGHT_STDDEV = 0.01
RESNET_VARIABLES = 'resnet_variables'
UPDATE_OPS_COLLECTION = 'resnet_update_ops' # must be grouped with training op
IMAGENET_MEAN_BGR = [103.062623801, 115.902882574, 123.151630838, ]tf.app.flags.DEFINE_integer('input_size', 224, "input image size")activation = tf.nn.reludef inference(x, is_training,num_classes=1000,num_blocks=[3, 4, 6, 3], # defaults to 50-layer networkuse_bias=False, # defaults to using batch normbottleneck=True):# 定义网络中各参数c = Config()c['bottleneck'] = bottleneckc['is_training'] = tf.convert_to_tensor(is_training,dtype='bool',name='is_training')c['ksize'] = 3c['stride'] = 1c['use_bias'] = use_biasc['fc_units_out'] = num_classesc['num_blocks'] = num_blocksc['stack_stride'] = 2with tf.variable_scope('scale1'):c['conv_filters_out'] = 64c['ksize'] = 7c['stride'] = 2x = conv(x, c)x = bn(x, c)x = activation(x)with tf.variable_scope('scale2'):x = _max_pool(x, ksize=3, stride=2)c['num_blocks'] = num_blocks[0]c['stack_stride'] = 1c['block_filters_internal'] = 64x = stack(x, c)with tf.variable_scope('scale3'):c['num_blocks'] = num_blocks[1]c['block_filters_internal'] = 128assert c['stack_stride'] == 2x = stack(x, c)with tf.variable_scope('scale4'):c['num_blocks'] = num_blocks[2]c['block_filters_internal'] = 256x = stack(x, c)with tf.variable_scope('scale5'):c['num_blocks'] = num_blocks[3]c['block_filters_internal'] = 512x = stack(x, c)# post-netx = tf.reduce_mean(x, reduction_indices=[1, 2], name="avg_pool")if num_classes != None:with tf.variable_scope('fc'):x = fc(x, c)return x# 图像转换,从RGB转到BGR
def _imagenet_preprocess(rgb):"""Changes RGB [0,1] valued image to BGR [0,255] with mean subtracted."""red, green, blue = tf.split(3, 3, rgb * 255.0)bgr = tf.concat(3, [blue, green, red])bgr -= IMAGENET_MEAN_BGRreturn bgr# 损失函数
def loss(logits, labels):cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)cross_entropy_mean = tf.reduce_mean(cross_entropy)regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)loss_ = tf.add_n([cross_entropy_mean] + regularization_losses)tf.scalar_summary('loss', loss_)return loss_def stack(x, c):for n in range(c['num_blocks']):s = c['stack_stride'] if n == 0 else 1c['block_stride'] = swith tf.variable_scope('block%d' % (n + 1)):x = block(x, c)return x# 定义网络模块
def block(x, c):filters_in = x.get_shape()[-1]# Note: filters_out isn't how many filters are outputed. # That is the case when bottleneck=False but when bottleneck is # True, filters_internal*4 filters are outputted. filters_internal is how many filters# the 3x3 convs output internally.m = 4 if c['bottleneck'] else 1filters_out = m * c['block_filters_internal']shortcut = x # branch 1c['conv_filters_out'] = c['block_filters_internal']if c['bottleneck']:with tf.variable_scope('a'):c['ksize'] = 1c['stride'] = c['block_stride']x = conv(x, c)x = bn(x, c)x = activation(x)with tf.variable_scope('b'):x = conv(x, c)x = bn(x, c)x = activation(x)with tf.variable_scope('c'):c['conv_filters_out'] = filters_outc['ksize'] = 1assert c['stride'] == 1x = conv(x, c)x = bn(x, c)else:with tf.variable_scope('A'):c['stride'] = c['block_stride']assert c['ksize'] == 3x = conv(x, c)x = bn(x, c)x = activation(x)with tf.variable_scope('B'):c['conv_filters_out'] = filters_outassert c['ksize'] == 3assert c['stride'] == 1x = conv(x, c)x = bn(x, c)with tf.variable_scope('shortcut'):if filters_out != filters_in or c['block_stride'] != 1:c['ksize'] = 1c['stride'] = c['block_stride']c['conv_filters_out'] = filters_outshortcut = conv(shortcut, c)shortcut = bn(shortcut, c)return activation(x + shortcut)# BN:批规范化
def bn(x, c):x_shape = x.get_shape()params_shape = x_shape[-1:]if c['use_bias']:bias = _get_variable('bias', params_shape,initializer=tf.zeros_initializer)return x + biasaxis = list(range(len(x_shape) - 1))beta = _get_variable('beta',params_shape,initializer=tf.zeros_initializer)gamma = _get_variable('gamma',params_shape,initializer=tf.ones_initializer)moving_mean = _get_variable('moving_mean',params_shape,initializer=tf.zeros_initializer,trainable=False)moving_variance = _get_variable('moving_variance',params_shape,initializer=tf.ones_initializer,trainable=False)# These ops will only be preformed when training.mean, variance = tf.nn.moments(x, axis)update_moving_mean = moving_averages.assign_moving_average(moving_mean,mean, BN_DECAY)update_moving_variance = moving_averages.assign_moving_average(moving_variance, variance, BN_DECAY)tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_mean)tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_variance)mean, variance = control_flow_ops.cond(c['is_training'], lambda: (mean, variance),lambda: (moving_mean, moving_variance))x = tf.nn.batch_normalization(x, mean, variance, beta, gamma, BN_EPSILON)#x.set_shape(inputs.get_shape()) ??return x# 定义全连接层
def fc(x, c):num_units_in = x.get_shape()[1]num_units_out = c['fc_units_out']weights_initializer = tf.truncated_normal_initializer(stddev=FC_WEIGHT_STDDEV)weights = _get_variable('weights',shape=[num_units_in, num_units_out],initializer=weights_initializer,weight_decay=FC_WEIGHT_STDDEV)biases = _get_variable('biases',shape=[num_units_out],initializer=tf.zeros_initializer)x = tf.nn.xw_plus_b(x, weights, biases)return xdef _get_variable(name,shape,initializer,weight_decay=0.0,dtype='float',trainable=True):"A little wrapper around tf.get_variable to do weight decay and add to""resnet collection"if weight_decay > 0:regularizer = tf.contrib.layers.l2_regularizer(weight_decay)else:regularizer = Nonecollections = [tf.GraphKeys.VARIABLES, RESNET_VARIABLES]return tf.get_variable(name,shape=shape,initializer=initializer,dtype=dtype,regularizer=regularizer,collections=collections,trainable=trainable)# 定义卷积层
def conv(x, c):ksize = c['ksize']stride = c['stride']filters_out = c['conv_filters_out']filters_in = x.get_shape()[-1]shape = [ksize, ksize, filters_in, filters_out]initializer = tf.truncated_normal_initializer(stddev=CONV_WEIGHT_STDDEV)weights = _get_variable('weights',shape=shape,dtype='float',initializer=initializer,weight_decay=CONV_WEIGHT_DECAY)return tf.nn.conv2d(x, weights, [1, stride, stride, 1], padding='SAME')# 定义池化层
def _max_pool(x, ksize=3, stride=2):return tf.nn.max_pool(x,ksize=[1, ksize, ksize, 1],strides=[1, stride, stride, 1],padding='SAME')
bottleneck resnet网络_深度学习|图像分类:ResNet(二)相关推荐
- 深度学习深度前馈网络_深度学习前馈网络中的讲义第4部分
深度学习深度前馈网络 FAU深度学习讲义 (FAU Lecture Notes in Deep Learning) These are the lecture notes for FAU's YouT ...
- 深度学习深度前馈网络_深度学习前馈网络中的讲义第1部分
深度学习深度前馈网络 FAU深度学习讲义 (FAU Lecture Notes in Deep Learning) These are the lecture notes for FAU's YouT ...
- resnet网络结构_深度学习之16——残差网络(ResNet)
残差网络在设计之初,主要是服务于卷积神经网络(CNN),在计算机视觉领域应用较多,但是随着CNN结构的发展,在很多文本处理,文本分类里面(n-gram),也同样展现出来很好的效果. 首先先明确一下几个 ...
- python实现胶囊网络_深度学习精要之CapsuleNets理论与实践(附Python代码)
摘要: 本文对胶囊网络进行了非技术性的简要概括,分析了其两个重要属性,之后针对MNIST手写体数据集上验证多层感知机.卷积神经网络以及胶囊网络的性能. 神经网络于上世纪50年代提出,直到最近十年里才得 ...
- 深度信念网络_深度学习如何感知跟踪位置变化
位置感知能力是基于位置的服务(LBS)的核心.但是,准确估计目标的位置有时候并不是一件容易的事.全球定位系统(GPS)是户外最好的位置感知计算使能者,能够直接输出地理空间坐标,但其误差可能会超出某些应 ...
- 深度学习 图像分类_深度学习时代您应该阅读的10篇文章了解图像分类
深度学习 图像分类 前言 (Foreword) Computer vision is a subject to convert images and videos into machine-under ...
- 【深度学习】ResNet系列网络结构
[深度学习]ResNet系列网络结构 ResNet中Residual的功能 DNN的反向传播算法 梯度弥散和梯度爆炸 网络退化 ResNet中Residual的功能 ResNet系列网络结构 结语 R ...
- 深度学习图像分类(六):Stochastic_Depth_Net
深度学习图像分类(六):Stochastic_Depth_Net 文章目录 深度学习图像分类(六):Stochastic_Depth_Net 前言 一.Motivation 二.核心结构:Drop P ...
- 无人驾驶汽车系统入门(十一)——深度前馈网络,深度学习的正则化,交通信号识别
无人驾驶汽车系统入门(十一)--深度前馈网络,深度学习的正则化,交通信号识别 在第九篇博客中我们介绍了神经网络,它是一种机器学习方法,基于经验风险最小化策略,凭借这神经网络的拟合任意函数的能力,我们可 ...
最新文章
- NYOJ 49 开心的小明
- VTK:可视化算法之StreamlinesWithLineWidget
- SAP Spartacus Popover Directive 构造函数的用途分析
- React Native之(var和let区别 )(简单解构)(map对象遍历)(可变顺序参数和不可以变顺序参数函数)
- python:校验邮箱格式
- 线程属性 pthread_attr_t
- python的celery的面试_python面试基础题总结
- CentOS Mysql安装配置
- java模拟键盘操作,java自动化操作
- 排序数据图-R/python
- [Bada开发]基于bada1.0的5种控件介绍[待续]
- 锚具ovm是什么意思_OVM锚具资料
- supMap加载天地图
- 如何查看自己电脑的本地IP地址
- DSP28335的eCAP模块
- 调色板的原理和调色板显示模式
- python神经网络预测股票组合_神经网络预测股票市场
- android壁纸和手机屏幕之间要怎么对应,安卓Android手机屏幕壁纸分辨率选择技巧...
- Python升级之路( Lv9 ) 文件操作
- 【历史上的今天】6 月 18 日:京东诞生;网店平台 Etsy 成立;Facebook 发布 Libra 白皮书