个人代码阅读笔记。
第二次更新:2019.4.3
# --------------------------------------------------------
# Tensorflow Faster R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Zheqi He and Xinlei Chen
# --------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import tensorflow.contrib.slim as slim
from tensorflow.contrib.slim import losses
from tensorflow.contrib.slim import arg_scope
from tensorflow.contrib.slim.python.slim.nets import resnet_utils
from tensorflow.contrib.slim.python.slim.nets import resnet_v1
from tensorflow.contrib.slim.python.slim.nets.resnet_v1 import resnet_v1_block
import numpy as np
from nets.network import Network
from model.config import cfg
#传入一些参数,比如batch_norm_decay传入到decay中。
def resnet_arg_scope(is_training=True,
batch_norm_decay=0.997,
batch_norm_epsilon=1e-5,
batch_norm_scale=True):
batch_norm_params = {
'is_training': False,
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon,
'scale': batch_norm_scale,
'trainable': False,
'updates_collections': tf.GraphKeys.UPDATE_OPS
}
#arg_scope是tensorflow的slime模块自带的组建,张开一个变量作用域,方便用户定义一些参数。
#打开arg_scope,定义一些参数
with arg_scope(
[slim.conv2d],
weights_regularizer=slim.l2_regularizer(cfg.TRAIN.WEIGHT_DECAY),
weights_initializer=slim.variance_scaling_initializer(),
trainable=is_training,
activation_fn=tf.nn.relu,
normalizer_fn=slim.batch_norm,
normalizer_params=batch_norm_params):
with arg_scope([slim.batch_norm], **batch_norm_params) as arg_sc:
return arg_sc
#resnetv1()为Network的子类。其中有一些父类的方法不能满足需要,在子类中进行了方法重写,入
class resnetv1(Network):
def __init__(self, num_layers=50):
Network.__init__(self)
self._feat_stride = [16, ]#原图到输出的缩小比例
self._feat_compress = [1. / float(self._feat_stride[0]), ]#同上,倒数
self._num_layers = num_layers#层数
self._scope = 'resnet_v1_%d' % num_layers#scope的名称,我用的resnet_v1_101,所以打开它的scope
self._decide_blocks()
def