本文整理汇总了Python中utils.box_utils.decode方法的典型用法代码示例。如果您正苦于以下问题:Python box_utils.decode方法的具体用法?Python box_utils.decode怎么用?Python box_utils.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.box_utils
的用法示例。
在下文中一共展示了box_utils.decode方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, x):
l_data, m_data, h_data = x
l_grid_wh = (l_data.size(3), l_data.size(2))
m_grid_wh = (m_data.size(3), m_data.size(2))
h_grid_wh = (h_data.size(3), h_data.size(2))
pred_l, stride_l = permute_sigmoid(l_data, self.input_wh, 3, self.num_classes)
pred_m, stride_m = permute_sigmoid(m_data, self.input_wh, 3, self.num_classes)
pred_h, stride_h = permute_sigmoid(h_data, self.input_wh, 3, self.num_classes)
anchors1 = self.anchors[self.anchors_mask[0][0]: self.anchors_mask[0][-1]+1]
anchors2 = self.anchors[self.anchors_mask[1][0]: self.anchors_mask[1][-1]+1]
anchors3 = self.anchors[self.anchors_mask[2][0]: self.anchors_mask[2][-1]+1]
decode_l = decode(pred_l.detach(), self.input_wh, anchors1, self.num_classes, stride_l)
decode_m = decode(pred_m.detach(), self.input_wh, anchors2, self.num_classes, stride_m)
decode_h = decode(pred_h.detach(), self.input_wh, anchors3, self.num_classes, stride_h)
decode_pred = torch.cat((decode_l, decode_m, decode_h), 1)
return decode_pred
示例2: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, loc_data, 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]
"""
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):
decoded_boxes = decode(loc_data[i], prior_data, self.variance)
# For each class, perform nms
conf_scores = conf_preds[i].clone()
for cl in range(1, self.num_classes):
c_mask = conf_scores[cl].gt(self.conf_thresh)
scores = conf_scores[cl][c_mask]
if scores.dim() == 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
ids, count = nms(boxes, scores, self.nms_thresh, 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.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
return output
示例3: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, predictions):
"""
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, conf, priors = predictions
if self.use_arm:
arm_loc, arm_conf, loc, conf, priors = predictions
arm_loc_data = arm_loc.data
arm_conf_data = arm_conf.data
arm_object_conf = arm_conf_data[:, 1:]
no_object_index = arm_object_conf <= 0.01 #self.object_score
conf.data[no_object_index.expand_as(conf.data)] = 0
else:
loc, conf, priors = predictions
loc_data = loc.data
conf_data = conf.data
prior_data = priors.data
num = loc_data.size(0) # batch size
self.num_priors = prior_data.size(0)
self.boxes = torch.zeros(1, self.num_priors, 4)
self.scores = torch.zeros(1, self.num_priors, self.num_classes)
if num == 1:
# size batch x num_classes x num_priors
conf_preds = conf_data.unsqueeze(0)
else:
conf_preds = conf_data.view(num, num_priors,
self.num_classes)
self.boxes.expand_(num, self.num_priors, 4)
self.scores.expand_(num, self.num_priors, self.num_classes)
# Decode predictions into bboxes.
for i in range(num):
if self.use_arm:
default = decode(arm_loc_data[i], prior_data, self.variance)
default = center_size(default)
decoded_boxes = decode(loc_data[i], default, self.variance)
# decoded_boxes = decode((loc_data[i]+arm_loc_data[i]), prior_data, self.variance)
else:
decoded_boxes = decode(loc_data[i], prior_data, self.variance)
# For each class, perform nms
conf_scores = conf_preds[i].clone()
'''
c_mask = conf_scores.gt(self.thresh)
decoded_boxes = decoded_boxes[c_mask]
conf_scores = conf_scores[c_mask]
'''
self.boxes[i] = decoded_boxes
self.scores[i] = conf_scores
return self.boxes, self.scores
示例4: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, predictions, prior):
"""
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, conf = predictions
loc_data = loc.data
conf_data = conf.data
prior_data = prior.data
num = loc_data.size(0) # batch size
self.num_priors = prior_data.size(0)
self.boxes = torch.zeros(1, self.num_priors, 4)
self.scores = torch.zeros(1, self.num_priors, self.num_classes)
if loc_data.is_cuda:
self.boxes = self.boxes.cuda()
self.scores = self.scores.cuda()
if num == 1:
# size batch x num_classes x num_priors
conf_preds = conf_data.unsqueeze(0)
else:
conf_preds = conf_data.view(num, num_priors,
self.num_classes)
self.boxes.expand_(num, self.num_priors, 4)
self.scores.expand_(num, self.num_priors, self.num_classes)
# Decode predictions into bboxes.
for i in range(num):
decoded_boxes = decode(loc_data[i], prior_data, self.variance)
conf_scores = conf_preds[i].clone()
self.boxes[i] = decoded_boxes
self.scores[i] = conf_scores
return self.boxes, self.scores
示例5: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, predictions):
"""
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, conf, priors = predictions
if self.cfg.MODEL.CASCADE:
arm_loc, arm_conf, loc, conf, priors = predictions
arm_conf = F.softmax(arm_conf.view(-1, 2), 1)
conf = F.softmax(conf.view(-1, self.num_classes), 1)
arm_loc_data = arm_loc.data
arm_conf_data = arm_conf.data
arm_object_conf = arm_conf_data[:, 1:]
no_object_index = arm_object_conf <= self.object_score
# print(torch.sum(no_object_index) / loc.data.size(0), loc.data.size(1))
conf.data[no_object_index.expand_as(conf.data)] = 0
else:
loc, conf, priors = predictions
conf = F.softmax(conf.view(-1, self.num_classes), 1)
loc_data = loc.data
conf_data = conf.data
# prior_data = priors.data
prior_data = priors[:loc_data.size(1), :]
num = loc_data.size(0) # batch size
self.num_priors = prior_data.size(0)
self.boxes = torch.zeros(num, self.num_priors, 4)
self.scores = torch.zeros(num, self.num_priors, self.num_classes)
conf_preds = conf_data.view(num, self.num_priors, self.num_classes)
batch_prior = prior_data.view(-1, self.num_priors, 4).expand(
(num, self.num_priors, 4))
batch_prior = batch_prior.contiguous().view(-1, 4)
if self.cfg.MODEL.CASCADE:
default = decode(
arm_loc_data.view(-1, 4), batch_prior, self.variance)
default = center_size(default)
decoded_boxes = decode(
loc_data.view(-1, 4), default, self.variance1)
else:
decoded_boxes = decode(
loc_data.view(-1, 4), batch_prior, self.variance)
self.scores = conf_preds.view(num, self.num_priors, self.num_classes)
self.boxes = decoded_boxes.view(num, self.num_priors, 4)
return self.boxes, self.scores
示例6: detect_face
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def detect_face(net, img, resize):
if resize != 1:
img = cv2.resize(img, None, None, fx=resize, fy=resize, interpolation=cv2.INTER_LINEAR)
im_height, im_width, _ = img.shape
scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
#img = img[[2, 1, 0], :, :]
img = torch.from_numpy(img).unsqueeze(0)
if args.cuda:
img = img.cuda()
scale = scale.cuda()
out = net(img) # forward pass
priorbox = PriorBox(cfg, out[2], (im_height, im_width), phase='test')
priors = priorbox.forward()
if args.cuda:
priors = priors.cuda()
loc, conf, _ = out
print(loc.size(), conf.size())
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
boxes = boxes * scale / resize
boxes = boxes.cpu().numpy()
scores = conf.data.cpu().numpy()[:, 1]
# ignore low scores
inds = np.where(scores > args.confidence_threshold)[0]
boxes = boxes[inds]
scores = scores[inds]
# keep top-K before NMS
order = scores.argsort()[::-1][:args.top_k]
boxes = boxes[order]
scores = scores[order]
#print(boxes)
# do NMS
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = nms(dets, args.nms_threshold, force_cpu=args.cpu)
dets = dets[keep, :]
#print(dets)
# keep top-K faster NMS
dets = dets[:args.keep_top_k, :]
return dets
示例7: detect_face
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def detect_face(net, img, resize):
if resize != 1:
img = cv2.resize(img, None, None, fx=resize, fy=resize, interpolation=cv2.INTER_LINEAR)
im_height, im_width, _ = img.shape
scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)
if args.cuda:
img = img.cuda()
scale = scale.cuda()
out = net(img) # forward pass
priorbox = PriorBox(cfg, out[2], (im_height, im_width), phase='test')
priors = priorbox.forward()
if args.cuda:
priors = priors.cuda()
loc, conf, _ = out
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
boxes = boxes * scale / resize
boxes = boxes.cpu().numpy()
scores = conf.data.cpu().numpy()[:, 1]
# ignore low scores
inds = np.where(scores > args.confidence_threshold)[0]
boxes = boxes[inds]
scores = scores[inds]
# keep top-K before NMS
order = scores.argsort()[::-1][:args.top_k]
boxes = boxes[order]
scores = scores[order]
#print(boxes)
# do NMS
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = nms(dets, args.nms_threshold, force_cpu=args.cpu)
dets = dets[keep, :]
#print(dets)
# keep top-K faster NMS
dets = dets[:args.keep_top_k, :]
return dets
示例8: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, predictions):
"""
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, conf, priors = predictions
if self.cfg.MODEL.REFINE:
arm_loc, arm_conf, loc, conf, priors = predictions
arm_conf = F.softmax(arm_conf.view(-1, 2), 1)
conf = F.softmax(conf.view(-1, self.num_classes), 1)
arm_loc_data = arm_loc.data
arm_conf_data = arm_conf.data
arm_object_conf = arm_conf_data[:, 1:]
no_object_index = arm_object_conf <= self.object_score
conf.data[no_object_index.expand_as(conf.data)] = 0
else:
loc, conf, priors = predictions
conf = F.softmax(conf.view(-1, self.num_classes), 1)
loc_data = loc.data
conf_data = conf.data
# prior_data = priors.data
prior_data = priors[:loc_data.size(1), :]
num = loc_data.size(0) # batch size
self.num_priors = prior_data.size(0)
self.boxes = torch.zeros(num, self.num_priors, 4)
self.scores = torch.zeros(num, self.num_priors, self.num_classes)
conf_preds = conf_data.view(num, self.num_priors, self.num_classes)
batch_prior = prior_data.view(-1, self.num_priors, 4).expand(
(num, self.num_priors, 4))
batch_prior = batch_prior.contiguous().view(-1, 4)
if self.cfg.MODEL.REFINE:
default = decode(
arm_loc_data.view(-1, 4), batch_prior, self.variance)
default = center_size(default)
decoded_boxes = decode(
loc_data.view(-1, 4), default, self.variance)
else:
decoded_boxes = decode(
loc_data.view(-1, 4), batch_prior, self.variance)
self.scores = conf_preds.view(num, self.num_priors, self.num_classes)
self.boxes = decoded_boxes.view(num, self.num_priors, 4)
return self.boxes, self.scores
示例9: forward
# 需要导入模块: from utils import box_utils [as 别名]
# 或者: from utils.box_utils import decode [as 别名]
def forward(self, predictions, prior, arm_data=None):
"""
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, conf = predictions
loc_data = loc.data
conf_data = conf.data
prior_data = prior.data
num = loc_data.size(0) # batch size
if arm_data:
arm_loc, arm_conf = arm_data
arm_loc_data = arm_loc.data
arm_conf_data = arm_conf.data
arm_object_conf = arm_conf_data[:, 1:]
no_object_index = arm_object_conf <= self.object_score
conf_data[no_object_index.expand_as(conf_data)] = 0
self.num_priors = prior_data.size(0)
self.boxes = torch.zeros(num, self.num_priors, 4)
self.scores = torch.zeros(num, self.num_priors, self.num_classes)
if num == 1:
# size batch x num_classes x num_priors
conf_preds = conf_data.unsqueeze(0)
else:
conf_preds = conf_data.view(num, self.num_priors,
self.num_classes)
self.boxes.expand(num, self.num_priors, 4)
self.scores.expand(num, self.num_priors, self.num_classes)
# Decode predictions into bboxes.
for i in range(num):
if arm_data:
default = decode(arm_loc_data[i], prior_data, self.variance)
default = center_size(default)
else:
default = prior_data
decoded_boxes = decode(loc_data[i], default, self.variance)
# For each class, perform nms
conf_scores = conf_preds[i].clone()
'''
c_mask = conf_scores.gt(self.thresh)
decoded_boxes = decoded_boxes[c_mask]
conf_scores = conf_scores[c_mask]
'''
self.boxes[i] = decoded_boxes
self.scores[i] = conf_scores
return self.boxes, self.scores