Giter VIP home page Giter VIP logo

yolo3-pytorch's Introduction

YOLOV3:You Only Look Once目标检测模型在Pytorch当中的实现


目录

  1. 仓库更新 Top News
  2. 相关仓库 Related code
  3. 性能情况 Performance
  4. 所需环境 Environment
  5. 文件下载 Download
  6. 训练步骤 How2train
  7. 预测步骤 How2predict
  8. 评估步骤 How2eval
  9. 参考资料 Reference

Top News

2022-04:支持多GPU训练,新增各个种类目标数量计算,新增heatmap。

2022-03:进行了大幅度的更新,修改了loss组成,使得分类、目标、回归loss的比例合适、支持step、cos学习率下降法、支持adam、sgd优化器选择、支持学习率根据batch_size自适应调整、新增图片裁剪。
BiliBili视频中的原仓库地址为:https://github.com/bubbliiiing/yolo3-pytorch/tree/bilibili

2021-10:进行了大幅度的更新,增加了大量注释、增加了大量可调整参数、对代码的组成模块进行修改、增加fps、视频预测、批量预测等功能。

相关仓库

模型 路径
YoloV3 https://github.com/bubbliiiing/yolo3-pytorch
Efficientnet-Yolo3 https://github.com/bubbliiiing/efficientnet-yolo3-pytorch
YoloV4 https://github.com/bubbliiiing/yolov4-pytorch
YoloV4-tiny https://github.com/bubbliiiing/yolov4-tiny-pytorch
Mobilenet-Yolov4 https://github.com/bubbliiiing/mobilenet-yolov4-pytorch
YoloV5-V5.0 https://github.com/bubbliiiing/yolov5-pytorch
YoloV5-V6.1 https://github.com/bubbliiiing/yolov5-v6.1-pytorch
YoloX https://github.com/bubbliiiing/yolox-pytorch
YoloV7 https://github.com/bubbliiiing/yolov7-pytorch
YoloV7-tiny https://github.com/bubbliiiing/yolov7-tiny-pytorch

性能情况

训练数据集 权值文件名称 测试数据集 输入图片大小 mAP 0.5:0.95 mAP 0.5
COCO-Train2017 yolo_weights.pth COCO-Val2017 416x416 38.0 67.2

所需环境

torch == 1.2.0
详情请看requirements.txt,文件具有一定兼容性,已测试pytorch1.7和1.7.1可以正常运行。

文件下载

训练所需的yolo_weights.pth可以在百度云下载。
链接: https://pan.baidu.com/s/1hCV4kg8NyStkywLiAeEr3g
提取码: 6da3

VOC数据集下载地址如下,里面已经包括了训练集、测试集、验证集(与测试集一样),无需再次划分:
链接: https://pan.baidu.com/s/19Mw2u_df_nBzsC2lg20fQA
提取码: j5ge

训练步骤

a、训练VOC07+12数据集

  1. 数据集的准备
    本文使用VOC格式进行训练,训练前需要下载好VOC07+12的数据集,解压后放在根目录

  2. 数据集的处理
    修改voc_annotation.py里面的annotation_mode=2,运行voc_annotation.py生成根目录下的2007_train.txt和2007_val.txt。

  3. 开始网络训练
    train.py的默认参数用于训练VOC数据集,直接运行train.py即可开始训练。

  4. 训练结果预测
    训练结果预测需要用到两个文件,分别是yolo.py和predict.py。我们首先需要去yolo.py里面修改model_path以及classes_path,这两个参数必须要修改。
    model_path指向训练好的权值文件,在logs文件夹里。
    classes_path指向检测类别所对应的txt。

    完成修改后就可以运行predict.py进行检测了。运行后输入图片路径即可检测。

b、训练自己的数据集

  1. 数据集的准备
    本文使用VOC格式进行训练,训练前需要自己制作好数据集,
    训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。
    训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。

  2. 数据集的处理
    在完成数据集的摆放之后,我们需要利用voc_annotation.py获得训练用的2007_train.txt和2007_val.txt。
    修改voc_annotation.py里面的参数。第一次训练可以仅修改classes_path,classes_path用于指向检测类别所对应的txt。
    训练自己的数据集时,可以自己建立一个cls_classes.txt,里面写自己所需要区分的类别。
    model_data/cls_classes.txt文件内容为:

cat
dog
...

修改voc_annotation.py中的classes_path,使其对应cls_classes.txt,并运行voc_annotation.py。

  1. 开始网络训练
    训练的参数较多,均在train.py中,大家可以在下载库后仔细看注释,其中最重要的部分依然是train.py里的classes_path。
    classes_path用于指向检测类别所对应的txt,这个txt和voc_annotation.py里面的txt一样!训练自己的数据集必须要修改!
    修改完classes_path后就可以运行train.py开始训练了,在训练多个epoch后,权值会生成在logs文件夹中。

  2. 训练结果预测
    训练结果预测需要用到两个文件,分别是yolo.py和predict.py。在yolo.py里面修改model_path以及classes_path。
    model_path指向训练好的权值文件,在logs文件夹里。
    classes_path指向检测类别所对应的txt。

    完成修改后就可以运行predict.py进行检测了。运行后输入图片路径即可检测。

预测步骤

a、使用预训练权重

  1. 下载完库后解压,在百度网盘下载yolo_weights.pth,放入model_data,运行predict.py,输入
img/street.jpg
  1. 在predict.py里面进行设置可以进行fps测试和video视频检测。

b、使用自己训练的权重

  1. 按照训练步骤训练。
  2. 在yolo.py文件里面,在如下部分修改model_path和classes_path使其对应训练好的文件;model_path对应logs文件夹下面的权值文件,classes_path是model_path对应分的类
_defaults = {
    #--------------------------------------------------------------------------#
    #   使用自己训练好的模型进行预测一定要修改model_path和classes_path!
    #   model_path指向logs文件夹下的权值文件,classes_path指向model_data下的txt
    #   如果出现shape不匹配,同时要注意训练时的model_path和classes_path参数的修改
    #--------------------------------------------------------------------------#
    "model_path"        : 'model_data/yolo_weights.pth',
    "classes_path"      : 'model_data/coco_classes.txt',
    #---------------------------------------------------------------------#
    #   anchors_path代表先验框对应的txt文件,一般不修改。
    #   anchors_mask用于帮助代码找到对应的先验框,一般不修改。
    #---------------------------------------------------------------------#
    "anchors_path"      : 'model_data/yolo_anchors.txt',
    "anchors_mask"      : [[6, 7, 8], [3, 4, 5], [0, 1, 2]],
    #---------------------------------------------------------------------#
    #   输入图片的大小,必须为32的倍数。
    #---------------------------------------------------------------------#
    "input_shape"       : [416, 416],
    #---------------------------------------------------------------------#
    #   只有得分大于置信度的预测框会被保留下来
    #---------------------------------------------------------------------#
    "confidence"        : 0.5,
    #---------------------------------------------------------------------#
    #   非极大抑制所用到的nms_iou大小
    #---------------------------------------------------------------------#
    "nms_iou"           : 0.3,
    #---------------------------------------------------------------------#
    #   该变量用于控制是否使用letterbox_image对输入图像进行不失真的resize,
    #   在多次测试后,发现关闭letterbox_image直接resize的效果更好
    #---------------------------------------------------------------------#
    "letterbox_image"   : False,
    #-------------------------------#
    #   是否使用Cuda
    #   没有GPU可以设置成False
    #-------------------------------#
    "cuda"              : True,
}
  1. 运行predict.py,输入
img/street.jpg
  1. 在predict.py里面进行设置可以进行fps测试和video视频检测。

评估步骤

a、评估VOC07+12的测试集

  1. 本文使用VOC格式进行评估。VOC07+12已经划分好了测试集,无需利用voc_annotation.py生成ImageSets文件夹下的txt。
  2. 在yolo.py里面修改model_path以及classes_path。model_path指向训练好的权值文件,在logs文件夹里。classes_path指向检测类别所对应的txt。
  3. 运行get_map.py即可获得评估结果,评估结果会保存在map_out文件夹中。

