Repository: pytorch/ignite

Issue:
[Feature Request] Save lr_finder plot instead of its showing
It would be great, if one can save plot of LRFinder module instead of showing it. Because, for example, in many cases we run our training scripts on Servers (e.g., AWS) and it is better to save the result plot (loss against learning rate plot) instead of showing it.
Thanks


Discussion:
@ahkarami thanks for the suggestion ! I agree that [plot method](https://github.com/pytorch/ignite/blob/13a42b90589112e67bcc6e1780e7c826a5a34199/ignite/contrib/handlers/lr_finder.py#L189) could return the figure and have filepath optional argument to save current plot on disk.

Resolving patch:
diff --git a/ignite/contrib/handlers/lr_finder.py b/ignite/contrib/handlers/lr_finder.py
--- a/ignite/contrib/handlers/lr_finder.py
+++ b/ignite/contrib/handlers/lr_finder.py
@@ -186,7 +186,14 @@ def get_results(self) -> Dict[str, List[Any]]:
         """
         return self._history
 
-    def plot(self, skip_start: int = 10, skip_end: int = 5, log_lr: bool = True) -> None:
+    def plot(
+        self,
+        skip_start: int = 10,
+        skip_end: int = 5,
+        log_lr: bool = True,
+        filepath: Optional[str] = None,
+        **kwargs: Any,
+    ) -> None:
         """Plots the learning rate range test.
 
         This method requires `matplotlib` package to be installed:
@@ -202,6 +209,9 @@ def plot(self, skip_start: int = 10, skip_end: int = 5, log_lr: bool = True) ->
                 Default: 5.
             log_lr: True to plot the learning rate in a logarithmic
                 scale; otherwise, plotted in a linear scale. Default: True.
+            filepath: The file name to save the plot to. Default: None.
+            kwargs: optional kwargs passed to ``plt.savefig`` if ``filepath`` is provided.
+                matplotlib method `savefig` to save the plot.
         """
         try:
             from matplotlib import pyplot as plt
@@ -237,6 +247,11 @@ def plot(self, skip_start: int = 10, skip_end: int = 5, log_lr: bool = True) ->
             plt.xscale("log")
         plt.xlabel("Learning rate")
         plt.ylabel("Loss")
+        if filepath is not None:
+            try:
+                plt.savefig(filepath, **kwargs)
+            except FileNotFoundError:
+                self.logger.warning(f"Cannot save the file, non existing path:{filepath}")
         plt.show()
 
     def lr_suggestion(self) -> Any:


Test patch:
diff --git a/tests/ignite/contrib/handlers/test_lr_finder.py b/tests/ignite/contrib/handlers/test_lr_finder.py
--- a/tests/ignite/contrib/handlers/test_lr_finder.py
+++ b/tests/ignite/contrib/handlers/test_lr_finder.py
@@ -1,4 +1,5 @@
 import copy
+from os import path
 
 import matplotlib
 import pytest
@@ -230,6 +231,15 @@ def test_plot(lr_finder, to_save, dummy_engine, dataloader):
 
     lr_finder.plot()
     lr_finder.plot(skip_end=0)
+    lr_finder.plot(skip_end=0, filepath="dummy.jpg")
+    lr_finder.plot(
+        skip_end=0, filepath="dummy.jpg", orientation="landscape", papertype="a4", format="png",
+    )
+    assert path.exists("dummy.jpg")
+    lr_finder.plot(
+        skip_end=0, filepath="/nonexisting/dummy.jpg", orientation="landscape", papertype="a4", format="png",
+    )
+    assert not path.exists("/nonexisting/dummy.jpg")
 
 
 def test_no_matplotlib(no_site_packages, lr_finder):