环境安装

使用虚拟环境conda,电脑上安装anaconda。

创建新的conda环境:

conda create --name ultralytics-env python=3.8 -y

激活环境:

conda activate ultralytics-env

安装yolo:

conda install -c conda-forge ultralytics

安装PyThorch(要下载很久很久):

在官网找到pip的下载连接:https://pytorch.org/get-started/locally/

推理

运行官方例子:

from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO('yolov8n.pt')

# Define path to video file
source = 'path/to/video.mp4'

# Run inference on the source
results = model(source, stream=True) # generator of Results objects

for result in results:
boxes = result.boxes # Boxes object for bbox outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs

少什么装什么

在Windows的Conda环境中解决缺少GStreamer插件的问题,

安装GStreamer库和插件。可以使用Conda包管理器来安装GStreamer及其相关的插件:

conda install -c conda-forge gstreamer

安装GStreamer的好用的Python绑定库:

conda install -c conda-forge pygobject

安装GStreamer的Python绑定库(Gst-Python):

conda install -c conda-forge gst-python

安装OpenCV(如果尚未安装):

conda install -c conda-forge opencv

其他训练方式和返回方式各种接口官方都有演示

 

训练

标注图片

安装labeling

pip install labelImg

运行

labelImg

标注之后就可以训练

yolo detect train data=coco128.yaml model=yolov8n.yaml pretrained=yolov8n.pt epochs=100 imgsz=640

这里卡了一会,一直没有搞明白coco128.yaml怎么来的

data=coco128.yaml这个在官方源码内有,复制粘贴就行,就是一个指示训练位置的一个配置文件。将内容path、train、val、test进行填写就行,分别是文件夹位置,文件夹位置下的训练集、验证集、测试集、下边的names就是对应标注的文件类别。比如我整套文件就是为了识别马里奥的,那么就一个种类是马里奥……,如果带上桃子公主,库霸王,那就是三个类。

 

# Ultralytics YOLO 🚀, AGPL-3.0 license
# COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
# Documentation: https://docs.ultralytics.com/datasets/detect/coco/
# Example usage: yolo train data=coco128.yaml
# parent
# ├── ultralytics
# └── datasets
# └── coco128 ← downloads here (7 MB)

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: /Users/zhang/yolo/datasets/trainzz # dataset root dir
train: images/train2017 # train images (relative to ‘path’) 128 images
val: images/train2017 # val images (relative to ‘path’) 128 images
test: # test images (optional)

# Classes
names:
0: person
1: bicycle
2: car
3: motorcycle
4: airplane
5: bus

 

 

model=yolov8n.yaml

这个是选择对应模型的配置文件填写对应位置就行

pretrained=yolov8n.pt

这个是选的模型位置

剩下两个参数是训练次数和图片大小

对应的配置文件位置可以填相对路径也可以填绝对路径。

理解参数之后运行就可以,会在当前运行目录生成\runs\detect\train,根据训练的不同次数生成\train*

在\train*\下就是训练的可视化的一些展示。一些数据的统计。

生成的模型在\train*\weights\下。一般两个 last.pt 和best.pt。

如果是完整训练完了两个没区别。

转化

官网给的参数很全

yolo export model=yolov8n.pt format=onnx

对应参数表,有对应的量化简化之类的。

使用

使用的方式太多了。能想到就能用上。

 

 

 

 

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注