【目标检测】yolov3的原理及代码复现

文章目录

  • YOLO学习
  • YOLO_V3的代码复现

YOLO学习

  • YOLO_V1
    原文:https://arxiv.org/pdf/1506.02640.pdf
    详解:https://www.jianshu.com/p/5500b5a77f40

  • YOLO_V2
    原文: https://arxiv.org/pdf/1612.08242.pdf
    详解:
    https://www.jianshu.com/p/da569d729ac8
    https://www.jianshu.com/p/3c3b49ef4ffc

  • NMS(Non-Max Suppression,非极大值抑制)

     NMS算法一般是为了去掉模型预测后的多余框,其一般设有一个nms_threshold=0.5,具体的实现思路如下:
     
     选取这类box中scores最大的那一个,记为box_best,并保留它
     计算box_best与其余的box的IOU
     如果其IOU>0.5了,那么就舍弃这个box(由于可能这两个box表示同一目标,所以保留分数高的那一个)
     从最后剩余的boxes中,再找出最大scores的那一个,如此循环往复
     
     可以理解为:
     先假设有6个输出的矩形框(即proposal_clip_box),根据分类器类别分类概率做排序,从小到大分别属于车辆的概率(scores)
     分别为A、B、C、D、E、F。
     (1)从最大概率矩形框F开始,分别判断A~E与F的重叠度IOU是否大于某个设定的阈值;
    
     (2)假设B、D与F的重叠度(IOU)超过阈值,那么就扔掉B、D;并标记第一个矩形框F,是我们保留下来的。
    
     (3)从剩下的矩形框A、C、E中,选择概率最大的E,然后判断E与A、C的重叠度,重叠度大于一定的阈值,那么就扔掉;并标记E是我们保留下来的第二个矩形框。
     
     就这样一直重复,找到所有被保留下来的矩形框。
    
  • YOLO_V3原理:
    https://www.jianshu.com/p/043966013dde
    https://www.cnblogs.com/AntonioSu/p/12164255.html

  • YOLO_V3代码:
    https://github.com/eriklindernoren/PyTorch-YOLOv3

YOLO_V3的代码复现

代码: https://codechina.csdn.net/mirrors/eriklindernoren/pytorch-yolov3?utm_source=csdn_github_accelerator

  • Installing from source

    sudo git clone https://github.com/eriklindernoren/PyTorch-YOLOv3
    cd PyTorch-YOLOv3/
    pip3 install poetry --user
    poetry install
    
  • Download pretrained weights

    xiaoxie@xiaoxie-Z10PE-D8-WS:~/data/yx/python/PyTorch-YOLOv3/weights$  sudo sh download_weights.sh
    

    报错:

    ERROR: cannot verify pjreddie.com's certificate, issued by ‘CN=R3,O=Let's Encrypt,C=US’:   Unable to locally verify the issuer's authority.
    

    解决:
    https://blog.csdn.net/liuqiangaliuv/article/details/90116046

  • Download COCO

    xiaoxie@xiaoxie-Z10PE-D8-WS:~/data/yx/python/PyTorch-YOLOv3/data$ sudo sh get_coco_dataset.sh 
    # 解决问题的方法同上面一样
    

    问题

    sh ./xxxxx.sh出现:“Syntax error:(” unexpected”
    

    解决:将sh改成bash

    (yolo) xiaoxie@xiaoxie-Z10PE-D8-WS:~/data/yx/python/PyTorch-YOLOv3/data/coco$ sudo bash get_coco_dataset.sh
    
  • Install via pip

     pip3 install pytorchyolo --user
    

    问题:

    ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
    launchpadlib 1.10.6 requires testresources, which is not installed.
    poetry 1.1.11 requires importlib-metadata<2.0.0,>=1.6.0; python_version < "3.8", but you have importlib-metadata 4.8.2 which is incompatible.
    poetry-core 1.0.7 requires importlib-metadata<2.0.0,>=1.7.0; python_version >= "2.7" and python_version < "2.8" or python_version >= "3.5" and python_version < "3.8", but you have importlib-metadata 4.8.2 which is incompatible.
    

    解决:

    (yolo) xiaoxie@xiaoxie-Z10PE-D8-WS:~/data/yx/python/PyTorch-YOLOv3$ pip install importlib-metadata==1.7.0 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
    
  • 文件组织架构

    ├── checkpoints/  #模型
    ├── data/  #数据
    │   ├── get_coco_dataset.sh
    │   ├── coco.names
    ├── utils/  #使用的函数
    │   ├── __init__.py
    │   ├── datasets.py
    │   └── utils.py
    ├── config/  #配置文件
    ├── output/  #输出预测
    ├── weights/ #模型权重
    ├── README.md 
    ├── models.py #模型
    ├── train.py  #训练
    ├── test.py   #测试
    ├── detect.py #快速使用模型
    └── requirements.txt  #环境
    
  • 数据架构
    【目标检测】yolov3的原理及代码复现_第1张图片

  • Test

     poetry run yolo-test --weights weights/yolov3.weights
     poetry run yolo-detect --images data/samples/
    

    【目标检测】yolov3的原理及代码复现_第2张图片

  • Train
    Example (COCO)
    To train on COCO using a Darknet-53 backend pretrained on ImageNet run:

     poetry run yolo-train --data config/coco.data  --pretrained_weights weights/darknet53.conv.74
    

    使用多块GPU
    https://blog.csdn.net/weixin_43593330/article/details/108636686
    https://blog.csdn.net/weixin_41990278/article/details/105127101

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
    
    # ############
    # Create model
    # ############
    
    model = load_model(args.model, args.pretrained_weights)
    # 使用多块GPU
    if torch.cuda.device_count()>1:
        model = torch.nn.DataParallel(model, device_ids = [0, 1, 2, 3])
    model.to(device)
    
    if isinstance(model,torch.nn.DataParallel):
        model = model.module
    

你可能感兴趣的:(pytorch,ubuntu,python)