转载自:faster rcnn源码解读(六)之minibatch - 野孩子的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/u010668907/article/details/51945917

faster rcnn用python版本的https://github.com/rbgirshick/py-faster-rcnn

minibatch源码:https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/roi_data_layer/minibatch.py

源码:

[python] view plaincopy print?
  1. # --------------------------------------------------------
  2. # Fast R-CNN
  3. # Copyright (c) 2015 Microsoft
  4. # Licensed under The MIT License [see LICENSE for details]
  5. # Written by Ross Girshick
  6. # --------------------------------------------------------
  7. """Compute minibatch blobs for training a Fast R-CNN network."""
  8. import numpy as np
  9. import numpy.random as npr
  10. import cv2
  11. from fast_rcnn.config import cfg
  12. from utils.blob import prep_im_for_blob, im_list_to_blob
  13. def get_minibatch(roidb, num_classes):
  14. """Given a roidb, construct a minibatch sampled from it."""
  15. num_images = len(roidb)
  16. # Sample random scales to use for each image in this batch
  17. random_scale_inds = npr.randint(0, high=len(cfg.TRAIN.SCALES),
  18. size=num_images)#随机索引组成的numpy,大小是roidb的长度
  19. assert(cfg.TRAIN.BATCH_SIZE % num_images == 0), \
  20. 'num_images ({}) must divide BATCH_SIZE ({})'. \
  21. format(num_images, cfg.TRAIN.BATCH_SIZE)
  22. rois_per_image = cfg.TRAIN.BATCH_SIZE / num_images
  23. fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
  24. # Get the input image blob, formatted for caffe
  25. im_blob, im_scales = _get_image_blob(roidb, random_scale_inds)
  26. blobs = {'data': im_blob}
  27. if cfg.TRAIN.HAS_RPN:
  28. assert len(im_scales) == 1, "Single batch only"
  29. assert len(roidb) == 1, "Single batch only"
  30. # gt boxes: (x1, y1, x2, y2, cls)
  31. gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]
  32. gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32)
  33. gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :] * im_scales[0]
  34. gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]
  35. blobs['gt_boxes'] = gt_boxes
  36. blobs['im_info'] = np.array(
  37. [[im_blob.shape[2], im_blob.shape[3], im_scales[0]]],
  38. dtype=np.float32)
  39. else: # not using RPN
  40. # Now, build the region of interest and label blobs
  41. rois_blob = np.zeros((0, 5), dtype=np.float32)
  42. labels_blob = np.zeros((0), dtype=np.float32)
  43. bbox_targets_blob = np.zeros((0, 4 * num_classes), dtype=np.float32)
  44. bbox_inside_blob = np.zeros(bbox_targets_blob.shape, dtype=np.float32)
  45. # all_overlaps = []
  46. for im_i in xrange(num_images):
  47. labels, overlaps, im_rois, bbox_targets, bbox_inside_weights \
  48. = _sample_rois(roidb[im_i], fg_rois_per_image, rois_per_image,
  49. num_classes)
  50. # Add to RoIs blob
  51. rois = _project_im_rois(im_rois, im_scales[im_i])
  52. batch_ind = im_i * np.ones((rois.shape[0], 1))
  53. rois_blob_this_image = np.hstack((batch_ind, rois))
  54. rois_blob = np.vstack((rois_blob, rois_blob_this_image))
  55. # Add to labels, bbox targets, and bbox loss blobs
  56. labels_blob = np.hstack((labels_blob, labels))
  57. bbox_targets_blob = np.vstack((bbox_targets_blob, bbox_targets))
  58. bbox_inside_blob = np.vstack((bbox_inside_blob, bbox_inside_weights))
  59. # all_overlaps = np.hstack((all_overlaps, overlaps))
  60. # For debug visualizations
  61. # _vis_minibatch(im_blob, rois_blob, labels_blob, all_overlaps)
  62. blobs['rois'] = rois_blob
  63. blobs['labels'] = labels_blob
  64. if cfg.TRAIN.BBOX_REG:
  65. blobs['bbox_targets'] = bbox_targets_blob
  66. blobs['bbox_inside_weights'] = bbox_inside_blob
  67. blobs['bbox_outside_weights'] = \
  68. np.array(bbox_inside_blob > 0).astype(np.float32)
  69. return blobs
  70. def _sample_rois(roidb, fg_rois_per_image, rois_per_image, num_classes):
  71. """Generate a random sample of RoIs comprising foreground and background
  72. examples.
  73. """
  74. # label = class RoI has max overlap with
  75. labels = roidb['max_classes']
  76. overlaps = roidb['max_overlaps']
  77. rois = roidb['boxes']
  78. # Select foreground RoIs as those with >= FG_THRESH overlap
  79. fg_inds = np.where(overlaps >= cfg.TRAIN.FG_THRESH)[0]
  80. # Guard against the case when an image has fewer than fg_rois_per_image
  81. # foreground RoIs
  82. fg_rois_per_this_image = np.minimum(fg_rois_per_image, fg_inds.size)
  83. # Sample foreground regions without replacement
  84. if fg_inds.size > 0:
  85. fg_inds = npr.choice(
  86. fg_inds, size=fg_rois_per_this_image, replace=False)
  87. # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
  88. bg_inds = np.where((overlaps < cfg.TRAIN.BG_THRESH_HI) &
  89. (overlaps >= cfg.TRAIN.BG_THRESH_LO))[0]
  90. # Compute number of background RoIs to take from this image (guarding
  91. # against there being fewer than desired)
  92. bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
  93. bg_rois_per_this_image = np.minimum(bg_rois_per_this_image,
  94. bg_inds.size)
  95. # Sample foreground regions without replacement
  96. if bg_inds.size > 0:
  97. bg_inds = npr.choice(
  98. bg_inds, size=bg_rois_per_this_image, replace=False)
  99. # The indices that we're selecting (both fg and bg)
  100. keep_inds = np.append(fg_inds, bg_inds)
  101. # Select sampled values from various arrays:
  102. labels = labels[keep_inds]
  103. # Clamp labels for the background RoIs to 0
  104. labels[fg_rois_per_this_image:] = 0
  105. overlaps = overlaps[keep_inds]
  106. rois = rois[keep_inds]
  107. bbox_targets, bbox_inside_weights = _get_bbox_regression_labels(
  108. roidb['bbox_targets'][keep_inds, :], num_classes)
  109. return labels, overlaps, rois, bbox_targets, bbox_inside_weights
  110. def _get_image_blob(roidb, scale_inds):
  111. """Builds an input blob from the images in the roidb at the specified
  112. scales.
  113. """
  114. num_images = len(roidb)
  115. processed_ims = []
  116. im_scales = []
  117. for i in xrange(num_images):
  118. im = cv2.imread(roidb[i]['image'])
  119. if roidb[i]['flipped']:
  120. im = im[:, ::-1, :]
  121. target_size = cfg.TRAIN.SCALES[scale_inds[i]]
  122. im, im_scale = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size,
  123. cfg.TRAIN.MAX_SIZE)prep_im_for_blob: util的blob.py中;用于将图片平均后缩放。#im_scales: 每张图片的缩放率
