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
(net, im, boxes)
| 152 | return boxes |
| 153 | |
| 154 | def im_detect(net, im, boxes): |
| 155 | """Detect object classes in an image given object proposals. |
| 156 | |
| 157 | Arguments: |
| 158 | net (caffe.Net): Fast R-CNN network to use |
| 159 | im (ndarray): color image to test (in BGR order) |
| 160 | boxes (ndarray): R x 4 array of object proposals |
| 161 | |
| 162 | Returns: |
| 163 | scores (ndarray): R x K array of object class scores (K includes |
| 164 | background as object category 0) |
| 165 | boxes (ndarray): R x (4*K) array of predicted bounding boxes |
| 166 | """ |
| 167 | blobs, unused_im_scale_factors = _get_blobs(im, boxes) |
| 168 | |
| 169 | # When mapping from image ROIs to feature map ROIs, there's some aliasing |
| 170 | # (some distinct image ROIs get mapped to the same feature ROI). |
| 171 | # Here, we identify duplicate feature ROIs, so we only compute features |
| 172 | # on the unique subset. |
| 173 | if cfg.DEDUP_BOXES > 0: |
| 174 | v = np.array([1, 1e3, 1e6, 1e9, 1e12]) |
| 175 | hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v) |
| 176 | _, index, inv_index = np.unique(hashes, return_index=True, |
| 177 | return_inverse=True) |
| 178 | blobs['rois'] = blobs['rois'][index, :] |
| 179 | boxes = boxes[index, :] |
| 180 | |
| 181 | # reshape network inputs |
| 182 | net.blobs['data'].reshape(*(blobs['data'].shape)) |
| 183 | net.blobs['rois'].reshape(*(blobs['rois'].shape)) |
| 184 | blobs_out = net.forward(data=blobs['data'].astype(np.float32, copy=False), |
| 185 | rois=blobs['rois'].astype(np.float32, copy=False)) |
| 186 | if cfg.TEST.SVM: |
| 187 | # use the raw scores before softmax under the assumption they |
| 188 | # were trained as linear SVMs |
| 189 | scores = net.blobs['cls_score'].data |
| 190 | else: |
| 191 | # use softmax estimated probabilities |
| 192 | scores = blobs_out['cls_prob'] |
| 193 | |
| 194 | if cfg.TEST.BBOX_REG: |
| 195 | # Apply bounding-box regression deltas |
| 196 | box_deltas = blobs_out['bbox_pred'] |
| 197 | pred_boxes = _bbox_pred(boxes, box_deltas) |
| 198 | pred_boxes = _clip_boxes(pred_boxes, im.shape) |
| 199 | else: |
| 200 | # Simply repeat the boxes, once for each class |
| 201 | pred_boxes = np.tile(boxes, (1, scores.shape[1])) |
| 202 | |
| 203 | if cfg.DEDUP_BOXES > 0: |
| 204 | # Map scores and predictions back to the original set of boxes |
| 205 | scores = scores[inv_index, :] |
| 206 | pred_boxes = pred_boxes[inv_index, :] |
| 207 | |
| 208 | return scores, pred_boxes |
| 209 | |
| 210 | def vis_detections(im, class_name, dets, thresh=0.3): |
| 211 | """Visual debugging of detections.""" |
no test coverage detected