目录
1. 效果图
2. 认识result内容
2.1 YOLOv7的result.txt
- 参考链接:YOLOv7结果分析,txt文件内容
0/299 14.7G 0.07522 0.009375 0.02266 0.1073 58 640 0.0002958 0.1458 0.0002676 4.469e-05 0.1005 0.01098 0.02545
训练轮数 GPU消耗 train/box_loss train/obj_loss train/cls_loss train/total_loss 本轮中数据中目标数量 输入图片大小 P R mAP@.5 mAP@.5:.95 val/Box_loss val/obj_loss val/cls_loss
2.2 YOLOv5 | YOLOv8的result.csv(每列含义相同)
每列的含义是相同的,可能因为是同一个作者写的代码,所以result.csv的保存内容没有发生改变
YOLOv5
YOLOv8
3. 代码(只有map和loss)
- 需要更改的地方:仅为
result_dict
中的内容,每行表示的是模型名称
和该模型训练得到的result文件地址
- v5、v8都是
csv
格式的,v7是txt
格式的 - 需要关注的重点地方:下面代码中
关于下标的注释位置
,下标都是从0
开始向右依次加一的 plt.savefig("mAP50.png", dpi=600)
# dpi可设为300/600/900,表示存为更高清的矢量图data = pd.read_csv(res_path, usecols=[6]).values.ravel()
表示读取csv
文件后,只取下标为6
的一列的值values
,并将值ravel()
展平为一维数组(如果想要绘制result.csv/result.txt
中的其他值,直接改为对应的下标即可。例如想要绘制train/box_loss
,则绘制map50
中的6改为1,10改为2)plt.plot(x, data, label=modelname, linewidth='1')
# 线条粗细设为1
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
if __name__ == '__main__':
# 列出待获取数据内容的文件位置
# v5、v8都是csv格式的,v7是txt格式的
result_dict = {
'YOLOv5m': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov5m\results.csv',
'YOLOv7': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7\results.txt',
'YOLOv7-tiny': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-tiny\results.txt',
'YOLOv7-tiny-large': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-tiny-large\results.txt',
'YOLOv7-tiny-PConv': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-tiny-PConv\results.txt',
'YOLOv7-tiny-GhostNetv2': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-tiny-GhostNetv2\results.txt',
# 'YOLOv8s': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov8s\results.csv',
'YOLOv7-tiny-large-GhostNetv2': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-tiny-large-GhostNetv2\results.txt',
'YOLOv7-GhostNetv2': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-GhostNetv2\results.txt',
'YOLOv7-FasterNet': r'C:\Users\dadandan\Desktop\workspace_temp\NeimengA_train_resultcsv\yolov7-FasterNet\results.txt'
}
# 绘制map50
for modelname in result_dict:
res_path = result_dict[modelname]
ext = res_path.split('.')[-1]
if ext == 'csv':
data = pd.read_csv(res_path, usecols=[6]).values.ravel() # 6是指map50的下标(每行从0开始向右数)
else: # 文件后缀是txt
with open(res_path, 'r') as f:
datalist = f.readlines()
data = []
for d in datalist:
data.append(float(d.strip().split()[10])) # 10是指map50的下标(每行从0开始向右数)
data = np.array(data)
x = range(len(data))
plt.plot(x, data, label=modelname, linewidth='1') # 线条粗细设为1
# 添加x轴和y轴标签
plt.xlabel('Epochs')
plt.ylabel('mAP@0.5')
plt.legend()
plt.grid()
# 显示图像
plt.savefig("mAP50.png", dpi=600) # dpi可设为300/600/900,表示存为更高清的矢量图
plt.show()
# 绘制map50-95
for modelname in result_dict:
res_path = result_dict[modelname]
ext = res_path.split('.')[-1]
if ext == 'csv':
data = pd.read_csv(res_path, usecols=[7]).values.ravel() # 7是指map50-95的下标(每行从0开始向右数)
else:
with open(res_path, 'r') as f:
datalist = f.readlines()
data = []
for d in datalist:
data.append(float(d.strip().split()[11])) # 11是指map50-95的下标(每行从0开始向右数)
data = np.array(data)
x = range(len(data))
plt.plot(x, data, label=modelname, linewidth='1')
# 添加x轴和y轴标签
plt.xlabel('Epochs')
plt.ylabel('mAP@0.5:0.95')
plt.legend()
plt.grid()
# 显示图像
plt.savefig("mAP50-95.png", dpi=600)
plt.show()
# 绘制训练的总loss
for modelname in result_dict:
res_path = result_dict[modelname]
ext = res_path.split('.')[-1]
if ext == 'csv':
box_loss = pd.read_csv(res_path, usecols=[1]).values.ravel()
obj_loss = pd.read_csv(res_path, usecols=[2]).values.ravel()
cls_loss = pd.read_csv(res_path, usecols=[3]).values.ravel()
data = np.round(box_loss + obj_loss + cls_loss, 5) # 3个loss相加并且保留小数点后5位(与v7一致)
else:
with open(res_path, 'r') as f:
datalist = f.readlines()
data = []
for d in datalist:
data.append(float(d.strip().split()[5]))
data = np.array(data)
x = range(len(data))
plt.plot(x, data, label=modelname, linewidth='1')
# 添加x轴和y轴标签
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.grid()
# 显示图像
plt.savefig("loss.png", dpi=600)
plt.show()