一个0到1的案例 后续会继续补充
环境python3.8.10
First
- github下载项目:https://github.com/ultralytics/yolov5/tree/master
- cd yolov5, pip install -r requirements.txt -i 清华源
- pip install lambelimg (打标签用的)
Second
- 本地创建文件夹(yolov5同目录),如下图(仅供参考)
- 利用labelimg打标签完后,开始训练
时间挺漫长,建议gpu跑
test_train代码如下:
import train
# if __name__ == '__main__':
# train.run(data=r'D:\develop2\python_virtuals\3\datasets\coco128\test.yaml',
# batch=8, epochs=100, single_cls=True,
# weights=r'D:\develop2\python_virtuals\3\yolov5-master\yolov5s.pt')
# test
if __name__ == '__main__':
train.run(data=r'E:\Code\Python\yolov5py38\yolov5\data\dog_and_cat.yaml',
batch=1, epochs=100, single_cls=False, resume=True, workers=1, device='cpu',
weights=r'E:\Code\Python\yolov5py38\yolov5\yolov5s.pt')
- 训练完后,生成属于自己的best.pt or last.pt文件(自行百度介绍)
Thrid
- 将
best.pt
复制到yolov5目录下,如图
- 使用命令:
python detect.py --weights best.pt --source ../dataset/dog_and_cat/images/val
(val存放验证集图片),效果如下:
可参考
https://github.com/ultralytics/yolov5/tree/master
https://zhuanlan.zhihu.com/p/501798155