Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resize-convolution instead transposed-convolution to avoid checkerboard artifacts #64

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions models/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ def __call__(self, input, target_is_real):
# downsampling/upsampling operations.
# Code and idea originally from Justin Johnson's architecture.
# https://github.com/jcjohnson/fast-neural-style/

class Printer(nn.Module):
def __init__(self, text='', only_size=True):
super(Printer, self).__init__()
self.only_size = only_size
self.text = text
def forward(self, x):
print(self.text, end=' ')
if self.only_size:
print(x.size())
else:
print(x)
return x

class ResnetGenerator(nn.Module):
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, gpu_ids=[], padding_type='reflect'):
assert(n_blocks >= 0)
Expand All @@ -149,21 +163,40 @@ def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_d
for i in range(n_downsampling):
mult = 2**i
model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3,
stride=2, padding=1),
stride=1, padding=1),
nn.MaxPool2d(2),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Why Pooling is used for avoiding checkerboard artifacts if
" Max pooling was previously linked to high-frequency artifacts in [12].)" https://distill.pub/2016/deconv-checkerboard/

norm_layer(ngf * mult * 2),
nn.ReLU(True)]
nn.ReLU(True),
#Printer('downsample %d'%mult)
]
# model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3,
# stride=2, padding=1),
# norm_layer(ngf * mult * 2),
# nn.ReLU(True),
# Printer('downsample %d'%mult)]

mult = 2**n_downsampling
for i in range(n_blocks):
model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout)]

for i in range(n_downsampling):
mult = 2**(n_downsampling - i)
model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
kernel_size=3, stride=2,
padding=1, output_padding=1),
norm_layer(int(ngf * mult / 2)),
nn.ReLU(True)]
model += [
nn.UpsamplingBilinear2d(scale_factor=2),
# nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(ngf * mult, int(ngf * mult / 2), 3, padding=1),
norm_layer(int(ngf * mult / 2)),
nn.ReLU(True),
#Printer('upsample %d'%mult)
]

# model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
# kernel_size=3, stride=2,
# padding=1, output_padding=1),
# norm_layer(int(ngf * mult / 2)),
# nn.ReLU(True)]
#

model += [nn.ReflectionPad2d(3)]
model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
model += [nn.Tanh()]
Expand Down