使用 Weights & Biases 跟踪和可视化 YOLO 实验
像 目标检测模型(例如 Ultralytics YOLO26)已成为许多计算机视觉应用不可或缺的一部分。然而,训练、评估和部署这些复杂模型会带来多项挑战。跟踪关键训练指标、比较模型变体、分析模型行为以及检测问题,都需要大量的埋点和实验管理工作。
观看: 如何将 Ultralytics YOLO26 与 Weights and Biases 搭配使用
本指南介绍了 Ultralytics YOLO26 与 Weights & Biases 的集成,用于实验跟踪、最佳模型工件管理和模型性能可视化。指南还包含集成设置、训练以及使用 Weights & Biases 交互式功能可视化结果的说明。
已经在 W&B 中记录了 YOLO 检查点?你可以通过 Settings > Integrations > Weights & Biases 将其项目导入 Ultralytics Platform。
Weights & Biases#
Weights & Biases 是一个专为跟踪、可视化和管理机器学习实验而设计的 MLOps 平台。它支持自动记录训练指标,确保实验可完全复现;提供交互式 UI,简化数据分析;还提供高效的模型管理工具,方便在各种环境中部署模型。
使用 Weights & Biases 训练 YOLO26#
你可以使用 Weights & Biases 提高 YOLO26 训练流程的效率并实现自动化。此集成可让你跟踪实验、比较模型,并根据数据做出决策,从而改进计算机视觉项目。
安装#
运行以下命令安装所需的软件包:
# Install the required packages for Ultralytics YOLO and Weights & Biases
pip install -U ultralytics wandb
# Enable W&B logging for Ultralytics
yolo settings wandb=True有关安装过程的详细说明和最佳实践,请务必查看我们的 YOLO26 安装指南。安装 YOLO26 所需的软件包时,如果遇到任何问题,请参阅我们的常见问题指南,获取解决方案和建议。
配置 Weights & Biases#
安装所需的软件包后,下一步是设置 Weights & Biases 环境。这包括创建 Weights & Biases 账户并获取所需的 API 密钥,以便开发环境与 W&B 平台顺畅连接。
首先,在工作区中初始化 Weights & Biases 环境。你可以运行以下命令,并按照提示完成操作。
import wandb
# 初始化 Weights & Biases 环境(提示输入 API key,或读取 WANDB_API_KEY)
wandb.login()前往 Weights & Biases 授权页面创建并获取 API 密钥。系统提示时,使用此密钥验证你的环境并连接到 W&B。
用法:使用 Weights & Biases 训练 YOLO26#
from ultralytics import YOLO
# 加载 YOLO 模型
model = YOLO("yolo26n.pt")
# 训练和微调模型
model.train(data="coco8.yaml", epochs=5, project="ultralytics", name="yolo26n")Ultralytics 创建的运行会以 epoch 为横轴绘制训练指标图表。如果你在训练前使用 wandb.init() 启动运行,Ultralytics 会将日志记录到该运行中,每一行都会包含一个 epoch 值,并保持运行开启,这样你就可以继续向其中记录日志,并自行调用 wandb.finish()。训练参数会添加到该运行的配置中,而你或 W&B sweep 已设置的任何键都会保留其值。
W&B 参数#
| 参数 | 默认值 | 说明 |
|---|---|---|
| project | None | 指定本地和 W&B 中记录的项目名称,用于将多个运行归到一起。如果未设置,W&B 运行记录将保存到 Ultralytics 项目中。 |
| name | None | 训练运行的名称。此名称将用于创建子文件夹,也将用于 W&B 记录。 |
| save | True | 确定是否将最佳检查点上传为 W&B 模型制品。 |
如果你想在 Ultralytics 中启用或禁用 Weights & Biases 记录,可以使用 yolo settings 命令。默认情况下,Weights & Biases 记录处于禁用状态。
# Enable Weights & Biases logging
yolo settings wandb=True
# Disable Weights & Biases logging
yolo settings wandb=False将 WANDB_MODE=offline 设置为在没有网络连接时记录日志,然后使用 wandb sync <save_dir>/wandb/offline-run-* 上传运行记录。使用 resume=True 恢复训练后,会继续同一个 W&B 运行,无论在线还是离线,也无论使用哪种操作系统;它会从原始保存目录的 wandb/ 目录中读取最近一次运行的 ID,因此请保留该目录。
了解输出#
运行上方的用法代码片段后,你会看到以下关键输出:
- 新运行已创建并分配唯一 ID,表示训练过程已开始。
- 简明的模型结构摘要,包括层数和参数数量。
- 在每个训练轮次期间,定期更新训练损失、精确率、召回率和 mAP 分数等重要指标。
- 训练结束时会显示详细指标,包括模型推理速度和整体准确率指标。
- 提供指向 Weights & Biases 信息中心的链接,便于深入分析和可视化训练过程,并提供本地日志文件位置的信息。
查看 Weights & Biases 信息中心#
运行用法代码片段后,你可以通过输出中提供的链接访问 Weights & Biases (W&B) 信息中心。此信息中心全面展示了 YOLO26 模型的训练过程。
Weights & Biases 信息中心的主要功能#
-
实时指标跟踪:观察 loss、准确率和验证分数等指标在训练过程中的变化,及时获取模型调优的参考信息。查看如何使用 Weights & Biases 跟踪实验。
-
超参数对比:每次运行都会记录训练参数,例如学习率和批次大小,方便你比较不同运行的配置。若要使用 W&B 日志进行自动搜索,请使用 Ray Tune。
-
对比分析:该平台支持并排比较不同的训练运行,这对于评估各种模型配置的影响以及了解哪些更改能提升性能至关重要。
-
训练进度可视化:通过关键指标的图形化展示,直观了解模型在各轮训练中的表现。了解 Weights & Biases 如何帮助你可视化验证结果。
-
资源监控:跟踪 CPU、GPU 和内存使用情况,以优化训练流程的效率,并发现工作流中潜在的瓶颈。
-
模型制品管理:访问并分享最佳模型检查点。训练结束时,该检查点会作为 W&B 制品上传,便于部署,也方便团队成员在复杂项目中协作。
借助这些功能,你可以跟踪、分析并优化 YOLO26 模型在目标检测任务中的训练过程。
总结#
本指南介绍了 Ultralytics YOLO 与 Weights & Biases 的集成,该集成可跟踪并可视化模型训练结果。借助 W&B,你可以简化机器学习工作流程、根据数据做出决策,并提升模型性能。
如需了解更多详情,请观看 Soumik Rakshit 在 YOLO VISION 2023 上关于此集成的演讲。
常见问题#
将 Weights & Biases 与 Ultralytics YOLO26 集成的步骤:
-
安装所需的软件包:
pip install -U ultralytics wandb yolo settings wandb=True -
登录你的 Weights & Biases 账户:
wandb login -
启用 W&B 记录并训练 YOLO26 模型:
from ultralytics import YOLO model = YOLO("yolo26n.pt") model.train(data="coco8.yaml", epochs=5, project="ultralytics", name="yolo26n")
这会自动将指标、超参数和模型制品记录到你的 W&B 项目中。
-
主要功能包括:
- 训练期间实时跟踪指标
- 不同运行之间的超参数对比
- 对比分析不同的训练运行
- 通过图表可视化训练进度
- 资源监控(CPU、GPU、内存使用情况)
- 管理和共享模型制品
这些功能有助于跟踪实验、优化模型,并更有效地协作开展 YOLO26 项目。
使用 W&B 集成运行训练脚本后:
- 控制台输出中会提供指向 W&B 信息中心的链接。
- 点击该链接,或前往 wandb.ai 并登录你的账户。
- 进入你的项目,查看详细指标、可视化结果和模型性能数据。
该信息中心可深入展示模型的训练过程,让你能够有效分析和改进 YOLO26 模型。
可以,你可以使用以下命令禁用 W&B 记录:
yolo settings wandb=False要重新启用记录,请使用:
yolo settings wandb=True这样你就可以控制何时使用 W&B 记录,而无需修改训练脚本。
Weights & Biases 通过以下方式帮助优化 YOLO26 模型:
- 提供训练指标的详细可视化结果
- 便于比较不同版本的模型
- 记录每次运行的训练参数,以便进行超参数调优对比
- 支持协作分析模型性能
- 便于共享模型制品和结果
这些功能可帮助研究人员和开发者更快地迭代,并根据数据做出决策,从而改进 YOLO26 模型。
评论25
提出问题、分享结果,或帮助其他人。
TJdeWet
Hi there,
I would like to know how (if possible) to log more than just these standard metrics for a yolov8 or v11 model during HP tuning logged with wandb: train/loss, val/loss, metrics/accuracy_top1, metrics_accuracy_top5.
This is what the code looks like:
!pip install -U ultralytics wandb !yolo settings wandb=True import wandb from ultralytics import YOLO wandb.login(key="...")
sweep_config = { 'method': "bayes", 'metric': { 'name': 'val/loss', 'goal': 'minimize', }, 'parameters': { .... } }
sweep_id = ...
def train(): run = wandb.init() model = YOLO('yolo11n-cls.pt')
results = model.train( data='...', batch=wandb.config.batch, lr0=wandb.config.lr0, epochs=..., optimizer=..., momentum=..., patience=..., save_period=..., imgsz=..., cache=False, save=True )wandb.agent(sweep_id, train, count=...)
Glenn JocherUltralytics@TJdeWet for custom metric logging beyond default training metrics in YOLO11 with W&B, you can access the complete results dictionary during training and log additional metrics manually through W&B's API. The automatically logged metrics vary by task type (detection/classification), but you can extend them using a callback. For implementation details, refer to the Weights & Biases integration documentation.
marcory-hub
Integration of Weights & Biases with YOLO11 in Google Colab
Is it currently possible to integrate Weights & Biases (W&B) with Ultralytics YOLO11 in Google Colab? If yes, where can I find the correct and up-to-date information to set this up properly?
My Issue
With YOLOv10, I was able to track all my training metrics in W&B seamlessly. However, when trying to replicate this with YOLO11, only GPU and system-related data are logged to the W&B platform. None of the training metrics or results appear.
Summary of things I've Tried
- Following the YouTube tutorial linked at the top of this page.
- Using the Try on Colab button on W&B's site
WARNING: Did not find branch or tag 'feat/ultralytics', assuming revision or ref. error: subprocess-exited-with-error × git checkout -q feat/ultralytics did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × git checkout -q feat/ultralytics did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Note: The Ultralytcs integration will be soon available as a fully supported feature on Weights & Biases once this pull request is merged.This is because the branch https://github.com/wandb/wandb@feat/ultralytics does not exist. To resolve this, I tried various fixes, including following older tutorials and experimenting with suggestions from forums and community discussions.
I’m particularly keen to switch to YOLO11 because it shows significant improvements in wildlife detection when compared to YOLOv10.
Any guidance, updates, or resources on enabling this integration would be greatly appreciated!
Glenn JocherUltralytics@marcory-hub yes, you can integrate Weights & Biases (W&B) with YOLO11 in Google Colab. Please ensure the latest
ultralyticsandwandbpackages are installed usingpip install -U ultralytics wandb. If training metrics are not being logged, ensure your W&B environment is authenticated (wandb.login(key="<API_KEY>")) and thatprojectandnameparameters are correctly specified inmodel.train(). Detailed setup instructions are available in our documentation: https://docs.ultralytics.com/integrations/weights-biases/. Let us know if further assistance is needed!marcory-hub
Thanks for the suggestions, @glenn-jocker ! I tried starting from scratch with W&B with YOLO11 but unfortunately, only system information showed up in W&B.
On top of that, it seems like my Colab runtime might be banned due to a potential issue with a compromised package. Hopefully, they'll restore access soon for everyone affected.
marcory-hub
Good news! My initial code now works for YOLO11. All metrics are automatically saved to W&B, and training is significantly faster than last week without maxing out Colab's RAM.
I'm not sure why it's significantly faster today, but I suspect the previous slowness prevented training from progressing beyond the warmup epochs, resulting in only system information being sent to W&B.
Glenn JocherUltralytics@marcory-hub glad to hear it's working well now, and thank you for sharing your experience! The faster training could indeed be related to runtime or hardware optimizations in Colab. Feel free to explore additional tips and resources in the YOLO11 W&B integration guide to further enhance your workflow. Let us know if you run into any other issues!
Pushtogithub23
I used this code, but it only logs system logs; no training metrics, media, or validation metrics are being logged as shown in the video. How can I log all the training metrics and media as demonstrated in the video? Can anyone help me with this?
from ultralytics import YOLO from wandb.integration.ultralytics import add_wandb_callback wandb.login() wandb.init(project = 'Hand-Gestures-Detection', name = 'yolov8n_5', job_type='training') model = YOLO("yolov8n.pt") add_wandb_callback(model, enable_model_checkpointing=True) model.train(data="HAND-GESTURE-DETECTION-1/data.yaml", epochs=5, imgsz = 640, project = "Hand-Gestures-Detection", name = 'yolov8n_5' )
Glenn JocherUltralytics@Pushtogithub23 to log all training metrics and media with Weights & Biases, ensure you have the latest versions of
ultralyticsandwandbinstalled. Also, verify that your Weights & Biases account is properly authenticated. For detailed setup instructions, visit the Weights & Biases integration guide.
TJdeWet
Hi!
I am using wandb to do hyperparameter tuning. Is it possible to maximise validation mAP50? This seems to be maximising the training mAP50 if I am not mistaken?
metric: name: mAP50 goal: maximise
Otherwise should I do hyperparameter using something like this instead:
metric: name: val/box_loss goal: minimize
Thanks!
Glenn JocherUltralytics(已编辑)Hi there!
For your hyperparameter tuning in Weights & Biases, if you're aiming to maximize validation mAP50 specifically, I recommend adjusting your metric settings accordingly. To focus on validation mAP50, your metric configuration might look something like this:
metric: name: metrics/mAP50(B) goal: maximizeThis setup ensures you're targeting the maximization of mAP50 on the validation data directly.
Using
val/box_losswith the goal to minimize can also be effective, especially if you prefer to focus on reducing the box prediction errors during validation:metric: name: val/box_loss goal: minimizeBoth approaches are valid and can be chosen based on your specific model performance goals. Hope this helps! If you have more questions, feel free to ask. 😊
TJdeWet
Hi Glenn,
Thanks for getting back to me. I just tried it, and it doesn't seem that tuning to maximize val/mAP50 is working - the only metrics I see that are being logged are: cls, box, and dfl loss (both train and val), and metrics/mAP50(B), metrics/mAP50-75(B), metrics/Recall(B), and metrics/Precision(B), which I believe are the training metrics? I would like to log those same metrics for validation, and tune based on val/mAP50. Is there a way I can do this?
Thanks!
Glenn JocherUltralytics(已编辑)Hi there!
In the Ultralytics W&B integration, validation metrics are logged automatically under names such as
metrics/mAP50(B)andmetrics/mAP50-95(B)(notval/mAP50; theval/prefix is used for validation losses likeval/box_loss). Use that name in your sweep config:metric: name: metrics/mAP50(B) goal: maximizeEnable the integration with
yolo settings wandb=True; no manualwandb.log()call is needed.
JiyongBoo
Is it possible to train RT-DETR model with wandb? In add_wandb_callback() function, there are nothing about rt-detr. I tried replacing yolov8 with rt-detr in the example code. In the result, I confirmed that runs.summary["Validation-Table"] and runs.summary["Train-Table"] did not appear. How do I solve this problem?
Paula DerrengerUltralytics(已编辑)@JiyongBoo hi there! 😊 RT-DETR trains with the same built-in Weights & Biases integration as other Ultralytics models, so you don't need
add_wandb_callback()or manualwandb.log()calls. Enable it withyolo settings wandb=True, then train as usual:from ultralytics import RTDETR model = RTDETR("rtdetr-l.pt") model.train(data="coco8.yaml", epochs=5, project="ultralytics", name="rtdetr-l")See the Weights & Biases integration guide for details.
DungHD-1997
from ultralytics import YOLO from wandb.integration.ultralytics import add_wandb_callback import wandb def train(): wandb.login() project_name = "test_ai" # Step 1: Initialize a Weights & Biases run wandb.init(project=project_name, job_type="training",name="old_data")
# Step 2: Define the YOLOv8 Model and Dataset model_name = "yolov8x" dataset_name = "C://Users//Administrator//project//yolov5_train_data//data//slant_2024.yaml" model = YOLO(f"{model_name}.pt") # Step 3: Add W&B Callback for Ultralytics add_wandb_callback(model, enable_model_checkpointing=True) model_name = "train_01" # Step 4: Train and Fine-Tune the Model model.train(project=project_name,name=model_name, data=dataset_name, epochs=100, imgsz=640, batch = 8) # Step 5: Validate the Model model.val() # Step 6: Perform Inference and Log Results #model(["path/to/image1", "path/to/image2"]) # Step 7: Finalize the W&B Run wandb.finish()if name == "main": train() I used the above code to train custom data, but when using wandb, the GPU memory continuously increases over epochs until an out of memory error appears. program has been stopped. Error message: torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 15.88 GiB of which 7.62 MiB is free. Of the allocated memory 14.53 GiB is allocated by PyTorch, and 103.44 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
DungHD-1997
If I don't use wandb, the GPU memory hardly changes over each epoch. Is there something wrong with my code?
Glenn JocherUltralytics(已编辑)Hey there! It looks like you're encountering a memory issue related to using Weights & Biases (W&B) with YOLOv8, specifically the GPU running out of memory during training. This can happen for a few reasons, including excessive memory allocation by PyTorch or W&B logging mechanisms. Here are a couple of suggestions that might help:
-
Reduce Batch Size: Lowering the batch size can significantly reduce memory usage. Although this might increase training time, it can help avoid out-of-memory errors.
model.train(batch=4) # Try reducing the batch size -
Clear CUDA Cache: Manually clearing the CUDA cache after each epoch might help release unused memory.
import torch torch.cuda.empty_cache() -
Optimize Model and DataLoader: Ensure that your DataLoader is not the source of the memory leak, and try to optimize your model's memory usage.
-
Disable W&B to isolate the cause: Run
yolo settings wandb=Falseand train again; if the out-of-memory error persists, W&B is not the cause.
Finally, consider profiling your code to identify specific lines or operations that cause the memory spike. PyTorch and W&B offer tools and documentation on best practices to optimize memory usage. 📘
Hope this helps! If the issue persists, feel free to reach out with more details or check the W&B and YOLOv8 docs for more insights.
-
danielst4
Hey there, I am trying to make a sweep using WandB but cant quite put together where to insert what into the YOLO call, to use different hyperparameters and track them. Even though I found a site where one has done this he too doesnt describe what he had to do. I am not sure if it is more complicated than I think and if I really have to insert stuff into the training function or if there is an easy way to achieve this. I would be really greatful for help to know what exactly is necessary to make a WandB hyperparameter sweep with YOLO Thanks Daniel
Paula DerrengerUltralytics(已编辑)Hey Daniel 👋,
Great to hear you're exploring hyperparameter sweeps with WandB and YOLO! It's simpler than it might seem at first. Here's a quick guide to get you started:
- Initialize WandB: Before starting your training, make sure to log in to WandB in your script or notebook using:
import wandb wandb.login() - Set Up the Sweep: Define your sweep configuration in WandB, using Ultralytics training argument names for the hyperparameters. For example:
method: random metric: name: val/box_loss goal: minimize parameters: lr0: min: 0.0001 max: 0.001 batch: values: [16, 32, 64] - Integrate Sweep with YOLO: Enable the built-in integration with
yolo settings wandb=True. If a W&B run is already active (as in a sweep agent), Ultralytics logs into it and keeps any config values the sweep already set.import wandb from ultralytics import YOLO def train(): wandb.init(project="your_project_name") model = YOLO("yolov8n.pt") model.train(data="coco128.yaml", epochs=5, imgsz=640, lr0=wandb.config.lr0, batch=wandb.config.batch) - Start the Sweep: Once everything is set up, you can start the sweep in WandB. This will iterate over the defined hyperparameter space, automatically logging each experiment's results.
Remember, the key parts are enabling the W&B setting and making sure your sweep parameter names match Ultralytics training arguments.
Hope this helps! If you have any more questions or run into issues, feel free to ask. Happy sweeping! 🚀
- Initialize WandB: Before starting your training, make sure to log in to WandB in your script or notebook using:
danielst4
Thank you Paula, this helped a lot! I can make a sweep now but I noticed that the values of the parameters which are given from the sweep to the script dont match the parameters used. Even though the command looks like this:
wandb.wandb_agent - INFO - About to run command: /usr/bin/env python training_rtdetr.py --batch=8 --epochs=6 --imgsz=1280 --learning_rate=0.0007013348811086657where "training_rtdetr.py" is a script with the code you gave me. (I am working with the RTDETR but it shouldnt make a difference as the metrics and calls are the same if I understand correctly?) But then after engine/trainer it says its using epochs=100 and it also uses that number for training. So just to be sure about a few things, I already logged in to WandB, I wrote a script with the code from your 3., I went to wandb, created a sweep there with the code from 2. + the lineprogram: training_rtdetr.pyand in a terminal I used the resulting callwandb agent [sweepid]Also I am kinda confused because you used "learning_rate" and "batch_size" but as far as I can tell these are not the parameters I can give for the training and they should be called "lr0" and "batch" respectively (which I both tried but they didnt work) I hope I provided all the necessary information otherwise just tell me what you need to know
Paula DerrengerUltralytics@danielst4 hey Daniel,
It sounds like you're on the right track with setting up your WandB sweep with YOLOv8! Regarding the mismatch in parameter values, it seems like there might be a discrepancy between the parameter names expected by your script and those defined in your WandB sweep configuration.
For YOLOv8, the correct parameter names for learning rate and batch size are indeed
lr0andbatch, respectively. If you're using these parameters in yourtraining_rtdetr.pyscript, ensure they're correctly parsed from the command line arguments. Here's a quick Python snippet usingargparsefor reference:import argparse # Parse command line arguments parser = argparse.ArgumentParser() parser.add_argument('--batch', type=int, help='batch size') parser.add_argument('--epochs', type=int, help='number of epochs') parser.add_argument('--imgsz', type=int, help='image size') parser.add_argument('--learning_rate', type=float, help='learning rate') args = parser.parse_args() # Now you can use args.batch, args.epochs, etc., in your scriptMake sure your WandB sweep configuration uses the same parameter names as those your script expects. For example:
program: training_rtdetr.py method: bayes metric: name: val/box_loss goal: minimize parameters: learning_rate: min: 0.0001 max: 0.001 batch: values: [8, 16, 32] epochs: value: 6 imgsz: value: 1280If you've already done this and the issue persists, it might be helpful to double-check how your script handles these parameters and ensure they're being correctly applied to your YOLOv8 model configuration.
Feel free to share more details if you're still encountering issues. We're here to help! 🚀
J0ELh
Is it possible to remove logging and only log progress in the command line? For my training process yolov8 seems to automatically connect with my wandb account and start logging. This process is very time-consuming so I'd like to know whether there's a flag like
log = Falseor something like that.J0ELh
^ this is because I'm done experimenting and need quick retraining options now.
Glenn JocherUltralytics(已编辑)Hey there! 👋 If you're looking to disable automatic logging to Weights & Biases (W&B) during your YOLOv8 training to speed things up, you can do so by setting the
wandbargument toFalsewhen initiating your training command. Here's how you can do it:yolo settings wandb=FalseThis is a global Ultralytics setting, not a train argument, and it stops YOLOv8 from connecting to your W&B account and logging training progress there. Happy training! 🚀