[python] view plaincopy print?
  1. #  cfg.PIXEL_MEANS: 原始图片会集体减去该值达到mean
  2. im_scales.append(im_scale)
  3. processed_ims.append(im)
  4. # Create a blob to hold the input images
  5. blob = im_list_to_blob(processed_ims)#将以list形式存放的图片数据处理成(batch elem, channel, height, width)的im_blob形式,height,width用的是此次计算所有图片的最大值
  6. return blob, im_scales#blob是一个字典,与name_to_top对应,方便把blob数据放进top
  7. def _project_im_rois(im_rois, im_scale_factor):
  8. """Project image RoIs into the rescaled training image."""
  9. rois = im_rois * im_scale_factor
  10. return rois
  11. def _get_bbox_regression_labels(bbox_target_data, num_classes):
  12. """Bounding-box regression targets are stored in a compact form in the
  13. roidb.
  14. This function expands those targets into the 4-of-4*K representation used
  15. by the network (i.e. only one class has non-zero targets). The loss weights
  16. are similarly expanded.
  17. Returns:
  18. bbox_target_data (ndarray): N x 4K blob of regression targets
  19. bbox_inside_weights (ndarray): N x 4K blob of loss weights
  20. """
  21. clss = bbox_target_data[:, 0]
  22. bbox_targets = np.zeros((clss.size, 4 * num_classes), dtype=np.float32)
  23. bbox_inside_weights = np.zeros(bbox_targets.shape, dtype=np.float32)
  24. inds = np.where(clss > 0)[0]
  25. for ind in inds:
  26. cls = clss[ind]
  27. start = 4 * cls
  28. end = start + 4
  29. bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
  30. bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
  31. return bbox_targets, bbox_inside_weights
  32. def _vis_minibatch(im_blob, rois_blob, labels_blob, overlaps):
  33. """Visualize a mini-batch for debugging."""
  34. import matplotlib.pyplot as plt
  35. for i in xrange(rois_blob.shape[0]):
  36. rois = rois_blob[i, :]
  37. im_ind = rois[0]
  38. roi = rois[1:]
  39. im = im_blob[im_ind, :, :, :].transpose((1, 2, 0)).copy()
  40. im += cfg.PIXEL_MEANS
  41. im = im[:, :, (2, 1, 0)]
  42. im = im.astype(np.uint8)
  43. cls = labels_blob[i]
  44. plt.imshow(im)
  45. print 'class: ', cls, ' overlap: ', overlaps[i]
  46. plt.gca().add_patch(
  47. plt.Rectangle((roi[0], roi[1]), roi[2] - roi[0],
  48. roi[3] - roi[1], fill=False,
  49. edgecolor='r', linewidth=3)
  50. )
  51. plt.show()

