YOLOv7训练时4个类别只出2个类别
正常是4个类别:
但是YOLOv7训练完后预测总是只有两个类别:
而且都是LFM和SFM
我一开始检查了下特征图大小,如果输入是640*640的话,三个尺度特征图是80*80,40*40,20*20;如果输入是416*416的话,三个尺度特征图是52*52,26*26,13*13。我一开始以为是信号太窄了,特征图大小设置有问题,但实际上在YOLOv3,YOLOv10下用的同样大小的特征图,YOLOv3,YOLOv10挺好使的。
后来检查了下anchors,用的是YOLO默认的anchors大小,和YOLOv3下用的一样(YOLOv3好使,别问YOLOv10下多少,YOLOv10是anchor-free的)
anchors =
array([
[ 10., 13.],[ 16., 30.],[ 33., 23.],
[ 30., 61.],[ 62., 45.],[ 59., 119.],
[116., 90.],[156., 198.],[373., 326.]
])
最后发现可能是detect.py下conf-thres参数的问题,这个参数不能太高,一开始我设的0.5,后来调到0.1就好了,设0.5的时候其实还是有极少的BPSK,Frank的类别是预测出来了的
if __name__ == '__main__':parser.add_argument('--conf-thres', type=float, default=0.1, help='object confidence threshold')parser.add_argument('--iou-thres', type=float, default=0.3, help='IOU threshold for NMS')
除了conf-thres,以下内容也是我检查过程中总结出来需要注意的
首先注意类别预测滤波器不要开
if __name__ == '__main__':parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
NMS操作时可以不使用classes参数
# Apply NMS
# pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, agnostic=opt.agnostic_nms)
另外一个就是YOLOv7的train.py下有这么两句话
# hyp['cls'] *= nc / 80. * 3. / nl # scale to classes and layers
# hyp['obj'] *= (imgsz / 640) ** 2 * 3. / nl # scale to image size and layers
hyp['cls'] *= nc / 4. * 3. / nl # scale to classes and layers
hyp['obj'] *= (imgsz / 416) ** 2 * 3. / nl # scale to image size and layers
类别数和图片大小记得改成自己的
某段时间我以为是那两个预测出来的类训练的不好,于是我记录了训练过程的各个类的loss值
具体方法是在:yolov7主路径/utils/loss.py下面写了一个函数
def save_every_class_loss_txt(self,pred_class, t):# zhouzhichao# 计算每个类别的损失self.class_loss_save_times = self.class_loss_save_times + 1# print("self.class_loss_save_times:",self.class_loss_save_times)if self.class_loss_save_times%50!=0:returnTXT_PATH = "D:\实验室\论文\论文-多信号参数估计\实验\YOLOv7\yolov7-main\\runs\class_loss.txt"class_losses = []for c in range(4):ps_c = pred_class[:, c] # 提取第c个类别的预测t_c = t[:, c] # 提取第c个类别的真值loss_c = self.BCEcls(ps_c, t_c) # 计算单类别损失# class_losses.append(round(loss_c.item(), 3)) # 保留4位小数class_losses.append(f"{loss_c.item():.3f}") # 使用 f-string 格式化# 转换为制表符分隔的字符串line = "\t".join(class_losses) + "\n"# 追加写入文件with open(TXT_PATH, "a", encoding="utf-8") as f:f.write(line)
class ComputeLossOTA:# Compute lossesdef __init__(self, model, autobalance=False):super(ComputeLossOTA, self).__init__()device = next(model.parameters()).device # get model deviceh = model.hyp # hyperparametersself.class_loss_save_times = 0
函数在ComputeLossOTA的__call__下调用:
# Classification
selected_tcls = targets[i][:, 1].long()
if self.nc > 1: # cls loss (only if multiple classes)t = torch.full_like(ps[:, 5:], self.cn, device=device) # targetst[range(n), selected_tcls] = self.cplcls += self.BCEcls(ps[:, 5:], t) # BCEself.save_every_class_loss_txt(ps[:, 5:], t)
原本计算的是全部类别的损失值:
lcls += self.BCEcls(ps[:, 5:], t) # BCE
我就拓展了下写出了save_every_class_loss_txt函数用于记录训练过程每个类别的损失值
画图的话就从txt里复制,粘贴到excel或origin都行,如下图
发现所有类别的损失值下降趋势看起来并没什么问题
这也使得我将预测类别缺失的问题限制在预测的阈值和Iou值设置上面了,最终调整阈值解决了问题