赞
踩
yolov8的推理速度对比如下,极大的提高了训练速度。
官方提供了两种形式的安装方法,这里如果只是玩玩的话建议使用是一种方法安装,为了扩展,开发的可以是第二种方法安装(本文采用的是第二种方法,为了更好的管理项目)
- #方法一(建议)
- pip install ultralytics
-
- #方法二
-
- git clone https://github.com/ultralytics/ultralytics
- cd ultralytics
- pip install -e .
这里也分为两种方法
1、命令行
Syntax | yolo TASK MODE ARGS Where TASK (optional) is one of [detect, segment, classify] |
Train | yolo train data=coco128.yaml model=yolov8n.pt epochs=10 lr0=0.01 |
Predict | yolo predict model=yolov8n-seg.pt source='https://youtu.be/Zgi9g1ksQHc' imgsz=320 |
Val | yolo val model=yolov8n.pt data=coco128.yaml batch=1 imgsz=640 |
Export | yolo export model=yolov8n-cls.pt format=onnx imgsz=224,128 |
Special | yolo help yolo checks yolo version yolo settings yolo copy-cfg yolo cfg |
下面是在命令行训练的例子:
在运行这个代码的时候可能会有些慢,因为要下载数据集和预训练权重,请小伙伴们耐心等待。
但是考虑到外网等问题,我将数据集和预训练权重放到了百度网盘网盘上,更新后的百度网盘地址需要的可以自取
我们可以找到我们下载的yolov8的位置,这里初始是只有ultralytics一个文件夹,我们新建一个datasets文件夹,并将从网盘下载的coco128移动到datasets文件夹内,将下载的权重移动到和ultralytics同层次的文件夹内。(在这里第二种方法下载yolov8的优势就体现出来了,可以很方便的找到对应的文件。
如果我们需要训练自己的数据集,我们的数据集可以按照coco128的形式整理,然后放到dataset下就可以(推荐大家使用makesense标注数据,直接为yolo格式的txt文件)。然后为在这里将标注的xml文件转为txt文件的代码,供大家参考。
- import xml.etree.ElementTree as ET
-
- import pickle
- import os
- from os import listdir , getcwd
- from os.path import join
- import glob
-
- classes = ["cone tank", "water horse bucket"]
-
- def convert(size, box):
-
- dw = 1.0/size[0]
- dh = 1.0/size[1]
- x = (box[0]+box[1])/2.0
- y = (box[2]+box[3])/2.0
- w = box[1] - box[0]
- h = box[3] - box[2]
- x = x*dw
- w = w*dw
- y = y*dh
- h = h*dh
- return (x,y,w,h)
-
- def convert_annotation(image_name):
- in_file = open('./indata/'+image_name[:-3]+'xml') #xml文件路径
- out_file = open('./labels/train/'+image_name[:-3]+'txt', 'w') #转换后的txt文件存放路径
- f = open('./indata/'+image_name[:-3]+'xml')
- xml_text = f.read()
- root = ET.fromstring(xml_text)
- f.close()
- size = root.find('size')
- w = int(size.find('width').text)
- h = int(size.find('height').text)
-
-
-
-
- for obj in root.iter('object'):
- cls = obj.find('name').text
- if cls not in classes:
- print(cls)
- continue
- cls_id = classes.index(cls)
- xmlbox = obj.find('bndbox')
- b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
- float(xmlbox.find('ymax').text))
- bb = convert((w,h), b)
- out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
-
- wd = getcwd()
-
- if __name__ == '__main__':
-
- for image_path in glob.glob("./images/train/*.jpg"): #每一张图片都对应一个xml文件这里写xml对应的图片的路径
- image_name = image_path.split('\\')[-1]
- convert_annotation(image_name)
大家把数据整理完成之后需要进入ultralytics文件夹内的datasets文件夹下新建mydata.yaml
其中mydata.yaml文件内部的内容是将coco128.yaml文件的内容复制进去,对其的修改如下图所示:
最后还需要修改ultralytics文件夹内的models文件夹的v8文件夹内yolov8.yaml,将其的nc由80改为你数据集的类别,我这里是两类。
到这里就可以运行代码,训练我们自己的数据了
eg:
2、使用python代码运行
- from ultralytics import YOLO
-
- # Create a new YOLO model from scratch
- model = YOLO('yolov8n.yaml')
-
- # Load a pretrained YOLO model (recommended for training)
- model = YOLO('yolov8n.pt')
-
- # Train the model using the 'coco128.yaml' dataset for 3 epochs
- results = model.train(data='coco128.yaml', epochs=3)
-
- # Evaluate the model's performance on the validation set
- results = model.val()
-
- # Perform object detection on an image using the model
- results = model('https://ultralytics.com/images/bus.jpg')
-
- # Export the model to ONNX format
- success = model.export(format='onnx')
话不多说,先放代码
- import cv2
- from ultralytics import YOLO
-
- # 模型加载权重
-
- model = YOLO('yolov8n.pt')
-
- # 视频路径
- video_path = "path/to/your/video/file.mp4"
- cap = cv2.VideoCapture(video_path)
-
- # 对视频中检测到目标画框标出来
- while cap.isOpened():
- # Read a frame from the video
- success, frame = cap.read()
-
- if success:
- # Run YOLOv8 inference on the frame
- results = model(frame)
-
- # Visualize the results on the frame
- annotated_frame = results[0].plot()
-
- # Display the annotated frame
- cv2.imshow("YOLOv8 Inference", annotated_frame)
-
- # Break the loop if 'q' is pressed
- if cv2.waitKey(1) & 0xFF == ord("q"):
- break
- else:
- # Break the loop if the end of the video is reached
- break
-
- # Release the video capture object and close the display window
- cap.release()
- cv2.destroyAllWindows()
没了没了,yolov8大概就这么多吧,然后给大家一个避坑技巧就是大家使用强制转化的图片训练的时候会有一个警告
大家可以把图片用opencv打开之后再保存即可代码如下:
- import os
- import cv2
- dataDir="images/val1/"
- saveDir="images/val/"
- if not os.path.exists(saveDir):
- print('ok')
- os.makedirs(saveDir)
- for one_pic in os.listdir(dataDir):
- one_path=dataDir+one_pic
- one_img=cv2.imread(one_path)
- new_path=saveDir+one_pic
- cv2.imwrite(new_path, one_img)
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。