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 1/7] =?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 2/7] =?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 3/7] =?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 4/7] 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 5/7] =?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 6/7] =?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 7/7] =?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