solver.step(1)-》reshape-》forward-》_get_next_minbatch-》_get_next_minbatch_inds-》(前面在layers里,现在进入minibatch组建真正的blob)get_minibatch

4.1 cfg.TRAIN.SCALES: 图片被缩放的target_size列表

4.2 random_scale_inds:列表的随机索引组成的numpy,大小是roidb的长度

4.3 cfg.PIXEL_MEANS: 原始图片会集体减去该值达到mean

4.4 im_scales: 每张图片的缩放率

缩放率的求法: im_scales = target_size/min(width, height);

if im_scales*max(width, height) > cfg.TRAIN.MAX_SIZE

im_scales = cfg.TRAIN.MAX_SIZE * max(width, height)

4.5 prep_im_for_blob: util的blob.py中;用于将图片平均后缩放。

4.6 im_list_to_blob(ims): 将以list形式存放的图片数据处理成(batch elem, channel, height, width)的im_blob形式,height,width用的是此次计算所有图片的最大值

4.7 blob是一个字典:data,一个batch的处理过的所有图片数据,即上面的im_blob;

im_info, [im_blob.shape[2], im_blob.shape[3], im_scales[0]]

gt_boxes, 是前四列是box的值,第五列是box的类别。

box=原box*im_scales

blob与name_to_top对应,方便把blob数据放进top

!!!minibatch.py中34行的代码表明,batchsize即cfg.TRAIN.IMS_PER_BATCH只能是1

