Linux下运行目标检测yolo26步骤(包含环境配置+模型运行)
q 话不多说直接上步骤;最终目的就是在全新的电脑上安装和使用Pytorch ,在GPU上训练YOLO26
1.确保安装NVIDIA 驱动 < nvidia-smi >
如果知道显卡类型(RTX 5090 )下载 NVIDIA 官方驱动 | NVIDIA 官网直接下载驱动
若不清楚,运行以下命令查看显卡类型
lspci | grep -i nvidia输出如下内容,其中 Device 2b87 代表设备号
| 01:00.0 VGA compatible controller: NVIDIA Corporation Device 2b87 (rev a1) 01:00.1 Audio device: NVIDIA Corporation Device 22e8 (rev a1) |
安装成功后 执行
nvidia-smi查看驱动版本,得到CUDA Version ,在下一步有用。
2. 安装CUDA Toolkit < nvcc -V >
CUDA是NVIDIA开发的并行计算平台。
已知 CUDA Version ,cuda toolkit 需要小于或等于此版本 ,通过官方地址进行下载安装配置环境变量 。
参考 Linux下安装cuda和对应版本的cudnn
官网 CUDA Toolkit 13.1 Update 1 Downloads | NVIDIA Developer

通过 nvcc --version 或者 nvcc -V 验证是否成功
3. 安装cuDNN
cuDNN是深度学习优化的库,选择合适的版本且与CUDA版本适配,官网下载
参考 Linux下安装cuda和对应版本的cudnn
Support Matrix — NVIDIA cuDNN Backend
cuDNN 9.19.1 Downloads | NVIDIA Developer
法一:手动安装,下载合适的版本包

解压缩,将cudnn内相应文件复制到cuda的安装路径,并修改权限。
cudnn-linux-x86_64-9.19.1.2_cuda13-archive/为解压缩后的文件路径
/usr/local/cuda/为cuda的安装路径,按照自己的实际情况替换
tar -xvf cudnn-linux-x86_64-9.19.1.2_cuda13-archive.tar.xz cd cudnn-linux-x86_64-9.19.1.2_cuda13-archive/ sudo cp ./include/cudnn* /usr/local/cuda/include sudo cp ./lib/libcudnn* /usr/local/cuda/lib64 sudo chmod a+r /usr/local/cuda/include/cudnn* /usr/local/cuda/lib64/libcudnn* # 验证 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2 # 若没有输出执行下面这个 cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2法二:根据apt-get 命令自动安装

根据cuda12 /13 选择不同的安装命令

cat /usr/include/x86_64-linux-gnu/cudnn_version.h | grep CUDNN_MAJOR -A 2 输出 #define CUDNN_MAJOR 9 # 主版本号:9 #define CUDNN_MINOR 6 # 次版本号:6 #define CUDNN_PATCHLEVEL 0 # 补丁级别:0后续也可通过pytorch验证
4. 安装conda 选择miniforge < conda --version >
参考 在ubuntu中安装miniforge并配置-ZEEKLOG博客
Miniforge:Anaconda 的理想替代品-ZEEKLOG博客
验证是否成功 并创建环境
source ~/.bashrc conda --version 创建环境,可指定python版本 conda create -n yolo26 python=3.12 为了后续安装速度快 添加清华源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes5.在新建conda环境中安装pytorch
激活conda 环境: conda activate yolo26
根据cuda toolkit 版本选择合适的pytorch版本

如果没有开代理,安装可能会很慢,可以将 --index-url 换成清华源
--index-url https://mirrors.tuna.tsinghua.edu.cn/pytorch-wheels/cu128在终端输入python ,执行以下代码进行验证
import torch print("PyTorch 版本:", torch.__version__) print("CUDA 是否可用:", torch.cuda.is_available()) print("CUDA 版本:", torch.version.cuda) print("GPU 数量:", torch.cuda.device_count()) print("cuDNN 是否可用:", torch.backends.cudnn.enabled) print("cuDNN 版本:", torch.backends.cudnn.version())6. 下载YOLO26 并配置环境运行
ultralytics/ultralytics: Ultralytics YOLO 🚀
6.1 安装 ultralytics
可加后缀使用清华源
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple 其他库函数,后续执行的时候缺少哪些就pip install 安装。
6.2 下载yolo26n.pt

6.3 预测测试
使用yolo26官方模型进行预测进行测试。
在主文件下创建 predict.py ,修改source图片路径,并执行 python predict.py
from ultralytics import YOLO import warnings warnings.filterwarnings('ignore') if __name__ == "__main__": mode = YOLO("./yolo26n.pt") results = mode.predict( source='../../test_data/test.jpg', imgsz=640, # stream=True, project='runs/predict', name='yolo26n', save=True, conf=0.5, # visualize=True # visualize model features maps )可在 runs/predict/yolo26n/ 查看输出结果
6.4 训练自己的数据集
1 准备好数据集文件,按照这样的格式放置

2 编辑dataset.yaml 修改为 rtts.yaml文件 ,并放置在 /ultralytics/ultralytics/cfg/datasets 下
path: ./datasets/RTTS/ train: ./datasets/RTTS/images/train val: ./datasets/RTTS/images/val test: ./datasets/RTTS/images/test names: 0: person 1: car 2: biycycle 3: motorbike 4: bus nc: 53 在主文件下创建 train.py ,根据自己的路径设定,并执行 python train.py
import sys sys.path.append(".") from ultralytics import YOLO import warnings warnings.filterwarnings("ignore") modle = YOLO("ultralytics/cfg/models/26/yolo26n.yaml") if __name__ == "__main__": modle.train(data="ultralytics/cfg/datasets/rtts.yaml", epochs=200, batch=32, imgsz=640, device='0', workers=8, project='train', name='exp', ) 可在runs/detect/train/exp 下查看训练输出;如果无法运行缺少函数库 执行 pip install 进行安装。
4 为了验证测试集精度,在主文件下创建 val.py ,根据自己的路径设定,并执行 python val.py
from ultralytics import YOLO import warnings warnings.filterwarnings("ignore") if __name__ == "__main__": mode = YOLO("./runs/train/exp/weights/best.pt") mode.val( data = "ultralytics/cfg/datasets/rtts.yaml", split = 'test', batch = 32, imgsz = 640, device = '0', workers = 8, project = 'runs/test', name = 'exp' )6.5 导出onnx
在主文件下创建 export.py ,根据自己的路径设定,并执行 python export.py
可通过predict 进行onnx模型预测。
import sys sys.path.insert(0, '.') from ultralytics import YOLO import warnings warnings.filterwarnings("ignore") if __name__ == "__main__": mode = YOLO("./runs/train/exp/weights/best.pt") mode.export( format = "onnx", simplify = True, # opset = None, )6.6 onnx 推理
import cv2 import onnxruntime as ort from PIL import Image import numpy as np import random confidence_thres = 0.5 classes = ['person', 'car', 'biycycle', 'motorbike', 'bus'] providers = [ ('CUDAExecutionProvider', { 'device_id': 0, # 可以选择GPU设备ID,如果你有多个GPU }), 'CPUExecutionProvider', # 也可以设置CPU作为备选 ] # 图像前处理 def preprocess(img, input_width, input_height): """ 在执行推理之前预处理输入图像。 返回: image_data: 为推理准备好的预处理后的图像数据。 """ img_height, img_width = img.shape[:2] img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (input_width, input_height)) # (640,640) image_data = np.array(img) / 255.0 image_data = np.transpose(image_data, (2, 0, 1)) image_data = np.expand_dims(image_data, axis=0).astype(np.float32) return image_data, img_height, img_width def postprocess(un_output, input_width, input_height, img_width, img_height): """ 对模型输出进行后处理,提取过滤后的边界框、得分和类别ID。 参数: un_output (numpy.ndarray): 模型的输出。 onnx的输出为 1x300x6 input_width (int): 模型输入宽度。 input_height (int): 模型输入高度。 img_width (int): 原始图像宽度。 img_height (int): 原始图像高度。 返回: boxes,scores,labels [] [] [] """ # 计算边界框坐标的缩放因子 x_factor = img_width / input_width y_factor = img_height / input_height print(f"img_width :{img_width},x_factor:{x_factor},y_factor:{y_factor}") un_outputs = np.squeeze(un_output[0]) #300*6 [ x1 y1 x2 y2 nc_id conf ] boxes, scores, class_ids = [], [], [] for det in un_outputs: x1, y1, x2, y2, conf,class_id, = det if conf >= confidence_thres: boxes.append([ int(x1 * x_factor), int(y1 * y_factor), int(x2 * x_factor), int(y2 * y_factor) ]) scores.append(conf) class_ids.append(int(class_id)) labels = [classes[cid] for cid in class_ids] if classes is not None else [str(cid) for cid in class_ids] return boxes, scores, labels def init_detect_model(model_path): session = ort.InferenceSession(model_path, providers=providers) model_inputs = session.get_inputs() input_shape = model_inputs[0].shape input_width = input_shape[2] input_height = input_shape[3] return session, model_inputs, input_width, input_height def detect_object(image, un_session, model_inputs, input_width, input_height): if isinstance(image, Image.Image): result_image = np.array(image) else: result_image = image img_data, img_height, img_width = preprocess(result_image, input_width, input_height) un_outputs = un_session.run(None, {model_inputs[0].name: img_data}) box,score,label = postprocess(result_image, un_outputs, input_width, input_height, img_width, img_height) return box,score,label def draw_boxes(image, boxes, scores, labels, use_random_colors=True): for (x1, y1, x2, y2), score, label in zip(boxes, scores, labels): color = ( random.randint(50, 255), random.randint(50, 255), random.randint(50, 255), ) if use_random_colors else (0, 255, 0) cv2.rectangle(image, (x1, y1), (x2, y2), color, 2) text = f"{label} {score:.2f}" (text_width, text_height), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1) overlay = image.copy() cv2.rectangle(overlay, (x1, y1 - text_height - 5), (x1 + text_width, y1), color, -1) image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0) cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1, cv2.LINE_AA) return image if __name__ == '__main__': un_model_path = "./runs/detect/train/exp/weights/best.onnx" print(f"un_model_path:{un_model_path}") un_session, model_inputs, input_width, input_height = init_detect_model(un_model_path) # 读取图像文件 image_data = cv2.imread("../../test_data/g1.jpg") boxes,scores,labels= detect_object(image_data,un_session, model_inputs, input_width, input_height) # print(boxes,scores,labels) result = draw_boxes(image_data.copy(), boxes, scores, labels, use_random_colors=True) # 将检测后的图像保存到文件 cv2.imwrite("./g11.jpg", result) 大体步骤就是这些,如有错误请批评指正。
注意事项
- 每步操作后验证输出是否正常
- 缺失依赖时使用
pip install补充