Repository: pytorch/ignite

Issue:
simple arg name mismatch for mnist tensorboard example
## 🐛 Bug description
<!-- A clear and concise description of what the bug is. -->
Example for mnist with tensorboard does not work due to wrong arg name.

<!-- Please, add steps on how to reproduce it. -->
>git clone https://github.com/pytorch/ignite
>cd examples/mnist
>python3.6 mnist_with_tensorboard.py --log_dir=/tmp/tensorboard_logs
Traceback (most recent call last):
  File "mnist_with_tensorboard.py", line 161, in <module>
    run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum, args.log_interval, args.log_dir)
  File "mnist_with_tensorboard.py", line 89, in run
    writer = create_summary_writer(model, train_loader, log_dir)
  File "mnist_with_tensorboard.py", line 76, in create_summary_writer
    writer = SummaryWriter(logdir=log_dir)
TypeError: __init__() got an unexpected keyword argument 'logdir'

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->
Traceback (most recent call last):
  File "mnist_with_tensorboard.py", line 161, in <module>
    run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum, args.log_interval, args.log_dir)
  File "mnist_with_tensorboard.py", line 89, in run
    writer = create_summary_writer(model, train_loader, log_dir)
  File "mnist_with_tensorboard.py", line 76, in create_summary_writer
    writer = SummaryWriter(logdir=log_dir)
TypeError: __init__() got an unexpected keyword argument 'logdir'

<!-- A clear and concise description of what you expected to happen. -->
Just something that runs ....

## Environment
Ubuntu 18.06 LTS, dual 1080Ti, 32 Go RAM, intel i7-2600K 
>pip3.6 install torch
>pip3.6 install torchvision
>pip3.6 install tensorboard

>pip3.6 list | grep -E 'tensor'\|'torch'
pytorch-ignite            0.4.0              
tensorboard               2.2.0              
tensorboard-plugin-wit    1.6.0.post2        
torch                     1.4.0              
torchvision               0.5.0              


simple arg name mismatch for mnist tensorboard example
## 🐛 Bug description
<!-- A clear and concise description of what the bug is. -->
Example for mnist with tensorboard does not work due to wrong arg name.

<!-- Please, add steps on how to reproduce it. -->
>git clone https://github.com/pytorch/ignite
>cd examples/mnist
>python3.6 mnist_with_tensorboard.py --log_dir=/tmp/tensorboard_logs
Traceback (most recent call last):
  File "mnist_with_tensorboard.py", line 161, in <module>
    run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum, args.log_interval, args.log_dir)
  File "mnist_with_tensorboard.py", line 89, in run
    writer = create_summary_writer(model, train_loader, log_dir)
  File "mnist_with_tensorboard.py", line 76, in create_summary_writer
    writer = SummaryWriter(logdir=log_dir)
TypeError: __init__() got an unexpected keyword argument 'logdir'

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->
Traceback (most recent call last):
  File "mnist_with_tensorboard.py", line 161, in <module>
    run(args.batch_size, args.val_batch_size, args.epochs, args.lr, args.momentum, args.log_interval, args.log_dir)
  File "mnist_with_tensorboard.py", line 89, in run
    writer = create_summary_writer(model, train_loader, log_dir)
  File "mnist_with_tensorboard.py", line 76, in create_summary_writer
    writer = SummaryWriter(logdir=log_dir)
TypeError: __init__() got an unexpected keyword argument 'logdir'

<!-- A clear and concise description of what you expected to happen. -->
Just something that runs ....

## Environment
Ubuntu 18.06 LTS, dual 1080Ti, 32 Go RAM, intel i7-2600K 
>pip3.6 install torch
>pip3.6 install torchvision
>pip3.6 install tensorboard

>pip3.6 list | grep -E 'tensor'\|'torch'
pytorch-ignite            0.4.0              
tensorboard               2.2.0              
tensorboard-plugin-wit    1.6.0.post2        
torch                     1.4.0              
torchvision               0.5.0              




Discussion:
@DhDeepLIT thanks for the report !
It looks like to be related to different signatures of SummayWriter between torch internal tensorboard and tensorboardX:
- https://pytorch.org/docs/stable/tensorboard.html#torch.utils.tensorboard.writer.SummaryWriter
vs
- https://github.com/lanpa/tensorboardX/blob/master/tensorboardX/writer.py#L210


@DhDeepLIT if you would like to contribute, do not hesitate to send a PR with a fix.
Seems like there are only two places where need to change: `logdir` -> `log_dir`:
- mnist_with_tensorboard.py
- mnist_save_resume_engine.py

Thanks !

@DhDeepLIT thanks for the report !
It looks like to be related to different signatures of SummayWriter between torch internal tensorboard and tensorboardX:
- https://pytorch.org/docs/stable/tensorboard.html#torch.utils.tensorboard.writer.SummaryWriter
vs
- https://github.com/lanpa/tensorboardX/blob/master/tensorboardX/writer.py#L210


@DhDeepLIT if you would like to contribute, do not hesitate to send a PR with a fix.
Seems like there are only two places where need to change: `logdir` -> `log_dir`:
- mnist_with_tensorboard.py
- mnist_save_resume_engine.py

Thanks !


Resolving patch:
diff --git a/examples/mnist/mnist_save_resume_engine.py b/examples/mnist/mnist_save_resume_engine.py
--- a/examples/mnist/mnist_save_resume_engine.py
+++ b/examples/mnist/mnist_save_resume_engine.py
@@ -15,7 +15,14 @@
 try:
     from tensorboardX import SummaryWriter
 except ImportError:
-    raise RuntimeError("No tensorboardX package is found. Please install with the command: \npip install tensorboardX")
+    try:
+        from torch.utils.tensorboard import SummaryWriter
+    except ImportError:
+        raise RuntimeError(
+            "This module requires either tensorboardX or torch >= 1.2.0. "
+            "You may install tensorboardX with command: \n pip install tensorboardX \n"
+            "or upgrade PyTorch using your package manager of choice (pip or conda)."
+        )
 
 from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
 from ignite.metrics import Accuracy, Loss
@@ -69,7 +76,7 @@ def run(
 
     train_loader, val_loader = get_data_loaders(train_batch_size, val_batch_size)
     model = Net()
-    writer = SummaryWriter(logdir=log_dir)
+    writer = SummaryWriter(log_dir=log_dir)
     device = "cpu"
 
     if torch.cuda.is_available():
diff --git a/examples/mnist/mnist_with_tensorboard.py b/examples/mnist/mnist_with_tensorboard.py
--- a/examples/mnist/mnist_with_tensorboard.py
+++ b/examples/mnist/mnist_with_tensorboard.py
@@ -73,7 +73,7 @@ def get_data_loaders(train_batch_size, val_batch_size):
 
 
 def create_summary_writer(model, data_loader, log_dir):
-    writer = SummaryWriter(logdir=log_dir)
+    writer = SummaryWriter(log_dir=log_dir)
     data_loader_iter = iter(data_loader)
     x, y = next(data_loader_iter)
     try:


Test patch: