From 1a29d6b4b7331e70b50e3118ef53b3a40da4ed45 Mon Sep 17 00:00:00 2001 From: Christoph Clement Date: Mon, 4 Jan 2021 13:55:39 +0100 Subject: [PATCH 1/6] Update args --- pl_bolts/callbacks/vision/image_generation.py | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/pl_bolts/callbacks/vision/image_generation.py b/pl_bolts/callbacks/vision/image_generation.py index 5572cbb363..53ac2b9e5a 100644 --- a/pl_bolts/callbacks/vision/image_generation.py +++ b/pl_bolts/callbacks/vision/image_generation.py @@ -1,3 +1,5 @@ +from typing import Optional, Tuple + import torch from pytorch_lightning import Callback, LightningModule, Trainer @@ -6,7 +8,7 @@ try: import torchvision except ModuleNotFoundError: - warn_missing_pkg('torchvision') # pragma: no-cover + warn_missing_pkg("torchvision") # pragma: no-cover class TensorboardGenerativeModelImageSampler(Callback): @@ -30,9 +32,24 @@ class TensorboardGenerativeModelImageSampler(Callback): trainer = Trainer(callbacks=[TensorboardGenerativeModelImageSampler()]) """ - def __init__(self, num_samples: int = 3) -> None: + def __init__( + self, + num_samples: int = 3, + nrow: int = 8, + padding: int = 2, + normalize: bool = False, + range: Optional[Tuple[int, int]] = None, + scale_each: bool = False, + pad_value: int = 0, + ) -> None: super().__init__() - self.num_samples: int = num_samples + self.num_samples = num_samples + self.nrow = nrow + self.padding = padding + self.normalize = normalize + self.range = range + self.scale_each = scale_each + self.pad_value = pad_value def on_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None: dim = (self.num_samples, pl_module.hparams.latent_dim) # type: ignore[union-attr] @@ -48,6 +65,16 @@ def on_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None: img_dim = pl_module.img_dim images = images.view(self.num_samples, *img_dim) - grid = torchvision.utils.make_grid(images) - str_title = f'{pl_module.__class__.__name__}_images' - trainer.logger.experiment.add_image(str_title, grid, global_step=trainer.global_step) + grid = torchvision.utils.make_grid( + tensor=images, + nrow=self.nrow, + padding=self.padding, + normalize=self.normalize, + range=self.range, + scale_each=self.scale_each, + pad_value=self.pad_value, + ) + str_title = f"{pl_module.__class__.__name__}_images" + trainer.logger.experiment.add_image( + str_title, grid, global_step=trainer.global_step + ) From 8806a6b8e243d33bfe1673515f3c111eaa0e7e3b Mon Sep 17 00:00:00 2001 From: Christoph Clement Date: Mon, 4 Jan 2021 13:58:51 +0100 Subject: [PATCH 2/6] Add docs --- pl_bolts/callbacks/vision/image_generation.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pl_bolts/callbacks/vision/image_generation.py b/pl_bolts/callbacks/vision/image_generation.py index 53ac2b9e5a..a8f8fbddbf 100644 --- a/pl_bolts/callbacks/vision/image_generation.py +++ b/pl_bolts/callbacks/vision/image_generation.py @@ -42,6 +42,20 @@ def __init__( scale_each: bool = False, pad_value: int = 0, ) -> None: + """ + Args: + nrow: Number of images displayed in each row of the grid. + The final grid size is ``(B / nrow, nrow)``. Default: ``8``. + padding: amount of padding. Default: ``2``. + normalize: If True, shift the image to the range (0, 1), + by the min and max values specified by :attr:`range`. Default: ``False``. + range: tuple (min, max) where min and max are numbers, + then these numbers are used to normalize the image. By default, min and max + are computed from the tensor. + scale_each: If ``True``, scale each image in the batch of + images separately rather than the (min, max) over all images. Default: ``False``. + pad_value: Value for the padded pixels. Default: ``0``. + """ super().__init__() self.num_samples = num_samples self.nrow = nrow From 2e4e5b519534b3e58b7a59a7513e2326fd852c74 Mon Sep 17 00:00:00 2001 From: Christoph Clement Date: Mon, 4 Jan 2021 17:55:51 +0100 Subject: [PATCH 3/6] Fix codefactor and update changelog --- CHANGELOG.md | 2 ++ pl_bolts/callbacks/vision/image_generation.py | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bdc457628..338e947f90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Refactored the rest of `pl_bolts.models.self_supervised` ([#481](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/481), [#479](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/479) +- Added all [`torchvision.utils.make_grid`(https://pytorch.org/docs/stable/torchvision/utils.html#torchvision.utils.make_grid)] kwargs to `TensorboardGenerativeModelImageSampler` ([#494(https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/494)]) + ### Fixed - Fixed duplicate warnings when optional packages are unavailable ([#341](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/341)) diff --git a/pl_bolts/callbacks/vision/image_generation.py b/pl_bolts/callbacks/vision/image_generation.py index a8f8fbddbf..870907524a 100644 --- a/pl_bolts/callbacks/vision/image_generation.py +++ b/pl_bolts/callbacks/vision/image_generation.py @@ -38,7 +38,7 @@ def __init__( nrow: int = 8, padding: int = 2, normalize: bool = False, - range: Optional[Tuple[int, int]] = None, + norm_range: Optional[Tuple[int, int]] = None, scale_each: bool = False, pad_value: int = 0, ) -> None: @@ -49,7 +49,7 @@ def __init__( padding: amount of padding. Default: ``2``. normalize: If True, shift the image to the range (0, 1), by the min and max values specified by :attr:`range`. Default: ``False``. - range: tuple (min, max) where min and max are numbers, + norm_range: tuple (min, max) where min and max are numbers, then these numbers are used to normalize the image. By default, min and max are computed from the tensor. scale_each: If ``True``, scale each image in the batch of @@ -61,7 +61,7 @@ def __init__( self.nrow = nrow self.padding = padding self.normalize = normalize - self.range = range + self.norm_range = norm_range self.scale_each = scale_each self.pad_value = pad_value @@ -84,7 +84,7 @@ def on_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None: nrow=self.nrow, padding=self.padding, normalize=self.normalize, - range=self.range, + range=self.norm_range, scale_each=self.scale_each, pad_value=self.pad_value, ) From 555e050db6138cf62ff43b84b99805330fefd00b Mon Sep 17 00:00:00 2001 From: Christoph Clement Date: Tue, 5 Jan 2021 10:10:12 +0100 Subject: [PATCH 4/6] Update docs --- pl_bolts/callbacks/vision/image_generation.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pl_bolts/callbacks/vision/image_generation.py b/pl_bolts/callbacks/vision/image_generation.py index 870907524a..0859aa42e2 100644 --- a/pl_bolts/callbacks/vision/image_generation.py +++ b/pl_bolts/callbacks/vision/image_generation.py @@ -44,12 +44,13 @@ def __init__( ) -> None: """ Args: + num_samples: Number of images displayed in the grid. Default: ``3``. nrow: Number of images displayed in each row of the grid. The final grid size is ``(B / nrow, nrow)``. Default: ``8``. - padding: amount of padding. Default: ``2``. - normalize: If True, shift the image to the range (0, 1), + padding: Amount of padding. Default: ``2``. + normalize: If ``True``, shift the image to the range (0, 1), by the min and max values specified by :attr:`range`. Default: ``False``. - norm_range: tuple (min, max) where min and max are numbers, + norm_range: Tuple (min, max) where min and max are numbers, then these numbers are used to normalize the image. By default, min and max are computed from the tensor. scale_each: If ``True``, scale each image in the batch of From d99f926336e6177c3edb0090a228fdcd9a619a7e Mon Sep 17 00:00:00 2001 From: Christoph Clement Date: Sun, 17 Jan 2021 14:07:42 +0100 Subject: [PATCH 5/6] Apply yapf --- pl_bolts/callbacks/vision/image_generation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pl_bolts/callbacks/vision/image_generation.py b/pl_bolts/callbacks/vision/image_generation.py index 0859aa42e2..83ee748d05 100644 --- a/pl_bolts/callbacks/vision/image_generation.py +++ b/pl_bolts/callbacks/vision/image_generation.py @@ -90,6 +90,4 @@ def on_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None: pad_value=self.pad_value, ) str_title = f"{pl_module.__class__.__name__}_images" - trainer.logger.experiment.add_image( - str_title, grid, global_step=trainer.global_step - ) + trainer.logger.experiment.add_image(str_title, grid, global_step=trainer.global_step) From 49a0cc8c041e7814ec67878f2b2ee094ba083244 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Mon, 18 Jan 2021 09:54:32 +0100 Subject: [PATCH 6/6] chlog --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 338e947f90..64adeb2add 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,8 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Refactored `pl_bolts.callbacks` ([#477](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/477)) - Refactored the rest of `pl_bolts.models.self_supervised` ([#481](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/481), [#479](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/479) - -- Added all [`torchvision.utils.make_grid`(https://pytorch.org/docs/stable/torchvision/utils.html#torchvision.utils.make_grid)] kwargs to `TensorboardGenerativeModelImageSampler` ([#494(https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/494)]) +- Update [`torchvision.utils.make_grid`(https://pytorch.org/docs/stable/torchvision/utils.html#torchvision.utils.make_grid)] kwargs to `TensorboardGenerativeModelImageSampler` ([#494](https://github.com/PyTorchLightning/pytorch-lightning-bolts/pull/494)) ### Fixed