1、下载源码-安装依赖
1.1、根据git下载-安装依赖
#拉取源码
git clone https://github.com/ultralytics/yolov5 # clone
#进入yolo5
cd yolov5
#运行安装命令
pip install -r requirements.txt # install
1.2、直接下载
url地址:https://github.com/ultralytics/yolov5
然后进入yolov5-master文件夹,进入文件夹cmd页面运行
pip install -r requirements.txt
pip install -r requirements.txt是yolo安装依赖命令,主要是源码主项目中的requirements.txt配置文件,命令需要运行在该文件所在目录。
1.3安装运行详情
2、项目结构解析
2.1、图片解析
3、yolo5模型下载
根据上面我们选择的yoloy5s.yaml配置文件我们选择这个模型即可。
下载地址:https://github.com/ultralytics/yolov5/releases
下载完后将模型放到项目中,运行train文件的时候修改一下对应模型地址即可。
4、测试官方模型
4.1、detect文件重点参数解读
#指定模型
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path or triton URL')
#指定测试图片
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob/screen/0(webcam)')
#指定data对应的配置文件
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
#置信度阈值
parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
#目标框和真实框的iou阈值
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--view-img', action='store_true', help='show results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--visualize', action='store_true', help='visualize features')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default=ROOT / 'runs/detect', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
parser.add_argument('--vid-stride', type=int, default=1, help='video frame-rate stride')
4.2、运行
右击点击运行
运行结果如图:
总结:这里主要使用的是yolo5s模型,这个模型yolo已经训练好了,在train文件中指定对应的模型,测试的图片即可,如果有些地方没有识别出来可以通过调整iou参数来进行识别。
5、自定义训练
5.1、下载图片标注工具-----labelimg
打开pycharm—>打开terminal----->运行pip install labelimg -i https://mirror.baidu.com/pypi/simple
5.2、准备训练数据
在data文件夹中新增一个train文件夹,下面新建images(存放训练数据)和labels文件夹(存放标注数据)
踩坑点
放训练图片的文件夹名必须为images,放标注的文件夹名必须为labels,训练的时候源码会去寻找这两个文件夹,然后读取数据,如果不是这个文件夹名会报错,可把我恶习死了
5.3、标注工具下载及使用
输入labelimg进入标注软件
标注完后
5.4、运行train进行训练
训练迭代次数及配置选择
图片个数 | 迭代次数 | 电脑配置 | 对应精确度 |
---|---|---|---|
5 | 100 | i5+12g | 0.03 |
5 | 500 | i5+12g | 0.6 |
7 | 400 | i5+16g | 0.7 |
运行内存小一次性训练的图片不能过多,不然会内存溢出,训练的图片画质越好,标注的越清晰,训练图片越多,迭代次数越多,模型精确度越高。
6、运行训练好的模型
修改一下验证图片地址