From d851817c43e98c5fec30f7db7e7be86360669892 Mon Sep 17 00:00:00 2001 From: kittentruck <771228437@qq.com> Date: Fri, 24 Apr 2026 22:44:33 +0800 Subject: [PATCH] Add YOLO latest model application case --- yolo/README.md | 50 +++ ...lo11_classification_train_eval_infer.ipynb | 360 ++++++++++++++++++ yolo/yolo11_detection_train_eval_infer.ipynb | 336 ++++++++++++++++ yolo/yolo11_pose_train_eval_infer.ipynb | 354 +++++++++++++++++ ...yolo11_segmentation_train_eval_infer.ipynb | 341 +++++++++++++++++ 5 files changed, 1441 insertions(+) create mode 100644 yolo/README.md create mode 100644 yolo/yolo11_classification_train_eval_infer.ipynb create mode 100644 yolo/yolo11_detection_train_eval_infer.ipynb create mode 100644 yolo/yolo11_pose_train_eval_infer.ipynb create mode 100644 yolo/yolo11_segmentation_train_eval_infer.ipynb diff --git a/yolo/README.md b/yolo/README.md new file mode 100644 index 0000000..d00cda7 --- /dev/null +++ b/yolo/README.md @@ -0,0 +1,50 @@ +# YOLO11 应用案例 + +本目录提供基于 MindSpore 适配实现的 `ultralytics` YOLO11 应用案例,包含以下四类任务: + +- 图像分类 +- 目标检测 +- 实例分割 +- 姿态估计 + +每个案例均提供训练、评估与推理的完整流程 + +## 环境准备 + +运行本目录下案例前,请先准备好以下环境: + +- Python >= 3.9, < 3.12 +- MindSpore >= 2.7.1 +- CANN >= 8.1.RC1 +- Ascend 环境可用 +- 已安装基于 MindSpore 适配实现的 `ultralytics` 运行环境 + +其中,MindSpore 安装请参考官方文档: +https://www.mindspore.cn/install + +说明: + +1. 本案例调用的 `ultralytics` 为基于 MindSpore 适配实现的版本,请确保 `from ultralytics import YOLO` 可正常导入。 +2. 当前 `applications` 仓主要提供应用案例文件,不单独包含 `ultralytics` 适配实现源码。 +3. 运行本案例前,请先在对应环境中完成 `ultralytics` 适配实现及相关依赖的安装与配置,具体步骤可参考 MindNLP 仓库中 `src/mindnlp/ultralytics/readme.md` 的说明。 +4. 若后续 `ultralytics` 能力正式并入 MindNLP,可根据实际集成方式调整安装与导入说明。 + +## 数据与权重准备 + +默认使用以下数据集配置文件: + +- 检测:`cfg/datasets/coco128.yaml` +- 分类:`cfg/datasets/imagenette2-160.yaml` +- 分割:`cfg/datasets/coco128-seg.yaml` +- 姿态:`cfg/datasets/coco8-pose.yaml` + +请根据对应 YAML 配置文件下载并放置数据集。推理阶段会优先使用数据集中的样例图片;如果当前环境下没有可用测试图片,案例代码会自动下载示例图片。 + + + +## Notebook 列表 + +- `yolo11_classification_train_eval_infer.ipynb`:YOLO11 目标检测案例 +- `yolo11_detection_train_eval_infer.ipynb`:YOLO11 图像分类案例 +- `yolo11_pose_train_eval_infer.ipynb`:YOLO11 实例分割案例 +- `yolo11_segmentation_train_eval_infer.ipynb`:YOLO11 姿态估计案例 diff --git a/yolo/yolo11_classification_train_eval_infer.ipynb b/yolo/yolo11_classification_train_eval_infer.ipynb new file mode 100644 index 0000000..1ed3f61 --- /dev/null +++ b/yolo/yolo11_classification_train_eval_infer.ipynb @@ -0,0 +1,360 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c68711f2", + "metadata": {}, + "source": [ + "# YOLO11 图像分类应用案例\n", + "\n", + "本案例基于 MindSpore 适配实现的 `ultralytics`,演示 YOLO11 图像分类任务的训练、评估与推理完整流程。" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "ce7a7994", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[WARNING] ME(31722:281473491372608,MainProcess):2026-04-24-17:47:15.121.000 [mindspore/context.py:1334] For 'context.set_context', the parameter 'device_target' will be deprecated and removed in a future version. Please use the api mindspore.set_device() instead.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MindSpore version: 2.8.0\n", + "ultralytics package: /root/mindnlp/src/mindnlp/ultralytics/__init__.py\n", + "package_dir: /root/mindnlp/src/mindnlp/ultralytics\n" + ] + } + ], + "source": [ + "import importlib\n", + "import sys\n", + "from pathlib import Path\n", + "\n", + "import mindspore as ms\n", + "\n", + "custom_ultralytics_parent = Path(\"/root/mindnlp/src/mindnlp\")\n", + "if str(custom_ultralytics_parent) not in sys.path:\n", + " sys.path.insert(0, str(custom_ultralytics_parent))\n", + "\n", + "# 清理所有已加载的 ultralytics 缓存,避免混入官方 PyTorch 版\n", + "for module_name in list(sys.modules):\n", + " if module_name == \"ultralytics\" or module_name.startswith(\"ultralytics.\"):\n", + " sys.modules.pop(module_name, None)\n", + "\n", + "importlib.invalidate_caches()\n", + "\n", + "import ultralytics\n", + "from ultralytics import YOLO\n", + "\n", + "ms.set_context(mode=ms.PYNATIVE_MODE, device_target=\"Ascend\")\n", + "\n", + "package_dir = Path(ultralytics.__file__).resolve().parent\n", + "work_dir = Path.cwd() / \"demo_inputs\"\n", + "work_dir.mkdir(parents=True, exist_ok=True)\n", + "\n", + "print(\"MindSpore version:\", ms.__version__)\n", + "print(\"ultralytics package:\", ultralytics.__file__)\n", + "print(\"package_dir:\", package_dir)" + ] + }, + { + "cell_type": "markdown", + "id": "214a6f71", + "metadata": {}, + "source": [ + "## 1. 数据与测试图片准备\n", + "\n", + "训练与评估默认使用 `cfg/datasets/imagenette2-160.yaml`。推理阶段优先使用分类数据集中的样例图片;若当前环境下没有可用测试图片,则自动下载一张示例图片。" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "26348698", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "数据配置: /root/mindnlp/src/mindnlp/ultralytics/cfg/datasets/imagenette2-160.yaml\n", + "推理图片: /root/mindnlp/src/mindnlp/ultralytics/datasets/imagenette2-160/val/n01440764/ILSVRC2012_val_00009111.JPEG\n" + ] + } + ], + "source": [ + "data_yaml = package_dir / \"cfg/datasets/imagenette2-160.yaml\"\n", + "finetune_model_path = package_dir / \"yolo11n-cls.pt\"\n", + "scratch_model_path = package_dir / \"cfg/models/11/yolo11-cls.yaml\"\n", + "\n", + "def resolve_source():\n", + " candidates = [\n", + " package_dir / \"datasets/imagenette2-160/val\",\n", + " package_dir / \"datasets/imagenette2-160/train\",\n", + " ]\n", + " suffixes = {\".jpg\", \".jpeg\", \".png\", \".bmp\"}\n", + " for candidate in candidates:\n", + " if candidate.is_file():\n", + " return candidate\n", + " if candidate.is_dir():\n", + " files = sorted([p for p in candidate.rglob(\"*\") if p.suffix.lower() in suffixes])\n", + " if files:\n", + " return files[0]\n", + "\n", + " image_path = work_dir / \"classify_demo.jpg\"\n", + " if not image_path.exists():\n", + " urllib.request.urlretrieve(\"https://ultralytics.com/images/bus.jpg\", image_path.as_posix())\n", + " print(\"测试图片下载完成:\", image_path)\n", + " else:\n", + " print(\"测试图片已存在:\", image_path)\n", + " return image_path\n", + "\n", + "source_img = resolve_source()\n", + "print(\"数据配置:\", data_yaml)\n", + "print(\"推理图片:\", source_img)" + ] + }, + { + "cell_type": "markdown", + "id": "b65313eb", + "metadata": {}, + "source": [ + "## 2. 模型训练\n", + "\n", + "分类任务支持使用预训练权重进行微调,也支持使用模型配置文件从头开始训练。" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "588a18ab", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "当前工作目录: /root/mindnlp/src/mindnlp/ultralytics\n", + "[MindNLP YOLO] 检测到传入 YAML 架构文件: /root/mindnlp/src/mindnlp/ultralytics/cfg/models/11/yolo11-cls.yaml\n", + "[MindNLP YOLO] 模式: 从头开始随机初始化训练 (跳过权重转换)。\n", + "[MindNLP YOLO] 准备启动 classify 任务的训练...\n", + "[INFO] 训练任务启动,总轮数: 1 epochs\n", + "Epoch [0/0] Step [0/147] | Loss: 2.3760\n", + "Epoch [0/0] Step [10/147] | Loss: 2.3020\n", + "Epoch [0/0] Step [20/147] | Loss: 2.3034\n", + "Epoch [0/0] Step [30/147] | Loss: 2.3253\n", + "Epoch [0/0] Step [40/147] | Loss: 2.3476\n", + "Epoch [0/0] Step [50/147] | Loss: 2.2402\n", + "Epoch [0/0] Step [60/147] | Loss: 2.2737\n", + "Epoch [0/0] Step [70/147] | Loss: 2.3028\n", + "Epoch [0/0] Step [80/147] | Loss: 2.2017\n", + "Epoch [0/0] Step [90/147] | Loss: 2.1495\n", + "Epoch [0/0] Step [100/147] | Loss: 2.0472\n", + "Epoch [0/0] Step [110/147] | Loss: 2.0304\n", + "Epoch [0/0] Step [120/147] | Loss: 2.0184\n", + "Epoch [0/0] Step [130/147] | Loss: 1.9591\n", + "Epoch [0/0] Step [140/147] | Loss: 1.9182\n", + "\n", + "[INFO] 开始执行 Epoch 0 验证程序...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 62/62 [00:38<00:00, 1.59it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "推理测速: preprocess: 0.0ms | inference: 8.7ms | postprocess: 0.6ms\n", + "验证结果 | Top-1 Acc: 0.0991 | Top-5 Acc: 0.5208\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--------------------------------------------------\n", + "[评估报告] Epoch 0\n", + " - accuracy_top1 : 0.09911\n", + " - accuracy_top5 : 0.52076\n", + "[INFO] 当前模型综合评价指标 (Fitness): 0.30994\n", + "--------------------------------------------------\n", + "\n", + "[INFO] 已更新最佳模型权重 (best.ckpt),当前最高精度: 0.3099\n", + "训练完成。\n", + "best_fitness: 0.30993630573248404\n", + "save_dir: runs/classify/train\n" + ] + } + ], + "source": [ + "import os\n", + "os.chdir(package_dir)\n", + "print(\"当前工作目录:\", Path.cwd())\n", + "\n", + "# 微调\n", + "#model = YOLO(finetune_model_path.as_posix())\n", + "# 从头开始训练\n", + "model = YOLO(scratch_model_path.as_posix())\n", + "\n", + "train_results = model.train(\n", + " data=data_yaml.as_posix(),\n", + " epochs=100,\n", + " imgsz=224,\n", + " batch=64,\n", + " amp=False,\n", + " val_interval=1,\n", + " workers=8\n", + ")\n", + "\n", + "print(\"训练完成。\")\n", + "print(\"best_fitness:\", getattr(train_results, \"best_fitness\", None))\n", + "print(\"save_dir:\", getattr(train_results, \"save_dir\", None))" + ] + }, + { + "cell_type": "markdown", + "id": "1e7e856e", + "metadata": {}, + "source": [ + "## 3. 模型评估\n", + "\n", + "训练完成后,在验证集上执行图像分类评估。" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "86efd19a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 classify 任务的验证...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 246/246 [00:53<00:00, 4.56it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "推理测速: preprocess: 0.0ms | inference: 12.6ms | postprocess: 0.5ms\n", + "验证结果 | Top-1 Acc: 0.0991 | Top-5 Acc: 0.5195\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "评估完成。\n", + "{'speed': {'preprocess': 0.0031651053459021694, 'inference': 12.608467089902064, 'postprocess': 0.4601196118980456}, 'metrics/accuracy_top1': 0.09910828025477707, 'metrics/accuracy_top5': 0.5194904458598726, 'fitness': 0.3092993630573248}\n" + ] + } + ], + "source": [ + "val_results = model.val(data=data_yaml.as_posix())\n", + "print(\"评估完成。\")\n", + "print(val_results)" + ] + }, + { + "cell_type": "markdown", + "id": "334fc07f", + "metadata": {}, + "source": [ + "## 4. 模型推理\n", + "\n", + "推理结果会保存到输出目录,用户可以根据目录中的结果图片或日志查看预测结果。" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "435d15e5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 classify 任务的推理...\n", + " 推理结果将保存至: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n", + "推理引擎启动,共探测到 1 份输入样本。\n", + "处理完成 [ILSVRC2012_val_00009111.JPEG] | 前向推理: 64.0ms | 后处理: 34.4ms\n", + "推理完成。\n", + "推理结果保存目录: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n" + ] + } + ], + "source": [ + "\n", + "predict_results = model(\n", + " source=source_img.as_posix(),\n", + " imgsz=224,\n", + " save=True,\n", + ")\n", + "\n", + "print(\"推理完成。\")\n", + "if len(predict_results) > 0:\n", + " print(\"推理结果保存目录:\", getattr(predict_results[0], \"save_dir\", None))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (mindnlp_yolo)", + "language": "python", + "name": "mindnlp_yolo" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.20" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/yolo/yolo11_detection_train_eval_infer.ipynb b/yolo/yolo11_detection_train_eval_infer.ipynb new file mode 100644 index 0000000..07bfc8a --- /dev/null +++ b/yolo/yolo11_detection_train_eval_infer.ipynb @@ -0,0 +1,336 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "540e93b2", + "metadata": {}, + "source": [ + "# YOLO11 目标检测应用案例\n", + "\n", + "本案例基于 MindSpore 适配实现的 `ultralytics`,演示 YOLO11 目标检测任务的训练、评估与推理完整流程。" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4ae4b0df", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[WARNING] ME(42604:281473233685056,MainProcess):2026-04-24-17:13:57.568.000 [mindspore/context.py:1334] For 'context.set_context', the parameter 'device_target' will be deprecated and removed in a future version. Please use the api mindspore.set_device() instead.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MindSpore version: 2.8.0\n", + "ultralytics package: /root/mindnlp/src/mindnlp/ultralytics/__init__.py\n", + "package_dir: /root/mindnlp/src/mindnlp/ultralytics\n", + "cwd: /root/mindnlp/src/mindnlp/ultralytics\n" + ] + } + ], + "source": [ + "import gc\n", + "import importlib\n", + "import os\n", + "import sys\n", + "import urllib.request\n", + "from pathlib import Path\n", + "\n", + "import mindspore as ms\n", + "\n", + "custom_ultralytics_parent = Path(\"/root/mindnlp/src/mindnlp\")\n", + "if str(custom_ultralytics_parent) not in sys.path:\n", + " sys.path.insert(0, str(custom_ultralytics_parent))\n", + "\n", + "for module_name in list(sys.modules):\n", + " if module_name == \"ultralytics\" or module_name.startswith(\"ultralytics.\"):\n", + " sys.modules.pop(module_name, None)\n", + "importlib.invalidate_caches()\n", + "\n", + "import ultralytics\n", + "from ultralytics import YOLO\n", + "\n", + "ms.set_context(mode=ms.PYNATIVE_MODE, device_target=\"Ascend\")\n", + "package_dir = Path(ultralytics.__file__).resolve().parent\n", + "os.chdir(package_dir)\n", + "work_dir = Path.cwd() / \"demo_inputs\"\n", + "work_dir.mkdir(parents=True, exist_ok=True)\n", + "\n", + "print(\"MindSpore version:\", ms.__version__)\n", + "print(\"ultralytics package:\", ultralytics.__file__)\n", + "print(\"package_dir:\", package_dir)\n", + "print(\"cwd:\", Path.cwd())" + ] + }, + { + "cell_type": "markdown", + "id": "8483e790", + "metadata": {}, + "source": [ + "## 1. 数据与测试图片准备\n", + "\n", + "训练与评估默认使用 `cfg/datasets/coco128.yaml`。推理阶段优先使用数据集中的样例图片;若当前环境下没有可用测试图片,则自动下载一张示例图片。" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "07f914f2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "数据配置: /root/mindnlp/src/mindnlp/ultralytics/cfg/datasets/coco128.yaml\n", + "推理图片: /root/mindnlp/src/mindnlp/ultralytics/datasets/coco128/images/train2017/000000000009.jpg\n" + ] + } + ], + "source": [ + "data_yaml = package_dir / \"cfg/datasets/coco128.yaml\"\n", + "finetune_model_path = package_dir / \"yolo11n.pt\"\n", + "scratch_model_path = package_dir / \"cfg/models/11/yolo11.yaml\"\n", + "\n", + "def resolve_source():\n", + " candidates = [\n", + " package_dir / \"datasets/coco128/images/train2017\",\n", + " package_dir / \"datasets/coco128/images/val2017\",\n", + " ]\n", + " suffixes = {\".jpg\", \".jpeg\", \".png\", \".bmp\"}\n", + " for candidate in candidates:\n", + " if candidate.is_file():\n", + " return candidate\n", + " if candidate.is_dir():\n", + " files = sorted([p for p in candidate.rglob(\"*\") if p.suffix.lower() in suffixes])\n", + " if files:\n", + " return files[0]\n", + "\n", + " image_path = work_dir / \"detect_bus.jpg\"\n", + " if not image_path.exists():\n", + " urllib.request.urlretrieve(\"https://ultralytics.com/images/bus.jpg\", image_path.as_posix())\n", + " print(\"测试图片下载完成:\", image_path)\n", + " else:\n", + " print(\"测试图片已存在:\", image_path)\n", + " return image_path\n", + "\n", + "source_img = resolve_source()\n", + "print(\"数据配置:\", data_yaml)\n", + "print(\"推理图片:\", source_img)" + ] + }, + { + "cell_type": "markdown", + "id": "22074144", + "metadata": {}, + "source": [ + "## 2. 模型训练\n", + "\n", + "检测任务支持使用预训练权重进行微调,也支持使用模型配置文件从头开始训练。" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "08a95c15", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 检测到传入 YAML 架构文件: /root/mindnlp/src/mindnlp/ultralytics/cfg/models/11/yolo11.yaml\n", + "[MindNLP YOLO] 模式: 从头开始随机初始化训练 (跳过权重转换)。\n", + "[MindNLP YOLO] 准备启动 detect 任务的训练...\n", + "[INFO] 训练任务启动,总轮数: 2 epochs\n", + "Epoch [0/1] Step [0/8] | Total Loss: 220.9965 | Box: 3.0431 | Cls: 213.7144 | DFL: 4.2390\n", + "Epoch [1/1] Step [0/8] | Total Loss: 185.0031 | Box: 3.2246 | Cls: 177.4669 | DFL: 4.3116\n", + "\n", + "[INFO] 开始执行 Epoch 1 验证程序...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 8/8 [01:58<00:00, 14.77s/it]\n", + "2026-04-24 17:33:08,271 - INFO - 推理测速: preprocess: 0.0ms | inference: 471.4ms | postprocess: 404.2ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--------------------------------------------------\n", + "[评估报告] Epoch 1\n", + " - mAP50(B) : 0.00000\n", + " - mAP50-95(B) : 0.00000\n", + "[INFO] 当前模型综合评价指标 (Fitness): 0.00000\n", + "--------------------------------------------------\n", + "\n", + "[INFO] 已更新最佳模型权重 (best.ckpt),当前最高精度: 0.0000\n", + "训练完成。\n", + "best_fitness: 2.581491654308886e-07\n", + "save_dir: runs/detect/train\n" + ] + } + ], + "source": [ + "# 微调\n", + "#model = YOLO(finetune_model_path.as_posix())\n", + "# 从头开始训练\n", + "model = YOLO(scratch_model_path.as_posix())\n", + "\n", + "train_results = model.train(\n", + " data=data_yaml.as_posix(),\n", + " epochs=100,\n", + " imgsz=640,\n", + " batch=16,\n", + " amp=False,\n", + " val_interval=10,\n", + " workers=8,\n", + ")\n", + "\n", + "print(\"训练完成。\")\n", + "print(\"best_fitness:\", getattr(train_results, \"best_fitness\", None))\n", + "print(\"save_dir:\", getattr(train_results, \"save_dir\", None))" + ] + }, + { + "cell_type": "markdown", + "id": "a0c9de91", + "metadata": {}, + "source": [ + "## 3. 模型评估\n", + "\n", + "训练完成后,在验证集上执行目标检测评估。" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8b09588c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 detect 任务的验证...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 8/8 [01:10<00:00, 8.75s/it]\n", + "2026-04-24 17:35:36,582 - INFO - 推理测速: preprocess: 0.0ms | inference: 297.1ms | postprocess: 220.2ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "评估完成。\n", + "{'speed': {'preprocess': 0.015323981642723083, 'inference': 297.07514308393, 'postprocess': 220.2438898384571}, 'metrics/mAP50(B)': 1.430186148306948e-06, 'metrics/mAP50-95(B)': 1.430186148306948e-07, 'fitness': 1.430186148306948e-07}\n" + ] + } + ], + "source": [ + "val_results = model.val(data=data_yaml.as_posix())\n", + "print(\"评估完成。\")\n", + "print(val_results)" + ] + }, + { + "cell_type": "markdown", + "id": "6c74ce93", + "metadata": {}, + "source": [ + "## 4. 模型推理\n", + "\n", + "推理结果会自动保存到输出目录,用户可直接查看生成的可视化图片。" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "71707953", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 17:35:42,424 - INFO - 推理结果将保存至: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n", + "2026-04-24 17:35:42,425 - INFO - 推理引擎启动,共探测到 1 份输入样本。\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 detect 任务的推理...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 17:35:43,465 - INFO - 处理完成 [000000000009.jpg] | 前向推理: 576.2ms | 后处理: 451.8ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "推理完成。\n", + "推理结果保存目录: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n" + ] + } + ], + "source": [ + "gc.collect()\n", + "\n", + "predict_results = model(\n", + " source=source_img.as_posix(),\n", + " imgsz=640,\n", + " conf=0.25,\n", + " iou=0.45,\n", + " save=True,\n", + ")\n", + "\n", + "print(\"推理完成。\")\n", + "if len(predict_results) > 0:\n", + " print(\"推理结果保存目录:\", getattr(predict_results[0], \"save_dir\", None))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (mindnlp_yolo)", + "language": "python", + "name": "mindnlp_yolo" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.20" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/yolo/yolo11_pose_train_eval_infer.ipynb b/yolo/yolo11_pose_train_eval_infer.ipynb new file mode 100644 index 0000000..27b57dd --- /dev/null +++ b/yolo/yolo11_pose_train_eval_infer.ipynb @@ -0,0 +1,354 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e478ecf1", + "metadata": {}, + "source": [ + "# YOLO11 姿态估计应用案例\n", + "\n", + "本案例基于 MindSpore 适配实现的 `ultralytics`,演示 YOLO11 姿态估计任务的训练、评估与推理完整流程。" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "0294190f", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[WARNING] ME(43403:281472906070592,MainProcess):2026-04-24-17:05:14.369.000 [mindspore/context.py:1334] For 'context.set_context', the parameter 'device_target' will be deprecated and removed in a future version. Please use the api mindspore.set_device() instead.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MindSpore version: 2.8.0\n", + "ultralytics package: /root/mindnlp/src/mindnlp/ultralytics/__init__.py\n", + "package_dir: /root/mindnlp/src/mindnlp/ultralytics\n", + "cwd: /root/mindnlp/src/mindnlp/ultralytics\n" + ] + } + ], + "source": [ + "import gc\n", + "import importlib\n", + "import os\n", + "import sys\n", + "import urllib.request\n", + "from pathlib import Path\n", + "\n", + "import mindspore as ms\n", + "\n", + "custom_ultralytics_parent = Path(\"/root/mindnlp/src/mindnlp\")\n", + "if str(custom_ultralytics_parent) not in sys.path:\n", + " sys.path.insert(0, str(custom_ultralytics_parent))\n", + "\n", + "for module_name in list(sys.modules):\n", + " if module_name == \"ultralytics\" or module_name.startswith(\"ultralytics.\"):\n", + " sys.modules.pop(module_name, None)\n", + "importlib.invalidate_caches()\n", + "\n", + "import ultralytics\n", + "from ultralytics import YOLO\n", + "\n", + "ms.set_context(mode=ms.PYNATIVE_MODE, device_target=\"Ascend\")\n", + "package_dir = Path(ultralytics.__file__).resolve().parent\n", + "os.chdir(package_dir)\n", + "work_dir = Path.cwd() / \"demo_inputs\"\n", + "work_dir.mkdir(parents=True, exist_ok=True)\n", + "\n", + "print(\"MindSpore version:\", ms.__version__)\n", + "print(\"ultralytics package:\", ultralytics.__file__)\n", + "print(\"package_dir:\", package_dir)\n", + "print(\"cwd:\", Path.cwd())" + ] + }, + { + "cell_type": "markdown", + "id": "ad1d568c", + "metadata": {}, + "source": [ + "## 1. 数据与测试图片准备\n", + "\n", + "训练与评估默认使用 `cfg/datasets/coco8-pose.yaml`。推理阶段优先使用数据集中的样例图片;若当前环境下没有可用测试图片,则自动下载一张示例图片。" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "82148a26", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "数据配置: /root/mindnlp/src/mindnlp/ultralytics/cfg/datasets/coco8-pose.yaml\n", + "推理图片: /root/mindnlp/src/mindnlp/ultralytics/datasets/coco8-pose/images/val/000000000110.jpg\n" + ] + } + ], + "source": [ + "data_yaml = package_dir / \"cfg/datasets/coco8-pose.yaml\"\n", + "scratch_model_path = package_dir / \"cfg/models/11/yolo11-pose.yaml\"\n", + "finetune_model_path = package_dir / \"yolo11n-pose.pt\"\n", + "\n", + "def resolve_source():\n", + " candidates = [\n", + " package_dir / \"datasets/coco8-pose/images/val\",\n", + " package_dir / \"datasets/coco8-pose/images/train\",\n", + " ]\n", + " suffixes = {\".jpg\", \".jpeg\", \".png\", \".bmp\"}\n", + " for candidate in candidates:\n", + " if candidate.is_file():\n", + " return candidate\n", + " if candidate.is_dir():\n", + " files = sorted([p for p in candidate.rglob(\"*\") if p.suffix.lower() in suffixes])\n", + " if files:\n", + " return files[0]\n", + "\n", + " image_path = work_dir / \"pose_bus.jpg\"\n", + " if not image_path.exists():\n", + " urllib.request.urlretrieve(\"https://ultralytics.com/images/bus.jpg\", image_path.as_posix())\n", + " print(\"测试图片下载完成:\", image_path)\n", + " else:\n", + " print(\"测试图片已存在:\", image_path)\n", + " return image_path\n", + "\n", + "source_img = resolve_source()\n", + "print(\"数据配置:\", data_yaml)\n", + "print(\"推理图片:\", source_img)" + ] + }, + { + "cell_type": "markdown", + "id": "079b3e4c", + "metadata": {}, + "source": [ + "## 2. 模型训练\n", + "\n", + "姿态估计任务支持使用预训练权重进行微调,也支持使用模型配置文件从头开始训练。" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5aae391c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 检测到传入 YAML 架构文件: /root/mindnlp/src/mindnlp/ultralytics/cfg/models/11/yolo11-pose.yaml\n", + "[MindNLP YOLO] 模式: 从头开始随机初始化训练 (跳过权重转换)。\n", + "[MindNLP YOLO] 准备启动 pose 任务的训练...\n", + "[INFO] 训练任务启动,总轮数: 100 epochs\n", + "Epoch [0/99] Step [0/1] | Total Loss: 34.5890 | Loss Items: ['2.8692', '7.1322', '4.3780', '17.0229', '3.1869']\n", + "Epoch [1/99] Step [0/1] | Total Loss: 34.3706 | Loss Items: ['2.8571', '7.0832', '4.3145', '17.1740', '2.9418']\n", + "Epoch [2/99] Step [0/1] | Total Loss: 34.6868 | Loss Items: ['2.9101', '7.1914', '4.3728', '17.2227', '2.9899']\n", + "Epoch [3/99] Step [0/1] | Total Loss: 33.8326 | Loss Items: ['2.8574', '7.0667', '4.3347', '16.6608', '2.9130']\n", + "Epoch [4/99] Step [0/1] | Total Loss: 34.5702 | Loss Items: ['2.8795', '6.8740', '4.3825', '17.5432', '2.8911']\n", + "Epoch [5/99] Step [0/1] | Total Loss: 34.3739 | Loss Items: ['2.9292', '6.7925', '4.2711', '17.5326', '2.8485']\n", + "Epoch [6/99] Step [0/1] | Total Loss: 34.0572 | Loss Items: ['2.9036', '6.6867', '4.2602', '17.3303', '2.8763']\n", + "Epoch [7/99] Step [0/1] | Total Loss: 33.5707 | Loss Items: ['2.8801', '6.5151', '4.2681', '17.2007', '2.7066']\n", + "Epoch [8/99] Step [0/1] | Total Loss: 33.0893 | Loss Items: ['2.8553', '6.3753', '4.3038', '16.8550', '2.6999']\n", + "Epoch [9/99] Step [0/1] | Total Loss: 32.5639 | Loss Items: ['2.8525', '6.2358', '4.2076', '16.5846', '2.6834']\n", + "\n", + "[INFO] 开始执行 Epoch 9 验证程序...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 1/1 [00:06<00:00, 6.93s/it]\n", + "2026-04-24 19:06:13,819 - INFO - 推理测速: preprocess: 0.2ms | inference: 678.1ms | postprocess: 499.5ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--------------------------------------------------\n", + "[评估报告] Epoch 9\n", + " - mAP50(B) : 0.00446\n", + " - mAP50-95(B) : 0.00180\n", + " - mAP50(P) : 0.00000\n", + " - mAP50-95(P) : 0.00000\n", + "[INFO] 当前模型综合评价指标 (Fitness): 0.00207\n", + "--------------------------------------------------\n", + "\n", + "[INFO] 已更新最佳模型权重 (best.ckpt),当前最高精度: 0.0021\n", + "Epoch [10/99] Step [0/1] | Total Loss: 32.2424 | Loss Items: ['2.7791', '6.0073', '4.2453', '16.5220', '2.6887']\n", + "Epoch [11/99] Step [0/1] | Total Loss: 32.2965 | Loss Items: ['2.8291', '5.9668', '4.2158', '16.7080', '2.5769']\n", + "Epoch [12/99] Step [0/1] | Total Loss: 31.7921 | Loss Items: ['2.7731', '5.8150', '4.1593', '16.4919', '2.5527']\n" + ] + } + ], + "source": [ + "# 微调\n", + "#model = YOLO(finetune_model_path.as_posix())\n", + "# 从头开始训练\n", + "model = YOLO(scratch_model_path.as_posix())\n", + "\n", + "train_results = model.train(\n", + " data=data_yaml.as_posix(),\n", + " epochs=100,\n", + " imgsz=640,\n", + " batch=4,\n", + " amp=False,\n", + " val_interval=10,\n", + " workers=8,\n", + ")\n", + "\n", + "print(\"训练完成。\")\n", + "print(\"best_fitness:\", getattr(train_results, \"best_fitness\", None))\n", + "print(\"save_dir:\", getattr(train_results, \"save_dir\", None))" + ] + }, + { + "cell_type": "markdown", + "id": "874c62ee", + "metadata": {}, + "source": [ + "## 3. 模型评估\n", + "\n", + "训练完成后,在验证集上执行姿态估计评估。" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "86f0407f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 pose 任务的验证...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 1/1 [00:06<00:00, 6.42s/it]\n", + "2026-04-24 17:10:17,165 - INFO - 推理测速: preprocess: 0.2ms | inference: 783.3ms | postprocess: 92.8ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "评估完成。\n", + "{'speed': {'preprocess': 0.16415119171142578, 'inference': 783.311665058136, 'postprocess': 92.78059005737305}, 'metrics/mAP50(B)': 0.584898528902131, 'metrics/mAP50-95(B)': 0.4416917374380735, 'metrics/mAP50(P)': 0.3501689781021898, 'metrics/mAP50-95(P)': 0.15226905618226055, 'fitness': 0.6280714649587327}\n" + ] + } + ], + "source": [ + "val_results = model.val(\n", + " data=data_yaml.as_posix(),\n", + " imgsz=640,\n", + " batch=4,\n", + " workers=8,\n", + " device=\"CPU\"\n", + ")\n", + "\n", + "print(\"评估完成。\")\n", + "print(val_results)" + ] + }, + { + "cell_type": "markdown", + "id": "166941cf", + "metadata": {}, + "source": [ + "## 4. 模型推理\n", + "\n", + "推理结果会自动保存到输出目录,用户可直接查看可视化结果。" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "0c8dfc47", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 17:10:28,408 - INFO - 推理结果将保存至: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n", + "2026-04-24 17:10:28,411 - INFO - 推理引擎启动,共探测到 1 份输入样本。\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 pose 任务的推理...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 17:10:30,160 - INFO - 处理完成 [000000000110.jpg] | 前向推理: 1332.1ms | 后处理: 393.2ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[Info] 渲染结果已保存至: runs/detect/predict/000000000110.jpg\n", + "推理完成。\n", + "推理结果保存目录: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n" + ] + } + ], + "source": [ + "gc.collect()\n", + "\n", + "predict_results = model(\n", + " source=source_img.as_posix(),\n", + " imgsz=640,\n", + " conf=0.25,\n", + " iou=0.45,\n", + " save=True,\n", + ")\n", + "\n", + "print(\"推理完成。\")\n", + "if len(predict_results) > 0:\n", + " print(\"推理结果保存目录:\", getattr(predict_results[0], \"save_dir\", None))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (mindnlp_yolo)", + "language": "python", + "name": "mindnlp_yolo" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.20" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/yolo/yolo11_segmentation_train_eval_infer.ipynb b/yolo/yolo11_segmentation_train_eval_infer.ipynb new file mode 100644 index 0000000..22cb4a1 --- /dev/null +++ b/yolo/yolo11_segmentation_train_eval_infer.ipynb @@ -0,0 +1,341 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c6d52e5e", + "metadata": {}, + "source": [ + "# YOLO11 实例分割应用案例\n", + "\n", + "本案例基于 MindSpore 适配实现的 `ultralytics`,演示 YOLO11 实例分割任务的训练、评估与推理完整流程。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "023c3cf3", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/root/.conda/envs/mindnlp_yolo/lib/python3.10/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for type is zero.\n", + " setattr(self, word, getattr(machar, word).flat[0])\n", + "/root/.conda/envs/mindnlp_yolo/lib/python3.10/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for type is zero.\n", + " return self._float_to_str(self.smallest_subnormal)\n", + "/root/.conda/envs/mindnlp_yolo/lib/python3.10/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for type is zero.\n", + " setattr(self, word, getattr(machar, word).flat[0])\n", + "/root/.conda/envs/mindnlp_yolo/lib/python3.10/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for type is zero.\n", + " return self._float_to_str(self.smallest_subnormal)\n", + "[WARNING] ME(755:281473668799040,MainProcess):2026-04-24-20:47:28.600.0 [mindspore/context.py:1334] For 'context.set_context', the parameter 'device_target' will be deprecated and removed in a future version. Please use the api mindspore.set_device() instead.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MindSpore version: 2.8.0\n", + "ultralytics package: /root/mindnlp/src/mindnlp/ultralytics/__init__.py\n", + "package_dir: /root/mindnlp/src/mindnlp/ultralytics\n", + "cwd: /root/mindnlp/src/mindnlp/ultralytics\n" + ] + } + ], + "source": [ + "import gc\n", + "import importlib\n", + "import os\n", + "import sys\n", + "import urllib.request\n", + "from pathlib import Path\n", + "\n", + "import mindspore as ms\n", + "\n", + "custom_ultralytics_parent = Path(\"/root/mindnlp/src/mindnlp\")\n", + "if str(custom_ultralytics_parent) not in sys.path:\n", + " sys.path.insert(0, str(custom_ultralytics_parent))\n", + "\n", + "for module_name in list(sys.modules):\n", + " if module_name == \"ultralytics\" or module_name.startswith(\"ultralytics.\"):\n", + " sys.modules.pop(module_name, None)\n", + "importlib.invalidate_caches()\n", + "\n", + "import ultralytics\n", + "from ultralytics import YOLO\n", + "\n", + "ms.set_context(mode=ms.PYNATIVE_MODE, device_target=\"Ascend\")\n", + "package_dir = Path(ultralytics.__file__).resolve().parent\n", + "os.chdir(package_dir)\n", + "work_dir = Path.cwd() / \"demo_inputs\"\n", + "work_dir.mkdir(parents=True, exist_ok=True)\n", + "\n", + "print(\"MindSpore version:\", ms.__version__)\n", + "print(\"ultralytics package:\", ultralytics.__file__)\n", + "print(\"package_dir:\", package_dir)\n", + "print(\"cwd:\", Path.cwd())" + ] + }, + { + "cell_type": "markdown", + "id": "1b4ec00c", + "metadata": {}, + "source": [ + "## 1. 数据与测试图片准备\n", + "\n", + "训练与评估默认使用 `cfg/datasets/coco128-seg.yaml`。推理阶段优先使用数据集中的样例图片;若当前环境下没有可用测试图片,则自动下载一张示例图片。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2802a93b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "数据配置: /root/mindnlp/src/mindnlp/ultralytics/cfg/datasets/coco128-seg.yaml\n", + "推理图片: /root/mindnlp/src/mindnlp/ultralytics/datasets/coco128-seg/images/train2017/000000000009.jpg\n" + ] + } + ], + "source": [ + "data_yaml = package_dir / \"cfg/datasets/coco128-seg.yaml\"\n", + "scratch_model_path = package_dir / \"cfg/models/11/yolo11-seg.yaml\"\n", + "finetune_model_path = package_dir / \"yolo11n-seg.pt\"\n", + "\n", + "def resolve_source():\n", + " candidates = [\n", + " package_dir / \"datasets/coco128-seg/images/train2017\",\n", + " package_dir / \"datasets/coco128-seg/images/val2017\",\n", + " ]\n", + " suffixes = {\".jpg\", \".jpeg\", \".png\", \".bmp\"}\n", + " for candidate in candidates:\n", + " if candidate.is_file():\n", + " return candidate\n", + " if candidate.is_dir():\n", + " files = sorted([p for p in candidate.rglob(\"*\") if p.suffix.lower() in suffixes])\n", + " if files:\n", + " return files[0]\n", + "\n", + " image_path = work_dir / \"segment_bus.jpg\"\n", + " if not image_path.exists():\n", + " urllib.request.urlretrieve(\"https://ultralytics.com/images/bus.jpg\", image_path.as_posix())\n", + " print(\"测试图片下载完成:\", image_path)\n", + " else:\n", + " print(\"测试图片已存在:\", image_path)\n", + " return image_path\n", + "\n", + "source_img = resolve_source()\n", + "print(\"数据配置:\", data_yaml)\n", + "print(\"推理图片:\", source_img)" + ] + }, + { + "cell_type": "markdown", + "id": "e5e5d1a7", + "metadata": {}, + "source": [ + "## 2. 模型训练\n", + "\n", + "分割任务支持使用预训练权重进行微调,也支持使用模型配置文件从头开始训练。" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ea48f0d7", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 发现已存在权重: /root/mindnlp/src/mindnlp/ultralytics/yolo11n-seg.ckpt,直接加载。\n", + "[MindNLP YOLO] 准备启动 segment 任务的训练...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 20:47:29,430 - INFO - 正在载入初始检查点权重: /root/mindnlp/src/mindnlp/ultralytics/yolo11n-seg.ckpt\n", + "2026-04-24 20:47:29,668 - INFO - ----------------------------------------\n", + "2026-04-24 20:47:29,669 - INFO - [权重匹配核查报告]\n", + "2026-04-24 20:47:29,670 - INFO - 待加载参数总量: 472\n", + "2026-04-24 20:47:29,670 - INFO - 网络未被初始化的参数数目: 0\n", + "2026-04-24 20:47:29,671 - INFO - ----------------------------------------\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[INFO] 训练任务启动,总轮数: 1 epochs\n", + "Epoch [0/0] Step [0/32] | Total Loss: 7.6210 | Box: 1.5483 | Cls: 1.8655 | DFL: 1.5363 | Mask: 2.6710\n", + "Epoch [0/0] Step [10/32] | Total Loss: 14.9426 | Box: 3.1877 | Cls: 3.8860 | DFL: 3.2736 | Mask: 4.5954\n", + "Epoch [0/0] Step [20/32] | Total Loss: 7.7878 | Box: 2.0396 | Cls: 2.7479 | DFL: 2.0061 | Mask: 0.9941\n", + "Epoch [0/0] Step [30/32] | Total Loss: 8.7381 | Box: 2.1941 | Cls: 3.3331 | DFL: 2.3557 | Mask: 0.8552\n", + "训练完成。\n", + "best_fitness: 0.0\n", + "save_dir: runs/segment/train\n" + ] + } + ], + "source": [ + "# 微调\n", + "model = YOLO(finetune_model_path.as_posix())\n", + "# 从头开始训练\n", + "#model = YOLO(scratch_model_path.as_posix())\n", + "\n", + "train_results = model.train(\n", + " data=data_yaml.as_posix(),\n", + " epochs=100,\n", + " imgsz=640,\n", + " batch_size=4,\n", + " amp=False,\n", + " val_interval=10,\n", + " workers=8,\n", + ")\n", + "\n", + "print(\"训练完成。\")\n", + "print(\"best_fitness:\", getattr(train_results, \"best_fitness\", None))\n", + "print(\"save_dir:\", getattr(train_results, \"save_dir\", None))" + ] + }, + { + "cell_type": "markdown", + "id": "9db6f272", + "metadata": {}, + "source": [ + "## 3. 模型评估\n", + "\n", + "训练完成后,在验证集上执行实例分割评估。" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "c9f43752", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 segment 任务的验证...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Validating: 100%|██████████| 8/8 [02:17<00:00, 17.18s/it]\n", + "2026-04-24 18:49:19,613 - INFO - 推理测速: preprocess: 34.5ms | inference: 433.7ms | postprocess: 565.8ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "评估完成。\n", + "{'speed': {'preprocess': 34.50571186840534, 'inference': 433.7215945124626, 'postprocess': 565.8360552042723}, 'metrics/mAP50(B)': 9.325964017929409e-06, 'metrics/mAP50-95(B)': 1.6029701454275816e-06, 'metrics/mAP50(M)': 0.0, 'metrics/mAP50-95(M)': 0.0, 'fitness': 2.3752695326777644e-06}\n" + ] + } + ], + "source": [ + "val_results = model.val(data=data_yaml.as_posix())\n", + "print(\"评估完成。\")\n", + "print(val_results)" + ] + }, + { + "cell_type": "markdown", + "id": "e31a361f", + "metadata": {}, + "source": [ + "## 4. 模型推理\n", + "\n", + "推理结果会自动保存到输出目录,用户可直接查看可视化结果。" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "3bdb84db", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 18:50:49,642 - INFO - 推理结果将保存至: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n", + "2026-04-24 18:50:49,644 - INFO - 推理引擎启动,共探测到 1 份输入样本。\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[MindNLP YOLO] 准备启动 segment 任务的推理...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-04-24 18:50:50,757 - INFO - 处理完成 [000000000009.jpg] | 前向推理: 903.8ms | 后处理: 105.1ms\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "推理完成。\n", + "推理结果保存目录: /root/mindnlp/src/mindnlp/ultralytics/runs/detect/predict\n" + ] + } + ], + "source": [ + "gc.collect()\n", + "\n", + "predict_results = model(\n", + " source=source_img.as_posix(),\n", + " imgsz=640,\n", + " conf=0.25,\n", + " iou=0.45,\n", + " save=True,\n", + ")\n", + "\n", + "print(\"推理完成。\")\n", + "if len(predict_results) > 0:\n", + " print(\"推理结果保存目录:\", getattr(predict_results[0], \"save_dir\", None))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (mindnlp_yolo)", + "language": "python", + "name": "mindnlp_yolo" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.20" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}