2018.12.5 test.py IOU 计算中间结果
print (rois)boxes
(1000, 4)
[[293.70677124 216.82310234 347.8397644 316.01534912]
[ 0. 137.32425522 192.77882927 319.02986984]
[547.2263681 300.37179657 587.50409088 399.08365906]
...
[361.68635696 223.90092567 388.90794937 250.33667107]
[140.34192856 275.79333344 184.92011536 291.87337738]
[196.69316254 213.97827049 295.75480316 229.21926292]]
print (return_dict[cls_score]) scores # 每个boxes确实是对应 81个类的分数
(1000, 81)
[[3.17424675e-03 8.14470968e-06 9.85136467e-07 ... 6.62868516e-08
2.57449972e-08 2.66096958e-08]
[9.85664129e-01 1.16057836e-05 1.03241391e-07 ... 4.64424978e-08
1.83257427e-07 1.35836364e-08]
[1.14539459e-01 1.86142628e-03 6.19632119e-05 ... 1.34900503e-03
8.71951284e-04 4.08387650e-03]
...
[9.05727684e-01 1.38080022e-05 4.03271372e-07 ... 9.74157430e-08
5.36474545e-08 1.18130082e-07]
[9.74292517e-01 1.10941858e-06 8.96971471e-07 ... 1.12314765e-07
9.16667977e-06 2.06134996e-06]
[9.98089612e-01 7.64774697e-08 1.08563796e-07 ... 4.65147458e-08
9.01894648e-09 7.56015783e-09]]
print (return_dict['bbox_pred)] box_deltas
(1000, 324)
[[-3.1669915e-03 1.3637245e-03 2.7642134e-03 ... 4.3703020e-03
-3.7418461e-01 -9.1514535e-02]
[-8.8923275e-03 -4.2027654e-04 -1.1328951e-02 ... -4.1025400e-01
-9.6839148e-01 -2.6914189e+00]
[-1.2756255e-02 2.5123931e-03 8.7424438e-04 ... -2.3023543e-01
-2.4734581e-01 2.7421701e-01]
...
[-9.3965298e-03 -2.2455705e-03 1.2613271e-03 ... -2.2549543e-01
-3.7283763e-01 -8.1783605e-01]
[-8.5360259e-03 -7.8438623e-03 -1.8574111e-02 ... -3.8885482e-02
6.1320138e-01 -1.1634141e+00]
[-2.4110742e-04 -2.5058060e-03 9.6967124e-04 ... -7.5031869e-02
-3.1831458e-01 2.7045879e-01]]
print (box_utils.bbox_transform) pred_boxes
(1000, 324)
[[2.9367404e+02 2.1672113e+02 3.4783755e+02 ... 2.1777545e+02
3.4612848e+02 3.1515057e+02]
[4.6966553e-02 1.3708270e+02 1.9238722e+02 ... 1.6785435e+02
1.6325914e+02 2.7350864e+02]
[5.4717010e+02 3.0031519e+02 5.8745502e+02 ... 2.9526544e+02
5.8610687e+02 3.9959857e+02]
...
[3.6165631e+02 2.2387383e+02 3.8888501e+02 ... 2.2535216e+02
3.8753378e+02 2.4764812e+02]
[1.4038753e+02 2.7575278e+02 1.8479671e+02 ... 2.7749982e+02
1.9012708e+02 2.9003412e+02]
[1.9668103e+02 2.1394470e+02 2.9576208e+02 ... 2.1340506e+02
2.9124518e+02 2.2954877e+02]]
print (box_utils.clip_tiled_boxes) pred_boxes
(1000, 324)
[[2.9367404e+02 2.1672113e+02 3.4783755e+02 ... 2.1777545e+02
3.4612848e+02 3.1515057e+02]
[4.6966553e-02 1.3708270e+02 1.9238722e+02 ... 1.6785435e+02
1.6325914e+02 2.7350864e+02]
[5.4717010e+02 3.0031519e+02 5.8745502e+02 ... 2.9526544e+02
5.8610687e+02 3.9959857e+02]
...
[3.6165631e+02 2.2387383e+02 3.8888501e+02 ... 2.2535216e+02
3.8753378e+02 2.4764812e+02]
[1.4038753e+02 2.7575278e+02 1.8479671e+02 ... 2.7749982e+02
1.9012708e+02 2.9003412e+02]
[1.9668103e+02 2.1394470e+02 2.9576208e+02 ... 2.1340506e+02
2.9124518e+02 2.2954877e+02]]
print np.tile pred_boxes
(1000, 324)
[[2.9367404e+02 2.1672113e+02 3.4783755e+02 ... 2.1777545e+02
3.4612848e+02 3.1515057e+02]
[4.6966553e-02 1.3708270e+02 1.9238722e+02 ... 1.6785435e+02
1.6325914e+02 2.7350864e+02]
[5.4717010e+02 3.0031519e+02 5.8745502e+02 ... 2.9526544e+02
5.8610687e+02 3.9959857e+02]
...
[3.6165631e+02 2.2387383e+02 3.8888501e+02 ... 2.2535216e+02
3.8753378e+02 2.4764812e+02]
[1.4038753e+02 2.7575278e+02 1.8479671e+02 ... 2.7749982e+02
1.9012708e+02 2.9003412e+02]
[1.9668103e+02 2.1394470e+02 2.9576208e+02 ... 2.1340506e+02
2.9124518e+02 2.2954877e+02]]
对应的代码
def im_detect_bbox(model, im, target_scale, target_max_size, boxes=None):
"""Prepare the bbox for testing"""
inputs, im_scale = _get_blobs(im, boxes, target_scale, target_max_size) # 获取网络的输入
if cfg.DEDUP_BOXES > 0 and not cfg.MODEL.FASTER_RCNN: # 用的就是FASTER_RCNN 所以这个就不用看了
v = np.array([1, 1e3, 1e6, 1e9, 1e12])
hashes = np.round(inputs['rois'] * cfg.DEDUP_BOXES).dot(v)
_, index, inv_index = np.unique(
hashes, return_index=True, return_inverse=True
)
inputs['rois'] = inputs['rois'][index, :]
boxes = boxes[index, :]
# Add multi-level rois for FPN
if cfg.FPN.MULTILEVEL_ROIS and not cfg.MODEL.FASTER_RCNN: # 用的就是FASTER_RCNN 所以这个就不用看了
_add_multilevel_rois_for_test(inputs, 'rois')
if cfg.PYTORCH_VERSION_LESS_THAN_040: # 不用看
inputs['data'] = [Variable(torch.from_numpy(inputs['data']), volatile=True)]
inputs['im_info'] = [Variable(torch.from_numpy(inputs['im_info']), volatile=True)]
else:
inputs['data'] = [torch.from_numpy(inputs['data'])]
inputs['im_info'] = [torch.from_numpy(inputs['im_info'])]
return_dict = model(**inputs) # 对应的代码 在 model_builder.py 335行 这个值得一看
if cfg.MODEL.FASTER_RCNN: # 这个要看
rois = return_dict['rois'].data.cpu().numpy()
# unscale back to raw image space 这个地方自己想看看内部的数据
boxes = rois[:, 1:5] / im_scale
print('print (rois)boxes')
print(boxes.shape)
print(boxes)
# cls prob (activations after softmax) 是最后的分类概率
scores = return_dict['cls_score'].data.cpu().numpy().squeeze() # 这个地方自己看看内部的数据打印输出
# In case there is 1 proposal
scores = scores.reshape([-1, scores.shape[-1]])
print('print (return_dict[cls_score]) scores')
print(scores.shape)
print(scores)
if cfg.TEST.BBOX_REG: # 为TRUE 所以要使用这个函数
# Apply bounding-box regression deltas
box_deltas = return_dict['bbox_pred'].data.cpu().numpy().squeeze() # 回归的数值
# In case there is 1 proposal
box_deltas = box_deltas.reshape([-1, box_deltas.shape[-1]]) # 这个值需要打印出来,看看
print("print (return_dict['bbox_pred)] box_deltas ")
print(box_deltas.shape)
print(box_deltas)
if cfg.MODEL.CLS_AGNOSTIC_BBOX_REG: # 为False
# Remove predictions for bg class (compat with MSRA code)
box_deltas = box_deltas[:, -4:]
if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # 为False 所以不用看
# (legacy) Optionally normalize targets by a precomputed mean and stdev
box_deltas = box_deltas.view(-1, 4) * cfg.TRAIN.BBOX_NORMALIZE_STDS \
+ cfg.TRAIN.BBOX_NORMALIZE_MEANS
pred_boxes = box_utils.bbox_transform(boxes, box_deltas, cfg.MODEL.BBOX_REG_WEIGHTS) # proposal变换后的结果,要看这个函数的变换格式
print('print (box_utils.bbox_transform) pred_boxes') # 自己加的
print(pred_boxes.shape)
print(pred_boxes) # 自己加的
if isinstance(im, list):
pred_boxes = box_utils.clip_tiled_boxes(pred_boxes, im[0].shape) # 裁剪预测的box 超出图片范围的部分,box的数量没有变化
else:
pred_boxes = box_utils.clip_tiled_boxes(pred_boxes, im.shape)
print('print (box_utils.clip_tiled_boxes) pred_boxes')
print(pred_boxes.shape)
print(pred_boxes) # 自己加的
if cfg.MODEL.CLS_AGNOSTIC_BBOX_REG: # 这个为Fasle 所以不用看
pred_boxes = np.tile(pred_boxes, (1, scores.shape[1])) # 这个要看
else:
# Simply repeat the boxes, once for each class
pred_boxes = np.tile(boxes, (1, scores.shape[1])) # 纵轴一共扩展到scores.shape[1]个,简单来说就是一直复制boxes
print('print np.tile pred_boxes ')
print(pred_boxes.shape)
print(pred_boxes) # 自己加的
if cfg.DEDUP_BOXES > 0 and not cfg.MODEL.FASTER_RCNN: # 这个不用看
# 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, im_scale, return_dict['blob_conv'] # pred_boxes 对应的就是boxes
boxes 与 Pred_boxes IOU计算
type(pred_boxes_iou_score)
<class 'numpy.ndarray'>
pred_boxes_iou_score.shape
(1000, 81)
pred_boxes_iou_score
[[0.9970651970990316 0.848458347062145 0.861951578724146 ...
0.8898822389558977 0.8946089222660959 0.9110657035490152]
[0.9951946397040794 0.6080425233311723 0.5269227997218349 ...
0.4539930495952731 0.6538900468260159 0.4809644430761452]
[0.9958224199218099 0.868749621321658 0.7925328608001213 ...
0.9483820683056805 0.9507868205540195 0.9032696011034462]
...
[0.9966042680376969 0.7724336091161139 0.917417258818896 ...
0.8333894756618091 0.862331233404576 0.7880968585824003]
[0.9931430747049722 0.8872629432047746 0.7669524721802448 ...
0.7998833907252942 0.8685132302925326 0.7181537747631469]
[0.9961873686701632 0.7347449762029296 0.861433635772388 ...
0.7590359222963079 0.9172133263191022 0.8918093743355766]]
print pred_boxes_iou_score[0]
[0.9970651970990316 0.848458347062145 0.861951578724146 0.7520613730937346
0.8296473740376588 0.7937327944849818 0.8435338553946191
0.6883975608260493 0.7412870715152228 0.792885120167304
0.9043385781556984 0.9149616350517709 0.8252571324928878
0.8956014020509359 0.849750963302514 0.8988282746806645
0.9005781597031239 0.8705353555327586 0.8991023262903651
0.8698407004355558 0.843811936161195 0.8878674501003369
0.9161613004866827 0.8990154363903917 0.9178012204391238
0.8459316870385046 0.9127154436658395 0.8362840134129913
0.8906107157634539 0.8864732282210392 0.832105517552161
0.8987337371057473 0.9265219667641058 0.8455185022092563
0.8901853338109534 0.8952545838570223 0.8237328287447613
0.8797171850636196 0.8644961975839518 0.9101555549372643
0.8588391943926155 0.8904682830932127 0.9190885215811717
0.8529128312355944 0.8369464161134802 0.8353255185517947
0.7846300551106614 0.8767701594187651 0.8615659777280352
0.8766657733222974 0.8508958832196214 0.8665994751037415
0.9173283474657742 0.8528957018750907 0.8415735447749917
0.8318379961015527 0.873514906360858 0.9485679397042603
0.8274268808425849 0.8814425090427731 0.8163655830364919
0.7642763321827548 0.9149081543620138 0.8686115922156735
0.8100667513104941 0.8333772646375969 0.8045294100252518
0.8132204241277718 0.8610227113575452 0.8632193509023083
0.834500263737587 0.8690941533248104 0.8208911994959939 0.897242971804189
0.858380925412819 0.8822129759570208 0.9279129856388239 0.891042421803356
0.8898822389558977 0.8946089222660959 0.9110657035490152]
(81,)