在进行模型训练结束后,模型代码会执行
Python val.py
对模型进行map准确率的验证,使用时候出现
talk is cheap ,show me the code.
找到warning的代码出处:
defnon_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False,
labels=(), max_det=300):"""Runs Non-Maximum Suppression (NMS) on inference results
Returns:
list of detections, on (n,6) tensor per image [xyxy, conf, cls]
"""
nc = prediction.shape[2]-5# number of classes
xc = prediction[...,4]> conf_thres # candidates# Checksassert0<= conf_thres <=1,f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0'assert0<= iou_thres <=1,f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0'# Settings
min_wh, max_wh =2,4096# (pixels) minimum and maximum box width and height
max_nms =30000# maximum number of boxes into torchvision.ops.nms()
time_limit =10.0# seconds to quit after
redundant =True# require redundant detections
multi_label &= nc >1# multiple labels per box (adds 0.5ms/img)
merge =False# use merge-NMS
t = time.time()
output =[torch.zeros((0,6), device=prediction.device)]* prediction.shape[0]for xi, x inenumerate(prediction):# image index, image inference# Apply constraints# x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height
x = x[xc[xi]]# confidence# Cat apriori labels if autolabellingif labels andlen(labels[xi]):
l = labels[xi]
v = torch.zeros((len(l), nc +5), device=x.device)
v[:,:4]= l[:,1:5]# box
v[:,4]=1.0# conf
v[range(len(l)), l[:,0].long()+5]=1.0# cls
x = torch.cat((x, v),0)# If none remain process next imageifnot x.shape[0]:continue# Compute conf
x[:,5:]*= x[:,4:5]# conf = obj_conf * cls_conf# Box (center x, center y, width, height) to (x1, y1, x2, y2)
box = xywh2xyxy(x[:,:4])# Detections matrix nx6 (xyxy, conf, cls)if multi_label:
i, j =(x[:,5:]> conf_thres).nonzero(as_tuple=False).T
x = torch.cat((box[i], x[i, j +5,None], j[:,None].float()),1)else:# best class only
conf, j = x[:,5:].max(1, keepdim=True)
x = torch.cat((box, conf, j.float()),1)[conf.view(-1)> conf_thres]# Filter by classif classes isnotNone:
x = x[(x[:,5:6]== torch.tensor(classes, device=x.device)).any(1)]# Apply finite constraint# if not torch.isfinite(x).all():# x = x[torch.isfinite(x).all(1)]# Check shape
n = x.shape[0]# number of boxesifnot n:# no boxescontinueelif n > max_nms:# excess boxes
x = x[x[:,4].argsort(descending=True)[:max_nms]]# sort by confidence# Batched NMS
c = x[:,5:6]*(0if agnostic else max_wh)# classes
boxes, scores = x[:,:4]+ c, x[:,4]# boxes (offset by class), scores
i = torchvision.ops.nms(boxes, scores, iou_thres)# NMSif i.shape[0]> max_det:# limit detections
i = i[:max_det]if merge and(1< n <3E3):# Merge NMS (boxes merged using weighted mean)# update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
iou = box_iou(boxes[i], boxes)> iou_thres # iou matrix
weights = iou * scores[None]# box weights
x[i,:4]= torch.mm(weights, x[:,:4]).float()/ weights.sum(1, keepdim=True)# merged boxesif redundant:
i = i[iou.sum(1)>1]# require redundancy
output[xi]= x[i]if(time.time()- t)> time_limit:print(f'WARNING: NMS time limit {time_limit}s exceeded')break# time limit exceededreturn output
以上是NMS非极大值抑制代码实现过程,其原理也很简单,解决的是多个锚框重叠的问题。
其实原因来看,进行NMS的时间断点太长了,将阈值也调大
#time_limit = 10.0 # seconds to quit after
time_limit =20.0# seconds to quit after
同时这个warning只会存在与前几轮,原因是模型加载同时模型还没有学到特征,进行模型推理速度太慢,训练几轮后,模型的提取特征能力增强,推理图片数据的性能自然会提升,警告也就消失了。
版权归原作者 千与编程 所有, 如有侵权,请联系我们删除。