b、评估自己的数据集

  1. 本文使用VOC格式进行评估。
  2. 如果在训练前已经运行过voc_annotation.py文件,代码会自动将数据集划分成训练集、验证集和测试集。如果想要修改测试集的比例,可以修改voc_annotation.py文件下的trainval_percent。trainval_percent用于指定(训练集+验证集)与测试集的比例,默认情况下 (训练集+验证集):测试集 = 9:1。train_percent用于指定(训练集+验证集)中训练集与验证集的比例,默认情况下 训练集:验证集 = 9:1。
  3. 利用voc_annotation.py划分测试集后,前往get_map.py文件修改classes_path,classes_path用于指向检测类别所对应的txt,这个txt和训练时的txt一样。评估自己的数据集必须要修改。
  4. 在yolo.py里面修改model_path以及classes_path。model_path指向训练好的权值文件,在logs文件夹里。classes_path指向检测类别所对应的txt。
  5. 运行get_map.py即可获得评估结果,评估结果会保存在map_out文件夹中。

Reference

https://github.com/qqwweee/pytorch-yolo3
https://github.com/eriklindernoren/PyTorch-YOLOv3
https://github.com/BobLiu20/YOLOv3_PyTorch

yolo3-pytorch's People

Contributors

bubbliiiing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yolo3-pytorch's Issues

map error

你好,我用voc数据集训练了模型,loss降到了8.2,但是我用voc测试集测map只有0.01%,input也是从新生成过得,请问这可能是哪里出现了问题。

ubuntu16.04 环境下的运行问题

@bubbliiiing , 您好:
非常感谢您的付出, 您解析的网络结构和 代码给了本人很好的帮助, 不过我有个疑问,请问的您代码 有在ubuntu 环境下测试过吗? 还是您只有在win 上运行的,然后git 上传的。
因为在ubuntu 下 git 下来, 无法运行, 如下图中, 文件路径明明是存在的, 就是显示不存在;
经过查询, 修改了dos 格式,并且把 相对路径 改成 绝对路径, 可以运行了。

所以 想表达的是,您 能否可以考虑 一下, 之后的上传的代码 可以在 环境ubuntu 下 运行通过,因为 个人观点,(个人原因实验室的显卡全部在linux 环境下) 如果您的代码无法在 linux 环境下运行,并且阅读, 有点小小的遗憾。
Screenshot from 2020-08-26 20-47-43
Screenshot from 2020-08-26 21-16-48

dataset

请问如果我的数据集格式是.json的形式,要怎么利用这个工程去实现呢

重新训练时没有预训权重

up你好,重新训练自己模型时没有预先训练好的权重,在yolo.py里model_path要怎么改,还是直接删除相关代码?否则加载你的20分类的权重会报错(我的是2分类)

为啥mAP比官网的高出挺多的?

官网的数据 [email protected]

Model Train Test mAP
YOLOv3-320 COCO trainval test-dev 51.5
YOLOv3-416 COCO trainval test-dev 55.3
YOLOv3-608 COCO trainval test-dev 57.9
YOLOv3-spp COCO trainval test-dev 60.6

开始我觉得是不是get_mAP.py 计算的问题,于是我将生成的结果json 保存,使用cocoapi计算结果也是很高。
有点懵。。。

change backbone

你好,请问我把backbone改成mobilnetv3后,应该怎么从头训练,如何初始化权重呢。

performance of the test and demo is poor

hi, I used your code to train yolov3, and the tarin loss converged to 9. But the performance of the test and demo is poor. Is there some details to pay attention to during the training?

关于yolo_training.py 234行中grid_x和grid_y

