def im_cls(net, im, boxes):
"""Classify object classes in an image given object proposals.
Arguments:
net (caffe.Net): Fast R-CNN network to use
im (ndarray): color image to test (in BGR order)
boxes (ndarray): R x 4 array of object proposals
Returns:
scores (ndarray): 1 x K array of object class scores
"""
blobs, unused_im_scale_factors = _get_blobs(im, boxes)
# When mapping from image ROIs to feature map ROIs, there's some aliasing
# (some distinct image ROIs get mapped to the same feature ROI).
# Here, we identify duplicate feature ROIs, so we only compute features
# on the unique subset.
for i in xrange(len(blobs['data'])):
if cfg.DEDUP_BOXES > 0:
v = np.array([1, 1e3, 1e6, 1e9, 1e12])
hashes = np.round(blobs['rois'][i] * cfg.DEDUP_BOXES).dot(v)
_, index, inv_index = np.unique(hashes, return_index=True,
return_inverse=True)
blobs['rois'][i] = blobs['rois'][i][index, :]
# reshape network inputs
net.blobs['data'].reshape(*(blobs['data'][i].shape))
net.blobs['rois'].reshape(*(blobs['rois'][i].shape))
net.blobs['shapes'].reshape(*(blobs['shapes'][i].shape))
blobs_out = net.forward(data=blobs['data'][i].astype(np.float32, copy=False),
rois=blobs['rois'][i].astype(np.float32, copy=False),
shapes=blobs['shapes'][i].astype(np.float32, copy=False))
if i == 0:
scores = blobs_out['cls_score_7'] + blobs_out['SPMMax8_1']
# scores = blobs_out['cls_score_7']
else:
scores = np.vstack((scores, blobs_out['cls_score_7'] + blobs_out['SPMMax8_1']))
# scores = np.vstack((scores, blobs_out['cls_score_7']))
return scores
评论列表
文章目录