文章目录
- 前言
- 一、平台环境配置
- 二、基础环境配置
- 1.代码下载
- 2.环境安装
- 3.模型下载
- 4.代码修改
- 三.单卡推理
- 四、单/多卡训练
前言
本章主要操作以yolov8为主,但是yolov10用该操作也能直接适用,开干!
一、平台环境配置
镜像选择:v24.06-torch2.3.0-catch1.21.0-ubuntu22.04-py310
驱动版本:HYG-2*370-5.10.29
存储卷记得挂载,已经有很多小伙伴根目录超限环境崩溃
二、基础环境配置
1.代码下载
git clone https://githubfast.com/ultralytics/ultralytics.git
注释ultralytics/pyproject.toml"torch>=1.8.0","torchvision>=0.9.0"
因为我们镜像中本身就自带了torch全家桶,如果不注释会安装torch1.8得最低版本包
之后源码安装该代码
2.环境安装
上述修改完成之后
pip install -e ./ultralytics
torchvision安装【其实就是安装个cpu版本的】
去这里面搜索https://download.pytorch.org/whl/torch_stable.html
torchvision-0.18.0+cpu-cp310-cp310-linux_x86_64.whl
进行本地安装
3.模型下载
yolov8n.pt
https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8n.pt模拟数据下载
https://ultralytics.com/assets/coco8.zip
4.代码修改
ultralytics/ultralytics/data/utils.py
#他下载速度太慢了还容易超时,有需要的自行下载
注释:
check_font("Arial.ttf" if is_ascii(data["names"]) else "Arial.Unicode.ttf")ultralytics/ultralytics/utils/tal.py
修改151-152行
count_tensor = torch.zeros(metrics.shape, dtype=torch.int8, device=topk_idxs.device)
ones = torch.ones_like(topk_idxs[:, :, :1], dtype=torch.int8, device=topk_idxs.device)
将两行的int8改成int32ultralytics/ultralytics/engine/trainer.py
最上面添加两行代码
import torch_mlu
from torch_mlu.utils.model_transfer import transfer
三.单卡推理
from ultralytics import YOLO
import torch_mlu
from torch_mlu.utils.model_transfer import transfer
# Load a model
model = YOLO("yolov8n.yaml") # build a new model from scratch
model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
# metrics = model.val() # evaluate model performance on the validation set
results = model("https://ultralytics.com/images/bus.jpg") # predict on an image
(pytorch) root@notebook-devenviron-0808-143257-13b1tdu-notebook-0:/workspace/volume/guojunceshi2/yolov8/ddd# python dete.py Found https://ultralytics.com/images/bus.jpg locally at bus.jpg
[2024-8-15 23:15:19] [CNNL] [Warning]:[cnnlGetConvolutionForwardAlgorithm] is deprecated and will be removed in the future release. See cnnlFindConvolutionForwardAlgorithm() API for replacement.
[2024-8-15 23:15:19] [CNNL] [Warning]:[cnnlGetNmsWorkspaceSize_v3] is deprecated and will be removed in the future release, please use [cnnlGetNmsWorkspaceSize_v4] instead.
/workspace/volume/guojunceshi2/yolov8/ultralytics/ultralytics/engine/results.py:646: UserWarning: Casting input of dtype int64 to int32, maybe overflow! (Triggered internally at /catch/torch_mlu/csrc/aten/utils/cnnl_util.cpp:117.)n = (boxes.cls == c).sum() # detections per class
image 1/1 /workspace/volume/guojunceshi2/yolov8/ddd/bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 11.7ms
Speed: 3.1ms preprocess, 11.7ms inference, 3.8ms postprocess per image at shape (1, 3, 640, 480)
/opt/py3.10/lib/python3.10/tempfile.py:860: ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/tmp/tmprq9rc0fi'>_warnings.warn(warn_message, ResourceWarning)
是不是就添加两行代码就跑通了,真的很方便
四、单/多卡训练
from ultralytics import YOLO
import torch_mlu
from torch_mlu.utils.model_transfer import transfer
# Load a model
model = YOLO("yolov8n.yaml") # build a new model from scratch
model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)# Use the model
model.train(data="coco8.yaml", epochs=3) # train the model
metrics = model.val() # evaluate model performance on the validation set
results = model("https://ultralytics.com/images/bus.jpg") # predict on an image
单卡成功训练
from ultralytics import YOLO
import torch_mlu
from torch_mlu.utils.model_transfer import transfer
# Load a model
model = YOLO("yolov8n.yaml") # build a new model from scratch
model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)# Use the model
model.train(data="coco8.yaml", epochs=100,device=[0,1]) # train the model
metrics = model.val() # evaluate model performance on the validation set
results = model("https://ultralytics.com/images/bus.jpg") # predict on an image
# path = model.export(format="onnx") # export the model to ONNX format
多卡成功训练