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:
vfdev-5: @Moh-Yakoub please fix mypy: https://github.com/pytorch/ignite/runs/2318812634
Moh-Yakoub: @vfdev-5 any more thoughts on this one?
vfdev-5: > @vfdev-5 any more thoughts on this one?

@Moh-Yakoub sorry for delay. I left another comment about the added test. Otherwise it looks good to be merged... 
Btw. have you checked if it works locally and we can see the image with the plot ?
Moh-Yakoub: > > @vfdev-5 any more thoughts on this one?
> 
> @Moh-Yakoub sorry for delay. I left another comment about the added test. Otherwise it looks good to be merged...
> Btw. have you checked if it works locally and we can see the image with the plot ?

Yes, I have tested using the example notebook and was able to reproduce the graph matching the output result.
Thanks a lot for reviewing this!
Moh-Yakoub: @vfdev-5 I've updated the test cases. the `savefig` doesn't create directory automatically if it doesn't exist. I added a test case to reflect that

Patch:
diff --git a/ignite/contrib/handlers/lr_finder.py b/ignite/contrib/handlers/lr_finder.py
index 651c9be8..d58db9ff 100644
--- a/ignite/contrib/handlers/lr_finder.py
+++ b/ignite/contrib/handlers/lr_finder.py
@@ -186,7 +186,14 @@ class FastaiLRFinder:
         """
         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 @@ class FastaiLRFinder:
                 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 @@ class FastaiLRFinder:
             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
index e4a60501..ab8fc814 100644
--- 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):