def get_minibatch(roidb, num_classes):
"""Given a roidb, construct a minibatch sampled from it."""
num_images = len(roidb)
assert num_images == 1, 'batch size should equal to 1!'
# Sample random scales to use for each image in this batch
random_scale_inds = npr.randint(0, high=len(cfg.TRAIN.SCALES),
size=num_images)
# Get the input image blob, formatted for caffe
im_blob, im_scales, im_shapes = _get_image_blob(roidb, random_scale_inds)
# Now, build the region of interest and label blobs
rois_blob = np.zeros((0, 5), dtype=np.float32)
labels_blob = np.zeros((0, 20), dtype=np.float32)
for im_i in xrange(num_images):
labels, im_rois = _sample_rois(roidb[im_i], num_classes)
# Add to RoIs blob
rois = _project_im_rois(im_rois, im_scales[im_i])
batch_ind = im_i * np.ones((rois.shape[0], 1))
rois_blob_this_image = np.hstack((batch_ind, rois))
if cfg.DEDUP_BOXES > 0:
v = np.array([1, 1e3, 1e6, 1e9, 1e12])
hashes = np.round(rois_blob_this_image * cfg.DEDUP_BOXES).dot(v)
_, index, inv_index = np.unique(hashes, return_index=True,
return_inverse=True)
rois_blob_this_image = rois_blob_this_image[index, :]
rois_blob = np.vstack((rois_blob, rois_blob_this_image))
# Add to labels blobs
labels_blob = np.vstack((labels_blob, labels))
blobs = {'data': im_blob,
'rois': rois_blob,
'labels': labels_blob}
return blobs
评论列表
文章目录