File size: 19,529 Bytes
c3bcb92 |
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
import warnings
import numpy as np
import torch
import torch.nn as nn
class NoOp(nn.Module):
def __init__(self, *args, **kwargs):
"""NoOp Pytorch Module.
Forwards the given input as is.
"""
super(NoOp, self).__init__()
def forward(self, x, *args, **kwargs):
return x
class ConvModule(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
conv_op=nn.Conv2d,
conv_params=None,
normalization_op=None,
normalization_params=None,
activation_op=nn.LeakyReLU,
activation_params=None,
):
"""Basic Conv Pytorch Conv Module
Has can have a Conv Op, a Normlization Op and a Non Linearity:
x = conv(x)
x = some_norm(x)
x = nonlin(x)
Args:
in_channels ([int]): [Number on input channels/ feature maps]
out_channels ([int]): [Number of ouput channels/ feature maps]
conv_op ([torch.nn.Module], optional): [Conv operation]. Defaults to nn.Conv2d.
conv_params ([dict], optional): [Init parameters for the conv operation]. Defaults to None.
normalization_op ([torch.nn.Module], optional): [Normalization Operation (e.g. BatchNorm, InstanceNorm,...)]. Defaults to None.
normalization_params ([dict], optional): [Init parameters for the normalization operation]. Defaults to None.
activation_op ([torch.nn.Module], optional): [Actiovation Operation/ Non-linearity (e.g. ReLU, Sigmoid,...)]. Defaults to nn.LeakyReLU.
activation_params ([dict], optional): [Init parameters for the activation operation]. Defaults to None.
"""
super(ConvModule, self).__init__()
self.conv_params = conv_params
if self.conv_params is None:
self.conv_params = {}
self.activation_params = activation_params
if self.activation_params is None:
self.activation_params = {}
self.normalization_params = normalization_params
if self.normalization_params is None:
self.normalization_params = {}
self.conv = None
if conv_op is not None and not isinstance(conv_op, str):
self.conv = conv_op(in_channels, out_channels, **self.conv_params)
self.normalization = None
if normalization_op is not None and not isinstance(normalization_op, str):
self.normalization = normalization_op(out_channels, **self.normalization_params)
self.activation = None
if activation_op is not None and not isinstance(activation_op, str):
self.activation = activation_op(**self.activation_params)
def forward(self, input, conv_add_input=None, normalization_add_input=None, activation_add_input=None):
x = input
if self.conv is not None:
if conv_add_input is None:
x = self.conv(x)
else:
x = self.conv(x, **conv_add_input)
if self.normalization is not None:
if normalization_add_input is None:
x = self.normalization(x)
else:
x = self.normalization(x, **normalization_add_input)
if self.activation is not None:
if activation_add_input is None:
x = self.activation(x)
else:
x = self.activation(x, **activation_add_input)
# nn.functional.dropout(x, p=0.95, training=True)
return x
class ConvBlock(nn.Module):
def __init__(
self,
n_convs: int,
n_featmaps: int,
conv_op=nn.Conv2d,
conv_params=None,
normalization_op=nn.BatchNorm2d,
normalization_params=None,
activation_op=nn.LeakyReLU,
activation_params=None,
):
"""Basic Conv block with repeated conv, build up from repeated @ConvModules (with same/fixed feature map size)
Args:
n_convs ([type]): [Number of convolutions]
n_featmaps ([type]): [Feature map size of the conv]
conv_op ([torch.nn.Module], optional): [Convulioton operation -> see ConvModule ]. Defaults to nn.Conv2d.
conv_params ([dict], optional): [Init parameters for the conv operation]. Defaults to None.
normalization_op ([torch.nn.Module], optional): [Normalization Operation (e.g. BatchNorm, InstanceNorm,...) -> see ConvModule]. Defaults to nn.BatchNorm2d.
normalization_params ([dict], optional): [Init parameters for the normalization operation]. Defaults to None.
activation_op ([torch.nn.Module], optional): [Actiovation Operation/ Non-linearity (e.g. ReLU, Sigmoid,...) -> see ConvModule]. Defaults to nn.LeakyReLU.
activation_params ([dict], optional): [Init parameters for the activation operation]. Defaults to None.
"""
super(ConvBlock, self).__init__()
self.n_featmaps = n_featmaps
self.n_convs = n_convs
self.conv_params = conv_params
if self.conv_params is None:
self.conv_params = {}
self.conv_list = nn.ModuleList()
for i in range(self.n_convs):
conv_layer = ConvModule(
n_featmaps,
n_featmaps,
conv_op=conv_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
)
self.conv_list.append(conv_layer)
def forward(self, input, **frwd_params):
x = input
for conv_layer in self.conv_list:
x = conv_layer(x)
return x
class ResBlock(nn.Module):
def __init__(
self,
n_convs,
n_featmaps,
conv_op=nn.Conv2d,
conv_params=None,
normalization_op=nn.BatchNorm2d,
normalization_params=None,
activation_op=nn.LeakyReLU,
activation_params=None,
):
"""Basic Conv block with repeated conv, build up from repeated @ConvModules (with same/fixed feature map size) and a skip/ residual connection:
x = input
x = conv_block(x)
out = x + input
Args:
n_convs ([type]): [Number of convolutions in the conv block]
n_featmaps ([type]): [Feature map size of the conv block]
conv_op ([torch.nn.Module], optional): [Convulioton operation -> see ConvModule ]. Defaults to nn.Conv2d.
conv_params ([dict], optional): [Init parameters for the conv operation]. Defaults to None.
normalization_op ([torch.nn.Module], optional): [Normalization Operation (e.g. BatchNorm, InstanceNorm,...) -> see ConvModule]. Defaults to nn.BatchNorm2d.
normalization_params ([dict], optional): [Init parameters for the normalization operation]. Defaults to None.
activation_op ([torch.nn.Module], optional): [Actiovation Operation/ Non-linearity (e.g. ReLU, Sigmoid,...) -> see ConvModule]. Defaults to nn.LeakyReLU.
activation_params ([dict], optional): [Init parameters for the activation operation]. Defaults to None.
"""
super(ResBlock, self).__init__()
self.n_featmaps = n_featmaps
self.n_convs = n_convs
self.conv_params = conv_params
if self.conv_params is None:
self.conv_params = {}
self.conv_block = ConvBlock(
n_featmaps,
n_convs,
conv_op=conv_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
)
def forward(self, input, **frwd_params):
x = input
x = self.conv_block(x)
out = x + input
return out
# Basic Generator
class BasicGenerator(nn.Module):
def __init__(
self,
input_size,
z_dim=256,
fmap_sizes=(256, 128, 64),
upsample_op=nn.ConvTranspose2d,
conv_params=None,
normalization_op=NoOp,
normalization_params=None,
activation_op=nn.LeakyReLU,
activation_params=None,
block_op=NoOp,
block_params=None,
to_1x1=True,
):
"""Basic configureable Generator/ Decoder.
Allows for mutilple "feature-map" levels defined by the feature map size, where for each feature map size a conv operation + optional conv block is used.
Args:
input_size ((int, int, int): Size of the input in format CxHxW):
z_dim (int, optional): [description]. Dimension of the latent / Input dimension (C channel-dim).
fmap_sizes (tuple, optional): [Defines the Upsampling-Levels of the generator, list/ tuple of ints, where each
int defines the number of feature maps in the layer]. Defaults to (256, 128, 64).
upsample_op ([torch.nn.Module], optional): [Upsampling operation used, to upsample to a new level/ featuremap size]. Defaults to nn.ConvTranspose2d.
conv_params ([dict], optional): [Init parameters for the conv operation]. Defaults to dict(kernel_size=3, stride=2, padding=1, bias=False).
normalization_op ([torch.nn.Module], optional): [Normalization Operation (e.g. BatchNorm, InstanceNorm,...) -> see ConvModule]. Defaults to nn.BatchNorm2d.
normalization_params ([dict], optional): [Init parameters for the normalization operation]. Defaults to None.
activation_op ([torch.nn.Module], optional): [Actiovation Operation/ Non-linearity (e.g. ReLU, Sigmoid,...) -> see ConvModule]. Defaults to nn.LeakyReLU.
activation_params ([dict], optional): [Init parameters for the activation operation]. Defaults to None.
block_op ([torch.nn.Module], optional): [Block operation used for each feature map size after each upsample op of e.g. ConvBlock/ ResidualBlock]. Defaults to NoOp.
block_params ([dict], optional): [Init parameters for the block operation]. Defaults to None.
to_1x1 (bool, optional): [If Latent dimesion is a z_dim x 1 x 1 vector (True) or if allows spatial resolution not to be 1x1 (z_dim x H x W) (False) ]. Defaults to True.
"""
super(BasicGenerator, self).__init__()
if conv_params is None:
conv_params = dict(kernel_size=4, stride=2, padding=1, bias=False)
if block_op is None:
block_op = NoOp
if block_params is None:
block_params = {}
n_channels = input_size[0]
input_size_ = np.array(input_size[1:])
if not isinstance(fmap_sizes, list) and not isinstance(fmap_sizes, tuple):
raise AttributeError("fmap_sizes has to be either a list or tuple or an int")
elif len(fmap_sizes) < 2:
raise AttributeError("fmap_sizes has to contain at least three elements")
else:
h_size_bot = fmap_sizes[0]
# We need to know how many layers we will use at the beginning
input_size_new = input_size_ // (2 ** len(fmap_sizes))
if np.min(input_size_new) < 2 and z_dim is not None:
raise AttributeError("fmap_sizes to long, one image dimension has already perished")
### Start block
start_block = []
if not to_1x1:
kernel_size_start = [min(conv_params["kernel_size"], i) for i in input_size_new]
else:
kernel_size_start = input_size_new.tolist()
if z_dim is not None:
self.start = ConvModule(
z_dim,
h_size_bot,
conv_op=upsample_op,
conv_params=dict(kernel_size=kernel_size_start, stride=1, padding=0, bias=False),
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
)
input_size_new = input_size_new * 2
else:
self.start = NoOp()
### Middle block (Done until we reach ? x input_size/2 x input_size/2)
self.middle_blocks = nn.ModuleList()
for h_size_top in fmap_sizes[1:]:
self.middle_blocks.append(block_op(h_size_bot, **block_params))
self.middle_blocks.append(
ConvModule(
h_size_bot,
h_size_top,
conv_op=upsample_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params={},
activation_op=activation_op,
activation_params=activation_params,
)
)
h_size_bot = h_size_top
input_size_new = input_size_new * 2
### End block
self.end = ConvModule(
h_size_bot,
n_channels,
conv_op=upsample_op,
conv_params=conv_params,
normalization_op=None,
activation_op=None,
)
def forward(self, inpt, **kwargs):
output = self.start(inpt, **kwargs)
for middle in self.middle_blocks:
output = middle(output, **kwargs)
output = self.end(output, **kwargs)
return output
# Basic Encoder
class BasicEncoder(nn.Module):
def __init__(
self,
input_size,
z_dim=256,
fmap_sizes=(64, 128, 256),
conv_op=nn.Conv2d,
conv_params=None,
normalization_op=NoOp,
normalization_params=None,
activation_op=nn.LeakyReLU,
activation_params=None,
block_op=NoOp,
block_params=None,
to_1x1=True,
):
"""Basic configureable Encoder.
Allows for mutilple "feature-map" levels defined by the feature map size, where for each feature map size a conv operation + optional conv block is used.
Args:
z_dim (int, optional): [description]. Dimension of the latent / Input dimension (C channel-dim).
fmap_sizes (tuple, optional): [Defines the Upsampling-Levels of the generator, list/ tuple of ints, where each
int defines the number of feature maps in the layer]. Defaults to (64, 128, 256).
conv_op ([torch.nn.Module], optional): [Convolutioon operation used to downsample to a new level/ featuremap size]. Defaults to nn.Conv2d.
conv_params ([dict], optional): [Init parameters for the conv operation]. Defaults to dict(kernel_size=3, stride=2, padding=1, bias=False).
normalization_op ([torch.nn.Module], optional): [Normalization Operation (e.g. BatchNorm, InstanceNorm,...) -> see ConvModule]. Defaults to nn.BatchNorm2d.
normalization_params ([dict], optional): [Init parameters for the normalization operation]. Defaults to None.
activation_op ([torch.nn.Module], optional): [Actiovation Operation/ Non-linearity (e.g. ReLU, Sigmoid,...) -> see ConvModule]. Defaults to nn.LeakyReLU.
activation_params ([dict], optional): [Init parameters for the activation operation]. Defaults to None.
block_op ([torch.nn.Module], optional): [Block operation used for each feature map size after each upsample op of e.g. ConvBlock/ ResidualBlock]. Defaults to NoOp.
block_params ([dict], optional): [Init parameters for the block operation]. Defaults to None.
to_1x1 (bool, optional): [If True, then the last conv layer goes to a latent dimesion is a z_dim x 1 x 1 vector (similar to fully connected) or if False allows spatial resolution not to be 1x1 (z_dim x H x W, uses the in the conv_params given conv-kernel-size) ]. Defaults to True.
"""
super(BasicEncoder, self).__init__()
if conv_params is None:
conv_params = dict(kernel_size=3, stride=2, padding=1, bias=False)
if block_op is None:
block_op = NoOp
if block_params is None:
block_params = {}
n_channels = input_size[0]
input_size_new = np.array(input_size[1:])
if not isinstance(fmap_sizes, list) and not isinstance(fmap_sizes, tuple):
raise AttributeError("fmap_sizes has to be either a list or tuple or an int")
# elif len(fmap_sizes) < 2:
# raise AttributeError("fmap_sizes has to contain at least three elements")
else:
h_size_bot = fmap_sizes[0]
### Start block
self.start = ConvModule(
n_channels,
h_size_bot,
conv_op=conv_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params={},
activation_op=activation_op,
activation_params=activation_params,
)
input_size_new = input_size_new // 2
### Middle block (Done until we reach ? x 4 x 4)
self.middle_blocks = nn.ModuleList()
for h_size_top in fmap_sizes[1:]:
self.middle_blocks.append(block_op(h_size_bot, **block_params))
self.middle_blocks.append(
ConvModule(
h_size_bot,
h_size_top,
conv_op=conv_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params={},
activation_op=activation_op,
activation_params=activation_params,
)
)
h_size_bot = h_size_top
input_size_new = input_size_new // 2
if np.min(input_size_new) < 2 and z_dim is not None:
raise ("fmap_sizes to long, one image dimension has already perished")
### End block
if not to_1x1:
kernel_size_end = [min(conv_params["kernel_size"], i) for i in input_size_new]
else:
kernel_size_end = input_size_new.tolist()
if z_dim is not None:
self.end = ConvModule(
h_size_bot,
z_dim,
conv_op=conv_op,
conv_params=dict(kernel_size=kernel_size_end, stride=1, padding=0, bias=False),
normalization_op=None,
activation_op=None,
)
if to_1x1:
self.output_size = (z_dim, 1, 1)
else:
self.output_size = (z_dim, *[i - (j - 1) for i, j in zip(input_size_new, kernel_size_end)])
else:
self.end = NoOp()
self.output_size = input_size_new
def forward(self, inpt, **kwargs):
output = self.start(inpt, **kwargs)
for middle in self.middle_blocks:
output = middle(output, **kwargs)
output = self.end(output, **kwargs)
return output
|