Skip to content

Commit 3549aef

Browse files
DmitryUlyanovfmassa
authored andcommitted
Add pad_value argument to make_grid and save_image (#142)
* add pad_value argument * add argument to save_image * fix space * requested change of order * update docs
1 parent f6d49d8 commit 3549aef

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

README.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ For example:
390390
Utils
391391
=====
392392

393-
make\_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale\_each=False)
393+
make\_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale\_each=False, pad\_value=0)
394394
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
395395

396396
Given a 4D mini-batch Tensor of shape (B x C x H x W),
@@ -406,9 +406,11 @@ normalize the image.
406406
scale_each=True will scale each image in the batch of images separately rather than
407407
computing the (min, max) over all images.
408408

409+
pad_value=<float> sets the value for the padded pixels.
410+
409411
`Example usage is given in this notebook` <https://gist.github.com/anonymous/bf16430f7750c023141c562f3e9f2a91>
410412

411-
save\_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale\_each=False)
413+
save\_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale\_each=False, pad\_value=0)
412414
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
413415

414416
Saves a given Tensor into an image file.

torchvision/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def make_grid(tensor, nrow=8, padding=2,
7-
normalize=False, range=None, scale_each=False):
7+
normalize=False, range=None, scale_each=False, pad_value=0):
88
"""
99
Given a 4D mini-batch Tensor of shape (B x C x H x W),
1010
or a list of images all of the same size,
@@ -19,6 +19,8 @@ def make_grid(tensor, nrow=8, padding=2,
1919
scale_each=True will scale each image in the batch of images separately rather than
2020
computing the (min, max) over all images.
2121
22+
pad_value=<float> sets the value for the padded pixels.
23+
2224
[Example usage is given in this notebook](https://gist.github.com/anonymous/bf16430f7750c023141c562f3e9f2a91)
2325
"""
2426
# if list of tensors, convert to a 4D mini-batch Tensor
@@ -65,7 +67,7 @@ def norm_range(t, range):
6567
xmaps = min(nrow, nmaps)
6668
ymaps = int(math.ceil(float(nmaps) / xmaps))
6769
height, width = int(tensor.size(2) + padding), int(tensor.size(3) + padding)
68-
grid = tensor.new(3, height * ymaps + 1 + padding // 2, width * xmaps + 1 + padding // 2).fill_(0)
70+
grid = tensor.new(3, height * ymaps + 1 + padding // 2, width * xmaps + 1 + padding // 2).fill_(pad_value)
6971
k = 0
7072
for y in irange(ymaps):
7173
for x in irange(xmaps):
@@ -79,7 +81,7 @@ def norm_range(t, range):
7981

8082

8183
def save_image(tensor, filename, nrow=8, padding=2,
82-
normalize=False, range=None, scale_each=False):
84+
normalize=False, range=None, scale_each=False, pad_value=0):
8385
"""
8486
Saves a given Tensor into an image file.
8587
If given a mini-batch tensor, will save the tensor as a grid of images by calling `make_grid`.
@@ -88,7 +90,7 @@ def save_image(tensor, filename, nrow=8, padding=2,
8890
"""
8991
from PIL import Image
9092
tensor = tensor.cpu()
91-
grid = make_grid(tensor, nrow=nrow, padding=padding,
93+
grid = make_grid(tensor, nrow=nrow, padding=padding, pad_value=pad_value,
9294
normalize=normalize, range=range, scale_each=scale_each)
9395
ndarr = grid.mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()
9496
im = Image.fromarray(ndarr)

0 commit comments

Comments
 (0)