Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
1b4455a5
Commit
1b4455a5
authored
Feb 21, 2019
by
Tongzhou Wang
Committed by
Francisco Massa
Feb 21, 2019
Browse files
Add 0.5 in save_images so we round to nearest int (#754)
parent
f75b8148
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
3 additions
and
2 deletions
+3
-2
torchvision/utils.py
torchvision/utils.py
+3
-2
No files found.
torchvision/utils.py
View file @
1b4455a5
...
...
@@ -74,7 +74,7 @@ def make_grid(tensor, nrow=8, padding=2,
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
):
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment