0%

Yolov5目标检测

通过Google Colab平台训练yolov5模型,实现对可乐罐的识别

1
2
3
4
5
6
7
8
9
10
# 导入Yolov5模块
!git clone https://github.com/ultralytics/yolov5 # clone repo
%cd yolov5
%pip install -qr requirements.txt # install dependencies

import torch
from IPython.display import Image, clear_output # to display images

clear_output()
print('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))
1
2
# 上传数据集并解压
!unrar x ../yolo_C ../
1
2
3
# 检测是否正常
!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images/
Image(filename='runs/detect/exp/zidane.jpg', width=600)
1
2
# 训练模型
!python train.py --img 640 --batch 50 --epochs 200 --data /content/yolo_C/A.yaml --weights yolov5s.pt --nosave --cache
1
2
3
4
# 使用模型进行检测
!python detect.py --weights /content/yolov5/runs/train/exp/weights/last.pt --img 640 --conf 0.25 --source ../test1.jpg

Image(filename='runs/detect/exp2/test1.jpg', width=600)

效果图: