本文整理汇总了Python中maskrcnn_benchmark.layers.nms方法的典型用法代码示例。如果您正苦于以下问题:Python layers.nms方法的具体用法?Python layers.nms怎么用?Python layers.nms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类maskrcnn_benchmark.layers
的用法示例。
在下文中一共展示了layers.nms方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: boxlist_nms
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def boxlist_nms(boxlist, nms_thresh, max_proposals=-1, score_field="scores"):
"""
Performs non-maximum suppression on a boxlist, with scores specified
in a boxlist field via score_field.
Arguments:
boxlist(BoxList)
nms_thresh (float)
max_proposals (int): if > 0, then only the top max_proposals are kept
after non-maximum suppression
score_field (str)
"""
if nms_thresh <= 0:
return boxlist
mode = boxlist.mode
boxlist = boxlist.convert("xyxy")
boxes = boxlist.bbox
score = boxlist.get_field(score_field)
keep = _box_nms(boxes, score, nms_thresh)
if max_proposals > 0:
keep = keep[: max_proposals]
boxlist = boxlist[keep]
return boxlist.convert(mode)
示例2: boxlist_nms
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def boxlist_nms(boxlist, nms_thresh, max_proposals=-1, score_field="score"):
"""
Performs non-maximum suppression on a boxlist, with scores specified
in a boxlist field via score_field.
Arguments:
boxlist(BoxList)
nms_thresh (float)
max_proposals (int): if > 0, then only the top max_proposals are kept
after non-maxium suppression
score_field (str)
"""
if nms_thresh <= 0:
return boxlist
mode = boxlist.mode
boxlist = boxlist.convert("xyxy")
boxes = boxlist.bbox
score = boxlist.get_field(score_field)
keep = _box_nms(boxes, score, nms_thresh)
if max_proposals > 0:
keep = keep[: max_proposals]
boxlist = boxlist[keep]
return boxlist.convert(mode)
示例3: boxlist_nms
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def boxlist_nms(boxlist, nms_thresh, max_proposals=-1, score_field="score"):
"""
Performs non-maximum suppression on a boxlist, with scores specified
in a boxlist field via score_field.
Arguments:
boxlist(BoxList)
nms_thresh (float)
max_proposals (int): if > 0, then only the top max_proposals are kept
after non-maxium suppression
score_field (str)
"""
if nms_thresh <= 0:
return boxlist
mode = boxlist.mode
boxlist = boxlist.convert("xyxy")
boxes = boxlist.bbox
score = boxlist.get_field(score_field).type(torch.cuda.FloatTensor)
keep = _box_nms(boxes, score, nms_thresh)
if max_proposals > 0:
keep = keep[: max_proposals]
boxlist = boxlist[keep]
return boxlist.convert(mode)
示例4: nms
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def nms(self, merge_result, scores):
from maskrcnn_benchmark.layers import nms as _box_nms
import torch
if scores is None:
scores = torch.ones(size=(len(merge_result),))
if not isinstance(scores, torch.Tensor):
scores = torch.Tensor(scores)
merge_result = torch.Tensor(merge_result)
keep = _box_nms(merge_result, scores, self.nms_th)
merge_result = merge_result[keep].detach().cpu().numpy()
return merge_result, keep.detach().cpu().numpy()
示例5: test_nms_cpu
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def test_nms_cpu(self):
""" Match unit test UtilsNMSTest.TestNMS in
caffe2/operators/generate_proposals_op_util_nms_test.cc
"""
inputs = (
np.array(
[
10,
10,
50,
60,
0.5,
11,
12,
48,
60,
0.7,
8,
9,
40,
50,
0.6,
100,
100,
150,
140,
0.9,
99,
110,
155,
139,
0.8,
]
)
.astype(np.float32)
.reshape(-1, 5)
)
boxes = torch.from_numpy(inputs[:, :4])
scores = torch.from_numpy(inputs[:, 4])
test_thresh = [0.1, 0.3, 0.5, 0.8, 0.9]
gt_indices = [[1, 3], [1, 3], [1, 3], [1, 2, 3, 4], [0, 1, 2, 3, 4]]
for thresh, gt_index in zip(test_thresh, gt_indices):
keep_indices = box_nms(boxes, scores, thresh)
keep_indices = np.sort(keep_indices)
np.testing.assert_array_equal(keep_indices, np.array(gt_index))
示例6: boxlist_nms
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def boxlist_nms(boxlist, nms_thresh, max_proposals=-1, score_field="score", GPU_ID=0):
"""
Performs non-maximum suppression on a boxlist, with scores specified
in a boxlist field via score_field.
Arguments:
boxlist(BoxList)
nms_thresh (float)
max_proposals (int): if > 0, then only the top max_proposals are kept
after non-maxium suppression
score_field (str)
"""
if nms_thresh <= 0:
return boxlist
# boxlist = boxlist.convert("xyxy")
boxes = boxlist.bbox
score = boxlist.get_field(score_field)
##################################################
# convert to numpy before calculate
boxes_np = boxes.data.cpu().numpy()
score_np = score.data.cpu().numpy()
# keep = _box_nms(boxes, score, nms_thresh)
ch_proposals = boxes_np.copy()
ch_proposals[:, 2:4] = ch_proposals[:, 3:1:-1]
# x,y,h,w,a
# print('ch_proposals:',ch_proposals.shape)
# print('score_np:', score_np.shape)
if ch_proposals.shape[0] < 1:
return boxlist
keep = rotate_gpu_nms(np.array(np.hstack((ch_proposals, score_np[..., np.newaxis])), np.float32), nms_thresh, GPU_ID) # D
# print time.time() - tic
if max_proposals > 0:
keep = keep[:max_proposals]
keep_th = torch.tensor(keep, dtype=torch.long).to(boxlist.bbox.device)
# print('keep_th:', keep_th.type())
##################################################
# proposals = proposals[keep, :]
# scores = scores[keep]
# if max_proposals > 0:
# keep = keep[:max_proposals]
boxlist = boxlist[keep_th]
# print('boxlist:', boxlist.bbox.type())
return boxlist #.convert(mode)
示例7: box_results_with_nms_and_limit
# 需要导入模块: from maskrcnn_benchmark import layers [as 别名]
# 或者: from maskrcnn_benchmark.layers import nms [as 别名]
def box_results_with_nms_and_limit(
scores, boxes, score_thresh=0.05, nms=0.5, detections_per_img=100
):
"""Returns bounding-box detection results by thresholding on scores and
applying non-maximum suppression (NMS).
`boxes` has shape (#detections, 4 * #classes), where each row represents
a list of predicted bounding boxes for each of the object classes in the
dataset (including the background class). The detections in each row
originate from the same object proposal.
`scores` has shape (#detection, #classes), where each row represents a list
of object detection confidence scores for each of the object classes in the
dataset (including the background class). `scores[i, j]`` corresponds to the
box at `boxes[i, j * 4:(j + 1) * 4]`.
"""
num_classes = scores.shape[1]
cls_boxes = []
cls_scores = []
labels = []
device = scores.device
# Apply threshold on detection probabilities and apply NMS
# Skip j = 0, because it's the background class
for j in range(1, num_classes):
inds = scores[:, j] > score_thresh
scores_j = scores[inds, j]
boxes_j = boxes[inds, j * 4 : (j + 1) * 4]
keep = box_nms(boxes_j, scores_j, nms)
cls_boxes.append(boxes_j[keep])
cls_scores.append(scores_j[keep])
# TODO see why we need the device argument
labels.append(torch.full_like(keep, j, device=device))
cls_scores = torch.cat(cls_scores, dim=0)
cls_boxes = torch.cat(cls_boxes, dim=0)
labels = torch.cat(labels, dim=0)
number_of_detections = len(cls_scores)
# Limit to max_per_image detections **over all classes**
if number_of_detections > detections_per_img > 0:
image_thresh, _ = torch.kthvalue(
cls_scores.cpu(), number_of_detections - detections_per_img + 1
)
keep = cls_scores >= image_thresh.item()
keep = torch.nonzero(keep)
keep = keep.squeeze(1) if keep.numel() else keep
cls_boxes = cls_boxes[keep]
cls_scores = cls_scores[keep]
labels = labels[keep]
return cls_scores, cls_boxes, labels