File size: 8,698 Bytes
00e0125 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.batchnorm import BatchNorm2d
from torch.nn.utils import spectral_norm
class SpectralConv2d(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self._conv = spectral_norm(
nn.Conv2d(*args, **kwargs)
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._conv(input)
class SpectralConvTranspose2d(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self._conv = spectral_norm(
nn.ConvTranspose2d(*args, **kwargs)
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._conv(input)
class Noise(nn.Module):
def __init__(self):
super().__init__()
self._weight = nn.Parameter(
torch.zeros(1),
requires_grad=True,
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
batch_size, _, height, width = input.shape
noise = torch.randn(batch_size, 1, height, width, device=input.device)
return self._weight * noise + input
class InitLayer(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._layers = nn.Sequential(
SpectralConvTranspose2d(
in_channels=in_channels,
out_channels=out_channels * 2,
kernel_size=4,
stride=1,
padding=0,
bias=False,
),
nn.BatchNorm2d(num_features=out_channels * 2),
nn.GLU(dim=1),
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._layers(input)
class SLEBlock(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._layers = nn.Sequential(
nn.AdaptiveAvgPool2d(output_size=4),
SpectralConv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=4,
stride=1,
padding=0,
bias=False,
),
nn.SiLU(),
SpectralConv2d(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
padding=0,
bias=False,
),
nn.Sigmoid(),
)
def forward(self, low_dim: torch.Tensor,
high_dim: torch.Tensor) -> torch.Tensor:
return high_dim * self._layers(low_dim)
class UpsampleBlockT1(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._layers = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
SpectralConv2d(
in_channels=in_channels,
out_channels=out_channels * 2,
kernel_size=3,
stride=1,
padding='same',
bias=False,
),
nn.BatchNorm2d(num_features=out_channels * 2),
nn.GLU(dim=1),
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._layers(input)
class UpsampleBlockT2(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._layers = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
SpectralConv2d(
in_channels=in_channels,
out_channels=out_channels * 2,
kernel_size=3,
stride=1,
padding='same',
bias=False,
),
Noise(),
BatchNorm2d(num_features=out_channels * 2),
nn.GLU(dim=1),
SpectralConv2d(
in_channels=out_channels,
out_channels=out_channels * 2,
kernel_size=3,
stride=1,
padding='same',
bias=False,
),
Noise(),
nn.BatchNorm2d(num_features=out_channels * 2),
nn.GLU(dim=1),
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._layers(input)
class DownsampleBlockT1(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._layers = nn.Sequential(
SpectralConv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=4,
stride=2,
padding=1,
bias=False,
),
nn.BatchNorm2d(num_features=out_channels),
nn.LeakyReLU(negative_slope=0.2),
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._layers(input)
class DownsampleBlockT2(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._layers_1 = nn.Sequential(
SpectralConv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=4,
stride=2,
padding=1,
bias=False,
),
nn.BatchNorm2d(num_features=out_channels),
nn.LeakyReLU(negative_slope=0.2),
SpectralConv2d(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding='same',
bias=False,
),
nn.BatchNorm2d(num_features=out_channels),
nn.LeakyReLU(negative_slope=0.2),
)
self._layers_2 = nn.Sequential(
nn.AvgPool2d(
kernel_size=2,
stride=2,
),
SpectralConv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
padding=0,
bias=False,
),
nn.BatchNorm2d(num_features=out_channels),
nn.LeakyReLU(negative_slope=0.2),
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
t1 = self._layers_1(input)
t2 = self._layers_2(input)
return (t1 + t2) / 2
class Decoder(nn.Module):
def __init__(self, in_channels: int,
out_channels: int):
super().__init__()
self._channels = {
16: 128,
32: 64,
64: 64,
128: 32,
256: 16,
512: 8,
1024: 4,
}
self._layers = nn.Sequential(
nn.AdaptiveAvgPool2d(output_size=8),
UpsampleBlockT1(in_channels=in_channels, out_channels=self._channels[16]),
UpsampleBlockT1(in_channels=self._channels[16], out_channels=self._channels[32]),
UpsampleBlockT1(in_channels=self._channels[32], out_channels=self._channels[64]),
UpsampleBlockT1(in_channels=self._channels[64], out_channels=self._channels[128]),
SpectralConv2d(
in_channels=self._channels[128],
out_channels=out_channels,
kernel_size=3,
stride=1,
padding='same',
bias=False,
),
nn.Tanh(),
)
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self._layers(input)
|