question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`log_graph = True` for tensorboard logger doesn't show model graph.

See original GitHub issue

Describe the bug I wanted to show the computational graph of the patchcore model and for that I enable project.logger: tensorboard and set log_graph = True

logger = AnomalibTensorBoardLogger(
            name="Tensorboard Logs",
            save_dir=os.path.join(config.project.path, "logs"),
            log_graph=True
        )

Now, when I finished training, I run the tensorboard and under the GRAPHS tag, it showed nothing.

image

To Reproduce Steps to reproduce the behavior:

  1. Go to the patchcore config file and set `logger: tensorboard’
  2. Next, working/anomaly_detection_engine/anomalib/anomalib/utils/loggers/__init__.py and set log_graph=True
  3. Run the anomalib with patchcore config
  4. After training, run the event log in tensorboard.

Expected behavior

Screenshots

  • (Mention above)

Hardware and Software Configuration

  • OS: [Ubuntu, OD] Ubuntu
  • PyTorch-Lighting: ‘1.5.9’
  • NVIDIA Driver Version [470.57.02]
  • CUDA Version [e.g. 11.4] 10.2
  • CUDNN Version [e.g. v11.4.120] 7605
  • OpenVINO Version [Optional e.g. v2021.4.2]

Additional context

  • I also found that the add_image function of the tensorboard callback, doesn’t write any image on the tensorboard either.
  • Is there any way, I can write the Histogram in tensorboard in pytorch-lighting. Histograms are made for weights and bias matrices in the network. They tell us about the distribution of weights and biases among themselves. Is there any convenient way to add it to anomalib’ models?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ashwinvaidya17commented, Apr 25, 2022

Edit: I have moved the for loop after trainer.fit so that it works with Patchcore @innat I am working on updating the documentation but for now you can use the following snippet in train.py to log model graph.

for logger in loggers:
    if isinstance(logger, AnomalibWandbLogger):
        # NOTE: log graph gets populated only after one backward pass. This won't work for models which do not
        # require training such as Padim
        logger.watch(model, log_graph=True, log="all")
    elif isinstance(logger, AnomalibTensorBoardLogger):
        logger._log_graph = True
        logger.log_graph(model, input_array=torch.ones((1, 3, 256,256)))

So your train method should look like this

def train():
    """Train an anomaly classification or segmentation model based on a provided configuration file."""
    args = get_args()
    config = get_configurable_parameters(model_name=args.model, config_path=args.config)

    if config.project.seed != 0:
        seed_everything(config.project.seed)

    datamodule = get_datamodule(config)
    model = get_model(config)
    loggers = get_logger(config)

    callbacks = get_callbacks(config)

    trainer = Trainer(**config.trainer, logger=loggers, callbacks=callbacks)
    trainer.fit(model=model, datamodule=datamodule)

    for logger in loggers:
        if isinstance(logger, AnomalibWandbLogger):
            # NOTE: log graph gets populated only after one backward pass. This won't work for models which do not
            # require training such as Padim
            logger.watch(model, log_graph=True, log="all")
        elif isinstance(logger, AnomalibTensorBoardLogger):
            logger._log_graph = True
            logger.log_graph(model, input_array=torch.ones((1, 3, 256,256)))

    # load best model from checkpoint before evaluating
    load_model_callback = LoadModelCallback(weights_path=trainer.checkpoint_callback.best_model_path)
    trainer.callbacks.insert(0, load_model_callback)

    trainer.test(model=model, datamodule=datamodule)

And here is the output tensorboard_graph

Not sure if this is what you wanted. Also, to access other methods provided by tensorboard you can access logger.experiment object. But I’d be more inclined towards using this https://pytorch-lightning.readthedocs.io/en/stable/api/pytorch_lightning.loggers.tensorboard.html rather than accessing experiment object. I’ll try to make this more clear in the PR.

1reaction
innatcommented, Apr 25, 2022

@ashwinvaidya17 Thanks a lot, it’s really helpful. I’m currently out of my workplace, will check soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tensorboard log_graph does not seem to do anything #4885
Bug While exploring Tensorboard's logging features I experimented with ... is displayed and Tensorboard does not recognise any logged graph.
Read more >
tensorboard — PyTorch Lightning 1.8.6 documentation
Record hyperparameters. TensorBoard logs with and without saved hyperparameters are incompatible, the hyperparameters are then not displayed in the TensorBoard.
Read more >
TensorBoard Scalars: Logging training metrics in Keras
As training progresses, the Keras model will start logging data. TensorBoard will periodically refresh and show you your scalar metrics.
Read more >
python - How to log model graph to tensorboard when using ...
The best way to do this (TF 2.3.0, TB 2.3.0) is to use tf.summary and pass the model through a wrapper function with...
Read more >
Deep Dive Into TensorBoard: Tutorial With Examples
This callback is responsible for logging events such as Activation Histograms, Metrics Summary Plots, Profiling and Training Graph Visualizations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found