Repository: chainer/chainer

Issue:
MNIST example gets invalid DISPLAY error when run without X11
If MNIST example is run in the environment where matplotlib is installed but X11 is not available (e.g. ssh), the training script fails with `RuntimeError: Invalid DISPLAY variable` error.

```
Exception in main training loop: Invalid DISPLAY variable
Traceback (most recent call last):
  File "/data/work/w/repos/chainer/chainer/training/trainer.py", line 302, in run
    entry.extension(self)
  File "/data/work/w/repos/chainer/chainer/training/extensions/plot_report.py", line 150, in __call__
    f = plt.figure()
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 534, in figure
    **kwargs)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 170, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 176, in new_figure_manager_given_figure
    canvas = cls.FigureCanvas(figure)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 35, in __init__
    super(FigureCanvasQTAggBase, self).__init__(figure=figure)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 235, in __init__
    _create_qApp()
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 122, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
  File "train_mnist.py", line 124, in <module>
    main()
  File "train_mnist.py", line 120, in main
    trainer.run()
  File "/data/work/w/repos/chainer/chainer/training/trainer.py", line 313, in run
    six.reraise(*sys.exc_info())
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise
    raise value
  File "/data/work/w/repos/chainer/chainer/training/trainer.py", line 302, in run
    entry.extension(self)
  File "/data/work/w/repos/chainer/chainer/training/extensions/plot_report.py", line 150, in __call__
    f = plt.figure()
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 534, in figure
    **kwargs)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 170, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 176, in new_figure_manager_given_figure
    canvas = cls.FigureCanvas(figure)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 35, in __init__
    super(FigureCanvasQTAggBase, self).__init__(figure=figure)
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 235, in __init__
    _create_qApp()
  File "/home/niboshi/anaconda/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 122, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
```


Discussion:

Resolving patch:
diff --git a/examples/mnist/train_mnist.py b/examples/mnist/train_mnist.py
--- a/examples/mnist/train_mnist.py
+++ b/examples/mnist/train_mnist.py
@@ -44,6 +44,8 @@ def main():
                         help='Resume the training from snapshot')
     parser.add_argument('--unit', '-u', type=int, default=1000,
                         help='Number of units')
+    parser.add_argument('--noplot', dest='plot', action='store_false',
+                        help='Disable PlotReport extension')
     args = parser.parse_args()
 
     print('GPU: {}'.format(args.gpu))
@@ -91,7 +93,7 @@ def main():
     trainer.extend(extensions.LogReport())
 
     # Save two plot images to the result dir
-    if extensions.PlotReport.available():
+    if args.plot and extensions.PlotReport.available():
         trainer.extend(
             extensions.PlotReport(['main/loss', 'validation/main/loss'],
                                   'epoch', file_name='loss.png'))


Test patch: