Skip to content

Commit

Permalink
Close matplotlib plots after opening (#4612)
Browse files Browse the repository at this point in the history
* Close plots

* Replace fig.close() for plt.close()
  • Loading branch information
glenn-jocher committed Aug 30, 2021
1 parent 11e7c7b commit bb5ebc2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions utils/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def plot(self, normalize=True, save_dir='', names=()):
fig.axes[0].set_xlabel('True')
fig.axes[0].set_ylabel('Predicted')
fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
plt.close()
except Exception as e:
print(f'WARNING: ConfusionMatrix plot failure: {e}')

Expand Down Expand Up @@ -308,6 +309,7 @@ def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()):
ax.set_ylim(0, 1)
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
fig.savefig(Path(save_dir), dpi=250)
plt.close()


def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'):
Expand All @@ -328,3 +330,4 @@ def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence'
ax.set_ylim(0, 1)
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
fig.savefig(Path(save_dir), dpi=250)
plt.close()
4 changes: 3 additions & 1 deletion utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ def profile_idetection(start=0, stop=0, labels=(), save_dir=''):
a.remove()
except Exception as e:
print('Warning: Plotting error for %s; %s' % (f, e))

ax[1].legend()
plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200)

Expand All @@ -371,6 +370,7 @@ def plot_evolve(evolve_csv=Path('path/to/evolve.csv')): # from utils.plots impo
print('%15s: %.3g' % (k, mu))
f = evolve_csv.with_suffix('.png') # filename
plt.savefig(f, dpi=200)
plt.close()
print(f'Saved {f}')


Expand All @@ -397,6 +397,7 @@ def plot_results(file='path/to/results.csv', dir=''):
print(f'Warning: Plotting error for {f}: {e}')
ax[1].legend()
fig.savefig(save_dir / 'results.png', dpi=200)
plt.close()


def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')):
Expand All @@ -423,3 +424,4 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec

print(f'Saving {save_dir / f}... ({n}/{channels})')
plt.savefig(save_dir / f, dpi=300, bbox_inches='tight')
plt.close()

0 comments on commit bb5ebc2

Please sign in to comment.