Skip to content

Commit 0b40cba

Browse files
committed
just always use nearest neighbor interpolation when resizing for low resolution conditioning, for #181
1 parent f141144 commit 0b40cba

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

dalle2_pytorch/dalle2_pytorch.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def resize_image_to(
146146
scale_factors = target_image_size / orig_image_size
147147
out = resize(image, scale_factors = scale_factors, **kwargs)
148148
else:
149-
out = F.interpolate(image, target_image_size, mode = 'nearest', align_corners = False)
149+
out = F.interpolate(image, target_image_size, mode = 'nearest')
150150

151151
if exists(clamp_range):
152152
out = out.clamp(*clamp_range)
@@ -1957,16 +1957,13 @@ class LowresConditioner(nn.Module):
19571957
def __init__(
19581958
self,
19591959
downsample_first = True,
1960-
downsample_mode_nearest = False,
19611960
blur_prob = 0.5,
19621961
blur_sigma = 0.6,
19631962
blur_kernel_size = 3,
19641963
input_image_range = None
19651964
):
19661965
super().__init__()
19671966
self.downsample_first = downsample_first
1968-
self.downsample_mode_nearest = downsample_mode_nearest
1969-
19701967
self.input_image_range = input_image_range
19711968

19721969
self.blur_prob = blur_prob
@@ -1983,7 +1980,7 @@ def forward(
19831980
blur_kernel_size = None
19841981
):
19851982
if self.downsample_first and exists(downsample_image_size):
1986-
cond_fmap = resize_image_to(cond_fmap, downsample_image_size, clamp_range = self.input_image_range, nearest = self.downsample_mode_nearest)
1983+
cond_fmap = resize_image_to(cond_fmap, downsample_image_size, clamp_range = self.input_image_range, nearest = True)
19871984

19881985
# blur is only applied 50% of the time
19891986
# section 3.1 in https://arxiv.org/abs/2106.15282
@@ -2010,7 +2007,7 @@ def forward(
20102007

20112008
cond_fmap = gaussian_blur2d(cond_fmap, cast_tuple(blur_kernel_size, 2), cast_tuple(blur_sigma, 2))
20122009

2013-
cond_fmap = resize_image_to(cond_fmap, target_image_size, clamp_range = self.input_image_range)
2010+
cond_fmap = resize_image_to(cond_fmap, target_image_size, clamp_range = self.input_image_range, nearest = True)
20142011
return cond_fmap
20152012

20162013
class Decoder(nn.Module):
@@ -2033,7 +2030,6 @@ def __init__(
20332030
image_sizes = None, # for cascading ddpm, image size at each stage
20342031
random_crop_sizes = None, # whether to random crop the image at that stage in the cascade (super resoluting convolutions at the end may be able to generalize on smaller crops)
20352032
lowres_downsample_first = True, # cascading ddpm - resizes to lower resolution, then to next conditional resolution + blur
2036-
lowres_downsample_mode_nearest = False, # cascading ddpm - whether to use nearest mode downsampling for lower resolution
20372033
blur_prob = 0.5, # cascading ddpm - when training, the gaussian blur is only applied 50% of the time
20382034
blur_sigma = 0.6, # cascading ddpm - blur sigma
20392035
blur_kernel_size = 3, # cascading ddpm - blur kernel size
@@ -2183,11 +2179,8 @@ def __init__(
21832179
lowres_conditions = tuple(map(lambda t: t.lowres_cond, self.unets))
21842180
assert lowres_conditions == (False, *((True,) * (len(self.unets) - 1))), 'the first unet must be unconditioned (by low resolution image), and the rest of the unets must have `lowres_cond` set to True'
21852181

2186-
self.lowres_downsample_mode_nearest = lowres_downsample_mode_nearest
2187-
21882182
self.to_lowres_cond = LowresConditioner(
21892183
downsample_first = lowres_downsample_first,
2190-
downsample_mode_nearest = lowres_downsample_mode_nearest,
21912184
blur_prob = blur_prob,
21922185
blur_sigma = blur_sigma,
21932186
blur_kernel_size = blur_kernel_size,
@@ -2510,7 +2503,7 @@ def sample(
25102503
shape = (batch_size, channel, image_size, image_size)
25112504

25122505
if unet.lowres_cond:
2513-
lowres_cond_img = resize_image_to(img, target_image_size = image_size, clamp_range = self.input_image_range, nearest = self.lowres_downsample_mode_nearest)
2506+
lowres_cond_img = resize_image_to(img, target_image_size = image_size, clamp_range = self.input_image_range, nearest = True)
25142507

25152508
is_latent_diffusion = isinstance(vae, VQGanVAE)
25162509
image_size = vae.get_encoded_fmap_size(image_size)
@@ -2580,7 +2573,7 @@ def forward(
25802573
assert not (not self.condition_on_text_encodings and exists(text_encodings)), 'decoder specified not to be conditioned on text, yet it is presented'
25812574

25822575
lowres_cond_img = self.to_lowres_cond(image, target_image_size = target_image_size, downsample_image_size = self.image_sizes[unet_index - 1]) if unet_number > 1 else None
2583-
image = resize_image_to(image, target_image_size)
2576+
image = resize_image_to(image, target_image_size, nearest = True)
25842577

25852578
if exists(random_crop_size):
25862579
aug = K.RandomCrop((random_crop_size, random_crop_size), p = 1.)

dalle2_pytorch/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.23.9'
1+
__version__ = '0.23.10'

0 commit comments

Comments
 (0)