最近集中学习了一下Resnet的实现代码,在这将每一个文件都进行一个详细的分解。
首先分解的是Resnet的网络实现代码resnet.py。
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_averages
from config import Config
import datetime
import numpy as np
import os
import time
MOVING_AVERAGE_DECAY = 0.9997 #滑动平均模型衰减率
BN_DECAY = MOVING_AVERAGE_DECAY #BN层衰减率
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, ] #图像的平均BGR
tf.app.flags.DEFINE_integer('input_size', 224, "input image size") #输入图像的尺寸
activation = tf.nn.relu
#定义整个网络结构的函数
def inference(x, is_training,
num_classes=6,
num_blocks=[3, 4, 6, 3], # defaults to 50-layer network
use_bias=False, # defaults to using batch norm
bottleneck=True):
c = Config()
c['bottleneck'] = bottleneck #残差学习单元常量
c['is_training'] = tf.convert_to_tensor(is_training,
dtype='bool',
name='is_training')
c['ksize'] = 3 #缺省ksize值
c['stride'] = 1 #缺省stride值
c['use_bias'] = use_bias
c['fc_units_out'] = num_classes #全连接层输出
c['num_blocks'] = num_blocks #每一层的block数
c['stack_stride'] = 2
with tf.variable_scope('scale1'):
c['conv_filters_out'] = 64
c['ksize'] = 7
c['stride'] = 2
x = conv(x, c)
x = bn(x, c)
x = activation(x)
with tf.variable_scope('scale2'): #第一个残差单元输入64通道输出256通道
x = _max_pool(x, ksize=3, stride=2)
c['num_blocks'] = num_blocks[0]
c['stack_stride']