二、训练

接下来回到train.py第160行,通过调用sw.train_model方法进行训练:

     def train_model(self, max_iters):
"""Network training loop."""
last_snapshot_iter = -1
timer = Timer()
model_paths = []
while self.solver.iter < max_iters:
# Make one SGD update
timer.tic()
self.solver.step(1)
timer.toc()
if self.solver.iter % (10 * self.solver_param.display) == 0:
print 'speed: {:.3f}s / iter'.format(timer.average_time) if self.solver.iter % cfg.TRAIN.SNAPSHOT_ITERS == 0:
last_snapshot_iter = self.solver.iter
model_paths.append(self.snapshot()) if last_snapshot_iter != self.solver.iter:
model_paths.append(self.snapshot())
return model_paths

方法中的self.solver.step(1)即是网络进行一次前向传播和反向传播。前向传播时,数据流会从第一层流动到最后一层,最后计算出loss,然后loss相对于各层输入的梯度会从最后一层计算回第一层。下面逐层来介绍faster-rcnn算法的运行过程。

2.1、input-data layer

第一层是由python代码构成的,其prototxt描述为:

layer {
name: 'input-data'
type: 'Python'
top: 'data'
top: 'im_info'
top: 'gt_boxes'
python_param {
module: 'roi_data_layer.layer'
layer: 'RoIDataLayer'
param_str: "'num_classes': 2"
}
}

从中可以看出,input-data层有三个输出:data、im_info、gt_boxes,其实现为RoIDataLayer类。这一层对数据的预处理操作为:对图片进行长宽等比例缩放,使短边缩放至600;如果缩放后,长边的长度大于1000,则以长边为基准,将长边缩放至1000,短边作相应的等比例缩放。这一层的3个输出分别为:

1、data:1, 3, h, w(一个batch只支持输入一张图)

2、im_info: im_info[0], im_info[1], im_info[2]分别为h, w, target_size/im_origin_size(缩放比例)

3、gt_boxes: (x1, y1, x2, y2, cls)

预处理部分涉及到的函数有_get_next_minibatchget_minibatch_get_image_blobprep_im_for_blobim_list_to_blob

网络在构造过程中(即self.solver = caffe.SGDSolver(solver_prototxt))会调用该类的setup方法:

 __C.TRAIN.IMS_PER_BATCH = 1
__C.TRAIN.SCALES = [600]
__C.TRAIN.MAX_SIZE = 1000
__C.TRAIN.HAS_RPN = True
__C.TRAIN.BBOX_REG = True def setup(self, bottom, top):
"""Setup the RoIDataLayer.""" # parse the layer parameter string, which must be valid YAML
layer_params = yaml.load(self.param_str_) self._num_classes = layer_params['num_classes'] self._name_to_top_map = {} # data blob: holds a batch of N images, each with 3 channels
idx = 0
top[idx].reshape(cfg.TRAIN.IMS_PER_BATCH, 3,
max(cfg.TRAIN.SCALES), cfg.TRAIN.MAX_SIZE)
self._name_to_top_map['data'] = idx
idx += 1 if cfg.TRAIN.HAS_RPN:
top[idx].reshape(1, 3)
self._name_to_top_map['im_info'] = idx
idx += 1 top[idx].reshape(1, 4)
self._name_to_top_map['gt_boxes'] = idx
idx += 1
else: # not using RPN
# rois blob: holds R regions of interest, each is a 5-tuple
# (n, x1, y1, x2, y2) specifying an image batch index n and a
# rectangle (x1, y1, x2, y2)
top[idx].reshape(1, 5)
self._name_to_top_map['rois'] = idx
idx += 1 # labels blob: R categorical labels in [0, ..., K] for K foreground
# classes plus background
top[idx].reshape(1)
self._name_to_top_map['labels'] = idx
idx += 1 if cfg.TRAIN.BBOX_REG:
# bbox_targets blob: R bounding-box regression targets with 4
# targets per class
top[idx].reshape(1, self._num_classes * 4)
self._name_to_top_map['bbox_targets'] = idx
idx += 1 # bbox_inside_weights blob: At most 4 targets per roi are active;
# thisbinary vector sepcifies the subset of active targets
top[idx].reshape(1, self._num_classes * 4)
self._name_to_top_map['bbox_inside_weights'] = idx
idx += 1 top[idx].reshape(1, self._num_classes * 4)
self._name_to_top_map['bbox_outside_weights'] = idx
idx += 1 print 'RoiDataLayer: name_to_top:', self._name_to_top_map
assert len(top) == len(self._name_to_top_map)

主要是对输出的shape进行定义。要说明的是,在前向传播的过程中,仍然会对输出的各top的shape进行重定义,并且二者定义的shape往往都是不同的。

最新文章

  1. java回调机制
  2. 图片懒加载--判断div ul中的li是否已经滑动到可视区域里
  3. Redis中的客户端redis-cli 命令总结
  4. 对于那本--你必须知道的499个C语言问题--总结
  5. [翻译]Spring框架参考文档(V4.3.3)-第二章Spring框架介绍 2.1 2.2 翻译--2.3待继续
  6. C#保存图片设置图片质量的方法
  7. 学习高博SLAM(1)
  8. Get the log from android device
  9. &lt;转&gt;RowState 介绍
  10. Python 发送邮件包含附件报表示例
  11. windows server 2003 禁止开机显示“关闭事件跟踪”
  12. linux块设备IO栈浅析
  13. CakePHP之请求与响应对象
  14. Js 导出Excel IE ActiveX控件
  15. MYSQL中的普通索引,主健,唯一,全文索引区别
  16. 转: requirejs中文api (详细)
  17. crm操作安全角色
  18. tkinter中布局pack、place和grid(八)
  19. new和malloc区别,delete和delete []区别
  20. face_recognition 模块安装

热门文章

  1. Laravel 5.4.36 session 发现
  2. checkbox与文字混排无法对齐到一行的解决办法
  3. IC验证概念总结
  4. 【PostgreSQL-9.6.3】临时表
  5. SQL基本操作——日期函数
  6. SQL基本操作——GROUP BY
  7. Centos6.7 编译安装 Apache PHP
  8. C# Request
  9. Appium 使用android_uiautomator定位元素时报错: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
  10. ES6 中set的用法