参考tf-faster-rcnn工程的layer.py函数,整体逻辑为:
- shuffle图片(roidb)顺序;
- 初始化batch的起始位置,读取配置文件中的batch(faster rcnn检测的batch为1);
- 根据起始位置和batch获得当前训练的图片;
- 迭代值起始位置等于图片总数时,重新shuffle图片顺序;
- 迭代值达到max_iters时停止读取数据;
def __init__(self, roidb, num_classes, random=False):
"""Set the roidb to be used by this layer during training."""
self._roidb = roidb
self._num_classes = num_classes
# Also set a random flag
self._random = random
self._shuffle_roidb_inds()
def _shuffle_roidb_inds(self):
"""Randomly permute the training roidb."""
# If the random flag is set,
# then the database is shuffled according to system time
# Useful for the validation set
if self._random: #验证集时设置随机种子,保证shuffle的顺序一致
st0 = np.random.get_state()
millis = int(round(time.time() * 1000)) % 4294967295
np.random.seed(millis)
if cfg.TRAIN.ASPECT_GROUPING: #默认为False
widths = np.array([r['width'] for r in self._roidb])
heig