Lightning-AI / pytorch-lightning

Pretrain, finetune ANY AI model of ANY size on multiple GPUs, TPUs with zero code changes.
https://lightning.ai
Apache License 2.0
28.47k stars 3.39k forks source link

Add log_image for Tensorboard logger #15733

Open gau-nernst opened 2 years ago

gau-nernst commented 2 years ago

🚀 Feature

Currently Wandb and Neptune loggers have .log_image() method. It would be great to have the same method for Tensorboard also, so that we don't need to modify the code when changing the logger.

Motivation

For quick experimentations, usually I start with Tensorboard logger first. Once the code is more stable and I want to do long-training, I switch to Wandb so that I can monitor the training progress remotely. Having the same method simplifies the user code.

Pitch

Currently Tensorboard logger can log images by directly call the .experiment.add_image() method. The new .log_image() method would simply call this method.

Alternatives

To handle different ways of logging images depending on the logger, user code either needs to (1) detect which logger is being used, and call the corresponding method to log images, or (2) override Tensorboard logger with the proposed change. Both options are not ideal.

Additional context


If you enjoy Lightning, check out our other projects! âš¡

cc @borda @awaelchli @Blaizzy

awaelchli commented 2 years ago

We tried to provide image logging and similar in a consistent way in the past, but it never worked out well. See https://github.com/Lightning-AI/lightning/issues/11837, https://github.com/Lightning-AI/lightning/issues/12183. The issue is that the APIs for logging images are so vastly different for each logger, that the user needs to change the code anyway (the input format of log_image).

gau-nernst commented 2 years ago

If unified image logging API is not possible or does not work well, and user is expected to have custom code for each logger type, then having .log_image() for only Wandb logger (I notice that .log_image() for Neptune is removed) is unfair to other loggers and provide inconsistent user experience.

Currently .log_image() for Wandb logger is a wrapper for creating wandb.Image objects and log the images. I think it is quite straightforward to implement similar functionality for Tensorboard logger.

Regarding the input format, Wandb .log_image() expects a list of image, in torch.Tensor, np.ndarray, or PIL.Image. Tensorboard logger from PyTorch link accepts torch.Tensor, np.ndarray format, so it is quite similar for image format (I suppose PIL.Image -> np.ndarray conversion is pretty trivial).

If we keep List[Image] signature, we can iterate over the list and call .add_image() from PyTorch's Tensorboard (which is identical to what Wandb logger is doing, iterating the list and convert them to wandb.Image). Perhaps adding support for PIL.Image by internally converting to np.ndarray.

There is also .add_images() from PyTorch's Tensorboard, which takes in a batched tensor and log a grid of images. This is a bit different from logging each image individually.

Last few points (and reiterate some)

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions - the Lightning Team!