YOLOv5 windows CPU搭建

编程入门 行业动态 更新时间:2024-10-15 18:24:05

YOLOv5 <a href=https://www.elefans.com/category/jswz/34/1771365.html style=windows CPU搭建"/>

YOLOv5 windows CPU搭建

目录

一、创建环境

二、导入pytorch库

三、源码测试

3.1下载源码

3.2更改解释器

 3.3安装依赖项

 

 3.4测试

图片测试

 其他测试

下载权重文件

四、训练数据集

4.1制作自己的数据集

4.2修改配置文件

cat_parameter.yaml

yolov5m.yaml

五、开始训练

5.1train.py参数配置

5.2训练报错整理

error1

solution

六、结果


本博客主要参考了以下两版教程,并对其中部分细节进行了进一步梳理。(25条消息) 在CPU上跑yolov5(详细步骤+适合入门)_yolov5 cpu_她叫谢雨路的博客-CSDN博客

(25条消息) 【Yolov5】1.认真总结6000字Yolov5保姆级教程(2022.06.28全新版本v6.1)_yolov5教程_若oo尘的博客-CSDN博客


一、创建环境

系统:win11

python:3.10

pytorch>1.7

安装anaconda

打开cmd窗口,输入conda create -n yolov5 python=3.10,创建新的环境,避免配置时出错影响其他环境。

激活新创建的环境:

 输入activate yolov5,当C:前面出现yolov5(设置环境名)时,激活成功。

二、导入pytorch库

进入Pytorch官网PyTorch,选择如下参数:

 复制run this command中的代码到cmd中运行。

也可以选择老版本Previous PyTorch Versions | PyTorch,但Pytorch版本应该大于1.7。

 出现done后表示pytorch添加成功。

三、源码测试

3.1下载源码

mirrors / ultralytics / yolov5 · GitCode下载ultralytics大神的yolov5开源代码。

3.2更改解释器

解压后以IDE打开,下面以vscode为例。

 选择解释器路径为刚创建的虚拟环境解释器。

 

 3.3安装依赖项

进入requirements.txt文件,复制第二行代码pip install -r requirements.txt,到终端中运行。

 

如果没有报错信息出现代表安装成功。

 3.4测试

图片测试

运行左侧的detect.py文件

如果该文件夹出现下列两个图片,则代表yolov5网络copy成功,后续可以尝试训练自己的数据集。

 其他测试

可以在终端输入下面这行代码,.后面是文件路径

python detect.py --source ./data/video/test.mp4 --weights ./weights/yolov5s.pt --conf 0.4

 下面是可供选择的文件类型,source和weight都可以更改,source放自己想要测试的视频/图片等,weight可以去下载作者做好的权重文件,也可以自己跑训练集后输入。

Usage - sources:$ python detect.py --weights yolov5s.pt --source 0                               # webcamimg.jpg                         # imagevid.mp4                         # videoscreen                          # screenshotpath/                           # directorylist.txt                        # list of imageslist.streams                    # list of streams'path/*.jpg'                    # glob''  # YouTube'rtsp://example/media.mp4'  # RTSP, RTMP, HTTP streamUsage - formats:$ python detect.py --weights yolov5s.pt                 # PyTorchyolov5s.torchscript        # TorchScriptyolov5s.onnx               # ONNX Runtime or OpenCV DNN with --dnnyolov5s_openvino_model     # OpenVINOyolov5s.engine             # TensorRTyolov5s.mlmodel            # CoreML (macOS-only)yolov5s_saved_model        # TensorFlow SavedModelyolov5s.pb                 # TensorFlow GraphDefyolov5s.tflite             # TensorFlow Liteyolov5s_edgetpu.tflite     # TensorFlow Edge TPUyolov5s_paddle_model       # PaddlePaddle

或者也可以通过更改detect.py的代码:

def parse_opt():parser = argparse.ArgumentParser()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)')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')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')opt = parser.parse_args()opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1  # expandprint_args(vars(opt))return opt
下载权重文件

可以在下面网站中下载作者训练好的权重文件,不同权重的识别效果不同,训练对应权重的时间也不同。
Releases · ultralytics/yolov5 (github)

 将下载好 的权重文件放在根目录下,与代码中的文件路径对应

四、训练数据集

引用下面博客

(25条消息) 在CPU上跑yolov5(详细步骤+适合入门)_yolov5 cpu_她叫谢雨路的博客-CSDN博客

4.1制作自己的数据集

 在根目录下创建好上述文件夹,将训练用图片导入到images的train中去,训练图可自己拍摄,也可以找公开的数据集,自己拍摄最好在100+张。

训练集为了节省时间直接和训练集用同一套了。

(26条消息) 如何正确使用机器学习中的训练集、验证集和测试集?_nkwshuyi的博客-CSDN博客

使用labelme函数为图片一一创建标签图像标注软件labelme - 知乎 (zhihu)

将labelme的输出路径选为/label/json,由于yolov5无法直接识别json文件,采用以下代码转化为txt,自己改下对应的路径和标签名称。

import json
import osname2id = {'cat': 0}  # 标签名称def convert(img_size, box):dw = 1. / (img_size[0])dh = 1. / (img_size[1])x = (box[0] + box[2]) / 2.0 - 1y = (box[1] + box[3]) / 2.0 - 1w = box[2] - box[0]h = box[3] - box[1]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def decode_json(json_floder_path, json_name):txt_name = 'D:/studydata/deeplearning/YOLO/yolov5-master-pytorch/yolov5-master/owndata/cat/datasets/labels/txt/' + json_name[0:-5] + '.txt'# txt文件夹的绝对路径txt_file = open(txt_name, 'w')json_path = os.path.join(json_floder_path, json_name)data = json.load(open(json_path, 'r', encoding='gb2312', errors='ignore'))img_w = data['imageWidth']img_h = data['imageHeight']for i in data['shapes']:label_name = i['label']if (i['shape_type'] == 'rectangle'):x1 = int(i['points'][0][0])y1 = int(i['points'][0][1])x2 = int(i['points'][1][0])y2 = int(i['points'][1][1])bb = (x1, y1, x2, y2)bbox = convert((img_w, img_h), bb)txt_file.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n')if __name__ == "__main__":json_floder_path = 'D:/studydata/deeplearning/YOLO/yolov5-master-pytorch/yolov5-master/owndata/cat/datasets/labels/json/'# json文件夹的绝对路径json_names = os.listdir(json_floder_path)for json_name in json_names:decode_json(json_floder_path, json_name)

4.2修改配置文件

cat_parameter.yaml

在yolov5/data/coco128.yaml中先复制一份,粘贴到owndata/cat/中,改名为cat_parameter.yaml

cat_parameter.yaml文件需要修改的参数是nc与names。nc是标签名个数,names就是标签的名字,以及文件路径也要修改。

path: owndata/cat/datasets  # dataset root dir
train: images/train  # train images (relative to 'path') 128 images
val: images/train  # val images (relative to 'path') 128 images
test:  # test images (optional)nc: 1  # number of classes
names: ['cat']  # class names

如果标签数多,names之间用,隔开。

path是绝对路径

train是在path绝对路径条件下的训练集路径,即:wzry/datasets/images/train

val同上,但是是验证集,为了方便,让训练集和验证集是一个。。

test可不填

yolov5m.yaml

 直接在yolov5/models复制对应的配置到cat文件夹中,更改里面nc参数

五、开始训练

5.1train.py参数配置

    parser = argparse.ArgumentParser()parser.add_argument('--weights', type=str, default=ROOT / 'yolov5m-seg.pt', help='initial weights path')parser.add_argument('--cfg', type=str, default='owndata/cat/yolov5m.yaml', help='model.yaml path')parser.add_argument('--data', type=str, default=ROOT / 'owndata/cat/cat_parameter.yaml', help='dataset.yaml path')parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path')parser.add_argument('--epochs', type=int, default=150, help='total training epochs')parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')parser.add_argument('--rect', action='store_true', help='rectangular training')parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')parser.add_argument('--noval', action='store_true', help='only validate final epoch')parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')parser.add_argument('--noplots', action='store_true', help='save no plot files')parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')parser.add_argument('--device', default='0', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW'], default='SGD', help='optimizer')parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name')parser.add_argument('--name', default='exp', help='save to project/name')parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')parser.add_argument('--quad', action='store_true', help='quad dataloader')parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')parser.add_argument('--seed', type=int, default=0, help='Global training seed')parser.add_argument('--local_rank', type=int, default=1, help='Automatic DDP Multi-GPU argument, do not modify')

主要更改default参数:

设置weights/cfg/data三者的路径,

epochs训练轮次,越多训练效果拟合越好,太多会过拟合

batch-size批次大小,越大越快,但是会导致cpu显存不够用

device选择cpu or gpu训练

workers多线程设置,大了容易报错

其他的可以看看help后面写的

5.2训练报错整理

error1

ImportError: Bad git executable.
The git executable must be specified in one of the following ways:
    - be included in your $PATH
    - be set via $GIT_PYTHON_GIT_EXECUTABLE
    - explicitly set via git.refresh()

All git commands will error until this is rectified.

This initial warning can be silenced or aggravated in the future by setting the

solution

安装git:(25条消息) Git 详细安装教程(详解 Git 安装过程的每一个步骤)_git安装_mukes的博客-CSDN博客

六、结果

最后结果如下所示

更多推荐

YOLOv5 windows CPU搭建

本文发布于:2024-03-13 17:19:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:windows   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!