Repository: espnet/espnet

Issue:
Disable attention plot with a command line parameter
**Describe the bug**
I'm training on clusters, but the machines don't have libpng installed. Since I don't really need the attention plot, I want to just disable the plot. However, I cannot find a way to disable that.

**To Reproduce**
Steps to reproduce the behavior:
1. run the librispeech recipe on a machine without libpng

**Error logs**
```
Traceback (most recent call last):
  File "/home/storage15/tangjiyang/espnet/tools/anaconda/envs/espnet/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/home/storage15/tangjiyang/espnet/tools/anaconda/envs/espnet/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/storage15/tangjiyang/espnet/espnet2/tasks/abs_task.py", line 1266, in main_worker
    cls.trainer.run(
  File "/home/storage15/tangjiyang/espnet/espnet2/train/trainer.py", line 310, in run
    cls.plot_attention(
  File "/home/storage15/tangjiyang/espnet/tools/anaconda/envs/espnet/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 49, in decorate_no_grad
    return func(*args, **kwargs)
  File "/home/storage15/tangjiyang/espnet/espnet2/train/trainer.py", line 766, in plot_attention
    fig.savefig(p)
  File "/home/storage15/tangjiyang/espnet/tools/anaconda/envs/espnet/lib/python3.8/site-packages/matplotlib/figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/home/storage15/tangjiyang/espnet/tools/anaconda/envs/espnet/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
    result = print_method(
  File "/home/storage15/tangjiyang/espnet/tools/anaconda/envs/espnet/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 501, in print_png
    from matplotlib import _png
```



Discussion:
Is it espnet1 or espnet2?
We may have a way to hack it.
Let me think.
> 
> 
> Is it espnet1 or espnet2?
> We may have a way to hack it.
> Let me think.

It's espnet2, I worked around this by commenting out some code at https://github.com/espnet/espnet/blob/master/espnet2/train/trainer.py#L305 and https://github.com/espnet/espnet/blob/master/espnet2/train/trainer.py#L334

but it would be nice if there's an arg for it...
Could you try `--num_att_plot 0`?
I don't know the exact behavior but according to https://github.com/espnet/espnet/blob/d7cb673a05280c7278f1ffd024a750c1cfb0f2ec/espnet2/tasks/abs_task.py#L1222-L1229, it can disable it.
Oops using `--num_att_plot` was actually the first thing that came up in my mind, but I was kinda confused by the [doc](https://espnet.github.io/espnet/apis/espnet2_bin.html#asr-train-py)

However, this line https://github.com/espnet/espnet/blob/master/espnet2/train/trainer.py#L334 is still causing the same error
I see.
Do you think 
```
                reporter.matplotlib_plot(output_dir / "images")
```
-->
```
                if plot_attention_iter_factory is not None:
                    reporter.matplotlib_plot(output_dir / "images")
```
with `--num_att_plot 0` solve the issue?
If so, I'll make a PR to fix it.


Resolving patch:
diff --git a/espnet2/train/trainer.py b/espnet2/train/trainer.py
--- a/espnet2/train/trainer.py
+++ b/espnet2/train/trainer.py
@@ -331,7 +331,8 @@ def run(
             if not distributed_option.distributed or distributed_option.dist_rank == 0:
                 # 3. Report the results
                 logging.info(reporter.log_message())
-                reporter.matplotlib_plot(output_dir / "images")
+                if plot_attention_iter_factory is not None:
+                    reporter.matplotlib_plot(output_dir / "images")
                 if summary_writer is not None:
                     reporter.tensorboard_add_scalar(summary_writer)
                 if trainer_options.use_wandb:


Test patch: