本文给大家带来的是YOLOv8系列的绘图功能,我将向大家介绍YOLO系列的绘图功能。我们在进行实验时,经常需要比较多个结果,针对这一问题,我写了点代码来解决这个问题,它可以根据训练结果绘制损失(loss)和mAP(平均精度均值)的对比图。这个工具不仅支持多个文件的对比分析,还允许大家在现有代码的基础上进行修,从而达到数据可视化的功能,大家也可以将对比图来放在论文中进行对比也是非常不错的选择。
先展示一下效果图->
损失对比图象->
?
目录
import os
import pandas as pd
import matplotlib.pyplot as plt
def plot_metrics_and_loss(experiment_names, metrics_info, loss_info, metrics_subplot_layout, loss_subplot_layout,
metrics_figure_size=(15, 10), loss_figure_size=(15, 10), base_directory='runs/train'):
# Plot metrics
plt.figure(figsize=metrics_figure_size)
for i, (metric_name, title) in enumerate(metrics_info):
plt.subplot(*metrics_subplot_layout, i + 1)
for name in experiment_names:
file_path = os.path.join(base_directory, name, 'results.csv')
data = pd.read_csv(file_path)
column_name = [col for col in data.columns if col.strip() == metric_name][0]
plt.plot(data[column_name], label=name)
plt.xlabel('Epoch')
plt.title(title)
plt.legend()
plt.tight_layout()
metrics_filename = 'metrics_curves.png'
plt.savefig(metrics_filename)
plt.show()
# Plot loss
plt.figure(figsize=loss_figure_size)
for i, (loss_name, title) in enumerate(loss_info):
plt.subplot(*loss_subplot_layout, i + 1)
for name in experiment_names:
file_path = os.path.join(base_directory, name, 'results.csv')
data = pd.read_csv(file_path)
column_name = [col for col in data.columns if col.strip() == loss_name][0]
plt.plot(data[column_name], label=name)
plt.xlabel('Epoch')
plt.title(title)
plt.legend()
plt.tight_layout()
loss_filename = 'loss_curves.png'
plt.savefig(loss_filename)
plt.show()
return metrics_filename, loss_filename
# Metrics to plot
metrics_info = [
('metrics/precision(B)', 'Precision'),
('metrics/recall(B)', 'Recall'),
('metrics/mAP50(B)', 'mAP at IoU=0.5'),
('metrics/mAP50-95(B)', 'mAP for IoU Range 0.5-0.95')
]
# Loss to plot
loss_info = [
('train/box_loss', 'Training Box Loss'),
('train/cls_loss', 'Training Classification Loss'),
('train/dfl_loss', 'Training DFL Loss'),
('val/box_loss', 'Validation Box Loss'),
('val/cls_loss', 'Validation Classification Loss'),
('val/dfl_loss', 'Validation DFL Loss')
]
# Plot the metrics and loss from multiple experiments
metrics_filename, loss_filename = plot_metrics_and_loss(
experiment_names=['exp294', 'exp297', 'exp293', 'exp291', 'exp287'],
metrics_info=metrics_info,
loss_info=loss_info,
metrics_subplot_layout=(2, 2),
loss_subplot_layout=(2, 3)
)
?
使用方式非常简单,我们首先创建一个文件,将核心代码粘贴进去,其中experiment_names这个参数就代表我们的每个训练结果的名字, 我们只需要修改这个即可,我这里就是五个结果进行对比,修改完成之后大家运行该文件即可。
?
到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv8改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,目前本专栏免费阅读(暂时,大家尽早关注不迷路~),如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~