Skip to content

Commit

Permalink
Add 0.5 in save_images so we round to nearest int
Browse files Browse the repository at this point in the history
  • Loading branch information
ssnl committed Feb 20, 2019
1 parent f75b814 commit 73b9c82
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions torchvision/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,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 + padding, width * xmaps + padding).fill_(pad_value)
grid = tensor.new_full((3, height * ymaps + padding, width * xmaps + padding), pad_value)
k = 0
for y in irange(ymaps):
for x in irange(xmaps):
Expand All @@ -99,6 +99,7 @@ def save_image(tensor, filename, nrow=8, padding=2,
from PIL import Image
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).cpu().numpy()
# Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
ndarr = grid.mul_(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy()
im = Image.fromarray(ndarr)
im.save(filename)

0 comments on commit 73b9c82

Please sign in to comment.