From 02a36d746ee7c3319b3ba3ec651e4aa61a1ff726 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Thu, 18 Dec 2025 16:35:16 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E8=BD=A6=E8=BE=86=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.添加新的README.md文件 2.提供了预训练权重,UA-DATRAC数据集和训练完成模型文件的下载地址 --- src/Yolov4_Vehicle_inspection/README.md | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/Yolov4_Vehicle_inspection/README.md diff --git a/src/Yolov4_Vehicle_inspection/README.md b/src/Yolov4_Vehicle_inspection/README.md new file mode 100644 index 0000000000..d1a82789a8 --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/README.md @@ -0,0 +1,62 @@ +## Yolov4: 基于Pytorch的Yolov4算法实现车辆检测以及跟踪(深度学习) + +--- + +本项目是基于**[yolov4-pytorch](https://github.com/bubbliiiing/yolov4-pytorch)**实现的 + +## 文件下载 + +### 预训练权重下载 +链接:https://pan.baidu.com/s/1Wt7_yn6m8B9Uud80ScLkPQ 提取码:6666 +其中yolov4_voc_weights.pth是voc数据集的预训练权重,yolov4_coco_weights.pth是coco数据集的预训练权重 + +### UA-DATRAC数据集下载 + +链接:https://pan.baidu.com/s/14G_LLbilhe7adrLDVq1k-w 提取码:6666 + +### 训练完成模型文件下载 +链接:https://pan.baidu.com/s/1Ipw7KXqdUIjmUTQ6i0clhw 提取码:6666 +该文件为以voc数据集预训练权重使用UA-DATRAC数据集训练了23个epoch后的模型文件 + +## 数据处理 + +其中`transfer.py`和`move_pic.py`是针对UA-DATRAC数据集进行处理的两个文件。 + +## 训练步骤 + +1. 将VOC格式文件放至data的文件夹 +2. JPEGImages文件夹中存放数据图片 +3. Annotations文件夹中存放与图片一一对应的标签文件 +4. 调整voc2yolo4.py文件中的`trainval_percent` 、`train_percent`的值,控制训练集、测试集、验证集的比例;调整完成后运行该文件,在`data/ImageSets/Main`文件夹中生成对应数据集的图片文件名 +5. 运行`voc_annotation.py`文件,运行前将文件中的`classes`列表替换为自己数据集的class的名字 +6. 运行后自动生成`yolo_train.txt`文件,文件中每一行的格式为图片路径和图片中标注框的位置以及分类,如下所示 + `data/JPEGImages/MVI_20011__img00001.jpg 592,378,752,540,3 557,120,604,163,2 545,88,580,118,2 508,67,536,92,3 553,70,582,89,7 731,114,783,153,2 902,250,960,357,3` +7. 修改`train.py`中的路径参数与自己电脑上的路径相匹配 + + classes_path:`classes.txt`文件路径,文件格式如下: + ``` + Truck-Flatbed + MiniVan + Sedan + Suv + Truck-Pickup + Police + Truck-Box-Med + Taxi + ... + ``` + + model_path:预训练权重文件路径或者中断后继续训练时前一个保存的模型文件的路径 + + annotation_path:第6步生成的`yolo_train.txt`文件 +8. 调整`train.py`中的参数后运行文件进行训练 + + +## 预测 +1. 修改`model_path`的参数,选择合适的模型用于预测,并根据自己电脑情况选择是否启用cuda +2. 修改`predict.py`中的`mode`参数,进入不同的预测模式:`predict`表示单张图片预测,`video`表示视频检测 +3. 在predict模式下运行`predict.py`文件,输入预测图片的路径即可进行前向推理,最后通过PIL库输出处理后的图片与处理时间 +4. 在video模式下,需要预先修改video_name参数的值,将路径指向需要检测的视频,修改video_fps参数可以控制输出视频的fps;调整好后运行`predict.py`即可对视频进行预测 + +## 效果 + +![1](data/%EF%BC%91.jpg) + +![2](data/%EF%BC%92.jpg) \ No newline at end of file From 7e23c06e0e3168b998de8e1f83ee6c54d86fcdda Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Thu, 18 Dec 2025 19:42:00 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9B=86=E8=AE=AD=E7=BB=83=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.添加了train.py文件 --- src/Yolov4_Vehicle_inspection/train.py | 332 +++++++++++++++++++++++++ 1 file changed, 332 insertions(+) create mode 100644 src/Yolov4_Vehicle_inspection/train.py diff --git a/src/Yolov4_Vehicle_inspection/train.py b/src/Yolov4_Vehicle_inspection/train.py new file mode 100644 index 0000000000..b2965c59cf --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/train.py @@ -0,0 +1,332 @@ +#-------------------------------------# +# 对数据集进行训练 +#-------------------------------------# +import numpy as np +import torch +import torch.backends.cudnn as cudnn +import torch.optim as optim +from torch.utils.data import DataLoader +from tqdm import tqdm + +from nets.yolo4 import YoloBody +from nets.yolo_training import LossHistory, YOLOLoss, weights_init +from utils.dataloader import YoloDataset, yolo_dataset_collate + + +#---------------------------------------------------# +# 获得类和先验框 +#---------------------------------------------------# +def get_classes(classes_path): + '''loads the classes''' + with open(classes_path) as f: + class_names = f.readlines() + class_names = [c.strip() for c in class_names] + return class_names + +def get_anchors(anchors_path): + '''loads the anchors from a file''' + with open(anchors_path) as f: + anchors = f.readline() + anchors = [float(x) for x in anchors.split(',')] + return np.array(anchors).reshape([-1,3,2])[::-1,:,:] + +def get_lr(optimizer): + for param_group in optimizer.param_groups: + return param_group['lr'] + + +def fit_one_epoch(net,yolo_loss,epoch,epoch_size,epoch_size_val,gen,genval,Epoch,cuda): + if Tensorboard: + global train_tensorboard_step, val_tensorboard_step + total_loss = 0 + val_loss = 0 + + net.train() + print('Start Train') + with tqdm(total=epoch_size,desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) as pbar: + for iteration, batch in enumerate(gen): + if iteration >= epoch_size: + break + images, targets = batch[0], batch[1] + with torch.no_grad(): + if cuda: + images = torch.from_numpy(images).type(torch.FloatTensor).cuda() + targets = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets] + else: + images = torch.from_numpy(images).type(torch.FloatTensor) + targets = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets] + #----------------------# + # 清零梯度 + #----------------------# + optimizer.zero_grad() + #----------------------# + # 前向传播 + #----------------------# + outputs = net(images) + losses = [] + num_pos_all = 0 + #----------------------# + # 计算损失 + #----------------------# + for i in range(3): + loss_item, num_pos = yolo_loss(outputs[i], targets) + losses.append(loss_item) + num_pos_all += num_pos + + loss = sum(losses) / num_pos_all + total_loss += loss.item() + + #----------------------# + # 反向传播 + #----------------------# + loss.backward() + optimizer.step() + + if Tensorboard: + # 将loss写入tensorboard,每一步都写 + writer.add_scalar('Train_loss', loss, train_tensorboard_step) + train_tensorboard_step += 1 + + pbar.set_postfix(**{'total_loss': total_loss / (iteration + 1), + 'lr' : get_lr(optimizer)}) + pbar.update(1) + + # 将loss写入tensorboard,下面注释的是每个世代保存一次 + # if Tensorboard: + # writer.add_scalar('Train_loss', total_loss/(iteration+1), epoch) + net.eval() + print('Start Validation') + with tqdm(total=epoch_size_val, desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) as pbar: + for iteration, batch in enumerate(genval): + if iteration >= epoch_size_val: + break + images_val, targets_val = batch[0], batch[1] + + with torch.no_grad(): + if cuda: + images_val = torch.from_numpy(images_val).type(torch.FloatTensor).cuda() + targets_val = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets_val] + else: + images_val = torch.from_numpy(images_val).type(torch.FloatTensor) + targets_val = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets_val] + optimizer.zero_grad() + + outputs = net(images_val) + losses = [] + num_pos_all = 0 + for i in range(3): + loss_item, num_pos = yolo_loss(outputs[i], targets_val) + losses.append(loss_item) + num_pos_all += num_pos + loss = sum(losses) / num_pos_all + val_loss += loss.item() + + # 将loss写入tensorboard, 下面注释的是每一步都写 + # if Tensorboard: + # writer.add_scalar('Val_loss', loss, val_tensorboard_step) + # val_tensorboard_step += 1 + pbar.set_postfix(**{'total_loss': val_loss / (iteration + 1)}) + pbar.update(1) + + # 将loss写入tensorboard,每个世代保存一次 + if Tensorboard: + writer.add_scalar('Val_loss',val_loss / (epoch_size_val+1), epoch) + loss_history.append_loss(total_loss/(epoch_size+1), val_loss/(epoch_size_val+1)) + print('Finish Validation') + print('Epoch:'+ str(epoch+1) + '/' + str(Epoch)) + print('Total Loss: %.4f || Val Loss: %.4f ' % (total_loss/(epoch_size+1),val_loss/(epoch_size_val+1))) + print('Saving state, iter:', str(epoch+1)) + torch.save(model.state_dict(), 'logs/Epoch%d-Total_Loss%.4f-Val_Loss%.4f.pth'%((epoch+1),total_loss/(epoch_size+1),val_loss/(epoch_size_val+1))) + +#----------------------------------------------------# +# 检测精度mAP和pr曲线计算参考视频 +# https://www.bilibili.com/video/BV1zE411u7Vw +#----------------------------------------------------# +if __name__ == "__main__": + #-------------------------------# + # 是否使用Tensorboard + #-------------------------------# + Tensorboard = False + #-------------------------------# + # 是否使用Cuda + # 没有GPU可以设置成False + #-------------------------------# + Cuda = True + #------------------------------------------------------# + # 是否对损失进行归一化,用于改变loss的大小 + # 用于决定计算最终loss是除上batch_size还是除上正样本数量 + #------------------------------------------------------# + normalize = False + #-------------------------------# + # 输入的shape大小 + # 显存比较小可以使用416x416 + # 显存比较大可以使用608x608 + #-------------------------------# + input_shape = (416,416) + #----------------------------------------------------# + # classes和anchor的路径,非常重要 + # 训练前一定要修改classes_path,使其对应自己的数据集 + #----------------------------------------------------# + anchors_path = 'model_data/yolo_anchors.txt' + classes_path = 'model_data/voc_classes.txt' + #------------------------------------------------------# + # Yolov4的tricks应用 + # mosaic 马赛克数据增强 True or False + # 实际测试时mosaic数据增强并不稳定,所以默认为False + # Cosine_scheduler 余弦退火学习率 True or False + # label_smoothing 标签平滑 0.01以下一般 如0.01、0.005 + #------------------------------------------------------# + mosaic = False + Cosine_lr = False + smoooth_label = 0 + + #----------------------------------------------------# + # 获取classes和anchor + #----------------------------------------------------# + class_names = get_classes(classes_path) + anchors = get_anchors(anchors_path) + num_classes = len(class_names) + + #------------------------------------------------------# + # 创建yolo模型 + # 训练前一定要修改classes_path和对应的txt文件 + #------------------------------------------------------# + model = YoloBody(len(anchors[0]), num_classes) + weights_init(model) + + #------------------------------------------------------# + # 权值文件请看README,百度网盘下载 + #------------------------------------------------------# + model_path = "model_data/yolo4_weights.pth" + print('Loading weights into state dict...') + device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') + model_dict = model.state_dict() + pretrained_dict = torch.load(model_path, map_location=device) + pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)} + model_dict.update(pretrained_dict) + model.load_state_dict(model_dict) + print('Finished!') + # torch.save(model.state_dict(),"logs/test.pth",_use_new_zipfile_serialization=False ) + + net = model.train() + + if Cuda: + net = torch.nn.DataParallel(model) + cudnn.benchmark = True + net = net.cuda() + + yolo_loss = YOLOLoss(np.reshape(anchors,[-1,2]), num_classes, (input_shape[1], input_shape[0]), smoooth_label, Cuda, normalize) + loss_history = LossHistory("logs/") + + #----------------------------------------------------# + # 获得图片路径和标签 + #----------------------------------------------------# + annotation_path = '2007_train.txt' + #----------------------------------------------------------------------# + # 验证集的划分在train.py代码里面进行 + # 2007_test.txt和2007_val.txt里面没有内容是正常的。训练不会使用到。 + # 当前划分方式下,验证集和训练集的比例为1:9 + #----------------------------------------------------------------------# + val_split = 0.1 + with open(annotation_path) as f: + lines = f.readlines() + np.random.seed(10101) + np.random.shuffle(lines) + np.random.seed(None) + num_val = int(len(lines)*val_split) + num_train = len(lines) - num_val + + if Tensorboard: + from tensorboardX import SummaryWriter + writer = SummaryWriter(log_dir='logs',flush_secs=60) + if Cuda: + graph_inputs = torch.randn(1,3,input_shape[0],input_shape[1]).type(torch.FloatTensor).cuda() + else: + graph_inputs = torch.randn(1,3,input_shape[0],input_shape[1]).type(torch.FloatTensor) + writer.add_graph(model, graph_inputs) + train_tensorboard_step = 1 + val_tensorboard_step = 1 + + #------------------------------------------------------# + # 主干特征提取网络特征通用,冻结训练可以加快训练速度 + # 也可以在训练初期防止权值被破坏。 + # Init_Epoch为起始世代 + # Freeze_Epoch为冻结训练的世代 + # Epoch总训练世代 + # 提示OOM或者显存不足请调小Batch_size + #------------------------------------------------------# + if True: + lr = 1e-3 + Batch_size = 4 + Init_Epoch = 0 + Freeze_Epoch = 50 + + #----------------------------------------------------------------------------# + # 我在实际测试时,发现optimizer的weight_decay起到了反作用, + # 所以去除掉了weight_decay,大家也可以开起来试试,一般是weight_decay=5e-4 + #----------------------------------------------------------------------------# + optimizer = optim.Adam(net.parameters(),lr) + if Cosine_lr: + lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=5, eta_min=1e-5) + else: + lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.92) + + train_dataset = YoloDataset(lines[:num_train], (input_shape[0], input_shape[1]), mosaic=mosaic, is_train=True) + val_dataset = YoloDataset(lines[num_train:], (input_shape[0], input_shape[1]), mosaic=False, is_train=False) + gen = DataLoader(train_dataset, shuffle=True, batch_size=Batch_size, num_workers=4, pin_memory=True, + drop_last=True, collate_fn=yolo_dataset_collate) + gen_val = DataLoader(val_dataset, shuffle=True, batch_size=Batch_size, num_workers=4,pin_memory=True, + drop_last=True, collate_fn=yolo_dataset_collate) + + epoch_size = num_train // Batch_size + epoch_size_val = num_val // Batch_size + + if epoch_size == 0 or epoch_size_val == 0: + raise ValueError("数据集过小,无法进行训练,请扩充数据集。") + #------------------------------------# + # 冻结一定部分训练 + #------------------------------------# + for param in model.backbone.parameters(): + param.requires_grad = False + + for epoch in range(Init_Epoch,Freeze_Epoch): + fit_one_epoch(net,yolo_loss,epoch,epoch_size,epoch_size_val,gen,gen_val,Freeze_Epoch,Cuda) + lr_scheduler.step() + + if True: + lr = 1e-4 + Batch_size = 2 + Freeze_Epoch = 50 + Unfreeze_Epoch = 100 + + #----------------------------------------------------------------------------# + # 我在实际测试时,发现optimizer的weight_decay起到了反作用, + # 所以去除掉了weight_decay,大家也可以开起来试试,一般是weight_decay=5e-4 + #----------------------------------------------------------------------------# + optimizer = optim.Adam(net.parameters(),lr) + if Cosine_lr: + lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=5, eta_min=1e-5) + else: + lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.92) + + train_dataset = YoloDataset(lines[:num_train], (input_shape[0], input_shape[1]), mosaic=mosaic, is_train=True) + val_dataset = YoloDataset(lines[num_train:], (input_shape[0], input_shape[1]), mosaic=False, is_train=False) + gen = DataLoader(train_dataset, shuffle=True, batch_size=Batch_size, num_workers=4, pin_memory=True, + drop_last=True, collate_fn=yolo_dataset_collate) + gen_val = DataLoader(val_dataset, shuffle=True, batch_size=Batch_size, num_workers=4,pin_memory=True, + drop_last=True, collate_fn=yolo_dataset_collate) + + epoch_size = num_train // Batch_size + epoch_size_val = num_val // Batch_size + + if epoch_size == 0 or epoch_size_val == 0: + raise ValueError("数据集过小,无法进行训练,请扩充数据集。") + #------------------------------------# + # 解冻后训练 + #------------------------------------# + for param in model.backbone.parameters(): + param.requires_grad = True + + for epoch in range(Freeze_Epoch,Unfreeze_Epoch): + fit_one_epoch(net,yolo_loss,epoch,epoch_size,epoch_size_val,gen,gen_val,Unfreeze_Epoch,Cuda) + lr_scheduler.step() From c81a417a11984603f18d4b865516f5492b84e3ea Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Thu, 18 Dec 2025 19:56:27 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0VOCdevkit=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.Annotations存放标签文件 2.ImageSets存放训练索引文件 3.JPEGImages存放图片文件 --- .../VOCdevkit/VOC2007/Annotations/README.md | 1 + .../VOCdevkit/VOC2007/ImageSets/README.md | 1 + .../VOCdevkit/VOC2007/JPEGImages/README.md | 1 + 3 files changed, 3 insertions(+) create mode 100644 src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/Annotations/README.md create mode 100644 src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/ImageSets/README.md create mode 100644 src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/JPEGImages/README.md diff --git a/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/Annotations/README.md b/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/Annotations/README.md new file mode 100644 index 0000000000..d73a1916c9 --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/Annotations/README.md @@ -0,0 +1 @@ +存放标签文件 \ No newline at end of file diff --git a/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/ImageSets/README.md b/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/ImageSets/README.md new file mode 100644 index 0000000000..2f64921067 --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/ImageSets/README.md @@ -0,0 +1 @@ +存放训练索引文件 \ No newline at end of file diff --git a/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/JPEGImages/README.md b/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/JPEGImages/README.md new file mode 100644 index 0000000000..58f32ea1aa --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/VOCdevkit/VOC2007/JPEGImages/README.md @@ -0,0 +1 @@ +存放图片文件 \ No newline at end of file From 150d737e2811f41ad50737bc9b3e59eec1d76f67 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Thu, 18 Dec 2025 20:09:13 +0800 Subject: [PATCH 04/12] Delete src/Yolov4_Vehicle_inspection/train.py --- src/Yolov4_Vehicle_inspection/train.py | 332 ------------------------- 1 file changed, 332 deletions(-) delete mode 100644 src/Yolov4_Vehicle_inspection/train.py diff --git a/src/Yolov4_Vehicle_inspection/train.py b/src/Yolov4_Vehicle_inspection/train.py deleted file mode 100644 index b2965c59cf..0000000000 --- a/src/Yolov4_Vehicle_inspection/train.py +++ /dev/null @@ -1,332 +0,0 @@ -#-------------------------------------# -# 对数据集进行训练 -#-------------------------------------# -import numpy as np -import torch -import torch.backends.cudnn as cudnn -import torch.optim as optim -from torch.utils.data import DataLoader -from tqdm import tqdm - -from nets.yolo4 import YoloBody -from nets.yolo_training import LossHistory, YOLOLoss, weights_init -from utils.dataloader import YoloDataset, yolo_dataset_collate - - -#---------------------------------------------------# -# 获得类和先验框 -#---------------------------------------------------# -def get_classes(classes_path): - '''loads the classes''' - with open(classes_path) as f: - class_names = f.readlines() - class_names = [c.strip() for c in class_names] - return class_names - -def get_anchors(anchors_path): - '''loads the anchors from a file''' - with open(anchors_path) as f: - anchors = f.readline() - anchors = [float(x) for x in anchors.split(',')] - return np.array(anchors).reshape([-1,3,2])[::-1,:,:] - -def get_lr(optimizer): - for param_group in optimizer.param_groups: - return param_group['lr'] - - -def fit_one_epoch(net,yolo_loss,epoch,epoch_size,epoch_size_val,gen,genval,Epoch,cuda): - if Tensorboard: - global train_tensorboard_step, val_tensorboard_step - total_loss = 0 - val_loss = 0 - - net.train() - print('Start Train') - with tqdm(total=epoch_size,desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) as pbar: - for iteration, batch in enumerate(gen): - if iteration >= epoch_size: - break - images, targets = batch[0], batch[1] - with torch.no_grad(): - if cuda: - images = torch.from_numpy(images).type(torch.FloatTensor).cuda() - targets = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets] - else: - images = torch.from_numpy(images).type(torch.FloatTensor) - targets = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets] - #----------------------# - # 清零梯度 - #----------------------# - optimizer.zero_grad() - #----------------------# - # 前向传播 - #----------------------# - outputs = net(images) - losses = [] - num_pos_all = 0 - #----------------------# - # 计算损失 - #----------------------# - for i in range(3): - loss_item, num_pos = yolo_loss(outputs[i], targets) - losses.append(loss_item) - num_pos_all += num_pos - - loss = sum(losses) / num_pos_all - total_loss += loss.item() - - #----------------------# - # 反向传播 - #----------------------# - loss.backward() - optimizer.step() - - if Tensorboard: - # 将loss写入tensorboard,每一步都写 - writer.add_scalar('Train_loss', loss, train_tensorboard_step) - train_tensorboard_step += 1 - - pbar.set_postfix(**{'total_loss': total_loss / (iteration + 1), - 'lr' : get_lr(optimizer)}) - pbar.update(1) - - # 将loss写入tensorboard,下面注释的是每个世代保存一次 - # if Tensorboard: - # writer.add_scalar('Train_loss', total_loss/(iteration+1), epoch) - net.eval() - print('Start Validation') - with tqdm(total=epoch_size_val, desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) as pbar: - for iteration, batch in enumerate(genval): - if iteration >= epoch_size_val: - break - images_val, targets_val = batch[0], batch[1] - - with torch.no_grad(): - if cuda: - images_val = torch.from_numpy(images_val).type(torch.FloatTensor).cuda() - targets_val = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets_val] - else: - images_val = torch.from_numpy(images_val).type(torch.FloatTensor) - targets_val = [torch.from_numpy(ann).type(torch.FloatTensor) for ann in targets_val] - optimizer.zero_grad() - - outputs = net(images_val) - losses = [] - num_pos_all = 0 - for i in range(3): - loss_item, num_pos = yolo_loss(outputs[i], targets_val) - losses.append(loss_item) - num_pos_all += num_pos - loss = sum(losses) / num_pos_all - val_loss += loss.item() - - # 将loss写入tensorboard, 下面注释的是每一步都写 - # if Tensorboard: - # writer.add_scalar('Val_loss', loss, val_tensorboard_step) - # val_tensorboard_step += 1 - pbar.set_postfix(**{'total_loss': val_loss / (iteration + 1)}) - pbar.update(1) - - # 将loss写入tensorboard,每个世代保存一次 - if Tensorboard: - writer.add_scalar('Val_loss',val_loss / (epoch_size_val+1), epoch) - loss_history.append_loss(total_loss/(epoch_size+1), val_loss/(epoch_size_val+1)) - print('Finish Validation') - print('Epoch:'+ str(epoch+1) + '/' + str(Epoch)) - print('Total Loss: %.4f || Val Loss: %.4f ' % (total_loss/(epoch_size+1),val_loss/(epoch_size_val+1))) - print('Saving state, iter:', str(epoch+1)) - torch.save(model.state_dict(), 'logs/Epoch%d-Total_Loss%.4f-Val_Loss%.4f.pth'%((epoch+1),total_loss/(epoch_size+1),val_loss/(epoch_size_val+1))) - -#----------------------------------------------------# -# 检测精度mAP和pr曲线计算参考视频 -# https://www.bilibili.com/video/BV1zE411u7Vw -#----------------------------------------------------# -if __name__ == "__main__": - #-------------------------------# - # 是否使用Tensorboard - #-------------------------------# - Tensorboard = False - #-------------------------------# - # 是否使用Cuda - # 没有GPU可以设置成False - #-------------------------------# - Cuda = True - #------------------------------------------------------# - # 是否对损失进行归一化,用于改变loss的大小 - # 用于决定计算最终loss是除上batch_size还是除上正样本数量 - #------------------------------------------------------# - normalize = False - #-------------------------------# - # 输入的shape大小 - # 显存比较小可以使用416x416 - # 显存比较大可以使用608x608 - #-------------------------------# - input_shape = (416,416) - #----------------------------------------------------# - # classes和anchor的路径,非常重要 - # 训练前一定要修改classes_path,使其对应自己的数据集 - #----------------------------------------------------# - anchors_path = 'model_data/yolo_anchors.txt' - classes_path = 'model_data/voc_classes.txt' - #------------------------------------------------------# - # Yolov4的tricks应用 - # mosaic 马赛克数据增强 True or False - # 实际测试时mosaic数据增强并不稳定,所以默认为False - # Cosine_scheduler 余弦退火学习率 True or False - # label_smoothing 标签平滑 0.01以下一般 如0.01、0.005 - #------------------------------------------------------# - mosaic = False - Cosine_lr = False - smoooth_label = 0 - - #----------------------------------------------------# - # 获取classes和anchor - #----------------------------------------------------# - class_names = get_classes(classes_path) - anchors = get_anchors(anchors_path) - num_classes = len(class_names) - - #------------------------------------------------------# - # 创建yolo模型 - # 训练前一定要修改classes_path和对应的txt文件 - #------------------------------------------------------# - model = YoloBody(len(anchors[0]), num_classes) - weights_init(model) - - #------------------------------------------------------# - # 权值文件请看README,百度网盘下载 - #------------------------------------------------------# - model_path = "model_data/yolo4_weights.pth" - print('Loading weights into state dict...') - device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') - model_dict = model.state_dict() - pretrained_dict = torch.load(model_path, map_location=device) - pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)} - model_dict.update(pretrained_dict) - model.load_state_dict(model_dict) - print('Finished!') - # torch.save(model.state_dict(),"logs/test.pth",_use_new_zipfile_serialization=False ) - - net = model.train() - - if Cuda: - net = torch.nn.DataParallel(model) - cudnn.benchmark = True - net = net.cuda() - - yolo_loss = YOLOLoss(np.reshape(anchors,[-1,2]), num_classes, (input_shape[1], input_shape[0]), smoooth_label, Cuda, normalize) - loss_history = LossHistory("logs/") - - #----------------------------------------------------# - # 获得图片路径和标签 - #----------------------------------------------------# - annotation_path = '2007_train.txt' - #----------------------------------------------------------------------# - # 验证集的划分在train.py代码里面进行 - # 2007_test.txt和2007_val.txt里面没有内容是正常的。训练不会使用到。 - # 当前划分方式下,验证集和训练集的比例为1:9 - #----------------------------------------------------------------------# - val_split = 0.1 - with open(annotation_path) as f: - lines = f.readlines() - np.random.seed(10101) - np.random.shuffle(lines) - np.random.seed(None) - num_val = int(len(lines)*val_split) - num_train = len(lines) - num_val - - if Tensorboard: - from tensorboardX import SummaryWriter - writer = SummaryWriter(log_dir='logs',flush_secs=60) - if Cuda: - graph_inputs = torch.randn(1,3,input_shape[0],input_shape[1]).type(torch.FloatTensor).cuda() - else: - graph_inputs = torch.randn(1,3,input_shape[0],input_shape[1]).type(torch.FloatTensor) - writer.add_graph(model, graph_inputs) - train_tensorboard_step = 1 - val_tensorboard_step = 1 - - #------------------------------------------------------# - # 主干特征提取网络特征通用,冻结训练可以加快训练速度 - # 也可以在训练初期防止权值被破坏。 - # Init_Epoch为起始世代 - # Freeze_Epoch为冻结训练的世代 - # Epoch总训练世代 - # 提示OOM或者显存不足请调小Batch_size - #------------------------------------------------------# - if True: - lr = 1e-3 - Batch_size = 4 - Init_Epoch = 0 - Freeze_Epoch = 50 - - #----------------------------------------------------------------------------# - # 我在实际测试时,发现optimizer的weight_decay起到了反作用, - # 所以去除掉了weight_decay,大家也可以开起来试试,一般是weight_decay=5e-4 - #----------------------------------------------------------------------------# - optimizer = optim.Adam(net.parameters(),lr) - if Cosine_lr: - lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=5, eta_min=1e-5) - else: - lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.92) - - train_dataset = YoloDataset(lines[:num_train], (input_shape[0], input_shape[1]), mosaic=mosaic, is_train=True) - val_dataset = YoloDataset(lines[num_train:], (input_shape[0], input_shape[1]), mosaic=False, is_train=False) - gen = DataLoader(train_dataset, shuffle=True, batch_size=Batch_size, num_workers=4, pin_memory=True, - drop_last=True, collate_fn=yolo_dataset_collate) - gen_val = DataLoader(val_dataset, shuffle=True, batch_size=Batch_size, num_workers=4,pin_memory=True, - drop_last=True, collate_fn=yolo_dataset_collate) - - epoch_size = num_train // Batch_size - epoch_size_val = num_val // Batch_size - - if epoch_size == 0 or epoch_size_val == 0: - raise ValueError("数据集过小,无法进行训练,请扩充数据集。") - #------------------------------------# - # 冻结一定部分训练 - #------------------------------------# - for param in model.backbone.parameters(): - param.requires_grad = False - - for epoch in range(Init_Epoch,Freeze_Epoch): - fit_one_epoch(net,yolo_loss,epoch,epoch_size,epoch_size_val,gen,gen_val,Freeze_Epoch,Cuda) - lr_scheduler.step() - - if True: - lr = 1e-4 - Batch_size = 2 - Freeze_Epoch = 50 - Unfreeze_Epoch = 100 - - #----------------------------------------------------------------------------# - # 我在实际测试时,发现optimizer的weight_decay起到了反作用, - # 所以去除掉了weight_decay,大家也可以开起来试试,一般是weight_decay=5e-4 - #----------------------------------------------------------------------------# - optimizer = optim.Adam(net.parameters(),lr) - if Cosine_lr: - lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=5, eta_min=1e-5) - else: - lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.92) - - train_dataset = YoloDataset(lines[:num_train], (input_shape[0], input_shape[1]), mosaic=mosaic, is_train=True) - val_dataset = YoloDataset(lines[num_train:], (input_shape[0], input_shape[1]), mosaic=False, is_train=False) - gen = DataLoader(train_dataset, shuffle=True, batch_size=Batch_size, num_workers=4, pin_memory=True, - drop_last=True, collate_fn=yolo_dataset_collate) - gen_val = DataLoader(val_dataset, shuffle=True, batch_size=Batch_size, num_workers=4,pin_memory=True, - drop_last=True, collate_fn=yolo_dataset_collate) - - epoch_size = num_train // Batch_size - epoch_size_val = num_val // Batch_size - - if epoch_size == 0 or epoch_size_val == 0: - raise ValueError("数据集过小,无法进行训练,请扩充数据集。") - #------------------------------------# - # 解冻后训练 - #------------------------------------# - for param in model.backbone.parameters(): - param.requires_grad = True - - for epoch in range(Freeze_Epoch,Unfreeze_Epoch): - fit_one_epoch(net,yolo_loss,epoch,epoch_size,epoch_size_val,gen,gen_val,Unfreeze_Epoch,Cuda) - lr_scheduler.step() From 7ad6454f0e073cf96ccb831842c03818562a1b92 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Thu, 18 Dec 2025 20:29:43 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.给predict.py添加更详细的注释 --- src/Yolov4_Vehicle_inspection/predict.py | 122 +++++++++++++++-------- 1 file changed, 82 insertions(+), 40 deletions(-) diff --git a/src/Yolov4_Vehicle_inspection/predict.py b/src/Yolov4_Vehicle_inspection/predict.py index 833ba458de..2ac46d0042 100644 --- a/src/Yolov4_Vehicle_inspection/predict.py +++ b/src/Yolov4_Vehicle_inspection/predict.py @@ -3,23 +3,25 @@ # 将单张图片预测、摄像头检测和FPS测试功能 # 整合到了一个py文件中,通过指定mode进行模式的修改。 # ----------------------------------------------------# -import time +import time # 导入时间模块,用于计时和FPS计算 +import cv2 # 导入OpenCV库,用于视频/图像的读取、处理和保存 +import numpy as np # 导入numpy库,用于数组格式转换 +from PIL import Image # 导入PIL库的Image模块,用于图像格式转换 -import cv2 -import numpy as np -from PIL import Image - -from yolo import YOLO +from yolo import YOLO # 导入自定义的YOLO检测类 if __name__ == "__main__": + # 初始化YOLO检测器,设置输入图像尺寸为640x640 yolo = YOLO(size=640) + # -------------------------------------------------------------------------# # mode用于指定测试的模式: # 'predict'表示单张图片预测 # 'video'表示视频检测 # 'fps'表示测试fps # -------------------------------------------------------------------------# - mode = "video" + mode = "video" # 当前运行模式设置为视频检测 + # -------------------------------------------------------------------------# # video_path用于指定视频的路径,当video_path=0时表示检测摄像头 # video_save_path表示视频保存的路径,当video_save_path=""时表示不保存 @@ -27,10 +29,10 @@ # video_path、video_save_path和video_fps仅在mode='video'时有效 # 保存视频时需要ctrl+c退出才会完成完整的保存步骤,不可直接结束程序。 # -------------------------------------------------------------------------# - video_name = "333" - video_path = r'vedio\int\%s.mp4' % video_name - video_save_path = r"vedio\out\%s.mp4" % video_name - video_fps = 30 + video_name = "333" # 视频文件名称(不含后缀) + video_path = r'vedio\int\%s.mp4' % video_name # 输入视频的路径 + video_save_path = r"vedio\out\%s.mp4" % video_name # 输出视频的保存路径 + video_fps = 30 # 保存视频的帧率 if mode == "predict": ''' @@ -43,82 +45,122 @@ 5、如果想要在预测图上写额外的字,比如检测到的特定目标的数量,可以进入yolo.detect_image函数,在绘图部分对predicted_class进行判断, 比如判断if predicted_class == 'car': 即可判断当前目标是否为车,然后记录数量即可。利用draw.text即可写字。 ''' + # 循环读取用户输入的图片路径,实现多次单张图片预测 while True: - img = input('Input image filename:') - start = time.time() + img = input('Input image filename:') # 提示用户输入图片文件路径 + start = time.time() # 记录图片检测开始时间 try: + # 尝试打开指定路径的图片文件 image = Image.open(img) except: + # 打开失败时提示错误并重新输入 print('Open Error! Try again!') continue else: + # 图片打开成功,调用YOLO的检测函数进行目标检测 r_image = yolo.detect_image(image) + # 打印单张图片检测耗时 print("time : ", time.time() - start) + # 显示检测后的图片 r_image.show() elif mode == "video": + # 初始化视频读取器,读取指定路径的视频文件 capture = cv2.VideoCapture(video_path) - start = time.time() + start = time.time() # 记录视频检测总开始时间 + + # 如果设置了视频保存路径,则初始化视频写入器 if video_save_path != "": + # 设置视频编码格式为mp4v(兼容mp4格式) fourcc = cv2.VideoWriter_fourcc(*'mp4v') + # 获取原视频的分辨率(宽度和高度) size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) + # 创建视频写入器,指定保存路径、编码格式、帧率和分辨率 out = cv2.VideoWriter(video_save_path, fourcc, video_fps, size) - fps = 0.0 - time_cnt = 0 - cars = [] - car_not_vis = [] + # 初始化FPS相关参数 + fps = 0.0 # 实时帧率(滑动平均) + time_cnt = 0 # 帧计数,记录已处理的帧数 + cars = [] # 存储当前可见的车辆目标信息 + car_not_vis = [] # 存储不可见的车辆目标信息 + + # 循环读取视频帧,直到视频结束或用户退出 while (True): - t1 = time.time() - # 读取某一帧 - ref, frame = capture.read() + t1 = time.time() # 记录当前帧处理开始时间 + # 读取视频的一帧,ref为读取成功标志,frame为帧数据 + ref, frame = capture.read() - if not ref: # 防止视频读取完/帧为空导致后续报错 + # 若读取失败(如视频结束、帧为空),跳出循环 + if not ref: break - - # 调整对比度(alpha)和亮度(beta),可根据实际视频微调: + # ====================== 图像预处理:对比度和亮度调整 ====================== # alpha>1增强对比度,alpha<1降低对比度;beta>0增亮,beta<0调暗 - alpha = 1.2 # 对比度增强20%(可改1.1~1.5) - beta = 8 # 亮度提升8(可改5~15) + alpha = 1.2 # 对比度增强20%(可根据实际视频调整1.1~1.5) + beta = 8 # 亮度提升8(可根据实际视频调整5~15) + # 调整帧的对比度和亮度,提升检测效果 frame = cv2.convertScaleAbs(frame, alpha=alpha, beta=beta) - # 格式转变,BGRtoRGB - frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 原try-except可删除,因为已提前判断ref - - # 转变成Image + # ====================== 格式转换:适配YOLO检测要求 ====================== + # OpenCV读取的帧是BGR格式,转换为RGB格式(YOLO检测要求RGB) + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + # 将numpy数组格式的帧转换为PIL的Image格式(YOLO检测函数入参要求) frame = Image.fromarray(np.uint8(frame)) - # 进行检测 + + # ====================== 目标检测:调用YOLO检测函数 ====================== + # 传入帧数据和车辆跟踪列表,执行目标检测并返回标注后的帧 frame = np.array(yolo.detect_image(frame, cars, car_not_vis)) - # RGBtoBGR满足opencv显示格式 + + # ====================== 格式还原:适配OpenCV显示/保存 ====================== + # 将RGB格式转换回BGR格式(OpenCV显示/保存要求BGR) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) + # ====================== FPS计算与信息标注 ====================== + # 滑动平均计算实时FPS:(上一帧FPS + 当前帧FPS) / 2 fps = (fps + (1. / (time.time() - t1))) / 2 + # 打印实时帧号、FPS和总耗时(\r实现单行覆盖打印) print("\rframe_id = %d\tfps = %.2f\ttime = %.2f" % (time_cnt, fps, time.time() - start), end="") - time_cnt += 1 + time_cnt += 1 # 帧计数+1 + + # 在帧上标注实时FPS(位置:左上角,字体:默认,大小:1,颜色:绿色,线宽:2) frame = cv2.putText(frame, "fps = %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + # 在帧上标注当前检测到的车辆数量(位置:FPS下方,样式同上) frame = cv2.putText(frame, "cars_num = %d" % (len(cars)), (0, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + # 可选:显示视频窗口(注释掉以提升处理速度) # cv2.imshow("video",frame) + + # 等待1ms,检测键盘输入(ESC键退出) c = cv2.waitKey(1) & 0xff + + # 如果设置了保存路径,将标注后的帧写入视频文件 if video_save_path != "": out.write(frame) - if c == 27: # 按ESC退出 + # 按ESC键(ASCII码27)退出视频检测循环 + if c == 27: break - # print("time: ", time.time() - start) + + # 换行,避免打印信息重叠 print() - capture.release() - if video_save_path != "": # 防止未保存视频时out未定义 + + # ====================== 资源释放 ====================== + capture.release() # 释放视频读取器资源 + # 若初始化了视频写入器,释放其资源 + if video_save_path != "": out.release() - cv2.destroyAllWindows() + cv2.destroyAllWindows() # 关闭所有OpenCV创建的窗口 elif mode == "fps": - test_interval = 100 - img = Image.open('img/street.jpg') + # FPS测试模式:计算模型处理单张图片的平均耗时和帧率 + test_interval = 100 # 测试次数(连续检测100次取平均) + img = Image.open('img/street.jpg') # 读取测试用图片 + # 调用YOLO的FPS测试函数,返回单次检测平均耗时(秒) tact_time = yolo.get_FPS(img, test_interval) + # 打印测试结果:单次耗时、帧率(1/耗时)、批量大小为1 print(str(tact_time) + ' seconds, ' + str(1 / tact_time) + 'FPS, @batch_size 1') else: + # 模式指定错误时抛出断言异常,提示正确的模式选项 raise AssertionError("Please specify the correct mode: 'predict', 'video' or 'fps'.") \ No newline at end of file From ffae88a535a7b530b85a6c17e7be56762e86a1d9 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Thu, 18 Dec 2025 21:45:17 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 给yolo4.py添加更详细的注释 --- src/Yolov4_Vehicle_inspection/nets/yolo4.py | 82 ++++++++++++++++++--- 1 file changed, 71 insertions(+), 11 deletions(-) diff --git a/src/Yolov4_Vehicle_inspection/nets/yolo4.py b/src/Yolov4_Vehicle_inspection/nets/yolo4.py index a1b2d8c76e..3103f0b241 100644 --- a/src/Yolov4_Vehicle_inspection/nets/yolo4.py +++ b/src/Yolov4_Vehicle_inspection/nets/yolo4.py @@ -1,16 +1,27 @@ +# 导入有序字典,用于按顺序构建神经网络层(保证层执行顺序) from collections import OrderedDict +# 导入PyTorch核心库,用于构建神经网络 import torch +# 导入PyTorch神经网络模块,包含各类层定义 import torch.nn as nn +# 导入自定义的CSPDarknet53主干网络(特征提取基础) from nets.CSPdarknet import darknet53 +# 定义基础卷积模块:Conv2d + BatchNorm2d + LeakyReLU +# filter_in: 输入通道数 | filter_out: 输出通道数 | kernel_size: 卷积核尺寸 | stride: 卷积步长 def conv2d(filter_in, filter_out, kernel_size, stride=1): + # 计算padding值,实现same padding(卷积后特征图尺寸不变) pad = (kernel_size - 1) // 2 if kernel_size else 0 + # 使用OrderedDict构建有序的卷积+BN+激活组合层 return nn.Sequential(OrderedDict([ + # 卷积层:无偏置(BN层已包含偏置参数,避免冗余) ("conv", nn.Conv2d(filter_in, filter_out, kernel_size=kernel_size, stride=stride, padding=pad, bias=False)), + # 批量归一化层:加速训练收敛,降低过拟合风险 ("bn", nn.BatchNorm2d(filter_out)), + # LeakyReLU激活:缓解梯度消失,负区间保留小梯度(斜率0.1) ("relu", nn.LeakyReLU(0.1)), ])) @@ -22,10 +33,13 @@ class SpatialPyramidPooling(nn.Module): def __init__(self, pool_sizes=[5, 9, 13]): super(SpatialPyramidPooling, self).__init__() + # 创建多个最大池化层,池化核5/9/13,步长1,padding=池化核//2(保证池化后尺寸不变) self.maxpools = nn.ModuleList([nn.MaxPool2d(pool_size, 1, pool_size//2) for pool_size in pool_sizes]) def forward(self, x): + # 逆序遍历池化层,对输入特征图做多尺度池化(13→9→5) features = [maxpool(x) for maxpool in self.maxpools[::-1]] + # 将多尺度池化结果与原始特征图在通道维度拼接(dim=1),融合多尺度特征 features = torch.cat(features + [x], dim=1) return features @@ -37,30 +51,36 @@ class Upsample(nn.Module): def __init__(self, in_channels, out_channels): super(Upsample, self).__init__() + # 上采样模块:先1x1卷积调整通道数,再最近邻上采样放大2倍(保持特征清晰) self.upsample = nn.Sequential( - conv2d(in_channels, out_channels, 1), - nn.Upsample(scale_factor=2, mode='nearest') + conv2d(in_channels, out_channels, 1), # 1x1卷积降维,减少计算量 + nn.Upsample(scale_factor=2, mode='nearest') # 最近邻上采样,尺寸放大2倍 ) def forward(self, x,): + # 执行上采样操作 x = self.upsample(x) return x #---------------------------------------------------# # 三次卷积块 #---------------------------------------------------# +# filters_list: [中间通道数, 3x3卷积通道数] | in_filters: 输入通道数 def make_three_conv(filters_list, in_filters): + # 三次卷积结构:1x1→3x3→1x1,提取特征并调整通道数 m = nn.Sequential( - conv2d(in_filters, filters_list[0], 1), - conv2d(filters_list[0], filters_list[1], 3), - conv2d(filters_list[1], filters_list[0], 1), + conv2d(in_filters, filters_list[0], 1), # 1x1卷积降维 + conv2d(filters_list[0], filters_list[1], 3),# 3x3卷积扩大感受野,提取特征 + conv2d(filters_list[1], filters_list[0], 1),# 1x1卷积再次降维 ) return m #---------------------------------------------------# # 五次卷积块 #---------------------------------------------------# +# filters_list: [中间通道数, 3x3卷积通道数] | in_filters: 输入通道数 def make_five_conv(filters_list, in_filters): + # 五次卷积结构:1x1→3x3→1x1→3x3→1x1,强化特征提取能力 m = nn.Sequential( conv2d(in_filters, filters_list[0], 1), conv2d(filters_list[0], filters_list[1], 3), @@ -73,10 +93,12 @@ def make_five_conv(filters_list, in_filters): #---------------------------------------------------# # 最后获得yolov4的输出 #---------------------------------------------------# +# filters_list: [3x3卷积通道数, 最终输出通道数] | in_filters: 输入通道数 def yolo_head(filters_list, in_filters): + # YOLO检测头:3x3卷积强化特征 + 1x1卷积输出预测结果 m = nn.Sequential( - conv2d(in_filters, filters_list[0], 3), - nn.Conv2d(filters_list[0], filters_list[1], 1), + conv2d(in_filters, filters_list[0], 3), # 3x3卷积提升特征表达 + nn.Conv2d(filters_list[0], filters_list[1], 1), # 1x1卷积输出最终检测结果(锚框数*(5+类别数)) ) return m @@ -86,107 +108,145 @@ def yolo_head(filters_list, in_filters): class YoloBody(nn.Module): def __init__(self, num_anchors, num_classes): super(YoloBody, self).__init__() - #---------------------------------------------------# + #---------------------------------------------------# # 生成CSPdarknet53的主干模型 # 获得三个有效特征层,他们的shape分别是: # 52,52,256 # 26,26,512 # 13,13,1024 #---------------------------------------------------# + # 初始化CSPDarknet53主干网络,用于提取基础特征 self.backbone = darknet53(None) + # 处理13x13x1024特征层:三次卷积(1024→512→1024→512) self.conv1 = make_three_conv([512,1024],1024) + # 接入SPP模块,融合多尺度特征 self.SPP = SpatialPyramidPooling() + # 再次三次卷积处理SPP输出(2048→512→1024→512) self.conv2 = make_three_conv([512,1024],2048) + # 上采样模块1:512→256,尺寸从13x13→26x26 self.upsample1 = Upsample(512,256) + # 调整26x26x512特征层通道数:512→256 self.conv_for_P4 = conv2d(512,256,1) + # 五次卷积融合26x26特征(512→256→512→256→512→256) self.make_five_conv1 = make_five_conv([256, 512],512) + # 上采样模块2:256→128,尺寸从26x26→52x52 self.upsample2 = Upsample(256,128) + # 调整52x52x256特征层通道数:256→128 self.conv_for_P3 = conv2d(256,128,1) + # 五次卷积融合52x52特征(256→128→256→128→256→128) self.make_five_conv2 = make_five_conv([128, 256],256) # 3*(5+num_classes) = 3*(5+20) = 3*(4+1+20)=75 + # 计算52x52特征层输出通道数:锚框数*(坐标4+置信度1+类别数) final_out_filter2 = num_anchors * (5 + num_classes) + # 52x52特征层的YOLO检测头(检测小目标) self.yolo_head3 = yolo_head([256, final_out_filter2],128) + # 下采样模块1:128→256,步长2,尺寸从52x52→26x26 self.down_sample1 = conv2d(128,256,3,stride=2) + # 五次卷积再次融合26x26特征(512→256→512→256→512→256) self.make_five_conv3 = make_five_conv([256, 512],512) # 3*(5+num_classes) = 3*(5+20) = 3*(4+1+20)=75 + # 计算26x26特征层输出通道数 final_out_filter1 = num_anchors * (5 + num_classes) + # 26x26特征层的YOLO检测头(检测中目标) self.yolo_head2 = yolo_head([512, final_out_filter1],256) + # 下采样模块2:256→512,步长2,尺寸从26x26→13x13 self.down_sample2 = conv2d(256,512,3,stride=2) + # 五次卷积再次融合13x13特征(1024→512→1024→512→1024→512) self.make_five_conv4 = make_five_conv([512, 1024],1024) # 3*(5+num_classes)=3*(5+20)=3*(4+1+20)=75 + # 计算13x13特征层输出通道数 final_out_filter0 = num_anchors * (5 + num_classes) + # 13x13特征层的YOLO检测头(检测大目标) self.yolo_head1 = yolo_head([1024, final_out_filter0],512) def forward(self, x): # backbone + # 前向传播通过主干网络,获取三个有效特征层 + # x2: 52x52x256 | x1:26x26x512 | x0:13x13x1024 x2, x1, x0 = self.backbone(x) - # 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 -> 13,13,2048 + # 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 -> 13,13,2048 + # 处理13x13x1024特征层:三次卷积→SPP→三次卷积 P5 = self.conv1(x0) P5 = self.SPP(P5) # 13,13,2048 -> 13,13,512 -> 13,13,1024 -> 13,13,512 P5 = self.conv2(P5) # 13,13,512 -> 13,13,256 -> 26,26,256 + # 上采样P5并与x1(26x26x512)融合 P5_upsample = self.upsample1(P5) # 26,26,512 -> 26,26,256 P4 = self.conv_for_P4(x1) # 26,26,256 + 26,26,256 -> 26,26,512 # P4 = torch.cat([P4,P5_upsample],axis=1) + # 通道维度拼接上采样后的P5和调整后的x1 P4 = torch.cat([P4, P5_upsample], dim=1) # 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256 + # 五次卷积融合特征 P4 = self.make_five_conv1(P4) # 26,26,256 -> 26,26,128 -> 52,52,128 + # 上采样P4并与x2(52x52x256)融合 P4_upsample = self.upsample2(P4) # 52,52,256 -> 52,52,128 P3 = self.conv_for_P3(x2) # 52,52,128 + 52,52,128 -> 52,52,256 # P3 = torch.cat([P3,P4_upsample],axis=1) + # 通道维度拼接上采样后的P4和调整后的x2 P3 = torch.cat([P3, P4_upsample], dim=1) # 52,52,256 -> 52,52,128 -> 52,52,256 -> 52,52,128 -> 52,52,256 -> 52,52,128 + # 五次卷积融合特征 P3 = self.make_five_conv2(P3) # 52,52,128 -> 26,26,256 + # 下采样P3,与P4融合 P3_downsample = self.down_sample1(P3) # 26,26,256 + 26,26,256 -> 26,26,512 # P4 = torch.cat([P3_downsample,P4],axis=1) + # 通道维度拼接下采样后的P3和原P4 P4 = torch.cat([P3_downsample, P4], dim=1) # 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256 -> 26,26,512 -> 26,26,256 + # 五次卷积融合特征 P4 = self.make_five_conv3(P4) # 26,26,256 -> 13,13,512 + # 下采样P4,与P5融合 P4_downsample = self.down_sample2(P4) # 13,13,512 + 13,13,512 -> 13,13,1024 # P5 = torch.cat([P4_downsample,P5],axis=1) + # 通道维度拼接下采样后的P4和原P5 P5 = torch.cat([P4_downsample, P5], dim=1) # 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 -> 13,13,1024 -> 13,13,512 + # 五次卷积融合特征 P5 = self.make_five_conv4(P5) #---------------------------------------------------# # 第三个特征层 # y3=(batch_size,75,52,52) #---------------------------------------------------# + # 52x52特征层输出检测结果(小目标) out2 = self.yolo_head3(P3) #---------------------------------------------------# # 第二个特征层 # y2=(batch_size,75,26,26) #---------------------------------------------------# + # 26x26特征层输出检测结果(中目标) out1 = self.yolo_head2(P4) #---------------------------------------------------# # 第一个特征层 # y1=(batch_size,75,13,13) #---------------------------------------------------# + # 13x13特征层输出检测结果(大目标) out0 = self.yolo_head1(P5) - return out0, out1, out2 - + # 返回三个尺度的检测结果(13x13、26x26、52x52) + return out0, out1, out2 \ No newline at end of file From 732e1ceeef7e1b759bf343a8ff14b66c62ef6578 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Fri, 19 Dec 2025 15:27:52 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.给detaloader.py添加更详细的注释 --- .../utils/dataloader.py | 144 +++++++++++------- 1 file changed, 89 insertions(+), 55 deletions(-) diff --git a/src/Yolov4_Vehicle_inspection/utils/dataloader.py b/src/Yolov4_Vehicle_inspection/utils/dataloader.py index 8da93d1b2d..eaa9324f7c 100644 --- a/src/Yolov4_Vehicle_inspection/utils/dataloader.py +++ b/src/Yolov4_Vehicle_inspection/utils/dataloader.py @@ -7,51 +7,63 @@ class YoloDataset(Dataset): + # 新增:YOLO模型专用数据集类,实现数据加载、实时数据增强(含Mosaic) def __init__(self, train_lines, image_size, mosaic=True, is_train=True): super(YoloDataset, self).__init__() - self.train_lines = train_lines - self.train_batches = len(train_lines) - self.image_size = image_size - self.mosaic = mosaic - self.flag = True - self.is_train = is_train + self.train_lines = train_lines # 数据集标注行列表,每行格式:图片路径 框1(x1,y1,x2,y2,cls) 框2(...) + self.train_batches = len(train_lines) # 数据集总样本数 + self.image_size = image_size # 模型输入图片尺寸 (h, w) + self.mosaic = mosaic # 是否启用Mosaic数据增强(提升小目标检测效果) + self.flag = True # Mosaic交替标志,控制是否每次取4张图拼接 + self.is_train = is_train # 是否为训练模式(训练模式开启数据增强,验证模式关闭) def __len__(self): + # 新增:返回数据集总长度,PyTorch Dataset必需实现的方法 return self.train_batches def rand(self, a=0, b=1): + # 新增:生成[a, b)区间的随机浮点数,用于数据增强的随机参数 return np.random.rand() * (b - a) + a def get_random_data(self, annotation_line, input_shape, jitter=.3, hue=.1, sat=1.5, val=1.5, random=True): """实时数据增强的随机预处理""" + # 新增:解析单条标注行,分割图片路径和目标框信息 line = annotation_line.split() - image = Image.open(line[0]) - iw, ih = image.size - h, w = input_shape + image = Image.open(line[0]) # 打开图片(PIL格式) + iw, ih = image.size # 原始图片宽、高 + h, w = input_shape # 模型输入尺寸(高、宽) + # 新增:解析目标框,格式转换为numpy数组,每个框:[x1, y1, x2, y2, 类别] box = np.array([np.array(list(map(int, box.split(',')))) for box in line[1:]]) + # 新增:验证模式(不做随机增强,仅缩放+居中) if not random: + # 计算等比例缩放系数(保证图片完整放入输入尺寸) scale = min(w/iw, h/ih) - nw = int(iw*scale) - nh = int(ih*scale) - dx = (w-nw)//2 - dy = (h-nh)//2 + nw = int(iw*scale) # 缩放后图片宽 + nh = int(ih*scale) # 缩放后图片高 + dx = (w-nw)//2 # x方向偏移(居中) + dy = (h-nh)//2 # y方向偏移(居中) + # 缩放图片(BICUBIC插值,保证缩放质量) image = image.resize((nw,nh), Image.BICUBIC) + # 新增:创建灰色背景画布(128,128,128),将缩放后的图片粘贴到中心 new_image = Image.new('RGB', (w,h), (128,128,128)) new_image.paste(image, (dx, dy)) - image_data = np.array(new_image, np.float32) + image_data = np.array(new_image, np.float32) # 转换为numpy数组 # 调整目标框坐标 box_data = np.zeros((len(box), 5)) if len(box) > 0: - np.random.shuffle(box) + np.random.shuffle(box) # 随机打乱框顺序(无实际作用,保持与训练模式一致) + # 新增:框坐标缩放 + 偏移(匹配图片的缩放和居中) box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy + # 新增:限制框坐标在图片范围内(防止越界) box[:, 0:2][box[:, 0:2] < 0] = 0 box[:, 2][box[:, 2] > w] = w box[:, 3][box[:, 3] > h] = h + # 新增:计算框的宽、高,过滤掉无效框(宽/高<1像素) box_w = box[:, 2] - box[:, 0] box_h = box[:, 3] - box[:, 1] box = box[np.logical_and(box_w > 1, box_h > 1)] # 保留有效框 @@ -60,56 +72,67 @@ def get_random_data(self, annotation_line, input_shape, jitter=.3, hue=.1, sat=1 return image_data, box_data - # 调整图片大小 + # 新增:训练模式(随机数据增强) + # 调整图片大小:随机宽高比抖动 new_ar = w / h * self.rand(1 - jitter, 1 + jitter) / self.rand(1 - jitter, 1 + jitter) - scale = self.rand(.25, 2) + scale = self.rand(.25, 2) # 随机缩放系数(0.25~2倍) if new_ar < 1: nh = int(scale * h) nw = int(nh * new_ar) else: nw = int(scale * w) nh = int(nw / new_ar) - image = image.resize((nw, nh), Image.BICUBIC) + image = image.resize((nw, nh), Image.BICUBIC) # 缩放图片 - # 放置图片 + # 放置图片:随机偏移(增加位置多样性) dx = int(self.rand(0, w - nw)) dy = int(self.rand(0, h - nh)) + # 新增:创建随机颜色背景画布(替代固定灰色,增强多样性) new_image = Image.new('RGB', (w, h), (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255))) new_image.paste(image, (dx, dy)) image = new_image - # 是否翻转图片 + # 是否翻转图片:50%概率水平翻转 flip = self.rand() < .5 if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT) - # 色域变换 + # 色域变换:HSV空间随机调整色调、饱和度、明度 hue = self.rand(-hue, hue) sat = self.rand(1, sat) if self.rand() < .5 else 1 / self.rand(1, sat) val = self.rand(1, val) if self.rand() < .5 else 1 / self.rand(1, val) + # 新增:PIL转OpenCV格式,RGB转HSV,归一化到0~1 x = cv2.cvtColor(np.array(image,np.float32)/255, cv2.COLOR_RGB2HSV) + # 调整色调(hue范围:-0.1~0.1,对应HSV的0~360) x[..., 0] += hue*360 x[..., 0][x[..., 0]>1] -= 1 x[..., 0][x[..., 0]<0] += 1 + # 调整饱和度、明度 x[..., 1] *= sat x[..., 2] *= val + # 新增:限制HSV值在有效范围(0~360 for H, 0~1 for S/V) x[x[:,:, 0]>360, 0] = 360 x[:, :, 1:][x[:, :, 1:]>1] = 1 x[x<0] = 0 + # 新增:HSV转回RGB,恢复到0~255范围 image_data = cv2.cvtColor(x, cv2.COLOR_HSV2RGB)*255 # 调整目标框坐标 box_data = np.zeros((len(box), 5)) if len(box) > 0: - np.random.shuffle(box) + np.random.shuffle(box) # 随机打乱框顺序 + # 新增:框坐标缩放 + 偏移(匹配图片的缩放和随机放置) box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy + # 新增:如果图片翻转,框的x坐标需要对称调整 if flip: box[:, [0, 2]] = w - box[:, [2, 0]] + # 新增:限制框坐标在图片范围内 box[:, 0:2][box[:, 0:2] < 0] = 0 box[:, 2][box[:, 2] > w] = w box[:, 3][box[:, 3] > h] = h + # 新增:过滤无效框(宽/高<1像素) box_w = box[:, 2] - box[:, 0] box_h = box[:, 3] - box[:, 1] box = box[np.logical_and(box_w > 1, box_h > 1)] # 保留有效框 @@ -119,16 +142,18 @@ def get_random_data(self, annotation_line, input_shape, jitter=.3, hue=.1, sat=1 return image_data, box_data def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat=1.5, val=1.5): + # 新增:Mosaic数据增强核心函数,将4张图片拼接成1张,提升小目标和多尺度检测能力 h, w = input_shape - min_offset_x = 0.3 - min_offset_y = 0.3 - scale_low = 1 - min(min_offset_x, min_offset_y) - scale_high = scale_low + 0.2 + min_offset_x = 0.3 # x方向最小偏移比例(避免图片重叠过多) + min_offset_y = 0.3 # y方向最小偏移比例 + scale_low = 1 - min(min_offset_x, min_offset_y) # 缩放系数下限 + scale_high = scale_low + 0.2 # 缩放系数上限 - image_datas = [] - box_datas = [] + image_datas = [] # 存储4张图片的预处理结果 + box_datas = [] # 存储4张图片的框数据 index = 0 + # 新增:4张图片的初始放置位置(左上、左下、右下、右上) place_x = [0, 0, int(w * min_offset_x), int(w * min_offset_x)] place_y = [0, int(h * min_offset_y), int(h * min_offset_y), 0] for line in annotation_line: @@ -136,7 +161,7 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat= line_content = line.split() # 打开图片 image = Image.open(line_content[0]) - image = image.convert("RGB") + image = image.convert("RGB") # 确保图片为RGB格式(避免灰度图) # 图片的大小 iw, ih = image.size # 保存框的位置 @@ -159,7 +184,7 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat= nh = int(nw / new_ar) image = image.resize((nw, nh), Image.BICUBIC) - # 进行色域变换 + # 进行色域变换(同单图增强逻辑) hue = self.rand(-hue, hue) sat = self.rand(1, sat) if self.rand() < .5 else 1 / self.rand(1, sat) val = self.rand(1, val) if self.rand() < .5 else 1 / self.rand(1, val) @@ -174,6 +199,7 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat= x[x<0] = 0 image = cv2.cvtColor(x, cv2.COLOR_HSV2RGB) # numpy array, 0 to 1 + # 新增:将归一化的图片转回0~255的PIL格式 image = Image.fromarray((image * 255).astype(np.uint8)) # 将图片进行放置,分别对应四张分割图片的位置 dx = place_x[index] @@ -185,7 +211,7 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat= index = index + 1 box_data = [] - # 对box进行重新处理 + # 对box进行重新处理(同单图增强逻辑) if len(box) > 0: np.random.shuffle(box) box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx @@ -202,64 +228,72 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat= image_datas.append(image_data) box_datas.append(box_data) - # 将图片分割,放在一起 + # 新增:随机生成切割点(将4张图片拼接成1张的核心步骤) cutx = np.random.randint(int(w * min_offset_x), int(w * (1 - min_offset_x))) cuty = np.random.randint(int(h * min_offset_y), int(h * (1 - min_offset_y))) + # 新增:拼接4张图片,按切割点组合成最终的Mosaic图片 new_image = np.zeros([h, w, 3]) - new_image[:cuty, :cutx, :] = image_datas[0][:cuty, :cutx, :] - new_image[cuty:, :cutx, :] = image_datas[1][cuty:, :cutx, :] - new_image[cuty:, cutx:, :] = image_datas[2][cuty:, cutx:, :] - new_image[:cuty, cutx:, :] = image_datas[3][:cuty, cutx:, :] + new_image[:cuty, :cutx, :] = image_datas[0][:cuty, :cutx, :] # 左上区域 + new_image[cuty:, :cutx, :] = image_datas[1][cuty:, :cutx, :] # 左下区域 + new_image[cuty:, cutx:, :] = image_datas[2][cuty:, cutx:, :] # 右下区域 + new_image[:cuty, cutx:, :] = image_datas[3][:cuty, cutx:, :] # 右上区域 - # 对框进行进一步的处理 + # 对框进行进一步的处理:合并4张图片的框,处理跨切割点的框 new_boxes = np.array(merge_bboxes(box_datas, cutx, cuty)) return new_image, new_boxes def __getitem__(self, index): + # 新增:PyTorch Dataset必需实现的方法,返回单样本的预处理数据 lines = self.train_lines n = self.train_batches - index = index % n + index = index % n # 新增:防止索引越界(支持多epoch循环) if self.mosaic: + # 新增:Mosaic模式,每次取4张连续的图片拼接(需保证索引不越界) if self.flag and (index + 4) < n: img, y = self.get_random_data_with_Mosaic(lines[index:index + 4], self.image_size[0:2]) else: + # 新增:不足4张时,退化为单图增强 img, y = self.get_random_data(lines[index], self.image_size[0:2], random=self.is_train) - self.flag = bool(1-self.flag) + self.flag = bool(1-self.flag) # 新增:交替开关,避免连续使用Mosaic导致显存波动 else: + # 新增:禁用Mosaic时,直接使用单图增强 img, y = self.get_random_data(lines[index], self.image_size[0:2], random=self.is_train) if len(y) != 0: - # 从坐标转换成0~1的百分比 + # 从坐标转换成0~1的百分比(归一化) boxes = np.array(y[:, :4], dtype=np.float32) - boxes[:, 0] = boxes[:, 0] / self.image_size[1] - boxes[:, 1] = boxes[:, 1] / self.image_size[0] - boxes[:, 2] = boxes[:, 2] / self.image_size[1] - boxes[:, 3] = boxes[:, 3] / self.image_size[0] - - boxes = np.maximum(np.minimum(boxes, 1), 0) - boxes[:, 2] = boxes[:, 2] - boxes[:, 0] - boxes[:, 3] = boxes[:, 3] - boxes[:, 1] - - boxes[:, 0] = boxes[:, 0] + boxes[:, 2] / 2 - boxes[:, 1] = boxes[:, 1] + boxes[:, 3] / 2 + boxes[:, 0] = boxes[:, 0] / self.image_size[1] # x1/w + boxes[:, 1] = boxes[:, 1] / self.image_size[0] # y1/h + boxes[:, 2] = boxes[:, 2] / self.image_size[1] # x2/w + boxes[:, 3] = boxes[:, 3] / self.image_size[0] # y2/h + + boxes = np.maximum(np.minimum(boxes, 1), 0) # 新增:限制归一化后的值在0~1之间 + # 新增:将x1y1x2y2格式转换为中心坐标+宽高格式(YOLO模型输入要求) + boxes[:, 2] = boxes[:, 2] - boxes[:, 0] # w = x2 - x1 + boxes[:, 3] = boxes[:, 3] - boxes[:, 1] # h = y2 - y1 + boxes[:, 0] = boxes[:, 0] + boxes[:, 2] / 2 # cx = x1 + w/2 + boxes[:, 1] = boxes[:, 1] + boxes[:, 3] / 2 # cy = y1 + h/2 + # 新增:合并中心坐标+宽高+类别标签 y = np.concatenate([boxes, y[:, -1:]], axis=-1) img = np.array(img, dtype=np.float32) + # 新增:调整图片维度顺序 (h, w, c) → (c, h, w)(适配PyTorch张量格式) tmp_inp = np.transpose(img / 255.0, (2, 0, 1)) - tmp_targets = np.array(y, dtype=np.float32) + tmp_targets = np.array(y, dtype=np.float32) # 框数据转为浮点型 return tmp_inp, tmp_targets # DataLoader中collate_fn使用 def yolo_dataset_collate(batch): + # 新增:自定义DataLoader批次处理函数,解决不同样本框数量不一致的问题 images = [] bboxes = [] for img, box in batch: images.append(img) bboxes.append(box) - images = np.array(images) - return images, bboxes - + images = np.array(images) # 新增:图片列表转为numpy数组(形状:[batch, c, h, w]) + # 新增:框数据保持列表格式(每个元素是不同长度的框数组) + return images, bboxes \ No newline at end of file From 6bb38915bbe277c2be8b9702f7e0b8f3a3bc3710 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Fri, 19 Dec 2025 16:57:19 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.为CSPdarknet.py添加详细的注释 --- .../nets/CSPdarknet.py | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Yolov4_Vehicle_inspection/nets/CSPdarknet.py b/src/Yolov4_Vehicle_inspection/nets/CSPdarknet.py index b6f7965bfc..d9ad967190 100644 --- a/src/Yolov4_Vehicle_inspection/nets/CSPdarknet.py +++ b/src/Yolov4_Vehicle_inspection/nets/CSPdarknet.py @@ -13,6 +13,7 @@ def __init__(self): super(Mish, self).__init__() def forward(self, x): + # 激活函数计算:x * tanh(ln(1 + e^x)),平滑非单调特性提升梯度流动 return x * torch.tanh(F.softplus(x)) #---------------------------------------------------# @@ -23,7 +24,9 @@ class BasicConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1): super(BasicConv, self).__init__() + # 卷积层采用same padding(kernel_size//2),无偏置(BN层替代偏置作用) self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, kernel_size//2, bias=False) + # BN层加速训练收敛,降低过拟合风险 self.bn = nn.BatchNorm2d(out_channels) self.activation = Mish() @@ -43,13 +46,14 @@ def __init__(self, channels, hidden_channels=None): if hidden_channels is None: hidden_channels = channels - + # 残差块内部:1x1卷积降维 -> 3x3卷积恢复维度,减少计算量同时保留特征 self.block = nn.Sequential( BasicConv(channels, hidden_channels, 1), BasicConv(hidden_channels, channels, 3) ) def forward(self, x): + # 残差连接:原始输入 + 卷积输出,避免梯度消失 return x + self.block(x) #--------------------------------------------------------------------# @@ -76,12 +80,12 @@ def __init__(self, in_channels, out_channels, num_blocks, first): #----------------------------------------------------------------# # 主干部分会对num_blocks进行循环,循环内部是残差结构。 #----------------------------------------------------------------# - self.split_conv1 = BasicConv(out_channels, out_channels, 1) + self.split_conv1 = BasicConv(out_channels, out_channels, 1) self.blocks_conv = nn.Sequential( Resblock(channels=out_channels, hidden_channels=out_channels//2), BasicConv(out_channels, out_channels, 1) ) - + # 拼接后通道数翻倍,1x1卷积整合通道维度 self.concat_conv = BasicConv(out_channels*2, out_channels, 1) else: #--------------------------------------------------------------------------# @@ -93,6 +97,7 @@ def __init__(self, in_channels, out_channels, num_blocks, first): # 主干部分会对num_blocks进行循环,循环内部是残差结构。 #----------------------------------------------------------------# self.split_conv1 = BasicConv(out_channels, out_channels//2, 1) + # 批量构建num_blocks个残差块,形成主干特征提取路径 self.blocks_conv = nn.Sequential( *[Resblock(out_channels//2) for _ in range(num_blocks)], BasicConv(out_channels//2, out_channels//2, 1) @@ -101,16 +106,20 @@ def __init__(self, in_channels, out_channels, num_blocks, first): self.concat_conv = BasicConv(out_channels, out_channels, 1) def forward(self, x): + # 下采样:特征图尺寸减半,通道数翻倍 x = self.downsample_conv(x) + # 大残差边路径:直接卷积,不经过残差堆叠 x0 = self.split_conv0(x) + # 主干路径:经过残差块堆叠提取特征 x1 = self.split_conv1(x) x1 = self.blocks_conv(x1) #------------------------------------# # 将大残差边再堆叠回来 #------------------------------------# + # dim=1按通道维度拼接,x1(主干) + x0(大残差边) x = torch.cat([x1, x0], dim=1) #------------------------------------# # 最后对通道数进行整合 @@ -130,6 +139,7 @@ def __init__(self, layers): self.inplanes = 32 # 416,416,3 -> 416,416,32 self.conv1 = BasicConv(3, self.inplanes, kernel_size=3, stride=1) + # 各阶段输出通道数,对应5次下采样的通道维度 self.feature_channels = [64, 128, 256, 512, 1024] self.stages = nn.ModuleList([ @@ -137,15 +147,16 @@ def __init__(self, layers): Resblock_body(self.inplanes, self.feature_channels[0], layers[0], first=True), # 208,208,64 -> 104,104,128 Resblock_body(self.feature_channels[0], self.feature_channels[1], layers[1], first=False), - # 104,104,128 -> 52,52,256 + # 104,104,128 -> 52,52,256 (输出特征层1:检测小目标) Resblock_body(self.feature_channels[1], self.feature_channels[2], layers[2], first=False), - # 52,52,256 -> 26,26,512 + # 52,52,256 -> 26,26,512 (输出特征层2:检测中目标) Resblock_body(self.feature_channels[2], self.feature_channels[3], layers[3], first=False), - # 26,26,512 -> 13,13,1024 + # 26,26,512 -> 13,13,1024(输出特征层3:检测大目标) Resblock_body(self.feature_channels[3], self.feature_channels[4], layers[4], first=False) ]) self.num_features = 1 + # 权重初始化:卷积层使用He正态初始化,BN层默认初始化 for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels @@ -156,21 +167,29 @@ def __init__(self, layers): def forward(self, x): + # 初始卷积层:提取基础特征 x = self.conv1(x) + # 前两个阶段:基础特征下采样 x = self.stages[0](x) x = self.stages[1](x) + # 第三阶段输出:52x52x256(小目标检测特征层) out3 = self.stages[2](x) + # 第四阶段输出:26x26x512(中目标检测特征层) out4 = self.stages[3](out3) + # 第五阶段输出:13x13x1024(大目标检测特征层) out5 = self.stages[4](out4) + # 返回三个不同尺度的特征层,适配YOLO多尺度检测 return out3, out4, out5 def darknet53(pretrained, **kwargs): + # layers=[1,2,8,8,4] 对应CSPDarkNet53的残差块数量配置 model = CSPDarkNet([1, 2, 8, 8, 4]) if pretrained: if isinstance(pretrained, str): + # 加载预训练权重,需保证权重文件与模型结构匹配 model.load_state_dict(torch.load(pretrained)) else: raise Exception("darknet request a pretrained path. got [{}]".format(pretrained)) - return model + return model \ No newline at end of file From 1dc34528a39e79d4654b07a02e2a31d67aa111bb Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Fri, 19 Dec 2025 21:05:37 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.给yolo_training.py添加更详细的注释 --- .../nets/yolo_training.py | 476 ++++++++++-------- 1 file changed, 271 insertions(+), 205 deletions(-) diff --git a/src/Yolov4_Vehicle_inspection/nets/yolo_training.py b/src/Yolov4_Vehicle_inspection/nets/yolo_training.py index e64a79700d..41a49837c1 100644 --- a/src/Yolov4_Vehicle_inspection/nets/yolo_training.py +++ b/src/Yolov4_Vehicle_inspection/nets/yolo_training.py @@ -7,39 +7,50 @@ import torch.nn as nn from matplotlib import pyplot as plt + def jaccard(_box_a, _box_b): + # 转换xywh格式为x1y1x2y2格式(左上角+右下角) b1_x1, b1_x2 = _box_a[:, 0] - _box_a[:, 2] / 2, _box_a[:, 0] + _box_a[:, 2] / 2 b1_y1, b1_y2 = _box_a[:, 1] - _box_a[:, 3] / 2, _box_a[:, 1] + _box_a[:, 3] / 2 b2_x1, b2_x2 = _box_b[:, 0] - _box_b[:, 2] / 2, _box_b[:, 0] + _box_b[:, 2] / 2 b2_y1, b2_y2 = _box_b[:, 1] - _box_b[:, 3] / 2, _box_b[:, 1] + _box_b[:, 3] / 2 + # 构造转换后的框张量 box_a = torch.zeros_like(_box_a) box_b = torch.zeros_like(_box_b) box_a[:, 0], box_a[:, 1], box_a[:, 2], box_a[:, 3] = b1_x1, b1_y1, b1_x2, b1_y2 box_b[:, 0], box_b[:, 1], box_b[:, 2], box_b[:, 3] = b2_x1, b2_y1, b2_x2, b2_y2 + # 获取两个框集合的数量 A = box_a.size(0) B = box_b.size(0) + # 广播计算所有框对的交集右下角(取最小值) max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2), box_b[:, 2:].unsqueeze(0).expand(A, B, 2)) + # 广播计算所有框对的交集左上角(取最大值) min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2), box_b[:, :2].unsqueeze(0).expand(A, B, 2)) + # 计算交集宽高,裁剪负值(无交集时为0) inter = torch.clamp((max_xy - min_xy), min=0) + # 计算交集面积 inter = inter[:, :, 0] * inter[:, :, 1] # 计算先验框和真实框各自的面积 - area_a = ((box_a[:, 2]-box_a[:, 0]) * - (box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter) # [A,B] - area_b = ((box_b[:, 2]-box_b[:, 0]) * - (box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter) # [A,B] + area_a = ((box_a[:, 2] - box_a[:, 0]) * + (box_a[:, 3] - box_a[:, 1])).unsqueeze(1).expand_as(inter) # [A,B] + area_b = ((box_b[:, 2] - box_b[:, 0]) * + (box_b[:, 3] - box_b[:, 1])).unsqueeze(0).expand_as(inter) # [A,B] # 求IOU union = area_a + area_b - inter - return inter / union # [A,B] - -#---------------------------------------------------# + return inter / union # [A,B] 返回所有框对的交并比矩阵 + + +# ---------------------------------------------------# # 平滑标签 -#---------------------------------------------------# -def smooth_labels(y_true, label_smoothing,num_classes): +# ---------------------------------------------------# +def smooth_labels(y_true, label_smoothing, num_classes): + # 标签平滑公式:缓解过拟合,提升模型泛化能力 return y_true * (1.0 - label_smoothing) + label_smoothing / num_classes + def box_ciou(b1, b2): """ 输入为: @@ -54,13 +65,13 @@ def box_ciou(b1, b2): # 求出预测框左上角右下角 b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] - b1_wh_half = b1_wh/2. + b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # 求出真实框左上角右下角 b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] - b2_wh_half = b2_wh/2. + b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half @@ -72,109 +83,122 @@ def box_ciou(b1, b2): b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] union_area = b1_area + b2_area - intersect_area - iou = intersect_area / torch.clamp(union_area,min = 1e-6) + # 防止分母为0,添加极小值裁剪 + iou = intersect_area / torch.clamp(union_area, min=1e-6) - # 计算中心的差距 + # 计算中心的欧氏距离 center_distance = torch.sum(torch.pow((b1_xy - b2_xy), 2), axis=-1) - + # 找到包裹两个框的最小框的左上角和右下角 enclose_mins = torch.min(b1_mins, b2_mins) enclose_maxes = torch.max(b1_maxes, b2_maxes) enclose_wh = torch.max(enclose_maxes - enclose_mins, torch.zeros_like(intersect_maxes)) - # 计算对角线距离 - enclose_diagonal = torch.sum(torch.pow(enclose_wh,2), axis=-1) - ciou = iou - 1.0 * (center_distance) / torch.clamp(enclose_diagonal,min = 1e-6) - - v = (4 / (math.pi ** 2)) * torch.pow((torch.atan(b1_wh[..., 0]/torch.clamp(b1_wh[..., 1],min = 1e-6)) - torch.atan(b2_wh[..., 0]/torch.clamp(b2_wh[..., 1],min = 1e-6))), 2) - alpha = v / torch.clamp((1.0 - iou + v),min=1e-6) + # 计算包裹框的对角线距离 + enclose_diagonal = torch.sum(torch.pow(enclose_wh, 2), axis=-1) + # 计算CIOU的中心距离惩罚项 + ciou = iou - 1.0 * (center_distance) / torch.clamp(enclose_diagonal, min=1e-6) + + # 计算长宽比惩罚项v和权重系数alpha + v = (4 / (math.pi ** 2)) * torch.pow((torch.atan(b1_wh[..., 0] / torch.clamp(b1_wh[..., 1], min=1e-6)) - torch.atan( + b2_wh[..., 0] / torch.clamp(b2_wh[..., 1], min=1e-6))), 2) + alpha = v / torch.clamp((1.0 - iou + v), min=1e-6) + # 最终CIOU:包含IOU、中心距离、长宽比三项惩罚 ciou = ciou - alpha * v return ciou - -def clip_by_tensor(t,t_min,t_max): - t=t.float() + + +def clip_by_tensor(t, t_min, t_max): + # 将张量t的值裁剪到[t_min, t_max]区间,避免后续对数计算出现无穷大 + t = t.float() result = (t >= t_min).float() * t + (t < t_min).float() * t_min result = (result <= t_max).float() * result + (result > t_max).float() * t_max return result -def MSELoss(pred,target): - return (pred-target)**2 -def BCELoss(pred,target): +def MSELoss(pred, target): + # 均方误差损失:用于回归任务(此处为预留接口) + return (pred - target) ** 2 + + +def BCELoss(pred, target): + # 二分类交叉熵损失:用于置信度和类别概率的预测 epsilon = 1e-7 pred = clip_by_tensor(pred, epsilon, 1.0 - epsilon) output = -target * torch.log(pred) - (1.0 - target) * torch.log(1.0 - pred) return output + class YOLOLoss(nn.Module): def __init__(self, anchors, num_classes, img_size, label_smooth=0, cuda=True, normalize=True): super(YOLOLoss, self).__init__() - self.anchors = anchors - self.num_anchors = len(anchors) - self.num_classes = num_classes - self.bbox_attrs = 5 + num_classes - self.img_size = img_size - self.feature_length = [img_size[0]//32,img_size[0]//16,img_size[0]//8] - self.label_smooth = label_smooth - - self.ignore_threshold = 0.5 - self.lambda_conf = 1.0 - self.lambda_cls = 1.0 - self.lambda_loc = 1.0 - self.cuda = cuda - self.normalize = normalize + self.anchors = anchors # 原始先验框尺寸(图像尺度) + self.num_anchors = len(anchors) # 先验框总数 + self.num_classes = num_classes # 目标类别数 + self.bbox_attrs = 5 + num_classes # 每个先验框的预测属性数(xywh+置信度+类别) + self.img_size = img_size # 输入图像尺寸 (w, h) + self.feature_length = [img_size[0] // 32, img_size[0] // 16, img_size[0] // 8] # 三个特征层的尺寸 + self.label_smooth = label_smooth # 标签平滑系数 + + self.ignore_threshold = 0.5 # 负样本忽略阈值 + self.lambda_conf = 1.0 # 置信度损失权重 + self.lambda_cls = 1.0 # 类别损失权重 + self.lambda_loc = 1.0 # 定位损失权重 + self.cuda = cuda # 是否使用GPU加速 + self.normalize = normalize # 是否按正样本数量归一化损失 def forward(self, input, targets=None): - #----------------------------------------------------# + # ----------------------------------------------------# # input的shape为 bs, 3*(5+num_classes), 13, 13 # bs, 3*(5+num_classes), 26, 26 # bs, 3*(5+num_classes), 52, 52 - #----------------------------------------------------# - - #-----------------------# + # ----------------------------------------------------# + + # -----------------------# # 一共多少张图片 - #-----------------------# + # -----------------------# bs = input.size(0) - #-----------------------# + # -----------------------# # 特征层的高 - #-----------------------# + # -----------------------# in_h = input.size(2) - #-----------------------# + # -----------------------# # 特征层的宽 - #-----------------------# + # -----------------------# in_w = input.size(3) - #-----------------------------------------------------------------------# + # -----------------------------------------------------------------------# # 计算步长 # 每一个特征点对应原来的图片上多少个像素点 # 如果特征层为13x13的话,一个特征点就对应原来的图片上的32个像素点 # 如果特征层为26x26的话,一个特征点就对应原来的图片上的16个像素点 # 如果特征层为52x52的话,一个特征点就对应原来的图片上的8个像素点 # stride_h = stride_w = 32、16、8 - #-----------------------------------------------------------------------# + # -----------------------------------------------------------------------# stride_h = self.img_size[1] / in_h stride_w = self.img_size[0] / in_w - - #-------------------------------------------------# + # -------------------------------------------------# # 此时获得的scaled_anchors大小是相对于特征层的 - #-------------------------------------------------# + # -------------------------------------------------# + # 将原始先验框缩放到当前特征层尺度 scaled_anchors = [(a_w / stride_w, a_h / stride_h) for a_w, a_h in self.anchors] - - #-----------------------------------------------# + + # -----------------------------------------------# # 输入的input一共有三个,他们的shape分别是 # batch_size, 3, 13, 13, 5 + num_classes # batch_size, 3, 26, 26, 5 + num_classes # batch_size, 3, 52, 52, 5 + num_classes - #-----------------------------------------------# - prediction = input.view(bs, int(self.num_anchors/3), + # -----------------------------------------------# + # 调整输入张量形状,便于逐先验框计算损失 + prediction = input.view(bs, int(self.num_anchors / 3), self.bbox_attrs, in_h, in_w).permute(0, 1, 3, 4, 2).contiguous() - - # 获得置信度,是否有物体 + + # 获得置信度,是否有物体(sigmoid激活转为0-1区间) conf = torch.sigmoid(prediction[..., 4]) - # 种类置信度 + # 种类置信度(sigmoid激活转为0-1区间) pred_cls = torch.sigmoid(prediction[..., 5:]) - #---------------------------------------------------------------# + # ---------------------------------------------------------------# # 找到哪些先验框内部包含物体 # 利用真实框和先验框计算交并比 # mask batch_size, 3, in_h, in_w 有目标的特征点 @@ -182,250 +206,281 @@ def forward(self, input, targets=None): # t_box batch_size, 3, in_h, in_w, 4 中心宽高的真实值 # tconf batch_size, 3, in_h, in_w 置信度真实值 # tcls batch_size, 3, in_h, in_w, num_classes 种类真实值 - #----------------------------------------------------------------# - mask, noobj_mask, t_box, tconf, tcls, box_loss_scale_x, box_loss_scale_y = self.get_target(targets, scaled_anchors,in_w, in_h,self.ignore_threshold) - - #---------------------------------------------------------------# + # ----------------------------------------------------------------# + # 生成损失计算所需的真实标签和掩码 + mask, noobj_mask, t_box, tconf, tcls, box_loss_scale_x, box_loss_scale_y = self.get_target(targets, + scaled_anchors, in_w, + in_h, + self.ignore_threshold) + + # ---------------------------------------------------------------# # 将预测结果进行解码,判断预测结果和真实值的重合程度 # 如果重合程度过大则忽略,因为这些特征点属于预测比较准确的特征点 # 作为负样本不合适 - #----------------------------------------------------------------# + # ----------------------------------------------------------------# + # 更新无目标掩码,忽略IOU过高的负样本 noobj_mask, pred_boxes_for_ciou = self.get_ignore(prediction, targets, scaled_anchors, in_w, in_h, noobj_mask) + # 将所有张量移至GPU(若启用) if self.cuda: mask, noobj_mask = mask.cuda(), noobj_mask.cuda() - box_loss_scale_x, box_loss_scale_y= box_loss_scale_x.cuda(), box_loss_scale_y.cuda() + box_loss_scale_x, box_loss_scale_y = box_loss_scale_x.cuda(), box_loss_scale_y.cuda() tconf, tcls = tconf.cuda(), tcls.cuda() pred_boxes_for_ciou = pred_boxes_for_ciou.cuda() t_box = t_box.cuda() + # 计算框损失缩放系数:小目标权重更大,大目标权重更小 box_loss_scale = 2 - box_loss_scale_x * box_loss_scale_y - #---------------------------------------------------------------# + # ---------------------------------------------------------------# # 计算预测结果和真实结果的CIOU - #----------------------------------------------------------------# - ciou = (1 - box_ciou( pred_boxes_for_ciou[mask.bool()], t_box[mask.bool()]))* box_loss_scale[mask.bool()] + # ----------------------------------------------------------------# + # 仅对有目标的先验框计算定位损失,并乘以缩放系数 + ciou = (1 - box_ciou(pred_boxes_for_ciou[mask.bool()], t_box[mask.bool()])) * box_loss_scale[mask.bool()] loss_loc = torch.sum(ciou) - # 计算置信度的loss + # 计算置信度的loss:分有目标和无目标两部分 loss_conf = torch.sum(BCELoss(conf, mask) * mask) + \ torch.sum(BCELoss(conf, mask) * noobj_mask) - - loss_cls = torch.sum(BCELoss(pred_cls[mask == 1], smooth_labels(tcls[mask == 1],self.label_smooth,self.num_classes))) - + + # 计算类别损失:仅对有目标的先验框计算,使用平滑标签 + loss_cls = torch.sum( + BCELoss(pred_cls[mask == 1], smooth_labels(tcls[mask == 1], self.label_smooth, self.num_classes))) + + # 总损失:加权求和三个部分的损失 loss = loss_conf * self.lambda_conf + loss_cls * self.lambda_cls + loss_loc * self.lambda_loc + # 损失归一化:避免批次正样本数量差异影响训练 if self.normalize: num_pos = torch.sum(mask) - num_pos = torch.max(num_pos, torch.ones_like(num_pos)) + num_pos = torch.max(num_pos, torch.ones_like(num_pos)) # 防止除以0 else: - num_pos = bs/3 + num_pos = bs / 3 - return loss, num_pos + return loss, num_pos # 返回总损失和归一化系数 def get_target(self, target, anchors, in_w, in_h, ignore_threshold): - #-----------------------------------------------------# + # -----------------------------------------------------# # 计算一共有多少张图片 - #-----------------------------------------------------# + # -----------------------------------------------------# bs = len(target) - #-------------------------------------------------------# + # -------------------------------------------------------# # 获得当前特征层先验框所属的编号,方便后面对先验框筛选 - #-------------------------------------------------------# - anchor_index = [[0,1,2],[3,4,5],[6,7,8]][self.feature_length.index(in_w)] - subtract_index = [0,3,6][self.feature_length.index(in_w)] - #-------------------------------------------------------# + # -------------------------------------------------------# + # 匹配当前特征层对应的先验框组(3个一组) + anchor_index = [[0, 1, 2], [3, 4, 5], [6, 7, 8]][self.feature_length.index(in_w)] + # 先验框组的偏移量,用于转换为组内索引 + subtract_index = [0, 3, 6][self.feature_length.index(in_w)] + # -------------------------------------------------------# # 创建全是0或者全是1的阵列 - #-------------------------------------------------------# - mask = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - noobj_mask = torch.ones(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - - tx = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - ty = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - tw = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - th = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - t_box = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, 4, requires_grad=False) - tconf = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - tcls = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, self.num_classes, requires_grad=False) - - box_loss_scale_x = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) - box_loss_scale_y = torch.zeros(bs, int(self.num_anchors/3), in_h, in_w, requires_grad=False) + # -------------------------------------------------------# + mask = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) # 有目标掩码 + noobj_mask = torch.ones(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) # 无目标掩码 + + # 真实框中心x/y、宽高w/h的标签(特征层尺度) + tx = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) + ty = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) + tw = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) + th = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) + t_box = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, 4, requires_grad=False) # 整合后的真实框 + tconf = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) # 置信度真实标签 + tcls = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, self.num_classes, requires_grad=False) # 类别真实标签 + + # 框损失缩放系数(原始图像尺度的宽高比) + box_loss_scale_x = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) + box_loss_scale_y = torch.zeros(bs, int(self.num_anchors / 3), in_h, in_w, requires_grad=False) + # 遍历每个批次的图像 for b in range(bs): - if len(target[b])==0: + if len(target[b]) == 0: # 该图像无目标,跳过 continue - #-------------------------------------------------------# + # -------------------------------------------------------# # 计算出正样本在特征层上的中心点 - #-------------------------------------------------------# - gxs = target[b][:, 0:1] * in_w - gys = target[b][:, 1:2] * in_h - - #-------------------------------------------------------# + # -------------------------------------------------------# + gxs = target[b][:, 0:1] * in_w # 真实框x中心(特征层尺度) + gys = target[b][:, 1:2] * in_h # 真实框y中心(特征层尺度) + + # -------------------------------------------------------# # 计算出正样本相对于特征层的宽高 - #-------------------------------------------------------# - gws = target[b][:, 2:3] * in_w - ghs = target[b][:, 3:4] * in_h + # -------------------------------------------------------# + gws = target[b][:, 2:3] * in_w # 真实框宽度(特征层尺度) + ghs = target[b][:, 3:4] * in_h # 真实框高度(特征层尺度) - #-------------------------------------------------------# + # -------------------------------------------------------# # 计算出正样本属于特征层的哪个特征点 - #-------------------------------------------------------# - gis = torch.floor(gxs) - gjs = torch.floor(gys) - - #-------------------------------------------------------# + # -------------------------------------------------------# + gis = torch.floor(gxs) # 真实框中心对应的特征点x坐标 + gjs = torch.floor(gys) # 真实框中心对应的特征点y坐标 + + # -------------------------------------------------------# # 将真实框转换一个形式 # num_true_box, 4 - #-------------------------------------------------------# + # -------------------------------------------------------# + # 构造(0,0,gw,gh)格式的真实框,用于计算与先验框的IOU gt_box = torch.FloatTensor(torch.cat([torch.zeros_like(gws), torch.zeros_like(ghs), gws, ghs], 1)) - - #-------------------------------------------------------# + + # -------------------------------------------------------# # 将先验框转换一个形式 # 9, 4 - #-------------------------------------------------------# - anchor_shapes = torch.FloatTensor(torch.cat((torch.zeros((self.num_anchors, 2)), torch.FloatTensor(anchors)), 1)) - #-------------------------------------------------------# + # -------------------------------------------------------# + # 构造(0,0,aw,ah)格式的先验框,用于计算与真实框的IOU + anchor_shapes = torch.FloatTensor( + torch.cat((torch.zeros((self.num_anchors, 2)), torch.FloatTensor(anchors)), 1)) + # -------------------------------------------------------# # 计算交并比 # num_true_box, 9 - #-------------------------------------------------------# - anch_ious = jaccard(gt_box, anchor_shapes) + # -------------------------------------------------------# + anch_ious = jaccard(gt_box, anchor_shapes) # 每个真实框与9个先验框的IOU - #-------------------------------------------------------# + # -------------------------------------------------------# # 计算重合度最大的先验框是哪个 - # num_true_box, - #-------------------------------------------------------# - best_ns = torch.argmax(anch_ious,dim=-1) + # num_true_box, + # -------------------------------------------------------# + best_ns = torch.argmax(anch_ious, dim=-1) # 每个真实框匹配的最佳先验框索引 + # 遍历每个真实框 for i, best_n in enumerate(best_ns): - if best_n not in anchor_index: + if best_n not in anchor_index: # 最佳先验框不属于当前特征层,跳过 continue - #-------------------------------------------------------------# + # -------------------------------------------------------------# # 取出各类坐标: # gi和gj代表的是真实框对应的特征点的x轴y轴坐标 # gx和gy代表真实框的x轴和y轴坐标 # gw和gh代表真实框的宽和高 - #-------------------------------------------------------------# + # -------------------------------------------------------------# gi = gis[i].long() gj = gjs[i].long() gx = gxs[i] gy = gys[i] gw = gws[i] gh = ghs[i] - if (gj < in_h) and (gi < in_w): - best_n = best_n - subtract_index - #----------------------------------------# + if (gj < in_h) and (gi < in_w): # 确保特征点坐标在特征层范围内 + best_n = best_n - subtract_index # 转换为组内索引(0-2) + # ----------------------------------------# # noobj_mask代表无目标的特征点 - #----------------------------------------# - noobj_mask[b, best_n, gj, gi] = 0 - #----------------------------------------# + # ----------------------------------------# + noobj_mask[b, best_n, gj, gi] = 0 # 取消无目标标记 + # ----------------------------------------# # mask代表有目标的特征点 - #----------------------------------------# - mask[b, best_n, gj, gi] = 1 - #----------------------------------------# + # ----------------------------------------# + mask[b, best_n, gj, gi] = 1 # 标记为有目标 + # ----------------------------------------# # tx、ty代表中心的真实值 - #----------------------------------------# + # ----------------------------------------# tx[b, best_n, gj, gi] = gx ty[b, best_n, gj, gi] = gy - #----------------------------------------# + # ----------------------------------------# # tw、th代表宽高的真实值 - #----------------------------------------# + # ----------------------------------------# tw[b, best_n, gj, gi] = gw th[b, best_n, gj, gi] = gh - #----------------------------------------# + # ----------------------------------------# # 用于获得xywh的比例 # 大目标loss权重小,小目标loss权重大 - #----------------------------------------# + # ----------------------------------------# box_loss_scale_x[b, best_n, gj, gi] = target[b][i, 2] box_loss_scale_y[b, best_n, gj, gi] = target[b][i, 3] - #----------------------------------------# + # ----------------------------------------# # tconf代表物体置信度 - #----------------------------------------# - tconf[b, best_n, gj, gi] = 1 - #----------------------------------------# + # ----------------------------------------# + tconf[b, best_n, gj, gi] = 1 # 置信度真实值为1 + # ----------------------------------------# # tcls代表种类置信度 - #----------------------------------------# - tcls[b, best_n, gj, gi, target[b][i, 4].long()] = 1 + # ----------------------------------------# + tcls[b, best_n, gj, gi, target[b][i, 4].long()] = 1 # 对应类别真实值为1 else: print('Step {0} out of bound'.format(b)) print('gj: {0}, height: {1} | gi: {2}, width: {3}'.format(gj, in_h, gi, in_w)) continue - t_box[...,0] = tx - t_box[...,1] = ty - t_box[...,2] = tw - t_box[...,3] = th + # 整合真实框的xywh + t_box[..., 0] = tx + t_box[..., 1] = ty + t_box[..., 2] = tw + t_box[..., 3] = th return mask, noobj_mask, t_box, tconf, tcls, box_loss_scale_x, box_loss_scale_y - - def get_ignore(self,prediction,target,scaled_anchors,in_w, in_h,noobj_mask): - #-----------------------------------------------------# + def get_ignore(self, prediction, target, scaled_anchors, in_w, in_h, noobj_mask): + # -----------------------------------------------------# # 计算一共有多少张图片 - #-----------------------------------------------------# + # -----------------------------------------------------# bs = len(target) - #-------------------------------------------------------# + # -------------------------------------------------------# # 获得当前特征层先验框所属的编号,方便后面对先验框筛选 - #-------------------------------------------------------# - anchor_index = [[0,1,2],[3,4,5],[6,7,8]][self.feature_length.index(in_w)] - scaled_anchors = np.array(scaled_anchors)[anchor_index] - - # 先验框的中心位置的调整参数 - x = torch.sigmoid(prediction[..., 0]) + # -------------------------------------------------------# + # 获取当前特征层对应的先验框组 + anchor_index = [[0, 1, 2], [3, 4, 5], [6, 7, 8]][self.feature_length.index(in_w)] + scaled_anchors = np.array(scaled_anchors)[anchor_index] # 当前特征层的先验框 + + # 先验框的中心位置的调整参数(sigmoid激活限制在0-1之间) + x = torch.sigmoid(prediction[..., 0]) y = torch.sigmoid(prediction[..., 1]) - # 先验框的宽高调整参数 + # 先验框的宽高调整参数(未激活,后续用exp转换为正数) w = prediction[..., 2] # Width h = prediction[..., 3] # Height + # 根据设备类型选择张量类型 FloatTensor = torch.cuda.FloatTensor if x.is_cuda else torch.FloatTensor LongTensor = torch.cuda.LongTensor if x.is_cuda else torch.LongTensor # 生成网格,先验框中心,网格左上角 + # 生成特征层网格的x坐标 grid_x = torch.linspace(0, in_w - 1, in_w).repeat(in_h, 1).repeat( - int(bs*self.num_anchors/3), 1, 1).view(x.shape).type(FloatTensor) + int(bs * self.num_anchors / 3), 1, 1).view(x.shape).type(FloatTensor) + # 生成特征层网格的y坐标 grid_y = torch.linspace(0, in_h - 1, in_h).repeat(in_w, 1).t().repeat( - int(bs*self.num_anchors/3), 1, 1).view(y.shape).type(FloatTensor) + int(bs * self.num_anchors / 3), 1, 1).view(y.shape).type(FloatTensor) # 生成先验框的宽高 anchor_w = FloatTensor(scaled_anchors).index_select(1, LongTensor([0])) anchor_h = FloatTensor(scaled_anchors).index_select(1, LongTensor([1])) - + + # 调整先验框宽高的形状,匹配预测结果 anchor_w = anchor_w.repeat(bs, 1).repeat(1, 1, in_h * in_w).view(w.shape) anchor_h = anchor_h.repeat(bs, 1).repeat(1, 1, in_h * in_w).view(h.shape) - - #-------------------------------------------------------# + + # -------------------------------------------------------# # 计算调整后的先验框中心与宽高 - #-------------------------------------------------------# + # -------------------------------------------------------# + # 解码预测框,得到特征层尺度的xywh pred_boxes = FloatTensor(prediction[..., :4].shape) - pred_boxes[..., 0] = x + grid_x - pred_boxes[..., 1] = y + grid_y - pred_boxes[..., 2] = torch.exp(w) * anchor_w - pred_boxes[..., 3] = torch.exp(h) * anchor_h + pred_boxes[..., 0] = x + grid_x # 预测框x中心 + pred_boxes[..., 1] = y + grid_y # 预测框y中心 + pred_boxes[..., 2] = torch.exp(w) * anchor_w # 预测框宽度 + pred_boxes[..., 3] = torch.exp(h) * anchor_h # 预测框高度 + # 遍历每个批次的图像 for i in range(bs): pred_boxes_for_ignore = pred_boxes[i] - #-------------------------------------------------------# + # -------------------------------------------------------# # 将预测结果转换一个形式 # pred_boxes_for_ignore num_anchors, 4 - #-------------------------------------------------------# - pred_boxes_for_ignore = pred_boxes_for_ignore.view(-1, 4) - #-------------------------------------------------------# + # -------------------------------------------------------# + pred_boxes_for_ignore = pred_boxes_for_ignore.view(-1, 4) # 展平为先验框数量x4 + # -------------------------------------------------------# # 计算真实框,并把真实框转换成相对于特征层的大小 # gt_box num_true_box, 4 - #-------------------------------------------------------# - if len(target[i]) > 0: + # -------------------------------------------------------# + if len(target[i]) > 0: # 该图像有目标 gx = target[i][:, 0:1] * in_w gy = target[i][:, 1:2] * in_h gw = target[i][:, 2:3] * in_w gh = target[i][:, 3:4] * in_h - gt_box = torch.FloatTensor(torch.cat([gx, gy, gw, gh],-1)).type(FloatTensor) + gt_box = torch.FloatTensor(torch.cat([gx, gy, gw, gh], -1)).type(FloatTensor) - #-------------------------------------------------------# + # -------------------------------------------------------# # 计算交并比 # anch_ious num_true_box, num_anchors - #-------------------------------------------------------# + # -------------------------------------------------------# anch_ious = jaccard(gt_box, pred_boxes_for_ignore) - #-------------------------------------------------------# + # -------------------------------------------------------# # 每个先验框对应真实框的最大重合度 # anch_ious_max num_anchors - #-------------------------------------------------------# - anch_ious_max, _ = torch.max(anch_ious,dim=0) + # -------------------------------------------------------# + anch_ious_max, _ = torch.max(anch_ious, dim=0) anch_ious_max = anch_ious_max.view(pred_boxes[i].size()[:3]) - noobj_mask[i][anch_ious_max>self.ignore_threshold] = 0 + # 忽略IOU大于阈值的负样本 + noobj_mask[i][anch_ious_max > self.ignore_threshold] = 0 return noobj_mask, pred_boxes + def weights_init(net, init_type='normal', init_gain=0.02): + # 网络权重初始化函数:初始化卷积层和批归一化层 def init_func(m): classname = m.__class__.__name__ if hasattr(m, 'weight') and classname.find('Conv') != -1: @@ -442,47 +497,57 @@ def init_func(m): elif classname.find('BatchNorm2d') != -1: torch.nn.init.normal_(m.weight.data, 1.0, 0.02) torch.nn.init.constant_(m.bias.data, 0.0) + print('initialize network with %s type' % init_type) - net.apply(init_func) + net.apply(init_func) # 递归应用初始化函数到网络所有层 + class LossHistory(): def __init__(self, log_dir): + # 损失历史记录类:用于保存和可视化训练/验证损失 import datetime curr_time = datetime.datetime.now() - time_str = datetime.datetime.strftime(curr_time,'%Y_%m_%d_%H_%M_%S') - self.log_dir = log_dir - self.time_str = time_str - self.save_path = os.path.join(self.log_dir, "loss_" + str(self.time_str)) - self.losses = [] - self.val_loss = [] - - os.makedirs(self.save_path) + time_str = datetime.datetime.strftime(curr_time, '%Y_%m_%d_%H_%M_%S') + self.log_dir = log_dir + self.time_str = time_str + self.save_path = os.path.join(self.log_dir, "loss_" + str(self.time_str)) + self.losses = [] # 训练损失列表 + self.val_loss = [] # 验证损失列表 + + os.makedirs(self.save_path) # 创建损失保存目录 def append_loss(self, loss, val_loss): + # 添加单次epoch的损失,并保存到文件和绘制图像 self.losses.append(loss) self.val_loss.append(val_loss) + # 保存训练损失 with open(os.path.join(self.save_path, "epoch_loss_" + str(self.time_str) + ".txt"), 'a') as f: f.write(str(loss)) f.write("\n") + # 保存验证损失 with open(os.path.join(self.save_path, "epoch_val_loss_" + str(self.time_str) + ".txt"), 'a') as f: f.write(str(val_loss)) f.write("\n") - self.loss_plot() + self.loss_plot() # 绘制损失曲线 def loss_plot(self): + # 绘制训练/验证损失曲线 iters = range(len(self.losses)) plt.figure() - plt.plot(iters, self.losses, 'red', linewidth = 2, label='train loss') - plt.plot(iters, self.val_loss, 'coral', linewidth = 2, label='val loss') + plt.plot(iters, self.losses, 'red', linewidth=2, label='train loss') + plt.plot(iters, self.val_loss, 'coral', linewidth=2, label='val loss') try: if len(self.losses) < 25: num = 5 else: num = 15 - - plt.plot(iters, scipy.signal.savgol_filter(self.losses, num, 3), 'green', linestyle = '--', linewidth = 2, label='smooth train loss') - plt.plot(iters, scipy.signal.savgol_filter(self.val_loss, num, 3), '#8B4513', linestyle = '--', linewidth = 2, label='smooth val loss') + + # 绘制平滑后的损失曲线,便于观察趋势 + plt.plot(iters, scipy.signal.savgol_filter(self.losses, num, 3), 'green', linestyle='--', linewidth=2, + label='smooth train loss') + plt.plot(iters, scipy.signal.savgol_filter(self.val_loss, num, 3), '#8B4513', linestyle='--', linewidth=2, + label='smooth val loss') except: pass @@ -491,4 +556,5 @@ def loss_plot(self): plt.ylabel('Loss') plt.legend(loc="upper right") - plt.savefig(os.path.join(self.save_path, "epoch_loss_" + str(self.time_str) + ".png")) + # 保存损失图像 + plt.savefig(os.path.join(self.save_path, "epoch_loss_" + str(self.time_str) + ".png")) \ No newline at end of file From c70211b9a5cfa29f96b328072b93cec7994bfbf2 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Fri, 19 Dec 2025 21:31:44 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86get=5Fgt=5Ftxt?= =?UTF-8?q?.py=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.该文件将 VOC 格式的 XML 标注文件,批量转换为用于目标检测模型评估(计算 mAP)的「ground-truth」(真实标签)文本文件。 --- src/Yolov4_Vehicle_inspection/get_gt_txt.py | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/Yolov4_Vehicle_inspection/get_gt_txt.py diff --git a/src/Yolov4_Vehicle_inspection/get_gt_txt.py b/src/Yolov4_Vehicle_inspection/get_gt_txt.py new file mode 100644 index 0000000000..ac85cc6b04 --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/get_gt_txt.py @@ -0,0 +1,63 @@ +#----------------------------------------------------# +# 获取测试集的ground-truth +# 具体视频教程可查看 +# https://www.bilibili.com/video/BV1zE411u7Vw +#----------------------------------------------------# +import sys +import os +import glob +import xml.etree.ElementTree as ET + +''' +!!!!!!!!!!!!!注意事项!!!!!!!!!!!!! +# 这一部分是当xml有无关的类的时候,下方有代码可以进行筛选! +''' +#---------------------------------------------------# +# 获得类 +#---------------------------------------------------# +def get_classes(classes_path): + '''loads the classes''' + with open(classes_path) as f: + class_names = f.readlines() + class_names = [c.strip() for c in class_names] + return class_names + +image_ids = open('VOCdevkit/VOC2007/ImageSets/Main/test.txt').read().strip().split() + +if not os.path.exists("./input"): + os.makedirs("./input") +if not os.path.exists("./input/ground-truth"): + os.makedirs("./input/ground-truth") + +for image_id in image_ids: + with open("./input/ground-truth/"+image_id+".txt", "w") as new_f: + root = ET.parse("VOCdevkit/VOC2007/Annotations/"+image_id+".xml").getroot() + for obj in root.findall('object'): + difficult_flag = False + if obj.find('difficult')!=None: + difficult = obj.find('difficult').text + if int(difficult)==1: + difficult_flag = True + obj_name = obj.find('name').text + ''' + !!!!!!!!!!!!注意事项!!!!!!!!!!!! + # 这一部分是当xml有无关的类的时候,可以取消下面代码的注释 + # 利用对应的classes.txt来进行筛选!!!!!!!!!!!! + ''' + # classes_path = 'model_data/voc_classes.txt' + # class_names = get_classes(classes_path) + # if obj_name not in class_names: + # continue + + bndbox = obj.find('bndbox') + left = bndbox.find('xmin').text + top = bndbox.find('ymin').text + right = bndbox.find('xmax').text + bottom = bndbox.find('ymax').text + + if difficult_flag: + new_f.write("%s %s %s %s %s difficult\n" % (obj_name, left, top, right, bottom)) + else: + new_f.write("%s %s %s %s %s\n" % (obj_name, left, top, right, bottom)) + +print("Conversion completed!") From fb628de6efac2d482ba259bee862f22b5fac4bfb Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Fri, 19 Dec 2025 22:39:37 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0get=5Fdr=5Ftxt.py?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.该文件的作用:使用 YOLO 目标检测模型,对 VOC 测试集的图片进行批量检测,生成用于计算 mAP(平均精度均值)的「detection-results」(模型预测结果)文本文件 --- src/Yolov4_Vehicle_inspection/get_dr_txt.py | 137 ++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/Yolov4_Vehicle_inspection/get_dr_txt.py diff --git a/src/Yolov4_Vehicle_inspection/get_dr_txt.py b/src/Yolov4_Vehicle_inspection/get_dr_txt.py new file mode 100644 index 0000000000..a36f5e1573 --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/get_dr_txt.py @@ -0,0 +1,137 @@ +#-------------------------------------# +# mAP所需文件计算代码 +# 具体教程请查看Bilibili +# Bubbliiiing +#-------------------------------------# +import os + +import numpy as np +import torch +from PIL import Image +from tqdm import tqdm + +from utils.utils import letterbox_image, non_max_suppression, yolo_correct_boxes +from yolo import YOLO + + +''' +这里设置的门限值较低是因为计算map需要用到不同门限条件下的Recall和Precision值。 +所以只有保留的框足够多,计算的map才会更精确,详情可以了解map的原理。 +计算map时输出的Recall和Precision值指的是门限为0.5时的Recall和Precision值。 + +此处获得的./input/detection-results/里面的txt的框的数量会比直接predict多一些,这是因为这里的门限低, +目的是为了计算不同门限条件下的Recall和Precision值,从而实现map的计算。 + +这里的self.iou指的是非极大抑制所用到的iou,具体的可以了解非极大抑制的原理, +如果低分框与高分框的iou大于这里设定的self.iou,那么该低分框将会被剔除。 + +可能有些同学知道有0.5和0.5:0.95的mAP,这里的self.iou=0.5不代表mAP0.5。 +如果想要设定mAP0.x,比如设定mAP0.75,可以去get_map.py设定MINOVERLAP。 +''' +class mAP_Yolo(YOLO): + #---------------------------------------------------# + # 检测图片 + #---------------------------------------------------# + def detect_image(self,image_id,image): + self.confidence = 0.01 + self.iou = 0.5 + f = open("./input/detection-results/"+image_id+".txt","w") + image_shape = np.array(np.shape(image)[0:2]) + + #---------------------------------------------------------# + # 给图像增加灰条,实现不失真的resize + # 也可以直接resize进行识别 + #---------------------------------------------------------# + if self.letterbox_image: + crop_img = np.array(letterbox_image(image, (self.model_image_size[1],self.model_image_size[0]))) + else: + crop_img = image.convert('RGB') + crop_img = crop_img.resize((self.model_image_size[1],self.model_image_size[0]), Image.BICUBIC) + photo = np.array(crop_img,dtype = np.float32) / 255.0 + photo = np.transpose(photo, (2, 0, 1)) + #---------------------------------------------------------# + # 添加上batch_size维度 + #---------------------------------------------------------# + images = [photo] + + with torch.no_grad(): + images = torch.from_numpy(np.asarray(images)) + if self.cuda: + images = images.cuda() + + #---------------------------------------------------------# + # 将图像输入网络当中进行预测! + #---------------------------------------------------------# + outputs = self.net(images) + output_list = [] + for i in range(3): + output_list.append(self.yolo_decodes[i](outputs[i])) + + #---------------------------------------------------------# + # 将预测框进行堆叠,然后进行非极大抑制 + #---------------------------------------------------------# + output = torch.cat(output_list, 1) + batch_detections = non_max_suppression(output, len(self.class_names), + conf_thres=self.confidence, + nms_thres=self.iou) + + #---------------------------------------------------------# + # 如果没有检测出物体,返回原图 + #---------------------------------------------------------# + try: + batch_detections = batch_detections[0].cpu().numpy() + except: + return + + #---------------------------------------------------------# + # 对预测框进行得分筛选 + #---------------------------------------------------------# + top_index = batch_detections[:,4] * batch_detections[:,5] > self.confidence + top_conf = batch_detections[top_index,4]*batch_detections[top_index,5] + top_label = np.array(batch_detections[top_index,-1],np.int32) + top_bboxes = np.array(batch_detections[top_index,:4]) + top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims(top_bboxes[:,0],-1),np.expand_dims(top_bboxes[:,1],-1),np.expand_dims(top_bboxes[:,2],-1),np.expand_dims(top_bboxes[:,3],-1) + + #-----------------------------------------------------------------# + # 在图像传入网络预测前会进行letterbox_image给图像周围添加灰条 + # 因此生成的top_bboxes是相对于有灰条的图像的 + # 我们需要对其进行修改,去除灰条的部分。 + #-----------------------------------------------------------------# + if self.letterbox_image: + boxes = yolo_correct_boxes(top_ymin,top_xmin,top_ymax,top_xmax,np.array([self.model_image_size[0],self.model_image_size[1]]),image_shape) + else: + top_xmin = top_xmin / self.model_image_size[1] * image_shape[1] + top_ymin = top_ymin / self.model_image_size[0] * image_shape[0] + top_xmax = top_xmax / self.model_image_size[1] * image_shape[1] + top_ymax = top_ymax / self.model_image_size[0] * image_shape[0] + boxes = np.concatenate([top_ymin,top_xmin,top_ymax,top_xmax], axis=-1) + + for i, c in enumerate(top_label): + predicted_class = self.class_names[c] + score = str(top_conf[i]) + + top, left, bottom, right = boxes[i] + f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)),str(int(bottom)))) + + f.close() + return + +yolo = mAP_Yolo() +image_ids = open('VOCdevkit/VOC2007/ImageSets/Main/test.txt').read().strip().split() + +if not os.path.exists("./input"): + os.makedirs("./input") +if not os.path.exists("./input/detection-results"): + os.makedirs("./input/detection-results") +if not os.path.exists("./input/images-optional"): + os.makedirs("./input/images-optional") + + +for image_id in tqdm(image_ids): + image_path = "./VOCdevkit/VOC2007/JPEGImages/"+image_id+".jpg" + image = Image.open(image_path) + # 开启后在之后计算mAP可以可视化 + # image.save("./input/images-optional/"+image_id+".jpg") + yolo.detect_image(image_id,image) + +print("Conversion completed!") From c8d2f6293bdf10264ef3eaa57bc7371457a4d432 Mon Sep 17 00:00:00 2001 From: aasdar <3415807009@qq.com> Date: Sat, 20 Dec 2025 14:42:16 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86get=5Fmap.py?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.该代码文件作用:目标检测任务中用于计算 mAP(平均精度均值,mean Average Precision)的评估工具 --- src/Yolov4_Vehicle_inspection/get_map.py | 901 +++++++++++++++++++++++ 1 file changed, 901 insertions(+) create mode 100644 src/Yolov4_Vehicle_inspection/get_map.py diff --git a/src/Yolov4_Vehicle_inspection/get_map.py b/src/Yolov4_Vehicle_inspection/get_map.py new file mode 100644 index 0000000000..e4b58092f4 --- /dev/null +++ b/src/Yolov4_Vehicle_inspection/get_map.py @@ -0,0 +1,901 @@ +import glob +import json +import os +import shutil +import operator +import sys +import argparse +import math + +import numpy as np + +''' +用于计算mAP +代码克隆自https://github.com/Cartucho/mAP +如果想要设定mAP0.x,比如计算mAP0.75,可以设定MINOVERLAP = 0.75。 +''' +MINOVERLAP = 0.5 + +parser = argparse.ArgumentParser() +parser.add_argument('-na', '--no-animation', help="no animation is shown.", action="store_true") +parser.add_argument('-np', '--no-plot', help="no plot is shown.", action="store_true") +parser.add_argument('-q', '--quiet', help="minimalistic console output.", action="store_true") +parser.add_argument('-i', '--ignore', nargs='+', type=str, help="ignore a list of classes.") +parser.add_argument('--set-class-iou', nargs='+', type=str, help="set IoU for a specific class.") +args = parser.parse_args() + +''' + 0,0 ------> x (width) + | + | (Left,Top) + | *_________ + | | | + | | + y |_________| + (height) * + (Right,Bottom) +''' + +if args.ignore is None: + args.ignore = [] + +specific_iou_flagged = False +if args.set_class_iou is not None: + specific_iou_flagged = True + +os.chdir(os.path.dirname(os.path.abspath(__file__))) + +GT_PATH = os.path.join(os.getcwd(), 'input', 'ground-truth') +DR_PATH = os.path.join(os.getcwd(), 'input', 'detection-results') +IMG_PATH = os.path.join(os.getcwd(), 'input', 'images-optional') +if os.path.exists(IMG_PATH): + for dirpath, dirnames, files in os.walk(IMG_PATH): + if not files: + args.no_animation = True +else: + args.no_animation = True + +show_animation = False +if not args.no_animation: + try: + import cv2 + show_animation = True + except ImportError: + print("\"opencv-python\" not found, please install to visualize the results.") + args.no_animation = True + +draw_plot = False +if not args.no_plot: + try: + import matplotlib.pyplot as plt + draw_plot = True + except ImportError: + print("\"matplotlib\" not found, please install it to get the resulting plots.") + args.no_plot = True + + +def log_average_miss_rate(precision, fp_cumsum, num_images): + """ + log-average miss rate: + Calculated by averaging miss rates at 9 evenly spaced FPPI points + between 10e-2 and 10e0, in log-space. + + output: + lamr | log-average miss rate + mr | miss rate + fppi | false positives per image + + references: + [1] Dollar, Piotr, et al. "Pedestrian Detection: An Evaluation of the + State of the Art." Pattern Analysis and Machine Intelligence, IEEE + Transactions on 34.4 (2012): 743 - 761. + """ + + if precision.size == 0: + lamr = 0 + mr = 1 + fppi = 0 + return lamr, mr, fppi + + fppi = fp_cumsum / float(num_images) + mr = (1 - precision) + + fppi_tmp = np.insert(fppi, 0, -1.0) + mr_tmp = np.insert(mr, 0, 1.0) + + ref = np.logspace(-2.0, 0.0, num = 9) + for i, ref_i in enumerate(ref): + j = np.where(fppi_tmp <= ref_i)[-1][-1] + ref[i] = mr_tmp[j] + + lamr = math.exp(np.mean(np.log(np.maximum(1e-10, ref)))) + + return lamr, mr, fppi + +""" + throw error and exit +""" +def error(msg): + print(msg) + sys.exit(0) + +""" + check if the number is a float between 0.0 and 1.0 +""" +def is_float_between_0_and_1(value): + try: + val = float(value) + if val > 0.0 and val < 1.0: + return True + else: + return False + except ValueError: + return False + +""" + Calculate the AP given the recall and precision array + 1st) We compute a version of the measured precision/recall curve with + precision monotonically decreasing + 2nd) We compute the AP as the area under this curve by numerical integration. +""" +def voc_ap(rec, prec): + """ + --- Official matlab code VOC2012--- + mrec=[0 ; rec ; 1]; + mpre=[0 ; prec ; 0]; + for i=numel(mpre)-1:-1:1 + mpre(i)=max(mpre(i),mpre(i+1)); + end + i=find(mrec(2:end)~=mrec(1:end-1))+1; + ap=sum((mrec(i)-mrec(i-1)).*mpre(i)); + """ + rec.insert(0, 0.0) # insert 0.0 at begining of list + rec.append(1.0) # insert 1.0 at end of list + mrec = rec[:] + prec.insert(0, 0.0) # insert 0.0 at begining of list + prec.append(0.0) # insert 0.0 at end of list + mpre = prec[:] + """ + This part makes the precision monotonically decreasing + (goes from the end to the beginning) + matlab: for i=numel(mpre)-1:-1:1 + mpre(i)=max(mpre(i),mpre(i+1)); + """ + for i in range(len(mpre)-2, -1, -1): + mpre[i] = max(mpre[i], mpre[i+1]) + """ + This part creates a list of indexes where the recall changes + matlab: i=find(mrec(2:end)~=mrec(1:end-1))+1; + """ + i_list = [] + for i in range(1, len(mrec)): + if mrec[i] != mrec[i-1]: + i_list.append(i) # if it was matlab would be i + 1 + """ + The Average Precision (AP) is the area under the curve + (numerical integration) + matlab: ap=sum((mrec(i)-mrec(i-1)).*mpre(i)); + """ + ap = 0.0 + for i in i_list: + ap += ((mrec[i]-mrec[i-1])*mpre[i]) + return ap, mrec, mpre + + +""" + Convert the lines of a file to a list +""" +def file_lines_to_list(path): + # open txt file lines to a list + with open(path) as f: + content = f.readlines() + # remove whitespace characters like `\n` at the end of each line + content = [x.strip() for x in content] + return content + +""" + Draws text in image +""" +def draw_text_in_image(img, text, pos, color, line_width): + font = cv2.FONT_HERSHEY_PLAIN + fontScale = 1 + lineType = 1 + bottomLeftCornerOfText = pos + cv2.putText(img, text, + bottomLeftCornerOfText, + font, + fontScale, + color, + lineType) + text_width, _ = cv2.getTextSize(text, font, fontScale, lineType)[0] + return img, (line_width + text_width) + +""" + Plot - adjust axes +""" +def adjust_axes(r, t, fig, axes): + # get text width for re-scaling + bb = t.get_window_extent(renderer=r) + text_width_inches = bb.width / fig.dpi + # get axis width in inches + current_fig_width = fig.get_figwidth() + new_fig_width = current_fig_width + text_width_inches + propotion = new_fig_width / current_fig_width + # get axis limit + x_lim = axes.get_xlim() + axes.set_xlim([x_lim[0], x_lim[1]*propotion]) + +""" + Draw plot using Matplotlib +""" +def draw_plot_func(dictionary, n_classes, window_title, plot_title, x_label, output_path, to_show, plot_color, true_p_bar): + # sort the dictionary by decreasing value, into a list of tuples + sorted_dic_by_value = sorted(dictionary.items(), key=operator.itemgetter(1)) + # unpacking the list of tuples into two lists + sorted_keys, sorted_values = zip(*sorted_dic_by_value) + # + if true_p_bar != "": + """ + Special case to draw in: + - green -> TP: True Positives (object detected and matches ground-truth) + - red -> FP: False Positives (object detected but does not match ground-truth) + - orange -> FN: False Negatives (object not detected but present in the ground-truth) + """ + fp_sorted = [] + tp_sorted = [] + for key in sorted_keys: + fp_sorted.append(dictionary[key] - true_p_bar[key]) + tp_sorted.append(true_p_bar[key]) + plt.barh(range(n_classes), fp_sorted, align='center', color='crimson', label='False Positive') + plt.barh(range(n_classes), tp_sorted, align='center', color='forestgreen', label='True Positive', left=fp_sorted) + # add legend + plt.legend(loc='lower right') + """ + Write number on side of bar + """ + fig = plt.gcf() # gcf - get current figure + axes = plt.gca() + r = fig.canvas.get_renderer() + for i, val in enumerate(sorted_values): + fp_val = fp_sorted[i] + tp_val = tp_sorted[i] + fp_str_val = " " + str(fp_val) + tp_str_val = fp_str_val + " " + str(tp_val) + # trick to paint multicolor with offset: + # first paint everything and then repaint the first number + t = plt.text(val, i, tp_str_val, color='forestgreen', va='center', fontweight='bold') + plt.text(val, i, fp_str_val, color='crimson', va='center', fontweight='bold') + if i == (len(sorted_values)-1): # largest bar + adjust_axes(r, t, fig, axes) + else: + plt.barh(range(n_classes), sorted_values, color=plot_color) + """ + Write number on side of bar + """ + fig = plt.gcf() # gcf - get current figure + axes = plt.gca() + r = fig.canvas.get_renderer() + for i, val in enumerate(sorted_values): + str_val = " " + str(val) # add a space before + if val < 1.0: + str_val = " {0:.2f}".format(val) + t = plt.text(val, i, str_val, color=plot_color, va='center', fontweight='bold') + # re-set axes to show number inside the figure + if i == (len(sorted_values)-1): # largest bar + adjust_axes(r, t, fig, axes) + # set window title + fig.canvas.set_window_title(window_title) + # write classes in y axis + tick_font_size = 12 + plt.yticks(range(n_classes), sorted_keys, fontsize=tick_font_size) + """ + Re-scale height accordingly + """ + init_height = fig.get_figheight() + # comput the matrix height in points and inches + dpi = fig.dpi + height_pt = n_classes * (tick_font_size * 1.4) # 1.4 (some spacing) + height_in = height_pt / dpi + # compute the required figure height + top_margin = 0.15 # in percentage of the figure height + bottom_margin = 0.05 # in percentage of the figure height + figure_height = height_in / (1 - top_margin - bottom_margin) + # set new height + if figure_height > init_height: + fig.set_figheight(figure_height) + + # set plot title + plt.title(plot_title, fontsize=14) + # set axis titles + # plt.xlabel('classes') + plt.xlabel(x_label, fontsize='large') + # adjust size of window + fig.tight_layout() + # save the plot + fig.savefig(output_path) + # show image + if to_show: + plt.show() + # close the plot + plt.close() + +""" + Create a ".temp_files/" and "results/" directory +""" +TEMP_FILES_PATH = ".temp_files" +if not os.path.exists(TEMP_FILES_PATH): # if it doesn't exist already + os.makedirs(TEMP_FILES_PATH) +results_files_path = "results" +if os.path.exists(results_files_path): # if it exist already + # reset the results directory + shutil.rmtree(results_files_path) + +os.makedirs(results_files_path) +if draw_plot: + os.makedirs(os.path.join(results_files_path, "AP")) + os.makedirs(os.path.join(results_files_path, "F1")) + os.makedirs(os.path.join(results_files_path, "Recall")) + os.makedirs(os.path.join(results_files_path, "Precision")) +if show_animation: + os.makedirs(os.path.join(results_files_path, "images", "detections_one_by_one")) + +""" + ground-truth + Load each of the ground-truth files into a temporary ".json" file. + Create a list of all the class names present in the ground-truth (gt_classes). +""" +# get a list with the ground-truth files +ground_truth_files_list = glob.glob(GT_PATH + '/*.txt') +if len(ground_truth_files_list) == 0: + error("Error: No ground-truth files found!") +ground_truth_files_list.sort() +# dictionary with counter per class +gt_counter_per_class = {} +counter_images_per_class = {} + +for txt_file in ground_truth_files_list: + #print(txt_file) + file_id = txt_file.split(".txt", 1)[0] + file_id = os.path.basename(os.path.normpath(file_id)) + # check if there is a correspondent detection-results file + temp_path = os.path.join(DR_PATH, (file_id + ".txt")) + if not os.path.exists(temp_path): + error_msg = "Error. File not found: {}\n".format(temp_path) + error_msg += "(You can avoid this error message by running extra/intersect-gt-and-dr.py)" + error(error_msg) + lines_list = file_lines_to_list(txt_file) + # create ground-truth dictionary + bounding_boxes = [] + is_difficult = False + already_seen_classes = [] + for line in lines_list: + try: + if "difficult" in line: + class_name, left, top, right, bottom, _difficult = line.split() + is_difficult = True + else: + class_name, left, top, right, bottom = line.split() + + except: + if "difficult" in line: + line_split = line.split() + _difficult = line_split[-1] + bottom = line_split[-2] + right = line_split[-3] + top = line_split[-4] + left = line_split[-5] + class_name = "" + for name in line_split[:-5]: + class_name += name + " " + class_name = class_name[:-1] + is_difficult = True + else: + line_split = line.split() + bottom = line_split[-1] + right = line_split[-2] + top = line_split[-3] + left = line_split[-4] + class_name = "" + for name in line_split[:-4]: + class_name += name + " " + class_name = class_name[:-1] + if class_name in args.ignore: + continue + bbox = left + " " + top + " " + right + " " +bottom + if is_difficult: + bounding_boxes.append({"class_name":class_name, "bbox":bbox, "used":False, "difficult":True}) + is_difficult = False + else: + bounding_boxes.append({"class_name":class_name, "bbox":bbox, "used":False}) + if class_name in gt_counter_per_class: + gt_counter_per_class[class_name] += 1 + else: + gt_counter_per_class[class_name] = 1 + + if class_name not in already_seen_classes: + if class_name in counter_images_per_class: + counter_images_per_class[class_name] += 1 + else: + counter_images_per_class[class_name] = 1 + already_seen_classes.append(class_name) + + + with open(TEMP_FILES_PATH + "/" + file_id + "_ground_truth.json", 'w') as outfile: + json.dump(bounding_boxes, outfile) + +gt_classes = list(gt_counter_per_class.keys()) +gt_classes = sorted(gt_classes) +n_classes = len(gt_classes) + +""" + Check format of the flag --set-class-iou (if used) + e.g. check if class exists +""" +if specific_iou_flagged: + n_args = len(args.set_class_iou) + error_msg = \ + '\n --set-class-iou [class_1] [IoU_1] [class_2] [IoU_2] [...]' + if n_args % 2 != 0: + error('Error, missing arguments. Flag usage:' + error_msg) + # [class_1] [IoU_1] [class_2] [IoU_2] + # specific_iou_classes = ['class_1', 'class_2'] + specific_iou_classes = args.set_class_iou[::2] # even + # iou_list = ['IoU_1', 'IoU_2'] + iou_list = args.set_class_iou[1::2] # odd + if len(specific_iou_classes) != len(iou_list): + error('Error, missing arguments. Flag usage:' + error_msg) + for tmp_class in specific_iou_classes: + if tmp_class not in gt_classes: + error('Error, unknown class \"' + tmp_class + '\". Flag usage:' + error_msg) + for num in iou_list: + if not is_float_between_0_and_1(num): + error('Error, IoU must be between 0.0 and 1.0. Flag usage:' + error_msg) + +""" + detection-results + Load each of the detection-results files into a temporary ".json" file. +""" +dr_files_list = glob.glob(DR_PATH + '/*.txt') +dr_files_list.sort() + +for class_index, class_name in enumerate(gt_classes): + bounding_boxes = [] + for txt_file in dr_files_list: + file_id = txt_file.split(".txt",1)[0] + file_id = os.path.basename(os.path.normpath(file_id)) + temp_path = os.path.join(GT_PATH, (file_id + ".txt")) + if class_index == 0: + if not os.path.exists(temp_path): + error_msg = "Error. File not found: {}\n".format(temp_path) + error_msg += "(You can avoid this error message by running extra/intersect-gt-and-dr.py)" + error(error_msg) + lines = file_lines_to_list(txt_file) + for line in lines: + try: + tmp_class_name, confidence, left, top, right, bottom = line.split() + except: + line_split = line.split() + bottom = line_split[-1] + right = line_split[-2] + top = line_split[-3] + left = line_split[-4] + confidence = line_split[-5] + tmp_class_name = "" + for name in line_split[:-5]: + tmp_class_name += name + " " + tmp_class_name = tmp_class_name[:-1] + + if tmp_class_name == class_name: + bbox = left + " " + top + " " + right + " " +bottom + bounding_boxes.append({"confidence":confidence, "file_id":file_id, "bbox":bbox}) + + bounding_boxes.sort(key=lambda x:float(x['confidence']), reverse=True) + with open(TEMP_FILES_PATH + "/" + class_name + "_dr.json", 'w') as outfile: + json.dump(bounding_boxes, outfile) + +""" + Calculate the AP for each class +""" +sum_AP = 0.0 +ap_dictionary = {} +lamr_dictionary = {} +with open(results_files_path + "/results.txt", 'w') as results_file: + results_file.write("# AP and precision/recall per class\n") + count_true_positives = {} + + for class_index, class_name in enumerate(gt_classes): + count_true_positives[class_name] = 0 + """ + Load detection-results of that class + """ + dr_file = TEMP_FILES_PATH + "/" + class_name + "_dr.json" + dr_data = json.load(open(dr_file)) + """ + Assign detection-results to ground-truth objects + """ + nd = len(dr_data) + tp = [0] * nd + fp = [0] * nd + score = [0] * nd + score05_idx = 0 + for idx, detection in enumerate(dr_data): + file_id = detection["file_id"] + score[idx] = float(detection["confidence"]) + if score[idx] > 0.5: + score05_idx = idx + + if show_animation: + ground_truth_img = glob.glob1(IMG_PATH, file_id + ".*") + if len(ground_truth_img) == 0: + error("Error. Image not found with id: " + file_id) + elif len(ground_truth_img) > 1: + error("Error. Multiple image with id: " + file_id) + else: + img = cv2.imread(IMG_PATH + "/" + ground_truth_img[0]) + img_cumulative_path = results_files_path + "/images/" + ground_truth_img[0] + if os.path.isfile(img_cumulative_path): + img_cumulative = cv2.imread(img_cumulative_path) + else: + img_cumulative = img.copy() + bottom_border = 60 + BLACK = [0, 0, 0] + img = cv2.copyMakeBorder(img, 0, bottom_border, 0, 0, cv2.BORDER_CONSTANT, value=BLACK) + + gt_file = TEMP_FILES_PATH + "/" + file_id + "_ground_truth.json" + ground_truth_data = json.load(open(gt_file)) + ovmax = -1 + gt_match = -1 + bb = [ float(x) for x in detection["bbox"].split() ] + for obj in ground_truth_data: + if obj["class_name"] == class_name: + bbgt = [ float(x) for x in obj["bbox"].split() ] + bi = [max(bb[0],bbgt[0]), max(bb[1],bbgt[1]), min(bb[2],bbgt[2]), min(bb[3],bbgt[3])] + iw = bi[2] - bi[0] + 1 + ih = bi[3] - bi[1] + 1 + if iw > 0 and ih > 0: + # compute overlap (IoU) = area of intersection / area of union + ua = (bb[2] - bb[0] + 1) * (bb[3] - bb[1] + 1) + (bbgt[2] - bbgt[0] + + 1) * (bbgt[3] - bbgt[1] + 1) - iw * ih + ov = iw * ih / ua + if ov > ovmax: + ovmax = ov + gt_match = obj + + if show_animation: + status = "NO MATCH FOUND!" + min_overlap = MINOVERLAP + if specific_iou_flagged: + if class_name in specific_iou_classes: + index = specific_iou_classes.index(class_name) + min_overlap = float(iou_list[index]) + if ovmax >= min_overlap: + if "difficult" not in gt_match: + if not bool(gt_match["used"]): + tp[idx] = 1 + gt_match["used"] = True + count_true_positives[class_name] += 1 + with open(gt_file, 'w') as f: + f.write(json.dumps(ground_truth_data)) + if show_animation: + status = "MATCH!" + else: + fp[idx] = 1 + if show_animation: + status = "REPEATED MATCH!" + else: + fp[idx] = 1 + if ovmax > 0: + status = "INSUFFICIENT OVERLAP" + + """ + Draw image to show animation + """ + if show_animation: + height, widht = img.shape[:2] + # colors (OpenCV works with BGR) + white = (255,255,255) + light_blue = (255,200,100) + green = (0,255,0) + light_red = (30,30,255) + # 1st line + margin = 10 + v_pos = int(height - margin - (bottom_border / 2.0)) + text = "Image: " + ground_truth_img[0] + " " + img, line_width = draw_text_in_image(img, text, (margin, v_pos), white, 0) + text = "Class [" + str(class_index) + "/" + str(n_classes) + "]: " + class_name + " " + img, line_width = draw_text_in_image(img, text, (margin + line_width, v_pos), light_blue, line_width) + if ovmax != -1: + color = light_red + if status == "INSUFFICIENT OVERLAP": + text = "IoU: {0:.2f}% ".format(ovmax*100) + "< {0:.2f}% ".format(min_overlap*100) + else: + text = "IoU: {0:.2f}% ".format(ovmax*100) + ">= {0:.2f}% ".format(min_overlap*100) + color = green + img, _ = draw_text_in_image(img, text, (margin + line_width, v_pos), color, line_width) + # 2nd line + v_pos += int(bottom_border / 2.0) + rank_pos = str(idx+1) # rank position (idx starts at 0) + text = "Detection #rank: " + rank_pos + " confidence: {0:.2f}% ".format(float(detection["confidence"])*100) + img, line_width = draw_text_in_image(img, text, (margin, v_pos), white, 0) + color = light_red + if status == "MATCH!": + color = green + text = "Result: " + status + " " + img, line_width = draw_text_in_image(img, text, (margin + line_width, v_pos), color, line_width) + + font = cv2.FONT_HERSHEY_SIMPLEX + if ovmax > 0: # if there is intersections between the bounding-boxes + bbgt = [ int(round(float(x))) for x in gt_match["bbox"].split() ] + cv2.rectangle(img,(bbgt[0],bbgt[1]),(bbgt[2],bbgt[3]),light_blue,2) + cv2.rectangle(img_cumulative,(bbgt[0],bbgt[1]),(bbgt[2],bbgt[3]),light_blue,2) + cv2.putText(img_cumulative, class_name, (bbgt[0],bbgt[1] - 5), font, 0.6, light_blue, 1, cv2.LINE_AA) + bb = [int(i) for i in bb] + cv2.rectangle(img,(bb[0],bb[1]),(bb[2],bb[3]),color,2) + cv2.rectangle(img_cumulative,(bb[0],bb[1]),(bb[2],bb[3]),color,2) + cv2.putText(img_cumulative, class_name, (bb[0],bb[1] - 5), font, 0.6, color, 1, cv2.LINE_AA) + # show image + cv2.imshow("Animation", img) + cv2.waitKey(20) # show for 20 ms + # save image to results + output_img_path = results_files_path + "/images/detections_one_by_one/" + class_name + "_detection" + str(idx) + ".jpg" + cv2.imwrite(output_img_path, img) + # save the image with all the objects drawn to it + cv2.imwrite(img_cumulative_path, img_cumulative) + + cumsum = 0 + for idx, val in enumerate(fp): + fp[idx] += cumsum + cumsum += val + + cumsum = 0 + for idx, val in enumerate(tp): + tp[idx] += cumsum + cumsum += val + + rec = tp[:] + for idx, val in enumerate(tp): + rec[idx] = float(tp[idx]) / np.maximum(gt_counter_per_class[class_name], 1) + + prec = tp[:] + for idx, val in enumerate(tp): + prec[idx] = float(tp[idx]) / np.maximum((fp[idx] + tp[idx]), 1) + + ap, mrec, mprec = voc_ap(rec[:], prec[:]) + F1 = np.array(rec)*np.array(prec)*2 / np.where((np.array(prec)+np.array(rec))==0, 1, (np.array(prec)+np.array(rec))) + + sum_AP += ap + text = "{0:.2f}%".format(ap*100) + " = " + class_name + " AP " #class_name + " AP = {0:.2f}%".format(ap*100) + + if len(prec)>0: + F1_text = "{0:.2f}".format(F1[score05_idx]) + " = " + class_name + " F1 " + Recall_text = "{0:.2f}%".format(rec[score05_idx]*100) + " = " + class_name + " Recall " + Precision_text = "{0:.2f}%".format(prec[score05_idx]*100) + " = " + class_name + " Precision " + else: + F1_text = "0.00" + " = " + class_name + " F1 " + Recall_text = "0.00%" + " = " + class_name + " Recall " + Precision_text = "0.00%" + " = " + class_name + " Precision " + + rounded_prec = [ '%.2f' % elem for elem in prec ] + rounded_rec = [ '%.2f' % elem for elem in rec ] + results_file.write(text + "\n Precision: " + str(rounded_prec) + "\n Recall :" + str(rounded_rec) + "\n\n") + if not args.quiet: + if len(prec)>0: + print(text + "\t||\tscore_threhold=0.5 : " + "F1=" + "{0:.2f}".format(F1[score05_idx])\ + + " ; Recall=" + "{0:.2f}%".format(rec[score05_idx]*100) + " ; Precision=" + "{0:.2f}%".format(prec[score05_idx]*100)) + else: + print(text + "\t||\tscore_threhold=0.5 : F1=0.00% ; Recall=0.00% ; Precision=0.00%") + ap_dictionary[class_name] = ap + + n_images = counter_images_per_class[class_name] + lamr, mr, fppi = log_average_miss_rate(np.array(rec), np.array(fp), n_images) + lamr_dictionary[class_name] = lamr + + """ + Draw plot + """ + if draw_plot: + plt.plot(rec, prec, '-o') + area_under_curve_x = mrec[:-1] + [mrec[-2]] + [mrec[-1]] + area_under_curve_y = mprec[:-1] + [0.0] + [mprec[-1]] + plt.fill_between(area_under_curve_x, 0, area_under_curve_y, alpha=0.2, edgecolor='r') + + fig = plt.gcf() + fig.canvas.set_window_title('AP ' + class_name) + + plt.title('class: ' + text) + plt.xlabel('Recall') + plt.ylabel('Precision') + axes = plt.gca() + axes.set_xlim([0.0,1.0]) + axes.set_ylim([0.0,1.05]) + fig.savefig(results_files_path + "/AP/" + class_name + ".png") + plt.cla() + + plt.plot(score, F1, "-", color='orangered') + plt.title('class: ' + F1_text + "\nscore_threhold=0.5") + plt.xlabel('Score_Threhold') + plt.ylabel('F1') + axes = plt.gca() + axes.set_xlim([0.0,1.0]) + axes.set_ylim([0.0,1.05]) + fig.savefig(results_files_path + "/F1/" + class_name + ".png") + plt.cla() + + plt.plot(score, rec, "-H", color='gold') + plt.title('class: ' + Recall_text + "\nscore_threhold=0.5") + plt.xlabel('Score_Threhold') + plt.ylabel('Recall') + axes = plt.gca() + axes.set_xlim([0.0,1.0]) + axes.set_ylim([0.0,1.05]) + fig.savefig(results_files_path + "/Recall/" + class_name + ".png") + plt.cla() + + plt.plot(score, prec, "-s", color='palevioletred') + plt.title('class: ' + Precision_text + "\nscore_threhold=0.5") + plt.xlabel('Score_Threhold') + plt.ylabel('Precision') + axes = plt.gca() + axes.set_xlim([0.0,1.0]) + axes.set_ylim([0.0,1.05]) + fig.savefig(results_files_path + "/Precision/" + class_name + ".png") + plt.cla() + + if show_animation: + cv2.destroyAllWindows() + + results_file.write("\n# mAP of all classes\n") + mAP = sum_AP / n_classes + text = "mAP = {0:.2f}%".format(mAP*100) + results_file.write(text + "\n") + print(text) + +# remove the temp_files directory +shutil.rmtree(TEMP_FILES_PATH) + +""" + Count total of detection-results +""" +# iterate through all the files +det_counter_per_class = {} +for txt_file in dr_files_list: + # get lines to list + lines_list = file_lines_to_list(txt_file) + for line in lines_list: + class_name = line.split()[0] + # check if class is in the ignore list, if yes skip + if class_name in args.ignore: + continue + # count that object + if class_name in det_counter_per_class: + det_counter_per_class[class_name] += 1 + else: + # if class didn't exist yet + det_counter_per_class[class_name] = 1 +#print(det_counter_per_class) +dr_classes = list(det_counter_per_class.keys()) + + +""" + Plot the total number of occurences of each class in the ground-truth +""" +if draw_plot: + window_title = "ground-truth-info" + plot_title = "ground-truth\n" + plot_title += "(" + str(len(ground_truth_files_list)) + " files and " + str(n_classes) + " classes)" + x_label = "Number of objects per class" + output_path = results_files_path + "/ground-truth-info.png" + to_show = False + plot_color = 'forestgreen' + draw_plot_func( + gt_counter_per_class, + n_classes, + window_title, + plot_title, + x_label, + output_path, + to_show, + plot_color, + '', + ) + +""" + Write number of ground-truth objects per class to results.txt +""" +with open(results_files_path + "/results.txt", 'a') as results_file: + results_file.write("\n# Number of ground-truth objects per class\n") + for class_name in sorted(gt_counter_per_class): + results_file.write(class_name + ": " + str(gt_counter_per_class[class_name]) + "\n") + +""" + Finish counting true positives +""" +for class_name in dr_classes: + # if class exists in detection-result but not in ground-truth then there are no true positives in that class + if class_name not in gt_classes: + count_true_positives[class_name] = 0 +#print(count_true_positives) + +""" + Plot the total number of occurences of each class in the "detection-results" folder +""" +if draw_plot: + window_title = "detection-results-info" + # Plot title + plot_title = "detection-results\n" + plot_title += "(" + str(len(dr_files_list)) + " files and " + count_non_zero_values_in_dictionary = sum(int(x) > 0 for x in list(det_counter_per_class.values())) + plot_title += str(count_non_zero_values_in_dictionary) + " detected classes)" + # end Plot title + x_label = "Number of objects per class" + output_path = results_files_path + "/detection-results-info.png" + to_show = False + plot_color = 'forestgreen' + true_p_bar = count_true_positives + draw_plot_func( + det_counter_per_class, + len(det_counter_per_class), + window_title, + plot_title, + x_label, + output_path, + to_show, + plot_color, + true_p_bar + ) + +""" + Write number of detected objects per class to results.txt +""" +with open(results_files_path + "/results.txt", 'a') as results_file: + results_file.write("\n# Number of detected objects per class\n") + for class_name in sorted(dr_classes): + n_det = det_counter_per_class[class_name] + text = class_name + ": " + str(n_det) + text += " (tp:" + str(count_true_positives[class_name]) + "" + text += ", fp:" + str(n_det - count_true_positives[class_name]) + ")\n" + results_file.write(text) + +""" + Draw log-average miss rate plot (Show lamr of all classes in decreasing order) +""" +if draw_plot: + window_title = "lamr" + plot_title = "log-average miss rate" + x_label = "log-average miss rate" + output_path = results_files_path + "/lamr.png" + to_show = False + plot_color = 'royalblue' + draw_plot_func( + lamr_dictionary, + n_classes, + window_title, + plot_title, + x_label, + output_path, + to_show, + plot_color, + "" + ) + +""" + Draw mAP plot (Show AP's of all classes in decreasing order) +""" +if draw_plot: + window_title = "mAP" + plot_title = "mAP = {0:.2f}%".format(mAP*100) + x_label = "Average Precision" + output_path = results_files_path + "/mAP.png" + to_show = True + plot_color = 'royalblue' + draw_plot_func( + ap_dictionary, + n_classes, + window_title, + plot_title, + x_label, + output_path, + to_show, + plot_color, + "" + )