Plot training results from a results CSV file. The function supports various types of data including segmentation, pose estimation, and classification. Plots are saved as 'results.png' in the directory where the CSV is located. Args: file (str, optional): Path to the CSV file c
(file='path/to/results.csv', dir='', segment=False, pose=False, classify=False, on_plot=None)
| 513 | |
| 514 | @plt_settings() |
| 515 | def plot_results(file='path/to/results.csv', dir='', segment=False, pose=False, classify=False, on_plot=None): |
| 516 | """ |
| 517 | Plot training results from a results CSV file. The function supports various types of data including segmentation, |
| 518 | pose estimation, and classification. Plots are saved as 'results.png' in the directory where the CSV is located. |
| 519 | |
| 520 | Args: |
| 521 | file (str, optional): Path to the CSV file containing the training results. Defaults to 'path/to/results.csv'. |
| 522 | dir (str, optional): Directory where the CSV file is located if 'file' is not provided. Defaults to ''. |
| 523 | segment (bool, optional): Flag to indicate if the data is for segmentation. Defaults to False. |
| 524 | pose (bool, optional): Flag to indicate if the data is for pose estimation. Defaults to False. |
| 525 | classify (bool, optional): Flag to indicate if the data is for classification. Defaults to False. |
| 526 | on_plot (callable, optional): Callback function to be executed after plotting. Takes filename as an argument. |
| 527 | Defaults to None. |
| 528 | |
| 529 | Example: |
| 530 | ```python |
| 531 | from ultralytics.utils.plotting import plot_results |
| 532 | |
| 533 | plot_results('path/to/results.csv', segment=True) |
| 534 | ``` |
| 535 | """ |
| 536 | import pandas as pd |
| 537 | from scipy.ndimage import gaussian_filter1d |
| 538 | save_dir = Path(file).parent if file else Path(dir) |
| 539 | if classify: |
| 540 | fig, ax = plt.subplots(2, 2, figsize=(6, 6), tight_layout=True) |
| 541 | index = [1, 4, 2, 3] |
| 542 | elif segment: |
| 543 | fig, ax = plt.subplots(2, 8, figsize=(18, 6), tight_layout=True) |
| 544 | index = [1, 2, 3, 4, 5, 6, 9, 10, 13, 14, 15, 16, 7, 8, 11, 12] |
| 545 | elif pose: |
| 546 | fig, ax = plt.subplots(2, 9, figsize=(21, 6), tight_layout=True) |
| 547 | index = [1, 2, 3, 4, 5, 6, 7, 10, 11, 14, 15, 16, 17, 18, 8, 9, 12, 13] |
| 548 | else: |
| 549 | fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True) |
| 550 | index = [1, 2, 3, 4, 5, 8, 9, 10, 6, 7] |
| 551 | ax = ax.ravel() |
| 552 | files = list(save_dir.glob('results*.csv')) |
| 553 | assert len(files), f'No results.csv files found in {save_dir.resolve()}, nothing to plot.' |
| 554 | for f in files: |
| 555 | try: |
| 556 | data = pd.read_csv(f) |
| 557 | s = [x.strip() for x in data.columns] |
| 558 | x = data.values[:, 0] |
| 559 | for i, j in enumerate(index): |
| 560 | y = data.values[:, j].astype('float') |
| 561 | # y[y == 0] = np.nan # don't show zero values |
| 562 | ax[i].plot(x, y, marker='.', label=f.stem, linewidth=2, markersize=8) # actual results |
| 563 | ax[i].plot(x, gaussian_filter1d(y, sigma=3), ':', label='smooth', linewidth=2) # smoothing line |
| 564 | ax[i].set_title(s[j], fontsize=12) |
| 565 | # if j in [8, 9, 10]: # share train and val loss y axes |
| 566 | # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5]) |
| 567 | except Exception as e: |
| 568 | LOGGER.warning(f'WARNING: Plotting error for {f}: {e}') |
| 569 | ax[1].legend() |
| 570 | fname = save_dir / 'results.png' |
| 571 | fig.savefig(fname, dpi=200) |
| 572 | plt.close() |
no test coverage detected