接着上一讲,我们来看下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(二)相关推荐

  1. 深度学习深度前馈网络_深度学习前馈网络中的讲义第4部分

    深度学习深度前馈网络 FAU深度学习讲义 (FAU Lecture Notes in Deep Learning) These are the lecture notes for FAU's YouT ...

  2. 深度学习深度前馈网络_深度学习前馈网络中的讲义第1部分

    深度学习深度前馈网络 FAU深度学习讲义 (FAU Lecture Notes in Deep Learning) These are the lecture notes for FAU's YouT ...

  3. resnet网络结构_深度学习之16——残差网络(ResNet)

    残差网络在设计之初,主要是服务于卷积神经网络(CNN),在计算机视觉领域应用较多,但是随着CNN结构的发展,在很多文本处理,文本分类里面(n-gram),也同样展现出来很好的效果. 首先先明确一下几个 ...

  4. python实现胶囊网络_深度学习精要之CapsuleNets理论与实践(附Python代码)

    摘要: 本文对胶囊网络进行了非技术性的简要概括,分析了其两个重要属性,之后针对MNIST手写体数据集上验证多层感知机.卷积神经网络以及胶囊网络的性能. 神经网络于上世纪50年代提出,直到最近十年里才得 ...

  5. 深度信念网络_深度学习如何感知跟踪位置变化

    位置感知能力是基于位置的服务(LBS)的核心.但是,准确估计目标的位置有时候并不是一件容易的事.全球定位系统(GPS)是户外最好的位置感知计算使能者,能够直接输出地理空间坐标,但其误差可能会超出某些应 ...

  6. 深度学习 图像分类_深度学习时代您应该阅读的10篇文章了解图像分类

    深度学习 图像分类 前言 (Foreword) Computer vision is a subject to convert images and videos into machine-under ...

  7. 【深度学习】ResNet系列网络结构

    [深度学习]ResNet系列网络结构 ResNet中Residual的功能 DNN的反向传播算法 梯度弥散和梯度爆炸 网络退化 ResNet中Residual的功能 ResNet系列网络结构 结语 R ...

  8. 深度学习图像分类(六):Stochastic_Depth_Net

    深度学习图像分类(六):Stochastic_Depth_Net 文章目录 深度学习图像分类(六):Stochastic_Depth_Net 前言 一.Motivation 二.核心结构:Drop P ...

  9. 无人驾驶汽车系统入门(十一)——深度前馈网络,深度学习的正则化,交通信号识别

    无人驾驶汽车系统入门(十一)--深度前馈网络,深度学习的正则化,交通信号识别 在第九篇博客中我们介绍了神经网络,它是一种机器学习方法,基于经验风险最小化策略,凭借这神经网络的拟合任意函数的能力,我们可 ...

最新文章

  1. Android--高德地图自动定位
  2. [JQuery] jQuery选择器ID、CLASS、标签获取对象值、属性、设置css样式
  3. 自定义一个SharedPreferences工具类
  4. 阿里云开源项目 OAM 负责人张磊入选「中国开源先锋 33 人」
  5. [原创] ASP.NET 应用程序中使用定时器
  6. RSF 分布式服务框架-传输协议层设计
  7. day023 常用模块02
  8. 企业信息化投入中咨询服务_嘉萱企业管理咨询服务 让发展中的企业真正实现全面性效益增长...
  9. 邮件服务器漏洞攻击,Exim邮件服务中的严重漏洞分析
  10. 打开com方式和dos常用命令
  11. 从零开始打造自己的PHP框架——第3章
  12. wps带阴影的边框怎么设置_wps如何添加阴影边框-wps设置阴影边框的方法 - 河东软件园...
  13. jTopo 功能完善
  14. Win10下安装Ubuntu20.04双系统,看这一篇就够了
  15. java获取生僻字_生僻字与16进制的转换
  16. 避坑外连腾讯云服务器redis 6379
  17. 扣减库存,redis你值得拥有
  18. python与idle区别_idle和python区别
  19. Linux自动重启服务
  20. 怎么用c语言编辑酷q,GitHub - traceless0929/Native.Cqp.Csharp: 完全由 C# 语言编写的 酷Q 插件SDK...

热门文章

  1. C/C++排序算法(3)冒泡排序
  2. ubuntu不会自动休眠_关机、睡眠、休眠有啥区别?微软说非特殊情况不要关机
  3. MySQL SQL 优化命令行问题 SQL 抓取方式
  4. 2020,国产数据库崭露峥嵘的发轫之年
  5. 化繁为简:数据库运维人员应该知道这些...
  6. 在Vue中使用JSX,很easy的
  7. M-SQL:超强的多任务表示学习方法
  8. 5步教你将MRS数据导入DWS
  9. 开发实践丨用小熊派STM32开发板模拟自动售货机
  10. “3+3”看华为云FusionInsight如何引领“数据新基建”持续发展