grid_x = torch.linspace(0, in_w - 1, in_w).repeat(in_w, 1).repeat(

请问这里torch.linspace(0, in_w - 1, in_w).repeat(in_w, 1)为什么不是repeat(in_h, 1),如果in_win_h不相等,那248行的x.data + grid_x是跑不通的吧?x的形状是(bs, num_anchors/3, in_h, in_w)而原代码里生成的grid_x(bs, num_anchors/3, in_w, in_w)

grid_y同理

不知道我理解的对不对

请教一点自定义数据集训练相关的问题

  1. 想要做身份证正反面的分类(即分为'front'和'back'两类),生成了500张训练图片进行了标注训练
  2. 训练完成后使用包含了身份证正反面图片进行测试,效果还是不错的

问题:使用完全不包含身份证的图片进行测试,比如拿张猫的图片,也会被检测为'front'类,而且置信度极高,请问这种情况是否是因为使用了预训练权重的原因,是否需要去除预训练权重从头训练?

image

CPU利用率的问题

为什么我训练的时候CPU利用率100%
而GPU利用率只有2%-5%左右
训练速度0.5s/step到0.8s/step左右
确认是使用的cuda,因为不用的话速度20s/step左右

CPU I5-4590
GPU GTX1060
有什么解决办法吗 cpu是不是拖后腿了

捕获

关于训练时的一个问题

box_loss_scale = 2 - box_loss_scale_x*box_loss_scale_y

loss_x = torch.sum(BCELoss(x, tx) / bs * box_loss_scale * mask)
loss_y = torch.sum(BCELoss(y, ty) / bs * box_loss_scale * mask)
loss_w = torch.sum(MSELoss(w, tw) / bs * 0.5 * box_loss_scale * mask)
loss_h = torch.sum(MSELoss(h, th) / bs * 0.5 * box_loss_scale * mask)

麻烦请教一下yolo_training.py这个文件里第90行这个式子是什么意思呢?

box_loss_scale = 2 - box_loss_scale_x*box_loss_scale_y

为什么要用2减那两个数的乘积呢?谢谢!

video

请问在运行video.py时出现这个问题是怎么回事啊?cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-2b5g8ysb\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

测试问题

你好博主,非常感谢分享。
请问测试时是直接运行test.py吗?

coco数据集转换voc格式

你好,如果想用coco数据集自己训练的话,该如何将coco数据集的格式转换成voc格式呢?这方面有没有什么好的参考?

[predict.py]Error(s) in loading state_dict for YoloBody

麻烦帮忙看看是啥问题么?谢谢
在根据B站教程中的P8测试完成后,运行predict.py后报错,报错日志如下:

(pytorch_gpu_0627) G:\0-App\8-Code\0CodeProject\yolo3-pytorch>python predict.py
Loading weights into state dict...
Traceback (most recent call last):
  File "predict.py", line 7, in <module>
    yolo = YOLO()
  File "G:\0-App\8-Code\0CodeProject\yolo3-pytorch\yolo.py", line 44, in __init__
    self.generate()
  File "G:\0-App\8-Code\0CodeProject\yolo3-pytorch\yolo.py", line 65, in generate
    self.net.load_state_dict(state_dict)
  File "G:\0-App\8-Code\Anaconda\envs\pytorch_gpu_0627\lib\site-packages\torch\nn\modules\module.py", line 845, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for YoloBody:
        size mismatch for last_layer0.6.weight: copying a param with shape torch.Size([75, 1024, 1, 1]) from checkpoint, the shape in current model is torch.Size([15, 1024, 1, 1]).
        size mismatch for last_layer0.6.bias: copying a param with shape torch.Size([75]) from checkpoint, the shape in current model is torch.Size([15]).
        size mismatch for last_layer1.6.weight: copying a param with shape torch.Size([75, 512, 1, 1]) from checkpoint, the shape in current model is torch.Size([15, 512, 1, 1]).
        size mismatch for last_layer1.6.bias: copying a param with shape torch.Size([75]) from checkpoint, the shape in current model is torch.Size([15]).
        size mismatch for last_layer2.6.weight: copying a param with shape torch.Size([75, 256, 1, 1]) from checkpoint, the shape in current model is torch.Size([15, 256, 1, 1]).
        size mismatch for last_layer2.6.bias: copying a param with shape torch.Size([75]) from checkpoint, the shape in current model is torch.Size([15]).

如何检测模型的精度?

您好,感谢您的代码分享。
我想问的问题是,如何利用测试集检测训练模型的精度 mAP呢?
我看到代码里有get-mAP的文件,但是怎么用您能说一下么?谢谢~

SPP模块

博主能把SPP模块也给实现了吗?SPP模块能给模型带来性能提升

boxloss改成ciou的问题

博主,您好:
我在尝试将这个v3版本的box的loss改成yolov4中的box的ciouloss, 用的也是您v4版本中的移植过来的,训练方式不变,但发现训练过程不稳定,loss跳动大,出来的检测结果也远不如v3, 请问您有什么好的解决方法嘛? 谢谢。

从头训练效果很差

模型在VOC2012从头训练效果很差,具体表现为经非极大值抑制后显示none,没有生成框

No module named 'utils.dataloader'

博主您好!从b站来的,我在train.py 的from utils.dataloader import yolo_dataset_collate, YoloDataset这里就报错No module named 'utils.dataloader'在百度上也搜不到所以来麻烦你,不知道怎么解决,跪求解答

关于noobj_mask

请问一下,noobj_mask看不太懂欸,它本身是跟mask数组反着的,然后在此基础上又把iou大于0.5的框设置为0,应该是为了不算这部分的loss,但我想不明白,他根本不影响mask数组,是怎么做到不算这部分的loss的?

Torch not compiled with CUDA enabled

The solution of using CPU to train in Windows Environment

1、 train.py
Change line 84 to Cuba = False
2、 train.py
Change line 108 to net = net.to(device)

坐等YOLO2

大佬,能分享一下yolov2的pytorch版本吗。

训练问题

你好,我是直接有图片和标签的txt文件,训练步骤中有一些没看懂,连续几步中能给出具体的文件格式吗,谢谢,我的邮箱是[email protected]

服务器训练好的权重文件放到本地无法预测

服务器环境torch170+cu101
本地环境 torch100+cu100
服务器训练好的权重文件传输到本地进行预测,报错_pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.

请问有人遇到过类似问题吗

IndexError: index 1 is out of bounds for dimension 4 with size 1

博主您好,这是我用自己的数据集出现的错误,找了半天也没解决,您看是怎么回事?谢谢!

Loading weights into state dict...
Finished!
Epoch 1/50: 0%| | 0/1 [00:08<?, ?it/s<class 'dict'>]
Traceback (most recent call last):
File "train.py", line 182, in
fit_ont_epoch(net,yolo_losses,epoch,epoch_size,epoch_size_val,gen,gen_val,Freeze_Epoch,Cuda)
File "train.py", line 44, in fit_ont_epoch
loss_item = yolo_losses[i](outputs[i], targets)
File "/home/dls/anaconda3/envs/cf/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in call
result = self.forward(*input, **kwargs)
File "/home/dls/文档/yolo3-pytorch-master/nets/yolo_training.py", line 108, in forward
self.ignore_threshold)
File "/home/dls/文档/yolo3-pytorch-master/nets/yolo_training.py", line 210, in get_target
tcls[b, best_n, gj, gi, int(target[b][i, 4])] = 1
IndexError: index 1 is out of bounds for dimension 4 with size 1

