本文整理汇总了Python中torchvision.ops.nms方法的典型用法代码示例。如果您正苦于以下问题:Python ops.nms方法的具体用法?Python ops.nms怎么用?Python ops.nms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.ops
的用法示例。
在下文中一共展示了ops.nms方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_boxes
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def filter_boxes(self, boxes: torch.Tensor) -> typing.List[np.ndarray]:
"""Performs NMS and score thresholding
Args:
boxes (torch.Tensor): shape [N, B, 5] with (xmin, ymin, xmax, ymax, score)
Returns:
list: N np.ndarray of shape [B, 5]
"""
final_output = []
for i in range(len(boxes)):
scores = boxes[i, :, 4]
keep_idx = scores >= self.confidence_threshold
boxes_ = boxes[i, keep_idx, :-1]
scores = scores[keep_idx]
if scores.dim() == 0:
final_output.append(torch.empty(0, 5))
continue
keep_idx = nms(boxes_, scores, self.nms_iou_threshold)
scores = scores[keep_idx].view(-1, 1)
boxes_ = boxes_[keep_idx].view(-1, 4)
output = torch.cat((boxes_, scores), dim=-1)
final_output.append(output)
return final_output
示例2: match_anchors
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def match_anchors(truths, priors, threshold=[0.4, 0.7]):
"""
by aalenzhang
Match each prior box with the ground truth box of the highest jaccard overlap,
don't encode the bounding box, then return them
"""
overlaps = jaccard(truths, priors)
# [1,num_priors] best ground truth for each prior
best_truth_overlap, best_truth_idx = overlaps.max(0, keepdim=True)
conf = torch.ones_like(best_truth_overlap).long()
conf[best_truth_overlap < threshold[1]] = -1 # ignore
conf[best_truth_overlap < threshold[0]] = 0 # background
remove_mask = torch.ones_like(conf)
keep_idx = _nms(priors, best_truth_overlap, iou_threshold=0.7)[:512]
remove_mask.index_fill_(0, keep_idx, 0)
# after NMS, ignore them
conf[remove_mask] = -1
return conf
示例3: forward
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def forward(self, inputs):
if self.is_training:
inputs, annotations = inputs
else:
inputs = inputs
x = self.extract_feat(inputs)
outs = self.bbox_head(x)
classification = torch.cat([out for out in outs[0]], dim=1)
regression = torch.cat([out for out in outs[1]], dim=1)
anchors = self.anchors(inputs)
if self.is_training:
return self.criterion(classification, regression, anchors, annotations)
else:
transformed_anchors = self.regressBoxes(anchors, regression)
transformed_anchors = self.clipBoxes(transformed_anchors, inputs)
scores = torch.max(classification, dim=2, keepdim=True)[0]
scores_over_thresh = (scores > self.threshold)[0, :, 0]
if scores_over_thresh.sum() == 0:
print('No boxes to NMS')
# no boxes to NMS, just return
return [torch.zeros(0), torch.zeros(0), torch.zeros(0, 4)]
classification = classification[:, scores_over_thresh, :]
transformed_anchors = transformed_anchors[:, scores_over_thresh, :]
scores = scores[:, scores_over_thresh, :]
anchors_nms_idx = nms(
transformed_anchors[0, :, :], scores[0, :, 0], iou_threshold=self.iou_threshold)
nms_scores, nms_class = classification[0, anchors_nms_idx, :].max(
dim=1)
return [nms_scores, nms_class, transformed_anchors[0, anchors_nms_idx, :]]
示例4: __init__
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def __init__(self, num_classes, size, bkg_label, top_k, conf_thresh, nms_thresh,
objectness_thre, keep_top_k):
self.num_classes = num_classes
self.background_label = bkg_label
self.top_k = top_k
self.keep_top_k = keep_top_k
# Parameters used in nms.
self.nms_thresh = nms_thresh
if nms_thresh <= 0:
raise ValueError('nms_threshold must be non negative.')
self.conf_thresh = conf_thresh
self.objectness_thre = objectness_thre
self.variance = cfg['variance']
示例5: forward
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def forward(self, inputs):
if self.training:
img_batch, annotations = inputs
else:
img_batch = inputs
x = self.efficientnet[0](img_batch)
x = self.efficientnet[1](x)
# Forward batch trough backbone
features = []
for block in self.efficientnet[2:]:
x = block(x)
if block._depthwise_conv.stride == [2, 2]:
features.append(x)
features = self.fpn(features[1:])
regression = torch.cat([self.regressionModel(feature) for feature in features], dim=1)
classification = torch.cat([self.classificationModel(feature) for feature in features], dim=1)
anchors = self.anchors(img_batch)
if self.training:
return self.focalLoss(classification, regression, anchors, annotations)
else:
transformed_anchors = self.regressBoxes(anchors, regression)
transformed_anchors = self.clipBoxes(transformed_anchors, img_batch)
scores = torch.max(classification, dim=2, keepdim=True)[0]
scores_over_thresh = (scores>0.05)[0, :, 0]
if scores_over_thresh.sum() == 0:
# no boxes to NMS, just return
return [torch.zeros(0), torch.zeros(0), torch.zeros(0, 4)]
classification = classification[:, scores_over_thresh, :]
transformed_anchors = transformed_anchors[:, scores_over_thresh, :]
scores = scores[:, scores_over_thresh, :]
anchors_nms_idx = nms(transformed_anchors, scores, 0.5)
nms_scores, nms_class = classification[0, anchors_nms_idx, :].max(dim=1)
return [nms_scores, nms_class, transformed_anchors[0, anchors_nms_idx, :]]
示例6: forward
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def forward(self, inputs):
if self.training:
img_batch, annotations = inputs
else:
img_batch = inputs
x = self.conv1(img_batch)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x1 = self.layer1(x)
x2 = self.layer2(x1)
x3 = self.layer3(x2)
x4 = self.layer4(x3)
features = self.fpn([x2, x3, x4])
regression = torch.cat([self.regressionModel(feature) for feature in features], dim=1)
classification = torch.cat([self.classificationModel(feature) for feature in features], dim=1)
anchors = self.anchors(img_batch)
if self.training:
return self.focalLoss(classification, regression, anchors, annotations)
else:
transformed_anchors = self.regressBoxes(anchors, regression)
transformed_anchors = self.clipBoxes(transformed_anchors, img_batch)
scores = torch.max(classification, dim=2, keepdim=True)[0]
scores_over_thresh = (scores>0.05)[0, :, 0]
if scores_over_thresh.sum() == 0:
# no boxes to NMS, just return
return [torch.zeros(0), torch.zeros(0), torch.zeros(0, 4)]
classification = classification[:, scores_over_thresh, :]
transformed_anchors = transformed_anchors[:, scores_over_thresh, :]
scores = scores[:, scores_over_thresh, :]
anchors_nms_idx = nms(transformed_anchors, scores, 0.5)
nms_scores, nms_class = classification[0, anchors_nms_idx, :].max(dim=1)
return [nms_scores, nms_class, transformed_anchors[0, anchors_nms_idx, :]]
示例7: batched_detect_with_landmarks
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def batched_detect_with_landmarks(
self, image: np.ndarray) -> typing.Tuple[np.ndarray, np.ndarray]:
"""Takes N images and performs and returns a set of bounding boxes as
detections
Args:
image (np.ndarray): shape [N, height, width, 3]
Returns:
np.ndarray: shape [N, 5] with (xmin, ymin, xmax, ymax, score)
np.ndarray: shape [N, 5, 2] with 5 landmarks with (x, y)
"""
image = image.astype(np.float32) - self.mean
image = np.moveaxis(image, -1, 1)
image = torch.from_numpy(image).to(self.device)
height, width = image.shape[2:]
boxes, landms = self._detect(image, return_landmarks=True)
scores = boxes[:, :, -1]
boxes = boxes[:, :, :-1]
final_output_box = []
final_output_landmarks = []
for i in range(len(boxes)):
boxes_ = boxes[i]
landms_ = landms[i]
scores_ = scores[i]
# Confidence thresholding
keep_idx = scores_ >= self.confidence_threshold
boxes_ = boxes_[keep_idx]
scores_ = scores_[keep_idx]
landms_ = landms_[keep_idx]
# Non maxima suppression
keep_idx = nms(
boxes_, scores_, self.nms_iou_threshold)
boxes_ = boxes_[keep_idx]
scores_ = scores_[keep_idx]
landms_ = landms_[keep_idx]
# Scale boxes
boxes_[:, [0, 2]] *= width
boxes_[:, [1, 3]] *= height
# Scale landmarks
landms_ = landms_.cpu().numpy().reshape(-1, 5, 2)
landms_[:, :, 0] *= width
landms_[:, :, 1] *= height
dets = torch.cat(
(boxes_, scores_.view(-1, 1)), dim=1).cpu().numpy()
final_output_box.append(dets)
final_output_landmarks.append(landms_)
return final_output_box, final_output_landmarks
示例8: forward
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def forward(self, arm_loc_data, arm_conf_data, odm_loc_data, odm_conf_data, prior_data):
"""
Args:
loc_data: (tensor) Loc preds from loc layers
Shape: [batch,num_priors*4]
conf_data: (tensor) Shape: Conf preds from conf layers
Shape: [batch*num_priors,num_classes]
prior_data: (tensor) Prior boxes and variances from priorbox layers
Shape: [1,num_priors,4]
"""
loc_data = odm_loc_data
conf_data = odm_conf_data
arm_object_conf = arm_conf_data.data[:, :, 1:]
no_object_index = arm_object_conf <= self.objectness_thre
conf_data[no_object_index.expand_as(conf_data)] = 0
num = loc_data.size(0) # batch size
num_priors = prior_data.size(0)
output = torch.zeros(num, self.num_classes, self.top_k, 5)
conf_preds = conf_data.view(num, num_priors,
self.num_classes).transpose(2, 1)
# Decode predictions into bboxes.
for i in range(num):
default = decode(arm_loc_data[i], prior_data, self.variance)
default = center_size(default)
decoded_boxes = decode(loc_data[i], default, self.variance)
# For each class, perform nms
conf_scores = conf_preds[i].clone()
#print(decoded_boxes, conf_scores)
for cl in range(1, self.num_classes):
c_mask = conf_scores[cl].gt(self.conf_thresh)
scores = conf_scores[cl][c_mask]
#print(scores.dim())
if scores.size(0) == 0:
continue
l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
boxes = decoded_boxes[l_mask].view(-1, 4)
# idx of highest scoring and non-overlapping boxes per class
#print(boxes, scores)
# ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
ids = nms(boxes, scores, self.nms_thresh)
count = min(len(ids), self.top_k)
output[i, cl, :count] = \
torch.cat((scores[ids[:count]].unsqueeze(1),
boxes[ids[:count]]), 1)
flt = output.contiguous().view(num, -1, 5)
_, idx = flt[:, :, 0].sort(1, descending=True)
_, rank = idx.sort(1)
flt[(rank < self.keep_top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
return output
示例9: multiclass_nms
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def multiclass_nms(multi_bboxes,
multi_scores,
score_thr=0.05,
max_num=1000,
score_factors=None):
"""NMS for multi-class bboxes.
Args:
multi_bboxes (Tensor): shape (n, #class*4) or (n, 4)
multi_scores (Tensor): shape (n, #class)
score_thr (float): bbox threshold, bboxes with scores lower than it
will not be considered.
nms_thr (float): NMS IoU threshold
max_num (int): if there are more than max_num bboxes after NMS,
only top max_num will be kept.
score_factors (Tensor): The factors multiplied to scores before
applying NMS
Returns:
tuple: (bboxes, labels), tensors of shape (k, 5) and (k, 1). Labels
are 0-based.
"""
num_classes = multi_scores.shape[1]
bboxes, labels = [], []
for i in range(1, num_classes):
cls_inds = multi_scores[:, i] > score_thr
if not cls_inds.any():
continue
# get bboxes and scores of this class
if multi_bboxes.shape[1] == 4:
_bboxes = multi_bboxes[cls_inds, :]
else:
_bboxes = multi_bboxes[cls_inds, i * 4:(i + 1) * 4]
_scores = multi_scores[cls_inds, i]
if score_factors is not None:
_scores *= score_factors[cls_inds]
cls_dets = torch.cat([_bboxes, _scores[:, None]], dim=1)
cls_dets = cls_dets[ops.nms(_bboxes, _scores, 0.5)]
cls_labels = multi_bboxes.new_full((cls_dets.shape[0], ),
i - 1,
dtype=torch.long)
bboxes.append(cls_dets)
labels.append(cls_labels)
if bboxes:
bboxes = torch.cat(bboxes)
labels = torch.cat(labels)
if bboxes.shape[0] > max_num:
_, inds = bboxes[:, -1].sort(descending=True)
inds = inds[:max_num]
bboxes = bboxes[inds]
labels = labels[inds]
else:
bboxes = multi_bboxes.new_zeros((0, 5))
labels = multi_bboxes.new_zeros((0, ), dtype=torch.long)
return bboxes, labels
示例10: non_max_suppression
# 需要导入模块: from torchvision import ops [as 别名]
# 或者: from torchvision.ops import nms [as 别名]
def non_max_suppression(prediction, conf_thres=0.5, nms_thres=0.4, method='standard'):
"""
Removes detections with lower object confidence score than 'conf_thres'
Non-Maximum Suppression to further filter detections.
Returns detections with shape:
(x1, y1, x2, y2, object_conf, class_score, class_pred)
Args:
prediction,
conf_thres,
nms_thres,
method = 'standard' or 'fast'
"""
output = [None for _ in range(len(prediction))]
for image_i, pred in enumerate(prediction):
# Filter out confidence scores below threshold
# Get score and class with highest confidence
v = pred[:, 4] > conf_thres
v = v.nonzero().squeeze()
if len(v.shape) == 0:
v = v.unsqueeze(0)
pred = pred[v]
# If none are remaining => process next image
nP = pred.shape[0]
if not nP:
continue
# From (center x, center y, width, height) to (x1, y1, x2, y2)
pred[:, :4] = xywh2xyxy(pred[:, :4])
# Non-maximum suppression
if method == 'standard':
nms_indices = nms(pred[:, :4], pred[:, 4], nms_thres)
elif method == 'fast':
nms_indices = fast_nms(pred[:, :4], pred[:, 4], iou_thres=nms_thres, conf_thres=conf_thres)
else:
raise ValueError('Invalid NMS type!')
det_max = pred[nms_indices]
if len(det_max) > 0:
# Add max detections to outputs
output[image_i] = det_max if output[image_i] is None else torch.cat((output[image_i], det_max))
return output