From d39134935e4d4ee731dd6e23a1d9848678e1a002 Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Thu, 5 Nov 2020 20:48:24 -0500 Subject: [PATCH 1/2] make_grid should not avoid DBZ by adding eps While this doesn't matter in most cases where the images is quantized to [0,255] afterwards, it still is not a faithful de-normalization. It is a simple change to make it use clamp instead. --- torchvision/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index be373138c5f..8c2b2ea579b 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -62,7 +62,7 @@ def make_grid( def norm_ip(img, min, max): img.clamp_(min=min, max=max) - img.add_(-min).div_(max - min + 1e-5) + img.add_(-min).div_(max(max - min, 1e-5)) def norm_range(t, range): if range is not None: From 46ffbf1f772c9ef39751dc68ea48fdbb968d78f6 Mon Sep 17 00:00:00 2001 From: Tongzhou Wang Date: Fri, 6 Nov 2020 13:59:52 -0500 Subject: [PATCH 2/2] fix max/min shadowing --- torchvision/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/torchvision/utils.py b/torchvision/utils.py index 8c2b2ea579b..d40284e09a5 100644 --- a/torchvision/utils.py +++ b/torchvision/utils.py @@ -60,9 +60,9 @@ def make_grid( assert isinstance(range, tuple), \ "range has to be a tuple (min, max) if specified. min and max are numbers" - def norm_ip(img, min, max): - img.clamp_(min=min, max=max) - img.add_(-min).div_(max(max - min, 1e-5)) + def norm_ip(img, low, high): + img.clamp_(min=low, max=high) + img.sub_(low).div_(max(high - low, 1e-5)) def norm_range(t, range): if range is not None: