From 7ff2da6029105e4a14f5e77f615bb08fc9b76d7f Mon Sep 17 00:00:00 2001 From: Dmitry Ulyanov Date: Wed, 5 Apr 2017 22:46:11 +0300 Subject: [PATCH 1/5] add pad_value argument --- torchvision/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index 8df97bfc789..b144250bddf 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -3,7 +3,7 @@ irange = range -def make_grid(tensor, nrow=8, padding=2, +def make_grid(tensor, nrow=8, padding=2, pad_value=0, normalize=False, range=None, scale_each=False): """ Given a 4D mini-batch Tensor of shape (B x C x H x W), @@ -19,6 +19,8 @@ def make_grid(tensor, nrow=8, padding=2, scale_each=True will scale each image in the batch of images separately rather than computing the (min, max) over all images. + pad_value= sets the value for the padded pixels. + [Example usage is given in this notebook](https://gist.github.com/anonymous/bf16430f7750c023141c562f3e9f2a91) """ # if list of tensors, convert to a 4D mini-batch Tensor @@ -65,7 +67,7 @@ def norm_range(t, range): xmaps = min(nrow, nmaps) ymaps = int(math.ceil(float(nmaps) / xmaps)) height, width = int(tensor.size(2) + padding), int(tensor.size(3) + padding) - grid = tensor.new(3, height * ymaps + 1 + padding // 2, width * xmaps + 1 + padding // 2).fill_(0) + grid = tensor.new(3, height * ymaps + 1 + padding // 2, width * xmaps + 1 + padding // 2).fill_(pad_value) k = 0 for y in irange(ymaps): for x in irange(xmaps): From 7fae66b6e4553f20aba54f9c8d051100e6717143 Mon Sep 17 00:00:00 2001 From: Dmitry Ulyanov Date: Wed, 5 Apr 2017 22:50:31 +0300 Subject: [PATCH 2/5] add argument to save_image --- torchvision/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index b144250bddf..a758cc33f65 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -80,7 +80,7 @@ def norm_range(t, range): return grid -def save_image(tensor, filename, nrow=8, padding=2, +def save_image(tensor, filename, nrow=8, padding=2, pad_value=0, normalize=False, range=None, scale_each=False): """ Saves a given Tensor into an image file. @@ -90,7 +90,7 @@ def save_image(tensor, filename, nrow=8, padding=2, """ from PIL import Image tensor = tensor.cpu() - grid = make_grid(tensor, nrow=nrow, padding=padding, + grid = make_grid(tensor, nrow=nrow, padding=padding, pad_value=pad_value, normalize=normalize, range=range, scale_each=scale_each) ndarr = grid.mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy() im = Image.fromarray(ndarr) From 39dcf373772e2b820f0c4287ce73abdf40860165 Mon Sep 17 00:00:00 2001 From: Dmitry Ulyanov Date: Thu, 6 Apr 2017 14:48:41 +0300 Subject: [PATCH 3/5] fix space --- torchvision/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index a758cc33f65..30f0c7bc97f 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -20,7 +20,7 @@ def make_grid(tensor, nrow=8, padding=2, pad_value=0, computing the (min, max) over all images. pad_value= sets the value for the padded pixels. - + [Example usage is given in this notebook](https://gist.github.com/anonymous/bf16430f7750c023141c562f3e9f2a91) """ # if list of tensors, convert to a 4D mini-batch Tensor From 914cb1836d1bdecc25b69829dd8d1068636625b5 Mon Sep 17 00:00:00 2001 From: Dmitry Ulyanov Date: Thu, 6 Apr 2017 22:50:58 +0300 Subject: [PATCH 4/5] requested change of order --- torchvision/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index 30f0c7bc97f..b9b15830a53 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -3,8 +3,8 @@ irange = range -def make_grid(tensor, nrow=8, padding=2, pad_value=0, - normalize=False, range=None, scale_each=False): +def make_grid(tensor, nrow=8, padding=2, + normalize=False, range=None, scale_each=False, pad_value=0): """ Given a 4D mini-batch Tensor of shape (B x C x H x W), or a list of images all of the same size, @@ -80,8 +80,8 @@ def norm_range(t, range): return grid -def save_image(tensor, filename, nrow=8, padding=2, pad_value=0, - normalize=False, range=None, scale_each=False): +def save_image(tensor, filename, nrow=8, padding=2, + normalize=False, range=None, scale_each=False, pad_value=0): """ Saves a given Tensor into an image file. If given a mini-batch tensor, will save the tensor as a grid of images by calling `make_grid`. From 36d1646034192b7e2c2f87e1c85a95a4459476df Mon Sep 17 00:00:00 2001 From: Dmitry Ulyanov Date: Fri, 7 Apr 2017 14:43:00 +0300 Subject: [PATCH 5/5] update docs --- README.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 134ea357241..5074d9464f4 100644 --- a/README.rst +++ b/README.rst @@ -388,7 +388,7 @@ For example: Utils ===== -make\_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale\_each=False) +make\_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale\_each=False, pad\_value=0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Given a 4D mini-batch Tensor of shape (B x C x H x W), @@ -404,9 +404,11 @@ normalize the image. scale_each=True will scale each image in the batch of images separately rather than computing the (min, max) over all images. +pad_value= sets the value for the padded pixels. + `Example usage is given in this notebook` -save\_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale\_each=False) +save\_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale\_each=False, pad\_value=0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Saves a given Tensor into an image file.