大佬这段代码为什么真实框要用0,0啊

        # 计算真实框的位置
        gt_box = torch.FloatTensor(torch.cat([torch.zeros_like(gws), torch.zeros_like(ghs), gws, ghs], 1))
        
        # 计算出所有先验框的位置
        anchor_shapes = torch.FloatTensor(torch.cat((torch.zeros((self.num_anchors, 2)), torch.FloatTensor(anchors)), 1))
        # 计算重合程度
        anch_ious = jaccard(gt_box, anchor_shapes)

2007_train.txt真实框位置

您好,请问真实框中框的4个数据是左上角和右下角两点坐标的组合,还是中心点坐标和框的长宽的组合?

MAP

请问在计算MAP时为什么只有人被检测到,只有人有MAP,然后groud truth生成的text文件里面也只有人呢

重新训练的问题

参考了yolov3gluoncv的训练指南,重新开始训练的话,也都要加载在imagenet上预训练的模型。
不知博主有没有能够兼容这个代码的预训练权重文件。

关于损失函数loss_conf的疑问

loss_conf = torch.sum(BCELoss(conf, mask) / batch_size * mask) + torch.sum(BCELoss(conf, mask) / batch_size * noobj_mask)
这一步总觉得有问题,负采样的地方为什么还是BCELoss(conf, mask)呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.