一、数据处理
将训练完成后得到的数据转换成csv文件
Class Images Instances Box(P R mAP50 mAP50-95):
all 63 259 0.916 0.776 0.845 0.421
missing_hole 63 52 0.974 1 0.992 0.553
mouse_bite 63 43 0.862 0.724 0.778 0.367
open_circuit 63 42 0.904 0.674 0.789 0.42
short 63 19 1 0.776 0.857 0.421
spur 63 69 0.956 0.628 0.808 0.374
spurious_copper 63 34 0.802 0.853 0.843 0.392
import csv
# 原始数据
raw_data = [
["Class", "Images", "Instances", "Box(P)", "R", "mAP50", "mAP50-95"],
["all", 63, 259, 0.916, 0.776, 0.845, 0.421],
["missing_hole", 63, 52, 0.974, 1, 0.992, 0.553],
["mouse_bite", 63, 43, 0.862, 0.724, 0.778, 0.367],
["open_circuit", 63, 42, 0.904, 0.674, 0.789, 0.42],
["short", 63, 19, 1, 0.776, 0.857, 0.421],
["spur", 63, 69, 0.956, 0.628, 0.808, 0.374],
["spurious_copper", 63, 34, 0.802, 0.853, 0.843, 0.392]
]
# 将数据保存到CSV文件
with open('data.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerows(raw_data)
print("Data has been saved to data.csv")
二、读取CSV文件并做树状图
代码部分
import matplotlib.pyplot as plt
import pandas as pd
# 从CSV文件读取数据
data = pd.read_csv('runs/train/exp8/class.csv')
# 获取类别和指标数据
categories = data['Class']
images = data['Images']
instances = data['Instances']
box_p = data['Box(P)']
R = data['R']
mAP50 = data['mAP50']
mAP50_95 = data['mAP50-95']
# 指标名称
metrics = ["Images", "Instances", "Box(P)", "R", "mAP50", "mAP50-95"]
# 设置子图
fig, axs = plt.subplots(2, 3, figsize=(15, 10))
fig.suptitle('Performance Metrics by Class', fontsize=16)
# 绘制条形图
for i, metric in enumerate(metrics):
row = i // 3
col = i % 3
ax = axs[row, col]
ax.bar(categories, [images, instances, box_p, R, mAP50, mAP50_95][i])
ax.set_title(metric)
ax.set_xticks(range(len(categories))) # 设置刻度位置
ax.set_xticklabels(categories, rotation=45, ha="right") # 设置刻度标签
# 调整布局
plt.tight_layout()
plt.subplots_adjust(top=0.9)
# 显示图表
plt.show()
例子展示
三、对比图(一张图全部对比)
将若干个训练完成后的不同类别精度做对比
代码部分
import matplotlib.pyplot as plt
import pandas as pd
'''所有分类所有指标对比在一张图上'''
# 读取第一个CSV文件
data1 = pd.read_csv('runs/train/exp8/class.csv')
# 读取第二个CSV文件
data2 = pd.read_csv('runs/train/exp9/class.csv')
# 设置类别
categories = data1['Class']
# 提取两个文件的不同指标数据
metrics = ["Images", "Instances", "Box(P)", "R", "mAP50", "mAP50-95"]
# 创建图表
plt.figure(figsize=(10, 6))
plt.title('Comparison of Metrics between two datasets')
width = 0.35
x = range(len(categories))
for i, metric in enumerate(metrics):
修改label=f'fasternet 在图中展示不同数据集
plt.bar([pos + i * width for pos in x], data1[metric], width=width, label=f'fasternet - {metric}')
plt.bar([pos + i * width for pos in x], data2[metric], width=width, label=f'c2f-faster - {metric}', alpha=0.6)
plt.xlabel('Class')
plt.ylabel('Values')
plt.xticks([pos + width * len(metrics) / 2 for pos in x], categories, rotation=45, ha="right")
plt.legend(loc='upper right')
# 显示图表
plt.tight_layout()
plt.show()
例子展示
如果类别太多则显示不明显
四、对比图(指标类型分开对比)
import matplotlib.pyplot as plt
import pandas as pd
'''单独指标对比,多少指标生成多少张图'''
# 读取第一个CSV文件
data1 = pd.read_csv('runs/train/exp8/class.csv')
# 读取第二个CSV文件
data2 = pd.read_csv('runs/train/exp9/class.csv')
# 设置类别
categories = data1['Class']
# 提取两个文件的不同指标数据
metrics = ["Images", "Instances", "Box(P)", "R", "mAP50", "mAP50-95"]
# 循环创建单独的图表
for metric in metrics:
plt.figure(figsize=(10, 6))
plt.title(f'Comparison of {metric} between two datasets')
plt.bar(categories, data1[metric], label='Dataset 1', alpha=0.6) # # 修改label=f'fasternet 在图中展示不同数据集
plt.bar(categories, data2[metric], label='Dataset 2', alpha=0.6)
plt.xlabel('Class')
plt.ylabel(metric)
plt.xticks(rotation=45, ha="right")
plt.legend()
plt.tight_layout()
# 显示图表
plt.show()