环境
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.youkuaiyun.com/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