我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用fast_rcnn.config.cfg.DEDUP_BOXES。
def extract_hypercolumns(net, im, boxes): 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. # if cfg.DEDUP_BOXES > 0: # v = np.array([1, 1e3, 1e6, 1e9, 1e12]) # hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v) # _, index, inv_index = np.unique(hashes, return_index=True, # return_inverse=True) # blobs['rois'] = blobs['rois'][index, :] # boxes = boxes[index, :] # reshape network inputs net.blobs['data'].reshape(*(blobs['data'].shape)) net.blobs['rois'].reshape(*(blobs['rois'].shape)) blobs_out = net.forward(data=blobs['data'].astype(np.float32, copy=False), rois=blobs['rois'].astype(np.float32, copy=False)) print dir(net.blobs), net.blobs.keys(), net.blobs['conv1'].data.shape hypercolumns = [] # layers = ['conv2', 'conv3', 'conv4', 'conv5'] layers = ['norm1', 'norm2'] layers = ['pool1', 'pool2', 'pool5'] # layers = ['fc6', 'fc7'] for layer in layers: print layer, net.blobs[layer].data.shape convmap = net.blobs[layer].data for fmap in convmap[0]: # print 'fmap', fmap.shape upscaled = sp.misc.imresize(fmap, size=(im.shape[0], im.shape[1]), mode="F", interp='bilinear') hypercolumns.append(upscaled) return np.asarray(hypercolumns) # data = net.blobs['fc7'].data # return data[inv_index, :]
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
def im_detect(net, im, boxes, layer='fc7'): """Detect 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): R x K array of object class scores (K includes background as object category 0) boxes (ndarray): R x (4*K) array of predicted bounding boxes """ 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. if cfg.DEDUP_BOXES > 0: v = np.array([1, 1e3, 1e6, 1e9, 1e12]) hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v) _, index, inv_index = np.unique(hashes, return_index=True, return_inverse=True) blobs['rois'] = blobs['rois'][index, :] boxes = boxes[index, :] # reshape network inputs net.blobs['data'].reshape(*(blobs['data'].shape)) net.blobs['rois'].reshape(*(blobs['rois'].shape)) blobs_out = net.forward(data=blobs['data'].astype(np.float32, copy=False), rois=blobs['rois'].astype(np.float32, copy=False)) data = net.blobs[layer].data return data[inv_index, :] # if cfg.TEST.SVM: # # use the raw scores before softmax under the assumption they # # were trained as linear SVMs # scores = net.blobs['cls_score'].data # else: # # use softmax estimated probabilities # scores = blobs_out['cls_prob'] # if cfg.TEST.BBOX_REG: # # Apply bounding-box regression deltas # box_deltas = blobs_out['bbox_pred'] # pred_boxes = _bbox_pred(boxes, box_deltas) # pred_boxes = _clip_boxes(pred_boxes, im.shape) # else: # # Simply repeat the boxes, once for each class # pred_boxes = np.tile(boxes, (1, scores.shape[1])) # if cfg.DEDUP_BOXES > 0: # # Map scores and predictions back to the original set of boxes # scores = scores[inv_index, :] # pred_boxes = pred_boxes[inv_index, :] # return scores, pred_boxes
def im_detect(net, im, boxes): """Detect 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): R x K array of object class scores (K includes background as object category 0) boxes (ndarray): R x (4*K) array of predicted bounding boxes """ 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)) blobs_out = net.forward(data=blobs['data'][i].astype(np.float32, copy=False), rois=blobs['rois'][i].astype(np.float32, copy=False)) scores_tmp = blobs_out['cls_score_7_1'] if cfg.DEDUP_BOXES > 0: # Map scores and predictions back to the original set of boxes scores_tmp = scores_tmp[inv_index, :] # pred_boxes = pred_boxes[inv_index, :] if i == 0: scores = np.copy(scores_tmp) else: scores += scores_tmp pred_boxes = np.tile(boxes, (1, scores.shape[1])) return scores, pred_boxes
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
def im_detect(net, im, boxes): """Detect 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): R x K array of object class scores (K includes background as object category 0) boxes (ndarray): R x (4*K) array of predicted bounding boxes """ 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. if cfg.DEDUP_BOXES > 0: v = np.array([1, 1e3, 1e6, 1e9, 1e12]) hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v) _, index, inv_index = np.unique(hashes, return_index=True, return_inverse=True) blobs['rois'] = blobs['rois'][index, :] boxes = boxes[index, :] # reshape network inputs net.blobs['data'].reshape(*(blobs['data'].shape)) net.blobs['rois'].reshape(*(blobs['rois'].shape)) blobs_out = net.forward(data=blobs['data'].astype(np.float32, copy=False), rois=blobs['rois'].astype(np.float32, copy=False)) if cfg.TEST.SVM: # use the raw scores before softmax under the assumption they # were trained as linear SVMs scores = net.blobs['cls_score'].data else: # use softmax estimated probabilities scores = blobs_out['cls_prob'] if cfg.TEST.BBOX_REG: # Apply bounding-box regression deltas box_deltas = blobs_out['bbox_pred'] pred_boxes = _bbox_pred(boxes, box_deltas) pred_boxes = _clip_boxes(pred_boxes, im.shape) else: # Simply repeat the boxes, once for each class pred_boxes = np.tile(boxes, (1, scores.shape[1])) if cfg.DEDUP_BOXES > 0: # Map scores and predictions back to the original set of boxes scores = scores[inv_index, :] pred_boxes = pred_boxes[inv_index, :] return scores, pred_boxes
def im_detect(net, im, boxes): """Detect 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): R x K array of object class scores (K includes background as object category 0) boxes (ndarray): R x (4*K) array of predicted bounding boxes """ 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. if cfg.DEDUP_BOXES > 0: v = np.array([1, 1e3, 1e6, 1e9, 1e12]) hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v) _, index, inv_index = np.unique(hashes, return_index=True, return_inverse=True) blobs['rois'] = blobs['rois'][index, :] boxes = boxes[index, :] # reshape network inputs net.blobs['data'].reshape(*(blobs['data'].shape)) net.blobs['rois'].reshape(*(blobs['rois'].shape)) blobs_out = net.forward(data=blobs['data'].astype(np.float32, copy=False), rois=blobs['rois'].astype(np.float32, copy=False)) if cfg.TEST.SVM: # use the raw scores before softmax under the assumption they # were trained as linear SVMs scores = net.blobs['cls_score'].data else: # use softmax estimated probabilities scores = blobs_out['cls_prob'][:, 1, :, 0] if cfg.TEST.BBOX_REG: # Apply bounding-box regression deltas box_deltas = blobs_out['bbox_pred_80'] pred_boxes = _bbox_pred(boxes, box_deltas) pred_boxes = _clip_boxes(pred_boxes, im.shape) else: # Simply repeat the boxes, once for each class pred_boxes = np.tile(boxes, (1, scores.shape[1])) if cfg.DEDUP_BOXES > 0: # Map scores and predictions back to the original set of boxes scores = scores[inv_index, :] pred_boxes = pred_boxes[inv_index, :] return scores, pred_boxes
def im_detect(net, im, boxes): """Detect 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): R x K array of object class scores (K includes background as object category 0) boxes (ndarray): R x (4*K) array of predicted bounding boxes """ 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. #if cfg.DEDUP_BOXES > 0: # v = np.array([1, 1e3, 1e6, 1e9, 1e12]) # hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v) # _, index, inv_index = np.unique(hashes, return_index=True, # return_inverse=True) # blobs['rois'] = blobs['rois'][index, :] # boxes = boxes[index, :] # reshape network inputs net.blobs['data'].reshape(*(blobs['data'].shape)) net.blobs['rois'].reshape(*(blobs['rois'].shape)) blobs_out = net.forward(data=blobs['data'].astype(np.float32, copy=False), rois=blobs['rois'].astype(np.float32, copy=False)) if cfg.TEST.SVM: # use the raw scores before softmax under the assumption they # were trained as linear SVMs scores = net.blobs['cls_score'].data else: # use softmax estimated probabilities scores = blobs_out['cls_prob'] if cfg.TEST.BBOX_REG: # Apply bounding-box regression deltas box_deltas = blobs_out['bbox_pred'] pred_boxes = _bbox_pred(boxes, box_deltas) pred_boxes = _clip_boxes(pred_boxes, im.shape) else: # Simply repeat the boxes, once for each class pred_boxes = np.tile(boxes, (1, scores.shape[1])) #if cfg.DEDUP_BOXES > 0: # # Map scores and predictions back to the original set of boxes # scores = scores[inv_index, :] # pred_boxes = pred_boxes[inv_index, :] return scores, pred_boxes