faster rcnn源码解读(六)之minibatch相关推荐

  1. faster rcnn源码解读(五)之layer(网络里的input-data)

    转载自:faster rcnn源码解读(五)之layer(网络里的input-data) - 野孩子的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/u010668 ...

  2. faster rcnn源码解读(四)之数据类型imdb.py和pascal_voc.py(主要是imdb和roidb数据类型的解说)

    转载自:faster rcnn源码解读(四)之数据类型imdb.py和pascal_voc.py(主要是imdb和roidb数据类型的解说) - 野孩子的专栏 - 博客频道 - CSDN.NET ht ...

  3. faster rcnn源码解读(三)train_faster_rcnn_alt_opt.py

    转载自:faster rcnn源码解读(三)train_faster_rcnn_alt_opt.py - 野孩子的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/u ...

  4. faster rcnn源码解读总结

    转载自:faster rcnn源码解读总结 - 野孩子的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/u010668907/article/details/519 ...

  5. 还不懂目标检测嘛?一起来看看Faster R-CNN源码解读

  6. Faster R-CNN源码中RPN的解析(自用)

    参考博客(一定要看前面两个) 一文看懂Faster R-CNN 详细的Faster R-CNN源码解析之RPN源码解析 关于RPN一些我的想法 rpn的中心思想就是在了anchors了,如何产生anc ...

  7. 【Faster R-CNN论文精度系列】从Faster R-CNN源码中,我们“学习”到了什么?

    [Faster R-CNN论文精度系列] (如下为建议阅读顺序) 1[Faster R-CNN论文精度系列]从Faster R-CNN源码中,我们"学习"到了什么? 2[Faste ...

  8. faster rcnn源码理解(二)之AnchorTargetLayer(网络中的rpn_data)

    转载自:faster rcnn源码理解(二)之AnchorTargetLayer(网络中的rpn_data) - 野孩子的专栏 - 博客频道 - CSDN.NET http://blog.csdn.n ...

  9. Mask RCNN源码解读

    Mask RCNN源码解读 前言 数据集 数据载入 模型搭建 模型输入 模型输出 resnet101 RPN网络 ProposalLayer DetectionTargetLayer fpn_clas ...

最新文章

  1. tf.variance_scaling_initializer() tensorflow学习:参数初始化
  2. c语言exit在哪个头文件_C语言函数执行成功时,返回1和返回0,究竟哪个好?
  3. ibatis.net私人学习资料,勿下(加密)
  4. LyaoutParameters作用
  5. python可以做什么有趣的东西-python能做哪些生活有趣的事情
  6. c语言编译器内部错误,C++致命错误C1001:编译器中发生内部错误
  7. zblog php标签,201502200101 zblogphp调整“显示常用标签”个数方法
  8. 102. Leetcode 198. 打家劫舍 (动态规划-打家劫舍)
  9. vue 点击div 获取位置_Vue中组件之间8种通信方式,值得收藏
  10. C++ cin.putback()输入【已知行数】但【未知每行数字个数】的思路
  11. ButterKnife的简单使用
  12. 青岛计算机类职业中学,青岛最好的职业学校有哪些?
  13. get与post的区别与联系
  14. 浅谈MaxCompute资源规划管理及评估
  15. angular封装富文本编辑器指令
  16. MVC的WebApi中开启Session会话支持
  17. nginx平滑升级至最新版的nginx-1.9.5
  18. 页面加载时,下方内容在上方图片位置闪现
  19. word选择性粘贴html和rtf,Word“选择性粘贴”功能有妙用
  20. 新浪微博热门话题 (30 分)

热门文章

  1. flink入门实战总结
  2. Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转
  3. java中hashMap的排序
  4. Dapper,大规模分布式系统的跟踪系统--转
  5. logback--How do I configure an AsyncAppender with code? 转载
  6. Java 编程的动态性,第 5 部分: 动态转换类--转载
  7. 【开发工具】学习记录 初学MATLAB
  8. 【SQL】数据库的SQL查询,涉及多个数据库
  9. RuoYi(若依开源框架)-前后台分离版-后端流程简单分析
  10. 世界上最浪费时间的三件事