pytorch_yolov4代码训练数据集

环境
windows10+python3.7 + pytorch(cuda10)

1.准备工作

1.pytorch_YOLOv4代码下载:pytorch_YOLOv4
导入相应的模块:

  • numpy==1.18.2
    pip install numpy==1.18.2 -i https://pypi.douban.com/simple/
    下面类推。
  • torch==1.4.0
  • tensorboardX==2.0
  • scikit_image==0.16.2`
  • matplotlib==2.2.3
  • tqdm==4.43.0
  • easydict==1.9
  • Pillow==7.1.2
  • opencv_python
  • pycocotools

2.权重文件下载
yolov4.conv.137.pth
或者百度云下载 提取码 kcel

将权重文件放到/data下
在这里插入图片描述

2.数据集处理

coco数据集下载
coco2017数据集
http://images.cocodataset.org/zips/train2017.zip
http://images.cocodataset.org/annotations/annotations_trainval2017.zip

http://images.cocodataset.org/zips/val2017.zip
http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip

http://images.cocodataset.org/zips/test2017.zip
http://images.cocodataset.org/annotations/image_info_test2017.zip

For coco dataset,you can use tool/coco_annotatin.py.
要修改一下代码,数据集的文件位置
在这里插入图片描述
最好要用绝对路径,你放在liunx上面跑代码你就知道了
在这里插入图片描述
处理对应的instances_train2017.json

运行coco_annotatin.py
在这里插入图片描述
会在data目录下生成train.txt文件

格式如下

# train.txt
image_path1 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
image_path2 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
...
...

在运行一下coco_annotatain.py 生成val.txt

额外加的模块 在这边重点说明一下

在这里插入图片描述
将coco数据集的三个文件下载下来解压 成以下三个文件夹
在这里插入图片描述
修改coco_annotatain.py文件中的代码 第一次生成train.txt文件 修改如下

json_file_path = 'F:/car/dataset/annotations/instances_train2017.json'
images_dir_path = 'F:/car/dataset/train2017'
output_path = './data/train.txt'

第二次生成val.txt文件,修改如下

json_file_path = 'F:/car/dataset/annotations/instances_val2017.json'
images_dir_path = 'F:/car/dataset/val2017'
output_path = './data/val.txt'

运行两次coco_annotatain.py就可以生成train.txt和val.txt文件了。
在这里插入图片描述
如果遇到这个ModuleNotFoundError: No module named ‘pycocotools‘错误
https://blog.csdn.net/canpian7/article/details/115370766?spm=1001.2014.3001.5501

3.训练

需要在cfg.py中修改几个地方

Cfg.gpu = '0'
Cfg.pretrained = 'path to yolov4.conv.137.pth'
Cfg.dataset_dir = ' '
Cfg.batch = 16  修改为合适的值
Cfg.classes = 80 修改为实际值

修改为第二步数据集处理生成的文件路径

Cfg.train_label = 'data/train.txt'
Cfg.val_label = 'data/val.txt'

修改中间结果路径,checkpoints和log的路径

Cfg.checkpoints = 'checkpoints'
Cfg.TRAIN_TENSORBOARD_DIR = 'log'

开始训练

因为修改了cfg.py所以这边就可以直接运行train.py了

python train.py

在这里插入图片描述
总算是跑起来了

预测
python demo.py -cfgfile cfg/yolov4.cfg -weightfile weights/yolov4.weights -imgfile data/dog.jpg

常见错误汇总

  1. ModuleNotFoundError: No module named ‘pycocotools‘
  2. FileNotFoundError: [Errno 2] No such file or directory: ‘train.txt’
  3. TypeError: expected str, bytes or os.PathLike object, not NoneType
  4. DefaultCPUAllocator: not enough memory: you tried to allocate 94633984 bytes. Buy new RAM!
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值