Spaces:
Runtime error
Runtime error
File size: 12,151 Bytes
f670afc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# Copyright (C) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# This work is made available under the Nvidia Source Code License-NC.
# To view a copy of this license, check out LICENSE.md
import warnings
from torch import nn
from torch.nn import Upsample as NearestUpsample
from imaginaire.layers import Conv2dBlock, Res2dBlock
class Generator(nn.Module):
r"""Improved UNIT generator.
Args:
gen_cfg (obj): Generator definition part of the yaml config file.
data_cfg (obj): Data definition part of the yaml config file.
"""
def __init__(self, gen_cfg, data_cfg):
super().__init__()
self.autoencoder_a = AutoEncoder(**vars(gen_cfg))
self.autoencoder_b = AutoEncoder(**vars(gen_cfg))
def forward(self, data, image_recon=True, cycle_recon=True):
r"""UNIT forward function"""
images_a = data['images_a']
images_b = data['images_b']
net_G_output = dict()
# encode input images into latent code
content_a = self.autoencoder_a.content_encoder(images_a)
content_b = self.autoencoder_b.content_encoder(images_b)
# decode (within domain)
if image_recon:
images_aa = self.autoencoder_a.decoder(content_a)
images_bb = self.autoencoder_b.decoder(content_b)
net_G_output.update(dict(images_aa=images_aa, images_bb=images_bb))
# decode (cross domain)
images_ba = self.autoencoder_a.decoder(content_b)
images_ab = self.autoencoder_b.decoder(content_a)
# cycle reconstruction
if cycle_recon:
content_ba = self.autoencoder_a.content_encoder(images_ba)
content_ab = self.autoencoder_b.content_encoder(images_ab)
images_aba = self.autoencoder_a.decoder(content_ab)
images_bab = self.autoencoder_b.decoder(content_ba)
net_G_output.update(
dict(content_ba=content_ba, content_ab=content_ab,
images_aba=images_aba, images_bab=images_bab))
# required outputs
net_G_output.update(dict(content_a=content_a, content_b=content_b,
images_ba=images_ba, images_ab=images_ab))
return net_G_output
def inference(self, data, a2b=True):
r"""UNIT inference.
Args:
data (dict): Training data at the current iteration.
- images_a (tensor): Images from domain A.
- images_b (tensor): Images from domain B.
a2b (bool): If ``True``, translates images from domain A to B,
otherwise from B to A.
"""
if a2b:
input_key = 'images_a'
content_encode = self.autoencoder_a.content_encoder
decode = self.autoencoder_b.decoder
else:
input_key = 'images_b'
content_encode = self.autoencoder_b.content_encoder
decode = self.autoencoder_a.decoder
content_images = data[input_key]
content = content_encode(content_images)
output_images = decode(content)
filename = '%s/%s' % (
data['key'][input_key]['sequence_name'][0],
data['key'][input_key]['filename'][0])
filenames = [filename]
return output_images, filenames
class AutoEncoder(nn.Module):
r"""Improved UNIT autoencoder.
Args:
num_filters (int): Base filter numbers.
max_num_filters (int): Maximum number of filters in the encoder.
num_res_blocks (int): Number of residual blocks at the end of the
content encoder.
num_downsamples_content (int): Number of times we reduce
resolution by 2x2 for the content image.
num_image_channels (int): Number of input image channels.
content_norm_type (str): Type of activation normalization in the
content encoder.
decoder_norm_type (str): Type of activation normalization in the
decoder.
weight_norm_type (str): Type of weight normalization.
output_nonlinearity (str): Type of nonlinearity before final output,
``'tanh'`` or ``'none'``.
pre_act (bool): If ``True``, uses pre-activation residual blocks.
apply_noise (bool): If ``True``, injects Gaussian noise in the decoder.
"""
def __init__(self,
num_filters=64,
max_num_filters=256,
num_res_blocks=4,
num_downsamples_content=2,
num_image_channels=3,
content_norm_type='instance',
decoder_norm_type='instance',
weight_norm_type='',
output_nonlinearity='',
pre_act=False,
apply_noise=False,
**kwargs):
super().__init__()
for key in kwargs:
if key != 'type':
warnings.warn(
"Generator argument '{}' is not used.".format(key))
self.content_encoder = ContentEncoder(num_downsamples_content,
num_res_blocks,
num_image_channels,
num_filters,
max_num_filters,
'reflect',
content_norm_type,
weight_norm_type,
'relu',
pre_act)
self.decoder = Decoder(num_downsamples_content,
num_res_blocks,
self.content_encoder.output_dim,
num_image_channels,
'reflect',
decoder_norm_type,
weight_norm_type,
'relu',
output_nonlinearity,
pre_act,
apply_noise)
def forward(self, images):
r"""Reconstruct an image.
Args:
images (Tensor): Input images.
Returns:
images_recon (Tensor): Reconstructed images.
"""
content = self.content_encoder(images)
images_recon = self.decoder(content)
return images_recon
class ContentEncoder(nn.Module):
r"""Improved UNIT encoder. The network consists of:
- input layers
- $(num_downsamples) convolutional blocks
- $(num_res_blocks) residual blocks.
- output layer.
Args:
num_downsamples (int): Number of times we reduce
resolution by 2x2.
num_res_blocks (int): Number of residual blocks at the end of the
content encoder.
num_image_channels (int): Number of input image channels.
num_filters (int): Base filter numbers.
max_num_filters (int): Maximum number of filters in the encoder.
padding_mode (string): Type of padding.
activation_norm_type (str): Type of activation normalization.
weight_norm_type (str): Type of weight normalization.
nonlinearity (str): Type of nonlinear activation function.
pre_act (bool): If ``True``, uses pre-activation residual blocks.
"""
def __init__(self,
num_downsamples,
num_res_blocks,
num_image_channels,
num_filters,
max_num_filters,
padding_mode,
activation_norm_type,
weight_norm_type,
nonlinearity,
pre_act=False):
super().__init__()
conv_params = dict(padding_mode=padding_mode,
activation_norm_type=activation_norm_type,
weight_norm_type=weight_norm_type,
nonlinearity=nonlinearity)
# Whether or not it is safe to use inplace nonlinear activation.
if not pre_act or (activation_norm_type != '' and
activation_norm_type != 'none'):
conv_params['inplace_nonlinearity'] = True
# The order of operations in residual blocks.
order = 'pre_act' if pre_act else 'CNACNA'
model = []
model += [Conv2dBlock(num_image_channels, num_filters, 7, 1, 3,
**conv_params)]
# Downsampling blocks.
for i in range(num_downsamples):
num_filters_prev = num_filters
num_filters = min(num_filters * 2, max_num_filters)
model += [Conv2dBlock(num_filters_prev, num_filters, 4, 2, 1,
**conv_params)]
# Residual blocks.
for _ in range(num_res_blocks):
model += [Res2dBlock(num_filters, num_filters,
**conv_params,
order=order)]
self.model = nn.Sequential(*model)
self.output_dim = num_filters
def forward(self, x):
r"""
Args:
x (tensor): Input image.
"""
return self.model(x)
class Decoder(nn.Module):
r"""Improved UNIT decoder. The network consists of:
- $(num_res_blocks) residual blocks.
- $(num_upsamples) residual blocks or convolutional blocks
- output layer.
Args:
num_upsamples (int): Number of times we increase resolution by 2x2.
num_res_blocks (int): Number of residual blocks.
num_filters (int): Base filter numbers.
num_image_channels (int): Number of input image channels.
padding_mode (string): Type of padding.
activation_norm_type (str): Type of activation normalization.
weight_norm_type (str): Type of weight normalization.
nonlinearity (str): Type of nonlinear activation function.
output_nonlinearity (str): Type of nonlinearity before final output,
``'tanh'`` or ``'none'``.
pre_act (bool): If ``True``, uses pre-activation residual blocks.
apply_noise (bool): If ``True``, injects Gaussian noise.
"""
def __init__(self,
num_upsamples,
num_res_blocks,
num_filters,
num_image_channels,
padding_mode,
activation_norm_type,
weight_norm_type,
nonlinearity,
output_nonlinearity,
pre_act=False,
apply_noise=False):
super().__init__()
conv_params = dict(padding_mode=padding_mode,
nonlinearity=nonlinearity,
inplace_nonlinearity=True,
apply_noise=apply_noise,
weight_norm_type=weight_norm_type,
activation_norm_type=activation_norm_type)
# The order of operations in residual blocks.
order = 'pre_act' if pre_act else 'CNACNA'
# Residual blocks.
self.decoder = nn.ModuleList()
for _ in range(num_res_blocks):
self.decoder += [Res2dBlock(num_filters, num_filters,
**conv_params,
order=order)]
# Convolutional blocks with upsampling.
for i in range(num_upsamples):
self.decoder += [NearestUpsample(scale_factor=2)]
self.decoder += [Conv2dBlock(num_filters, num_filters // 2,
5, 1, 2, **conv_params)]
num_filters //= 2
self.decoder += [Conv2dBlock(num_filters, num_image_channels, 7, 1, 3,
nonlinearity=output_nonlinearity,
padding_mode=padding_mode)]
def forward(self, x):
r"""
Args:
x (tensor): Content embedding of the content image.
"""
for block in self.decoder:
x = block(x)
return x
|