Tensorflow版Faster RCNN源码解析(TFFRCNN) (9) roi_data_layer/layer.py

本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记

---------------个人学习笔记---------------

----------------本文作者吴疆--------------

------点击此处链接至博客园原文------

该脚本定义了一个RoIDataLayer类,类内定义了如下函数:

class RoIDataLayer(object):
    """Fast R-CNN data layer used for training."""

1.__init__(self,roidb,num_classes)类的构造函数

实例化RoIDataLayer类对象时自动执行,并调用_shuffle_roidb_inds(...)函数生成self._permself._cur

    def __init__(self, roidb, num_classes):
        """Set the roidb to be used by this layer during training."""
        self._roidb = roidb
        self._num_classes = num_classes
        # 得到self._perm(为0---len(self._roidb)打乱顺序构成的数组)和置self._cur = 0
        self._shuffle_roidb_inds()

2._shuffle_roidb_inds(self)对所有图像构成的roidb打乱索引顺序得到self._perm、self._cur置0,表示获取(minibatch.py中roidb的索引的起始索引位置),被_get_next_minibatch_inds(...)调用

    def _shuffle_roidb_inds(self):
        """Randomly permute改序 the training roidb."""
        # np.random.permutation()函数打乱数组顺序
        # roidb随机顺序
        self._perm = np.random.permutation(np.arange(len(self._roidb)))
        # minibatch_rois索引的开始标志
        self._cur = 0

3._get_next_minibatch_inds(self)获取下一个mini_batch中roidb在所有图像构成的roidb中的索引并返回,被_get_next_minibatch(...)调用

默认使用RPN时每个minibatch使用2张图像?但从minibatch.py中看仅允许single batch单张图像?

    # 获取下一个mini_batch中roidb的索引
    def _get_next_minibatch_inds(self):
        """Return the roidb indices for the next minibatch."""
        # 默认TRAIN.HAS_RPN = True
        if cfg.TRAIN.HAS_RPN:
            # 默认使用RPN时每个minibatch使用2张图像???但从minibatch.py中看仅允许single batch单张图像???
            # 默认TRAIN.IMS_PER_BATCH = 2(Images to use per minibatch )
            # 将self._cur置0,self._perm打乱顺序,进行下一轮(所有图像的roidb迭代算为一轮)
            if self._cur + cfg.TRAIN.IMS_PER_BATCH >= len(self._roidb):
                self._shuffle_roidb_inds()
            # 默认每次取2张图像的roidb(的索引),对应构成的roidb即为minibatch.py中的roidb
            db_inds = self._perm[self._cur:self._cur + cfg.TRAIN.IMS_PER_BATCH]
            # 更新self._cur
            self._cur += cfg.TRAIN.IMS_PER_BATCH
# 若不使用RPN
else: # sample images db_inds = np.zeros((cfg.TRAIN.IMS_PER_BATCH), dtype=np.int32) i = 0 while (i < cfg.TRAIN.IMS_PER_BATCH): ind = self._perm[self._cur] num_objs = self._roidb[ind]['boxes'].shape[0] if num_objs != 0: db_inds[i] = ind i += 1 self._cur += 1 if self._cur >= len(self._roidb): # 将self._cur置0,self._perm打乱顺序,进行下一轮(所有图像roidb迭代算为一轮)训练 self._shuffle_roidb_inds() return db_inds

4._get_next_minibatch(self)

获取下一个mini_batch中roidb(即为minibatch.py中roidb,部分图像的rois相关信息),调用_get_next_minibatch_inds()获取下一个mini_batch中roidb的索引--->获取minibatch_db(即minibatch.py中的roidb)--->以minibatch_db作为参数调用get_minibatch(minibatch_db, self._num_classes)构造网络输入blobs(默认训练阶段使用RPN时,blob含'data'、'gt_boxes'、'gt_ishard'、'dontcare_area'、'im_info'、'im_name'字段),被forward(...)调用

    def _get_next_minibatch(self):
        """
        Return the blobs to be used for the next minibatch.
        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        db_inds = self._get_next_minibatch_inds()
        # minibatch_db即为minibatch.py中的roidb,为一列表,每个元素包含了一张图像rois相关信息
        minibatch_db = [self._roidb[i] for i in db_inds]
        # (调用minibatch.py中get_minibatch(...))构造网络的输入blobs
        return get_minibatch(minibatch_db, self._num_classes)

5.forward(self)

调用_get_next_minibatch()得到blobs并返回,未见调用

    def forward(self):
        """Get blobs and copy them into this layer's top blob vector."""
        # 获取网络输入的blobs
        blobs = self._get_next_minibatch()
        return blobs

猜你喜欢

转载自www.cnblogs.com/deeplearning1314/p/11314595.html