[map, loss, pr, F1 drawing in YOLO series result] use matplotlib to draw the result file generated after training according to v5, v8, v7

1. Rendering

insert image description here

2. Understand the result content

2.1 YOLOv7的result.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

insert image description here

2.2 YOLOv5 | YOLOv8 result.csv (each column has the same meaning)

The meaning of each column is the same , probably because the same author wrote the code, so the saved content of result.csv has not changed

YOLOv5

insert image description here

YOLOv8

insert image description here

3. Code (only map and loss)

  • What needs to be changed : only the content in, each line represents andresult_dict模型名称该模型训练得到的result文件地址
  • Both v5 and v8 are csvin format, and v7 is txtin format
  • Key points to pay attention to : In the following code 关于下标的注释位置, the subscripts are all 0incremented from the beginning to the right
  • plt.savefig("mAP50.png", dpi=600)# dpi can be set to 300/600/900, which means saving as a higher-definition vector image
  • data = pd.read_csv(res_path, usecols=[6]).values.ravel()Indicates that after reading csvthe file, only take the 6value of the subscripted column values, and ravel()flatten the value into a one-dimensional array (if you want to draw result.csv/result.txtother values, just change the corresponding subscript directly. For example, if you want to draw train/box_loss, Then 绘制map50change 6 to 1, and change 10 to 2)
  • plt.plot(x, data, label=modelname, linewidth='1')# Line thickness is set to 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('[email protected]')
    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('[email protected]: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()

4. For drawing PR and F1, check this article

Guess you like

Origin blog.csdn.net/LWD19981223/article/details/130017781#comments_27795997