WongKinYiu / yolov9

Implementation of paper - YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
GNU General Public License v3.0
8.94k stars 1.41k forks source link

Generating or Plotting a 'result.png' from 'result.csv' #557

Open law4percent opened 2 months ago

law4percent commented 2 months ago

After training a YOLOv9 instance segmentation model, I noticed that only a 'result.csv' file was generated, but no 'result.png' graph was created. How can I generate a 'result.png' graph using the information from the 'result.csv' file?

By the way, I have already made a code but I think the smooth() function was the problem.

import pandas as pd
import matplotlib.pyplot as plt
import os
import numpy as np

# Load your CSV file
file_path = '/content/gdrive/MyDrive/YOLOv9-SEGMENTATION/runs/segment/train/results.csv'
df = pd.read_csv(file_path)

# Strip leading/trailing whitespace from column names
df.columns = df.columns.str.strip()

print(df.head()) # Check the column names after stripping whitespace

output_dir = '/content/gdrive/MyDrive/YOLOv9-SEGMENTATION/plots'
os.makedirs(output_dir, exist_ok=True)

columns_to_plot = [
    'train/box_loss', 'train/seg_loss', 'train/cls_loss', 'train/dfl_loss',
    'metrics/precision(B)', 'metrics/recall(B)', 'metrics/precision(M)', 'metrics/recall(M)',
    'val/box_loss', 'val/seg_loss', 'val/cls_loss', 'val/dfl_loss',
    'metrics/mAP50(B)', 'metrics/mAP50-95(B)', 'metrics/mAP50(M)', 'metrics/mAP50-95(M)'
]

def smooth(y, box_pts):
    box = np.ones(box_pts) / box_pts
    y_smooth = np.convolve(y, box, mode='same')
    return y_smooth

n_cols = 8
n_rows = 2

fig, axs = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(24, 6))
axs = axs.flatten()

# Plot each column
for i, column in enumerate(columns_to_plot):
    # Check if the column exists before plotting
    if column in df.columns: 
        axs[i].plot(df.index, df[column], marker='o', linestyle='-', color='#0072BD', label='results')
        axs[i].plot(df.index, smooth(df[column], 5), linestyle=':', color='darkorange', label='smooth')
        axs[i].set_title(column)
        # axs[i].set_xlabel('epoch')
        # axs[i].set_ylabel(column)
        if i == 1:
          axs[i].legend()
    else:
        print(f"Warning: Column '{column}' not found in DataFrame.")

# Remove any unused subplots
for j in range(i + 1, len(axs)):
    fig.delaxes(axs[j])

plt.tight_layout()
plt.savefig(os.path.join(output_dir, 'all_plots_with_smoothing.png'), dpi=300)
plt.show()

Result sample_1 result

Result sample_2 download

law4percent commented 2 months ago

Ok I have solved the problem... thanks self 👌

def smooth(y, box_pts):
    box = np.ones(box_pts) / box_pts
    y_smooth = np.convolve(y, box, mode='valid')
    pad = (len(y) - len(y_smooth)) // 2
    y_smooth = np.pad(y_smooth, (pad, len(y) - len(y_smooth) - pad), mode='constant', constant_values=np.nan)
    return y_smooth

Sample result results