博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Faster-RCNN Pytorch实现的minibatch包装
阅读量:4555 次
发布时间:2019-06-08

本文共 4468 字,大约阅读时间需要 14 分钟。

实际上faster-rcnn对于输入的图片是有resize操作的,在resize的图片基础上提取feature map,而后generate一定数量的RoI。

我想首先去掉这个resize的操作,对每张图都是在原始图片基础上进行识别,所以要找到它到底在哪里resize了图片。

直接搜 grep 'resize' ./lib/ -r

./lib/crnn/utils.py: v.data.resize_(data.size()).copy_(data)

./lib/model/config.py:# Option to set if max-pooling is appended after crop_and_resize.
./lib/model/config.py:# if true, the region will be resized to a square of 2xPOOLING_SIZE,
./lib/model/config.py:# resized to a square of POOLING_SIZE
./lib/model/test.py: im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
./lib/nets/network.py:from scipy.misc import imresize
./lib/nets/network.py: image = imresize(image[0], self._im_info[:2] / self._im_info[2])
./lib/utils/blob.py: im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,

这里在training过程中应当是调用了./lib/utils/blob.py,

该文件包含了两个函数:

1 def im_list_to_blob(ims): 2   """Convert a list of images into a network input. 3   Assumes images are already prepared (means subtracted, BGR order, ...). 4   """ 5   max_shape = np.array([im.shape for im in ims]).max(axis=0) 6   num_images = len(ims) 7   blob = np.zeros((num_images, max_shape[0], max_shape[1], 3), 8                   dtype=np.float32) 9   for i in range(num_images):10     im = ims[i]11     blob[i, 0:im.shape[0], 0:im.shape[1], :] = im12 13   return blob14 15 16 def prep_im_for_blob(im, pixel_means, target_size, max_size):17   """Mean subtract and scale an image for use in a blob."""18   im = im.astype(np.float32, copy=False)19   im -= pixel_means20   im_shape = im.shape21   im_size_min = np.min(im_shape[0:2])22   im_size_max = np.max(im_shape[0:2])23   im_scale = float(target_size) / float(im_size_min)24   # Prevent the biggest axis from being more than MAX_SIZE25   if np.round(im_scale * im_size_max) > max_size:26     im_scale = float(max_size) / float(im_size_max)27   im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,28                   interpolation=cv2.INTER_LINEAR)29 30   return im, im_scale

而这两个函数都是在./lib/roi_data_layer/minibatch.py 下被调用的。

而该文件也定义了两个函数,其中get_minibatch() 调用了另一个子函数_get_image_blob()。

1 def get_minibatch(roidb, num_classes): 2   """Given a roidb, construct a minibatch sampled from it.""" 3   num_images = len(roidb) 4   # Sample random scales to use for each image in this batch 5   random_scale_inds = npr.randint(0, high=len(cfg.TRAIN.SCALES), 6                   size=num_images) 7   assert(cfg.TRAIN.BATCH_SIZE % num_images == 0), \ 8     'num_images ({}) must divide BATCH_SIZE ({})'. \ 9     format(num_images, cfg.TRAIN.BATCH_SIZE)10 11   # Get the input image blob, formatted for caffe12   im_blob, im_scales = _get_image_blob(roidb, random_scale_inds)13 14   blobs = {
'data': im_blob}15 16 assert len(im_scales) == 1, "Single batch only"17 assert len(roidb) == 1, "Single batch only"18 19 # gt boxes: (x1, y1, x2, y2, cls)20 if cfg.TRAIN.USE_ALL_GT:21 # Include all ground truth boxes22 gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]23 else:24 # For the COCO ground truth boxes, exclude the ones that are ''iscrowd'' 25 gt_inds = np.where(roidb[0]['gt_classes'] != 0 & np.all(roidb[0]['gt_overlaps'].toarray() > -1.0, axis=1))[0]26 gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32)27 gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :] * im_scales[0]28 gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]29 blobs['gt_boxes'] = gt_boxes30 blobs['im_info'] = np.array(31 [im_blob.shape[1], im_blob.shape[2], im_scales[0]],32 dtype=np.float32)33 34 return blobs35 36 def _get_image_blob(roidb, scale_inds):37 """Builds an input blob from the images in the roidb at the specified38 scales.39 """40 num_images = len(roidb)41 processed_ims = []42 im_scales = []43 for i in range(num_images):44 im = cv2.imread(roidb[i]['image'])45 if roidb[i]['flipped']:46 im = im[:, ::-1, :]47 target_size = cfg.TRAIN.SCALES[scale_inds[i]]48 im, im_scale = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size,49 cfg.TRAIN.MAX_SIZE)50 im_scales.append(im_scale)51 processed_ims.append(im)52 53 # Create a blob to hold the input images54 blob = im_list_to_blob(processed_ims)55 56 return blob, im_scales

get_minibatch()又是被./lib/roi_data_layer/layer.py中的类RoIDataLayer的一个方法forward()中调用的另一个方法_get_next_minibatch()调用的。

至此,由于RoIDataLayer类在类Network中被调用,终于把这些都接起来了。

faster-RCNN的代码实在是冗杂,来来回回定义了很多完全可以用一个函数实现的很多很多个函数。我佛了!

 

转载于:https://www.cnblogs.com/beatets/p/9287990.html

你可能感兴趣的文章
仿照APP直播点赞
查看>>
[百度校招]打印全排列
查看>>
Java 第四天 Mysql
查看>>
SQL去重复记录,留一条符合条件记录
查看>>
ELMAH (ASP.NET错误日志处理)使用方法
查看>>
目录操作-SYSTEM.IO
查看>>
HDU 1005题解,这种题目关键在于寻找循环节,这很重要1,很容易找到循环环节是49,AC就是理所当然...
查看>>
结对第二次—文献摘要热词统计及进阶需求
查看>>
73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0
查看>>
Design Pattern --- Composite
查看>>
我从没理解js的闭包,直到他人向我这么解释。。。
查看>>
移动开发之css3实现背景几种渐变效果
查看>>
前端切图:自制简易音乐播放器
查看>>
Nescafé2 月之谜 题解
查看>>
用git revert 撤消merge操作
查看>>
Sql server 数据库默认路径
查看>>
5. 监视和ZooKeeper操作
查看>>
hbase读写流程
查看>>
Android自定义控件:动画类---插值器Interpolator
查看>>
Android自定义控件:动画类(八)----ObjectAnimator基本使用
查看>>