Spaces:
Paused
Paused
Upload 7 files
Browse files- basicsr/archs/__init__.py +25 -0
- basicsr/archs/arcface_arch.py +245 -0
- basicsr/archs/arch_util.py +318 -0
- basicsr/archs/codeformer_arch.py +280 -0
- basicsr/archs/rrdbnet_arch.py +119 -0
- basicsr/archs/vgg_arch.py +161 -0
- basicsr/archs/vqgan_arch.py +434 -0
basicsr/archs/__init__.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
from copy import deepcopy
|
| 3 |
+
from os import path as osp
|
| 4 |
+
|
| 5 |
+
from basicsr.utils import get_root_logger, scandir
|
| 6 |
+
from basicsr.utils.registry import ARCH_REGISTRY
|
| 7 |
+
|
| 8 |
+
__all__ = ['build_network']
|
| 9 |
+
|
| 10 |
+
# automatically scan and import arch modules for registry
|
| 11 |
+
# scan all the files under the 'archs' folder and collect files ending with
|
| 12 |
+
# '_arch.py'
|
| 13 |
+
arch_folder = osp.dirname(osp.abspath(__file__))
|
| 14 |
+
arch_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(arch_folder) if v.endswith('_arch.py')]
|
| 15 |
+
# import all the arch modules
|
| 16 |
+
_arch_modules = [importlib.import_module(f'basicsr.archs.{file_name}') for file_name in arch_filenames]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def build_network(opt):
|
| 20 |
+
opt = deepcopy(opt)
|
| 21 |
+
network_type = opt.pop('type')
|
| 22 |
+
net = ARCH_REGISTRY.get(network_type)(**opt)
|
| 23 |
+
logger = get_root_logger()
|
| 24 |
+
logger.info(f'Network [{net.__class__.__name__}] is created.')
|
| 25 |
+
return net
|
basicsr/archs/arcface_arch.py
ADDED
|
@@ -0,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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch.nn as nn
|
| 2 |
+
from basicsr.utils.registry import ARCH_REGISTRY
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def conv3x3(inplanes, outplanes, stride=1):
|
| 6 |
+
"""A simple wrapper for 3x3 convolution with padding.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
inplanes (int): Channel number of inputs.
|
| 10 |
+
outplanes (int): Channel number of outputs.
|
| 11 |
+
stride (int): Stride in convolution. Default: 1.
|
| 12 |
+
"""
|
| 13 |
+
return nn.Conv2d(inplanes, outplanes, kernel_size=3, stride=stride, padding=1, bias=False)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class BasicBlock(nn.Module):
|
| 17 |
+
"""Basic residual block used in the ResNetArcFace architecture.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
inplanes (int): Channel number of inputs.
|
| 21 |
+
planes (int): Channel number of outputs.
|
| 22 |
+
stride (int): Stride in convolution. Default: 1.
|
| 23 |
+
downsample (nn.Module): The downsample module. Default: None.
|
| 24 |
+
"""
|
| 25 |
+
expansion = 1 # output channel expansion ratio
|
| 26 |
+
|
| 27 |
+
def __init__(self, inplanes, planes, stride=1, downsample=None):
|
| 28 |
+
super(BasicBlock, self).__init__()
|
| 29 |
+
self.conv1 = conv3x3(inplanes, planes, stride)
|
| 30 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
| 31 |
+
self.relu = nn.ReLU(inplace=True)
|
| 32 |
+
self.conv2 = conv3x3(planes, planes)
|
| 33 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
| 34 |
+
self.downsample = downsample
|
| 35 |
+
self.stride = stride
|
| 36 |
+
|
| 37 |
+
def forward(self, x):
|
| 38 |
+
residual = x
|
| 39 |
+
|
| 40 |
+
out = self.conv1(x)
|
| 41 |
+
out = self.bn1(out)
|
| 42 |
+
out = self.relu(out)
|
| 43 |
+
|
| 44 |
+
out = self.conv2(out)
|
| 45 |
+
out = self.bn2(out)
|
| 46 |
+
|
| 47 |
+
if self.downsample is not None:
|
| 48 |
+
residual = self.downsample(x)
|
| 49 |
+
|
| 50 |
+
out += residual
|
| 51 |
+
out = self.relu(out)
|
| 52 |
+
|
| 53 |
+
return out
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class IRBlock(nn.Module):
|
| 57 |
+
"""Improved residual block (IR Block) used in the ResNetArcFace architecture.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
inplanes (int): Channel number of inputs.
|
| 61 |
+
planes (int): Channel number of outputs.
|
| 62 |
+
stride (int): Stride in convolution. Default: 1.
|
| 63 |
+
downsample (nn.Module): The downsample module. Default: None.
|
| 64 |
+
use_se (bool): Whether use the SEBlock (squeeze and excitation block). Default: True.
|
| 65 |
+
"""
|
| 66 |
+
expansion = 1 # output channel expansion ratio
|
| 67 |
+
|
| 68 |
+
def __init__(self, inplanes, planes, stride=1, downsample=None, use_se=True):
|
| 69 |
+
super(IRBlock, self).__init__()
|
| 70 |
+
self.bn0 = nn.BatchNorm2d(inplanes)
|
| 71 |
+
self.conv1 = conv3x3(inplanes, inplanes)
|
| 72 |
+
self.bn1 = nn.BatchNorm2d(inplanes)
|
| 73 |
+
self.prelu = nn.PReLU()
|
| 74 |
+
self.conv2 = conv3x3(inplanes, planes, stride)
|
| 75 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
| 76 |
+
self.downsample = downsample
|
| 77 |
+
self.stride = stride
|
| 78 |
+
self.use_se = use_se
|
| 79 |
+
if self.use_se:
|
| 80 |
+
self.se = SEBlock(planes)
|
| 81 |
+
|
| 82 |
+
def forward(self, x):
|
| 83 |
+
residual = x
|
| 84 |
+
out = self.bn0(x)
|
| 85 |
+
out = self.conv1(out)
|
| 86 |
+
out = self.bn1(out)
|
| 87 |
+
out = self.prelu(out)
|
| 88 |
+
|
| 89 |
+
out = self.conv2(out)
|
| 90 |
+
out = self.bn2(out)
|
| 91 |
+
if self.use_se:
|
| 92 |
+
out = self.se(out)
|
| 93 |
+
|
| 94 |
+
if self.downsample is not None:
|
| 95 |
+
residual = self.downsample(x)
|
| 96 |
+
|
| 97 |
+
out += residual
|
| 98 |
+
out = self.prelu(out)
|
| 99 |
+
|
| 100 |
+
return out
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
class Bottleneck(nn.Module):
|
| 104 |
+
"""Bottleneck block used in the ResNetArcFace architecture.
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
inplanes (int): Channel number of inputs.
|
| 108 |
+
planes (int): Channel number of outputs.
|
| 109 |
+
stride (int): Stride in convolution. Default: 1.
|
| 110 |
+
downsample (nn.Module): The downsample module. Default: None.
|
| 111 |
+
"""
|
| 112 |
+
expansion = 4 # output channel expansion ratio
|
| 113 |
+
|
| 114 |
+
def __init__(self, inplanes, planes, stride=1, downsample=None):
|
| 115 |
+
super(Bottleneck, self).__init__()
|
| 116 |
+
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
|
| 117 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
| 118 |
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
| 119 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
| 120 |
+
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
|
| 121 |
+
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
|
| 122 |
+
self.relu = nn.ReLU(inplace=True)
|
| 123 |
+
self.downsample = downsample
|
| 124 |
+
self.stride = stride
|
| 125 |
+
|
| 126 |
+
def forward(self, x):
|
| 127 |
+
residual = x
|
| 128 |
+
|
| 129 |
+
out = self.conv1(x)
|
| 130 |
+
out = self.bn1(out)
|
| 131 |
+
out = self.relu(out)
|
| 132 |
+
|
| 133 |
+
out = self.conv2(out)
|
| 134 |
+
out = self.bn2(out)
|
| 135 |
+
out = self.relu(out)
|
| 136 |
+
|
| 137 |
+
out = self.conv3(out)
|
| 138 |
+
out = self.bn3(out)
|
| 139 |
+
|
| 140 |
+
if self.downsample is not None:
|
| 141 |
+
residual = self.downsample(x)
|
| 142 |
+
|
| 143 |
+
out += residual
|
| 144 |
+
out = self.relu(out)
|
| 145 |
+
|
| 146 |
+
return out
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class SEBlock(nn.Module):
|
| 150 |
+
"""The squeeze-and-excitation block (SEBlock) used in the IRBlock.
|
| 151 |
+
|
| 152 |
+
Args:
|
| 153 |
+
channel (int): Channel number of inputs.
|
| 154 |
+
reduction (int): Channel reduction ration. Default: 16.
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
def __init__(self, channel, reduction=16):
|
| 158 |
+
super(SEBlock, self).__init__()
|
| 159 |
+
self.avg_pool = nn.AdaptiveAvgPool2d(1) # pool to 1x1 without spatial information
|
| 160 |
+
self.fc = nn.Sequential(
|
| 161 |
+
nn.Linear(channel, channel // reduction), nn.PReLU(), nn.Linear(channel // reduction, channel),
|
| 162 |
+
nn.Sigmoid())
|
| 163 |
+
|
| 164 |
+
def forward(self, x):
|
| 165 |
+
b, c, _, _ = x.size()
|
| 166 |
+
y = self.avg_pool(x).view(b, c)
|
| 167 |
+
y = self.fc(y).view(b, c, 1, 1)
|
| 168 |
+
return x * y
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
@ARCH_REGISTRY.register()
|
| 172 |
+
class ResNetArcFace(nn.Module):
|
| 173 |
+
"""ArcFace with ResNet architectures.
|
| 174 |
+
|
| 175 |
+
Ref: ArcFace: Additive Angular Margin Loss for Deep Face Recognition.
|
| 176 |
+
|
| 177 |
+
Args:
|
| 178 |
+
block (str): Block used in the ArcFace architecture.
|
| 179 |
+
layers (tuple(int)): Block numbers in each layer.
|
| 180 |
+
use_se (bool): Whether use the SEBlock (squeeze and excitation block). Default: True.
|
| 181 |
+
"""
|
| 182 |
+
|
| 183 |
+
def __init__(self, block, layers, use_se=True):
|
| 184 |
+
if block == 'IRBlock':
|
| 185 |
+
block = IRBlock
|
| 186 |
+
self.inplanes = 64
|
| 187 |
+
self.use_se = use_se
|
| 188 |
+
super(ResNetArcFace, self).__init__()
|
| 189 |
+
|
| 190 |
+
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False)
|
| 191 |
+
self.bn1 = nn.BatchNorm2d(64)
|
| 192 |
+
self.prelu = nn.PReLU()
|
| 193 |
+
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 194 |
+
self.layer1 = self._make_layer(block, 64, layers[0])
|
| 195 |
+
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
|
| 196 |
+
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
|
| 197 |
+
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
|
| 198 |
+
self.bn4 = nn.BatchNorm2d(512)
|
| 199 |
+
self.dropout = nn.Dropout()
|
| 200 |
+
self.fc5 = nn.Linear(512 * 8 * 8, 512)
|
| 201 |
+
self.bn5 = nn.BatchNorm1d(512)
|
| 202 |
+
|
| 203 |
+
# initialization
|
| 204 |
+
for m in self.modules():
|
| 205 |
+
if isinstance(m, nn.Conv2d):
|
| 206 |
+
nn.init.xavier_normal_(m.weight)
|
| 207 |
+
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d):
|
| 208 |
+
nn.init.constant_(m.weight, 1)
|
| 209 |
+
nn.init.constant_(m.bias, 0)
|
| 210 |
+
elif isinstance(m, nn.Linear):
|
| 211 |
+
nn.init.xavier_normal_(m.weight)
|
| 212 |
+
nn.init.constant_(m.bias, 0)
|
| 213 |
+
|
| 214 |
+
def _make_layer(self, block, planes, num_blocks, stride=1):
|
| 215 |
+
downsample = None
|
| 216 |
+
if stride != 1 or self.inplanes != planes * block.expansion:
|
| 217 |
+
downsample = nn.Sequential(
|
| 218 |
+
nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
|
| 219 |
+
nn.BatchNorm2d(planes * block.expansion),
|
| 220 |
+
)
|
| 221 |
+
layers = []
|
| 222 |
+
layers.append(block(self.inplanes, planes, stride, downsample, use_se=self.use_se))
|
| 223 |
+
self.inplanes = planes
|
| 224 |
+
for _ in range(1, num_blocks):
|
| 225 |
+
layers.append(block(self.inplanes, planes, use_se=self.use_se))
|
| 226 |
+
|
| 227 |
+
return nn.Sequential(*layers)
|
| 228 |
+
|
| 229 |
+
def forward(self, x):
|
| 230 |
+
x = self.conv1(x)
|
| 231 |
+
x = self.bn1(x)
|
| 232 |
+
x = self.prelu(x)
|
| 233 |
+
x = self.maxpool(x)
|
| 234 |
+
|
| 235 |
+
x = self.layer1(x)
|
| 236 |
+
x = self.layer2(x)
|
| 237 |
+
x = self.layer3(x)
|
| 238 |
+
x = self.layer4(x)
|
| 239 |
+
x = self.bn4(x)
|
| 240 |
+
x = self.dropout(x)
|
| 241 |
+
x = x.view(x.size(0), -1)
|
| 242 |
+
x = self.fc5(x)
|
| 243 |
+
x = self.bn5(x)
|
| 244 |
+
|
| 245 |
+
return x
|
basicsr/archs/arch_util.py
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import collections.abc
|
| 2 |
+
import math
|
| 3 |
+
import torch
|
| 4 |
+
import torchvision
|
| 5 |
+
import warnings
|
| 6 |
+
from distutils.version import LooseVersion
|
| 7 |
+
from itertools import repeat
|
| 8 |
+
from torch import nn as nn
|
| 9 |
+
from torch.nn import functional as F
|
| 10 |
+
from torch.nn import init as init
|
| 11 |
+
from torch.nn.modules.batchnorm import _BatchNorm
|
| 12 |
+
|
| 13 |
+
from basicsr.ops.dcn import ModulatedDeformConvPack, modulated_deform_conv
|
| 14 |
+
from basicsr.utils import get_root_logger
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@torch.no_grad()
|
| 18 |
+
def default_init_weights(module_list, scale=1, bias_fill=0, **kwargs):
|
| 19 |
+
"""Initialize network weights.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
module_list (list[nn.Module] | nn.Module): Modules to be initialized.
|
| 23 |
+
scale (float): Scale initialized weights, especially for residual
|
| 24 |
+
blocks. Default: 1.
|
| 25 |
+
bias_fill (float): The value to fill bias. Default: 0
|
| 26 |
+
kwargs (dict): Other arguments for initialization function.
|
| 27 |
+
"""
|
| 28 |
+
if not isinstance(module_list, list):
|
| 29 |
+
module_list = [module_list]
|
| 30 |
+
for module in module_list:
|
| 31 |
+
for m in module.modules():
|
| 32 |
+
if isinstance(m, nn.Conv2d):
|
| 33 |
+
init.kaiming_normal_(m.weight, **kwargs)
|
| 34 |
+
m.weight.data *= scale
|
| 35 |
+
if m.bias is not None:
|
| 36 |
+
m.bias.data.fill_(bias_fill)
|
| 37 |
+
elif isinstance(m, nn.Linear):
|
| 38 |
+
init.kaiming_normal_(m.weight, **kwargs)
|
| 39 |
+
m.weight.data *= scale
|
| 40 |
+
if m.bias is not None:
|
| 41 |
+
m.bias.data.fill_(bias_fill)
|
| 42 |
+
elif isinstance(m, _BatchNorm):
|
| 43 |
+
init.constant_(m.weight, 1)
|
| 44 |
+
if m.bias is not None:
|
| 45 |
+
m.bias.data.fill_(bias_fill)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def make_layer(basic_block, num_basic_block, **kwarg):
|
| 49 |
+
"""Make layers by stacking the same blocks.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
basic_block (nn.module): nn.module class for basic block.
|
| 53 |
+
num_basic_block (int): number of blocks.
|
| 54 |
+
|
| 55 |
+
Returns:
|
| 56 |
+
nn.Sequential: Stacked blocks in nn.Sequential.
|
| 57 |
+
"""
|
| 58 |
+
layers = []
|
| 59 |
+
for _ in range(num_basic_block):
|
| 60 |
+
layers.append(basic_block(**kwarg))
|
| 61 |
+
return nn.Sequential(*layers)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class ResidualBlockNoBN(nn.Module):
|
| 65 |
+
"""Residual block without BN.
|
| 66 |
+
|
| 67 |
+
It has a style of:
|
| 68 |
+
---Conv-ReLU-Conv-+-
|
| 69 |
+
|________________|
|
| 70 |
+
|
| 71 |
+
Args:
|
| 72 |
+
num_feat (int): Channel number of intermediate features.
|
| 73 |
+
Default: 64.
|
| 74 |
+
res_scale (float): Residual scale. Default: 1.
|
| 75 |
+
pytorch_init (bool): If set to True, use pytorch default init,
|
| 76 |
+
otherwise, use default_init_weights. Default: False.
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
def __init__(self, num_feat=64, res_scale=1, pytorch_init=False):
|
| 80 |
+
super(ResidualBlockNoBN, self).__init__()
|
| 81 |
+
self.res_scale = res_scale
|
| 82 |
+
self.conv1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1, bias=True)
|
| 83 |
+
self.conv2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1, bias=True)
|
| 84 |
+
self.relu = nn.ReLU(inplace=True)
|
| 85 |
+
|
| 86 |
+
if not pytorch_init:
|
| 87 |
+
default_init_weights([self.conv1, self.conv2], 0.1)
|
| 88 |
+
|
| 89 |
+
def forward(self, x):
|
| 90 |
+
identity = x
|
| 91 |
+
out = self.conv2(self.relu(self.conv1(x)))
|
| 92 |
+
return identity + out * self.res_scale
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class Upsample(nn.Sequential):
|
| 96 |
+
"""Upsample module.
|
| 97 |
+
|
| 98 |
+
Args:
|
| 99 |
+
scale (int): Scale factor. Supported scales: 2^n and 3.
|
| 100 |
+
num_feat (int): Channel number of intermediate features.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
def __init__(self, scale, num_feat):
|
| 104 |
+
m = []
|
| 105 |
+
if (scale & (scale - 1)) == 0: # scale = 2^n
|
| 106 |
+
for _ in range(int(math.log(scale, 2))):
|
| 107 |
+
m.append(nn.Conv2d(num_feat, 4 * num_feat, 3, 1, 1))
|
| 108 |
+
m.append(nn.PixelShuffle(2))
|
| 109 |
+
elif scale == 3:
|
| 110 |
+
m.append(nn.Conv2d(num_feat, 9 * num_feat, 3, 1, 1))
|
| 111 |
+
m.append(nn.PixelShuffle(3))
|
| 112 |
+
else:
|
| 113 |
+
raise ValueError(f'scale {scale} is not supported. Supported scales: 2^n and 3.')
|
| 114 |
+
super(Upsample, self).__init__(*m)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def flow_warp(x, flow, interp_mode='bilinear', padding_mode='zeros', align_corners=True):
|
| 118 |
+
"""Warp an image or feature map with optical flow.
|
| 119 |
+
|
| 120 |
+
Args:
|
| 121 |
+
x (Tensor): Tensor with size (n, c, h, w).
|
| 122 |
+
flow (Tensor): Tensor with size (n, h, w, 2), normal value.
|
| 123 |
+
interp_mode (str): 'nearest' or 'bilinear'. Default: 'bilinear'.
|
| 124 |
+
padding_mode (str): 'zeros' or 'border' or 'reflection'.
|
| 125 |
+
Default: 'zeros'.
|
| 126 |
+
align_corners (bool): Before pytorch 1.3, the default value is
|
| 127 |
+
align_corners=True. After pytorch 1.3, the default value is
|
| 128 |
+
align_corners=False. Here, we use the True as default.
|
| 129 |
+
|
| 130 |
+
Returns:
|
| 131 |
+
Tensor: Warped image or feature map.
|
| 132 |
+
"""
|
| 133 |
+
assert x.size()[-2:] == flow.size()[1:3]
|
| 134 |
+
_, _, h, w = x.size()
|
| 135 |
+
# create mesh grid
|
| 136 |
+
grid_y, grid_x = torch.meshgrid(torch.arange(0, h).type_as(x), torch.arange(0, w).type_as(x))
|
| 137 |
+
grid = torch.stack((grid_x, grid_y), 2).float() # W(x), H(y), 2
|
| 138 |
+
grid.requires_grad = False
|
| 139 |
+
|
| 140 |
+
vgrid = grid + flow
|
| 141 |
+
# scale grid to [-1,1]
|
| 142 |
+
vgrid_x = 2.0 * vgrid[:, :, :, 0] / max(w - 1, 1) - 1.0
|
| 143 |
+
vgrid_y = 2.0 * vgrid[:, :, :, 1] / max(h - 1, 1) - 1.0
|
| 144 |
+
vgrid_scaled = torch.stack((vgrid_x, vgrid_y), dim=3)
|
| 145 |
+
output = F.grid_sample(x, vgrid_scaled, mode=interp_mode, padding_mode=padding_mode, align_corners=align_corners)
|
| 146 |
+
|
| 147 |
+
# TODO, what if align_corners=False
|
| 148 |
+
return output
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def resize_flow(flow, size_type, sizes, interp_mode='bilinear', align_corners=False):
|
| 152 |
+
"""Resize a flow according to ratio or shape.
|
| 153 |
+
|
| 154 |
+
Args:
|
| 155 |
+
flow (Tensor): Precomputed flow. shape [N, 2, H, W].
|
| 156 |
+
size_type (str): 'ratio' or 'shape'.
|
| 157 |
+
sizes (list[int | float]): the ratio for resizing or the final output
|
| 158 |
+
shape.
|
| 159 |
+
1) The order of ratio should be [ratio_h, ratio_w]. For
|
| 160 |
+
downsampling, the ratio should be smaller than 1.0 (i.e., ratio
|
| 161 |
+
< 1.0). For upsampling, the ratio should be larger than 1.0 (i.e.,
|
| 162 |
+
ratio > 1.0).
|
| 163 |
+
2) The order of output_size should be [out_h, out_w].
|
| 164 |
+
interp_mode (str): The mode of interpolation for resizing.
|
| 165 |
+
Default: 'bilinear'.
|
| 166 |
+
align_corners (bool): Whether align corners. Default: False.
|
| 167 |
+
|
| 168 |
+
Returns:
|
| 169 |
+
Tensor: Resized flow.
|
| 170 |
+
"""
|
| 171 |
+
_, _, flow_h, flow_w = flow.size()
|
| 172 |
+
if size_type == 'ratio':
|
| 173 |
+
output_h, output_w = int(flow_h * sizes[0]), int(flow_w * sizes[1])
|
| 174 |
+
elif size_type == 'shape':
|
| 175 |
+
output_h, output_w = sizes[0], sizes[1]
|
| 176 |
+
else:
|
| 177 |
+
raise ValueError(f'Size type should be ratio or shape, but got type {size_type}.')
|
| 178 |
+
|
| 179 |
+
input_flow = flow.clone()
|
| 180 |
+
ratio_h = output_h / flow_h
|
| 181 |
+
ratio_w = output_w / flow_w
|
| 182 |
+
input_flow[:, 0, :, :] *= ratio_w
|
| 183 |
+
input_flow[:, 1, :, :] *= ratio_h
|
| 184 |
+
resized_flow = F.interpolate(
|
| 185 |
+
input=input_flow, size=(output_h, output_w), mode=interp_mode, align_corners=align_corners)
|
| 186 |
+
return resized_flow
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# TODO: may write a cpp file
|
| 190 |
+
def pixel_unshuffle(x, scale):
|
| 191 |
+
""" Pixel unshuffle.
|
| 192 |
+
|
| 193 |
+
Args:
|
| 194 |
+
x (Tensor): Input feature with shape (b, c, hh, hw).
|
| 195 |
+
scale (int): Downsample ratio.
|
| 196 |
+
|
| 197 |
+
Returns:
|
| 198 |
+
Tensor: the pixel unshuffled feature.
|
| 199 |
+
"""
|
| 200 |
+
b, c, hh, hw = x.size()
|
| 201 |
+
out_channel = c * (scale**2)
|
| 202 |
+
assert hh % scale == 0 and hw % scale == 0
|
| 203 |
+
h = hh // scale
|
| 204 |
+
w = hw // scale
|
| 205 |
+
x_view = x.view(b, c, h, scale, w, scale)
|
| 206 |
+
return x_view.permute(0, 1, 3, 5, 2, 4).reshape(b, out_channel, h, w)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
class DCNv2Pack(ModulatedDeformConvPack):
|
| 210 |
+
"""Modulated deformable conv for deformable alignment.
|
| 211 |
+
|
| 212 |
+
Different from the official DCNv2Pack, which generates offsets and masks
|
| 213 |
+
from the preceding features, this DCNv2Pack takes another different
|
| 214 |
+
features to generate offsets and masks.
|
| 215 |
+
|
| 216 |
+
Ref:
|
| 217 |
+
Delving Deep into Deformable Alignment in Video Super-Resolution.
|
| 218 |
+
"""
|
| 219 |
+
|
| 220 |
+
def forward(self, x, feat):
|
| 221 |
+
out = self.conv_offset(feat)
|
| 222 |
+
o1, o2, mask = torch.chunk(out, 3, dim=1)
|
| 223 |
+
offset = torch.cat((o1, o2), dim=1)
|
| 224 |
+
mask = torch.sigmoid(mask)
|
| 225 |
+
|
| 226 |
+
offset_absmean = torch.mean(torch.abs(offset))
|
| 227 |
+
if offset_absmean > 50:
|
| 228 |
+
logger = get_root_logger()
|
| 229 |
+
logger.warning(f'Offset abs mean is {offset_absmean}, larger than 50.')
|
| 230 |
+
|
| 231 |
+
if LooseVersion(torchvision.__version__) >= LooseVersion('0.9.0'):
|
| 232 |
+
return torchvision.ops.deform_conv2d(x, offset, self.weight, self.bias, self.stride, self.padding,
|
| 233 |
+
self.dilation, mask)
|
| 234 |
+
else:
|
| 235 |
+
return modulated_deform_conv(x, offset, mask, self.weight, self.bias, self.stride, self.padding,
|
| 236 |
+
self.dilation, self.groups, self.deformable_groups)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def _no_grad_trunc_normal_(tensor, mean, std, a, b):
|
| 240 |
+
# From: https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/layers/weight_init.py
|
| 241 |
+
# Cut & paste from PyTorch official master until it's in a few official releases - RW
|
| 242 |
+
# Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
|
| 243 |
+
def norm_cdf(x):
|
| 244 |
+
# Computes standard normal cumulative distribution function
|
| 245 |
+
return (1. + math.erf(x / math.sqrt(2.))) / 2.
|
| 246 |
+
|
| 247 |
+
if (mean < a - 2 * std) or (mean > b + 2 * std):
|
| 248 |
+
warnings.warn(
|
| 249 |
+
'mean is more than 2 std from [a, b] in nn.init.trunc_normal_. '
|
| 250 |
+
'The distribution of values may be incorrect.',
|
| 251 |
+
stacklevel=2)
|
| 252 |
+
|
| 253 |
+
with torch.no_grad():
|
| 254 |
+
# Values are generated by using a truncated uniform distribution and
|
| 255 |
+
# then using the inverse CDF for the normal distribution.
|
| 256 |
+
# Get upper and lower cdf values
|
| 257 |
+
low = norm_cdf((a - mean) / std)
|
| 258 |
+
up = norm_cdf((b - mean) / std)
|
| 259 |
+
|
| 260 |
+
# Uniformly fill tensor with values from [low, up], then translate to
|
| 261 |
+
# [2l-1, 2u-1].
|
| 262 |
+
tensor.uniform_(2 * low - 1, 2 * up - 1)
|
| 263 |
+
|
| 264 |
+
# Use inverse cdf transform for normal distribution to get truncated
|
| 265 |
+
# standard normal
|
| 266 |
+
tensor.erfinv_()
|
| 267 |
+
|
| 268 |
+
# Transform to proper mean, std
|
| 269 |
+
tensor.mul_(std * math.sqrt(2.))
|
| 270 |
+
tensor.add_(mean)
|
| 271 |
+
|
| 272 |
+
# Clamp to ensure it's in the proper range
|
| 273 |
+
tensor.clamp_(min=a, max=b)
|
| 274 |
+
return tensor
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
def trunc_normal_(tensor, mean=0., std=1., a=-2., b=2.):
|
| 278 |
+
r"""Fills the input Tensor with values drawn from a truncated
|
| 279 |
+
normal distribution.
|
| 280 |
+
|
| 281 |
+
From: https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/layers/weight_init.py
|
| 282 |
+
|
| 283 |
+
The values are effectively drawn from the
|
| 284 |
+
normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`
|
| 285 |
+
with values outside :math:`[a, b]` redrawn until they are within
|
| 286 |
+
the bounds. The method used for generating the random values works
|
| 287 |
+
best when :math:`a \leq \text{mean} \leq b`.
|
| 288 |
+
|
| 289 |
+
Args:
|
| 290 |
+
tensor: an n-dimensional `torch.Tensor`
|
| 291 |
+
mean: the mean of the normal distribution
|
| 292 |
+
std: the standard deviation of the normal distribution
|
| 293 |
+
a: the minimum cutoff value
|
| 294 |
+
b: the maximum cutoff value
|
| 295 |
+
|
| 296 |
+
Examples:
|
| 297 |
+
>>> w = torch.empty(3, 5)
|
| 298 |
+
>>> nn.init.trunc_normal_(w)
|
| 299 |
+
"""
|
| 300 |
+
return _no_grad_trunc_normal_(tensor, mean, std, a, b)
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
# From PyTorch
|
| 304 |
+
def _ntuple(n):
|
| 305 |
+
|
| 306 |
+
def parse(x):
|
| 307 |
+
if isinstance(x, collections.abc.Iterable):
|
| 308 |
+
return x
|
| 309 |
+
return tuple(repeat(x, n))
|
| 310 |
+
|
| 311 |
+
return parse
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
to_1tuple = _ntuple(1)
|
| 315 |
+
to_2tuple = _ntuple(2)
|
| 316 |
+
to_3tuple = _ntuple(3)
|
| 317 |
+
to_4tuple = _ntuple(4)
|
| 318 |
+
to_ntuple = _ntuple
|
basicsr/archs/codeformer_arch.py
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from torch import nn, Tensor
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
from typing import Optional, List
|
| 7 |
+
|
| 8 |
+
from basicsr.archs.vqgan_arch import *
|
| 9 |
+
from basicsr.utils import get_root_logger
|
| 10 |
+
from basicsr.utils.registry import ARCH_REGISTRY
|
| 11 |
+
|
| 12 |
+
def calc_mean_std(feat, eps=1e-5):
|
| 13 |
+
"""Calculate mean and std for adaptive_instance_normalization.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
feat (Tensor): 4D tensor.
|
| 17 |
+
eps (float): A small value added to the variance to avoid
|
| 18 |
+
divide-by-zero. Default: 1e-5.
|
| 19 |
+
"""
|
| 20 |
+
size = feat.size()
|
| 21 |
+
assert len(size) == 4, 'The input feature should be 4D tensor.'
|
| 22 |
+
b, c = size[:2]
|
| 23 |
+
feat_var = feat.view(b, c, -1).var(dim=2) + eps
|
| 24 |
+
feat_std = feat_var.sqrt().view(b, c, 1, 1)
|
| 25 |
+
feat_mean = feat.view(b, c, -1).mean(dim=2).view(b, c, 1, 1)
|
| 26 |
+
return feat_mean, feat_std
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def adaptive_instance_normalization(content_feat, style_feat):
|
| 30 |
+
"""Adaptive instance normalization.
|
| 31 |
+
|
| 32 |
+
Adjust the reference features to have the similar color and illuminations
|
| 33 |
+
as those in the degradate features.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
content_feat (Tensor): The reference feature.
|
| 37 |
+
style_feat (Tensor): The degradate features.
|
| 38 |
+
"""
|
| 39 |
+
size = content_feat.size()
|
| 40 |
+
style_mean, style_std = calc_mean_std(style_feat)
|
| 41 |
+
content_mean, content_std = calc_mean_std(content_feat)
|
| 42 |
+
normalized_feat = (content_feat - content_mean.expand(size)) / content_std.expand(size)
|
| 43 |
+
return normalized_feat * style_std.expand(size) + style_mean.expand(size)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class PositionEmbeddingSine(nn.Module):
|
| 47 |
+
"""
|
| 48 |
+
This is a more standard version of the position embedding, very similar to the one
|
| 49 |
+
used by the Attention is all you need paper, generalized to work on images.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
|
| 53 |
+
super().__init__()
|
| 54 |
+
self.num_pos_feats = num_pos_feats
|
| 55 |
+
self.temperature = temperature
|
| 56 |
+
self.normalize = normalize
|
| 57 |
+
if scale is not None and normalize is False:
|
| 58 |
+
raise ValueError("normalize should be True if scale is passed")
|
| 59 |
+
if scale is None:
|
| 60 |
+
scale = 2 * math.pi
|
| 61 |
+
self.scale = scale
|
| 62 |
+
|
| 63 |
+
def forward(self, x, mask=None):
|
| 64 |
+
if mask is None:
|
| 65 |
+
mask = torch.zeros((x.size(0), x.size(2), x.size(3)), device=x.device, dtype=torch.bool)
|
| 66 |
+
not_mask = ~mask
|
| 67 |
+
y_embed = not_mask.cumsum(1, dtype=torch.float32)
|
| 68 |
+
x_embed = not_mask.cumsum(2, dtype=torch.float32)
|
| 69 |
+
if self.normalize:
|
| 70 |
+
eps = 1e-6
|
| 71 |
+
y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale
|
| 72 |
+
x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale
|
| 73 |
+
|
| 74 |
+
dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device)
|
| 75 |
+
dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)
|
| 76 |
+
|
| 77 |
+
pos_x = x_embed[:, :, :, None] / dim_t
|
| 78 |
+
pos_y = y_embed[:, :, :, None] / dim_t
|
| 79 |
+
pos_x = torch.stack(
|
| 80 |
+
(pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4
|
| 81 |
+
).flatten(3)
|
| 82 |
+
pos_y = torch.stack(
|
| 83 |
+
(pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4
|
| 84 |
+
).flatten(3)
|
| 85 |
+
pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)
|
| 86 |
+
return pos
|
| 87 |
+
|
| 88 |
+
def _get_activation_fn(activation):
|
| 89 |
+
"""Return an activation function given a string"""
|
| 90 |
+
if activation == "relu":
|
| 91 |
+
return F.relu
|
| 92 |
+
if activation == "gelu":
|
| 93 |
+
return F.gelu
|
| 94 |
+
if activation == "glu":
|
| 95 |
+
return F.glu
|
| 96 |
+
raise RuntimeError(F"activation should be relu/gelu, not {activation}.")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class TransformerSALayer(nn.Module):
|
| 100 |
+
def __init__(self, embed_dim, nhead=8, dim_mlp=2048, dropout=0.0, activation="gelu"):
|
| 101 |
+
super().__init__()
|
| 102 |
+
self.self_attn = nn.MultiheadAttention(embed_dim, nhead, dropout=dropout)
|
| 103 |
+
# Implementation of Feedforward model - MLP
|
| 104 |
+
self.linear1 = nn.Linear(embed_dim, dim_mlp)
|
| 105 |
+
self.dropout = nn.Dropout(dropout)
|
| 106 |
+
self.linear2 = nn.Linear(dim_mlp, embed_dim)
|
| 107 |
+
|
| 108 |
+
self.norm1 = nn.LayerNorm(embed_dim)
|
| 109 |
+
self.norm2 = nn.LayerNorm(embed_dim)
|
| 110 |
+
self.dropout1 = nn.Dropout(dropout)
|
| 111 |
+
self.dropout2 = nn.Dropout(dropout)
|
| 112 |
+
|
| 113 |
+
self.activation = _get_activation_fn(activation)
|
| 114 |
+
|
| 115 |
+
def with_pos_embed(self, tensor, pos: Optional[Tensor]):
|
| 116 |
+
return tensor if pos is None else tensor + pos
|
| 117 |
+
|
| 118 |
+
def forward(self, tgt,
|
| 119 |
+
tgt_mask: Optional[Tensor] = None,
|
| 120 |
+
tgt_key_padding_mask: Optional[Tensor] = None,
|
| 121 |
+
query_pos: Optional[Tensor] = None):
|
| 122 |
+
|
| 123 |
+
# self attention
|
| 124 |
+
tgt2 = self.norm1(tgt)
|
| 125 |
+
q = k = self.with_pos_embed(tgt2, query_pos)
|
| 126 |
+
tgt2 = self.self_attn(q, k, value=tgt2, attn_mask=tgt_mask,
|
| 127 |
+
key_padding_mask=tgt_key_padding_mask)[0]
|
| 128 |
+
tgt = tgt + self.dropout1(tgt2)
|
| 129 |
+
|
| 130 |
+
# ffn
|
| 131 |
+
tgt2 = self.norm2(tgt)
|
| 132 |
+
tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2))))
|
| 133 |
+
tgt = tgt + self.dropout2(tgt2)
|
| 134 |
+
return tgt
|
| 135 |
+
|
| 136 |
+
class Fuse_sft_block(nn.Module):
|
| 137 |
+
def __init__(self, in_ch, out_ch):
|
| 138 |
+
super().__init__()
|
| 139 |
+
self.encode_enc = ResBlock(2*in_ch, out_ch)
|
| 140 |
+
|
| 141 |
+
self.scale = nn.Sequential(
|
| 142 |
+
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1),
|
| 143 |
+
nn.LeakyReLU(0.2, True),
|
| 144 |
+
nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1))
|
| 145 |
+
|
| 146 |
+
self.shift = nn.Sequential(
|
| 147 |
+
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1),
|
| 148 |
+
nn.LeakyReLU(0.2, True),
|
| 149 |
+
nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1))
|
| 150 |
+
|
| 151 |
+
def forward(self, enc_feat, dec_feat, w=1):
|
| 152 |
+
enc_feat = self.encode_enc(torch.cat([enc_feat, dec_feat], dim=1))
|
| 153 |
+
scale = self.scale(enc_feat)
|
| 154 |
+
shift = self.shift(enc_feat)
|
| 155 |
+
residual = w * (dec_feat * scale + shift)
|
| 156 |
+
out = dec_feat + residual
|
| 157 |
+
return out
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
@ARCH_REGISTRY.register()
|
| 161 |
+
class CodeFormer(VQAutoEncoder):
|
| 162 |
+
def __init__(self, dim_embd=512, n_head=8, n_layers=9,
|
| 163 |
+
codebook_size=1024, latent_size=256,
|
| 164 |
+
connect_list=['32', '64', '128', '256'],
|
| 165 |
+
fix_modules=['quantize','generator'], vqgan_path=None):
|
| 166 |
+
super(CodeFormer, self).__init__(512, 64, [1, 2, 2, 4, 4, 8], 'nearest',2, [16], codebook_size)
|
| 167 |
+
|
| 168 |
+
if vqgan_path is not None:
|
| 169 |
+
self.load_state_dict(
|
| 170 |
+
torch.load(vqgan_path, map_location='cpu')['params_ema'])
|
| 171 |
+
|
| 172 |
+
if fix_modules is not None:
|
| 173 |
+
for module in fix_modules:
|
| 174 |
+
for param in getattr(self, module).parameters():
|
| 175 |
+
param.requires_grad = False
|
| 176 |
+
|
| 177 |
+
self.connect_list = connect_list
|
| 178 |
+
self.n_layers = n_layers
|
| 179 |
+
self.dim_embd = dim_embd
|
| 180 |
+
self.dim_mlp = dim_embd*2
|
| 181 |
+
|
| 182 |
+
self.position_emb = nn.Parameter(torch.zeros(latent_size, self.dim_embd))
|
| 183 |
+
self.feat_emb = nn.Linear(256, self.dim_embd)
|
| 184 |
+
|
| 185 |
+
# transformer
|
| 186 |
+
self.ft_layers = nn.Sequential(*[TransformerSALayer(embed_dim=dim_embd, nhead=n_head, dim_mlp=self.dim_mlp, dropout=0.0)
|
| 187 |
+
for _ in range(self.n_layers)])
|
| 188 |
+
|
| 189 |
+
# logits_predict head
|
| 190 |
+
self.idx_pred_layer = nn.Sequential(
|
| 191 |
+
nn.LayerNorm(dim_embd),
|
| 192 |
+
nn.Linear(dim_embd, codebook_size, bias=False))
|
| 193 |
+
|
| 194 |
+
self.channels = {
|
| 195 |
+
'16': 512,
|
| 196 |
+
'32': 256,
|
| 197 |
+
'64': 256,
|
| 198 |
+
'128': 128,
|
| 199 |
+
'256': 128,
|
| 200 |
+
'512': 64,
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
# after second residual block for > 16, before attn layer for ==16
|
| 204 |
+
self.fuse_encoder_block = {'512':2, '256':5, '128':8, '64':11, '32':14, '16':18}
|
| 205 |
+
# after first residual block for > 16, before attn layer for ==16
|
| 206 |
+
self.fuse_generator_block = {'16':6, '32': 9, '64':12, '128':15, '256':18, '512':21}
|
| 207 |
+
|
| 208 |
+
# fuse_convs_dict
|
| 209 |
+
self.fuse_convs_dict = nn.ModuleDict()
|
| 210 |
+
for f_size in self.connect_list:
|
| 211 |
+
in_ch = self.channels[f_size]
|
| 212 |
+
self.fuse_convs_dict[f_size] = Fuse_sft_block(in_ch, in_ch)
|
| 213 |
+
|
| 214 |
+
def _init_weights(self, module):
|
| 215 |
+
if isinstance(module, (nn.Linear, nn.Embedding)):
|
| 216 |
+
module.weight.data.normal_(mean=0.0, std=0.02)
|
| 217 |
+
if isinstance(module, nn.Linear) and module.bias is not None:
|
| 218 |
+
module.bias.data.zero_()
|
| 219 |
+
elif isinstance(module, nn.LayerNorm):
|
| 220 |
+
module.bias.data.zero_()
|
| 221 |
+
module.weight.data.fill_(1.0)
|
| 222 |
+
|
| 223 |
+
def forward(self, x, w=0, detach_16=True, code_only=False, adain=False):
|
| 224 |
+
# ################### Encoder #####################
|
| 225 |
+
enc_feat_dict = {}
|
| 226 |
+
out_list = [self.fuse_encoder_block[f_size] for f_size in self.connect_list]
|
| 227 |
+
for i, block in enumerate(self.encoder.blocks):
|
| 228 |
+
x = block(x)
|
| 229 |
+
if i in out_list:
|
| 230 |
+
enc_feat_dict[str(x.shape[-1])] = x.clone()
|
| 231 |
+
|
| 232 |
+
lq_feat = x
|
| 233 |
+
# ################# Transformer ###################
|
| 234 |
+
# quant_feat, codebook_loss, quant_stats = self.quantize(lq_feat)
|
| 235 |
+
pos_emb = self.position_emb.unsqueeze(1).repeat(1,x.shape[0],1)
|
| 236 |
+
# BCHW -> BC(HW) -> (HW)BC
|
| 237 |
+
feat_emb = self.feat_emb(lq_feat.flatten(2).permute(2,0,1))
|
| 238 |
+
query_emb = feat_emb
|
| 239 |
+
# Transformer encoder
|
| 240 |
+
for layer in self.ft_layers:
|
| 241 |
+
query_emb = layer(query_emb, query_pos=pos_emb)
|
| 242 |
+
|
| 243 |
+
# output logits
|
| 244 |
+
logits = self.idx_pred_layer(query_emb) # (hw)bn
|
| 245 |
+
logits = logits.permute(1,0,2) # (hw)bn -> b(hw)n
|
| 246 |
+
|
| 247 |
+
if code_only: # for training stage II
|
| 248 |
+
# logits doesn't need softmax before cross_entropy loss
|
| 249 |
+
return logits, lq_feat
|
| 250 |
+
|
| 251 |
+
# ################# Quantization ###################
|
| 252 |
+
# if self.training:
|
| 253 |
+
# quant_feat = torch.einsum('btn,nc->btc', [soft_one_hot, self.quantize.embedding.weight])
|
| 254 |
+
# # b(hw)c -> bc(hw) -> bchw
|
| 255 |
+
# quant_feat = quant_feat.permute(0,2,1).view(lq_feat.shape)
|
| 256 |
+
# ------------
|
| 257 |
+
soft_one_hot = F.softmax(logits, dim=2)
|
| 258 |
+
_, top_idx = torch.topk(soft_one_hot, 1, dim=2)
|
| 259 |
+
quant_feat = self.quantize.get_codebook_feat(top_idx, shape=[x.shape[0],16,16,256])
|
| 260 |
+
# preserve gradients
|
| 261 |
+
# quant_feat = lq_feat + (quant_feat - lq_feat).detach()
|
| 262 |
+
|
| 263 |
+
if detach_16:
|
| 264 |
+
quant_feat = quant_feat.detach() # for training stage III
|
| 265 |
+
if adain:
|
| 266 |
+
quant_feat = adaptive_instance_normalization(quant_feat, lq_feat)
|
| 267 |
+
|
| 268 |
+
# ################## Generator ####################
|
| 269 |
+
x = quant_feat
|
| 270 |
+
fuse_list = [self.fuse_generator_block[f_size] for f_size in self.connect_list]
|
| 271 |
+
|
| 272 |
+
for i, block in enumerate(self.generator.blocks):
|
| 273 |
+
x = block(x)
|
| 274 |
+
if i in fuse_list: # fuse after i-th block
|
| 275 |
+
f_size = str(x.shape[-1])
|
| 276 |
+
if w>0:
|
| 277 |
+
x = self.fuse_convs_dict[f_size](enc_feat_dict[f_size].detach(), x, w)
|
| 278 |
+
out = x
|
| 279 |
+
# logits doesn't need softmax before cross_entropy loss
|
| 280 |
+
return out, logits, lq_feat
|
basicsr/archs/rrdbnet_arch.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn as nn
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
|
| 5 |
+
from basicsr.utils.registry import ARCH_REGISTRY
|
| 6 |
+
from .arch_util import default_init_weights, make_layer, pixel_unshuffle
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class ResidualDenseBlock(nn.Module):
|
| 10 |
+
"""Residual Dense Block.
|
| 11 |
+
|
| 12 |
+
Used in RRDB block in ESRGAN.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
num_feat (int): Channel number of intermediate features.
|
| 16 |
+
num_grow_ch (int): Channels for each growth.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def __init__(self, num_feat=64, num_grow_ch=32):
|
| 20 |
+
super(ResidualDenseBlock, self).__init__()
|
| 21 |
+
self.conv1 = nn.Conv2d(num_feat, num_grow_ch, 3, 1, 1)
|
| 22 |
+
self.conv2 = nn.Conv2d(num_feat + num_grow_ch, num_grow_ch, 3, 1, 1)
|
| 23 |
+
self.conv3 = nn.Conv2d(num_feat + 2 * num_grow_ch, num_grow_ch, 3, 1, 1)
|
| 24 |
+
self.conv4 = nn.Conv2d(num_feat + 3 * num_grow_ch, num_grow_ch, 3, 1, 1)
|
| 25 |
+
self.conv5 = nn.Conv2d(num_feat + 4 * num_grow_ch, num_feat, 3, 1, 1)
|
| 26 |
+
|
| 27 |
+
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
| 28 |
+
|
| 29 |
+
# initialization
|
| 30 |
+
default_init_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)
|
| 31 |
+
|
| 32 |
+
def forward(self, x):
|
| 33 |
+
x1 = self.lrelu(self.conv1(x))
|
| 34 |
+
x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
|
| 35 |
+
x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
|
| 36 |
+
x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
|
| 37 |
+
x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
|
| 38 |
+
# Emperically, we use 0.2 to scale the residual for better performance
|
| 39 |
+
return x5 * 0.2 + x
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class RRDB(nn.Module):
|
| 43 |
+
"""Residual in Residual Dense Block.
|
| 44 |
+
|
| 45 |
+
Used in RRDB-Net in ESRGAN.
|
| 46 |
+
|
| 47 |
+
Args:
|
| 48 |
+
num_feat (int): Channel number of intermediate features.
|
| 49 |
+
num_grow_ch (int): Channels for each growth.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
def __init__(self, num_feat, num_grow_ch=32):
|
| 53 |
+
super(RRDB, self).__init__()
|
| 54 |
+
self.rdb1 = ResidualDenseBlock(num_feat, num_grow_ch)
|
| 55 |
+
self.rdb2 = ResidualDenseBlock(num_feat, num_grow_ch)
|
| 56 |
+
self.rdb3 = ResidualDenseBlock(num_feat, num_grow_ch)
|
| 57 |
+
|
| 58 |
+
def forward(self, x):
|
| 59 |
+
out = self.rdb1(x)
|
| 60 |
+
out = self.rdb2(out)
|
| 61 |
+
out = self.rdb3(out)
|
| 62 |
+
# Emperically, we use 0.2 to scale the residual for better performance
|
| 63 |
+
return out * 0.2 + x
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@ARCH_REGISTRY.register()
|
| 67 |
+
class RRDBNet(nn.Module):
|
| 68 |
+
"""Networks consisting of Residual in Residual Dense Block, which is used
|
| 69 |
+
in ESRGAN.
|
| 70 |
+
|
| 71 |
+
ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks.
|
| 72 |
+
|
| 73 |
+
We extend ESRGAN for scale x2 and scale x1.
|
| 74 |
+
Note: This is one option for scale 1, scale 2 in RRDBNet.
|
| 75 |
+
We first employ the pixel-unshuffle (an inverse operation of pixelshuffle to reduce the spatial size
|
| 76 |
+
and enlarge the channel size before feeding inputs into the main ESRGAN architecture.
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
num_in_ch (int): Channel number of inputs.
|
| 80 |
+
num_out_ch (int): Channel number of outputs.
|
| 81 |
+
num_feat (int): Channel number of intermediate features.
|
| 82 |
+
Default: 64
|
| 83 |
+
num_block (int): Block number in the trunk network. Defaults: 23
|
| 84 |
+
num_grow_ch (int): Channels for each growth. Default: 32.
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
def __init__(self, num_in_ch, num_out_ch, scale=4, num_feat=64, num_block=23, num_grow_ch=32):
|
| 88 |
+
super(RRDBNet, self).__init__()
|
| 89 |
+
self.scale = scale
|
| 90 |
+
if scale == 2:
|
| 91 |
+
num_in_ch = num_in_ch * 4
|
| 92 |
+
elif scale == 1:
|
| 93 |
+
num_in_ch = num_in_ch * 16
|
| 94 |
+
self.conv_first = nn.Conv2d(num_in_ch, num_feat, 3, 1, 1)
|
| 95 |
+
self.body = make_layer(RRDB, num_block, num_feat=num_feat, num_grow_ch=num_grow_ch)
|
| 96 |
+
self.conv_body = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
| 97 |
+
# upsample
|
| 98 |
+
self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
| 99 |
+
self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
| 100 |
+
self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
| 101 |
+
self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
|
| 102 |
+
|
| 103 |
+
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
| 104 |
+
|
| 105 |
+
def forward(self, x):
|
| 106 |
+
if self.scale == 2:
|
| 107 |
+
feat = pixel_unshuffle(x, scale=2)
|
| 108 |
+
elif self.scale == 1:
|
| 109 |
+
feat = pixel_unshuffle(x, scale=4)
|
| 110 |
+
else:
|
| 111 |
+
feat = x
|
| 112 |
+
feat = self.conv_first(feat)
|
| 113 |
+
body_feat = self.conv_body(self.body(feat))
|
| 114 |
+
feat = feat + body_feat
|
| 115 |
+
# upsample
|
| 116 |
+
feat = self.lrelu(self.conv_up1(F.interpolate(feat, scale_factor=2, mode='nearest')))
|
| 117 |
+
feat = self.lrelu(self.conv_up2(F.interpolate(feat, scale_factor=2, mode='nearest')))
|
| 118 |
+
out = self.conv_last(self.lrel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|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
from torch import nn as nn
|
| 5 |
+
from torchvision.models import vgg as vgg
|
| 6 |
+
|
| 7 |
+
from basicsr.utils.registry import ARCH_REGISTRY
|
| 8 |
+
|
| 9 |
+
VGG_PRETRAIN_PATH = 'experiments/pretrained_models/vgg19-dcbb9e9d.pth'
|
| 10 |
+
NAMES = {
|
| 11 |
+
'vgg11': [
|
| 12 |
+
'conv1_1', 'relu1_1', 'pool1', 'conv2_1', 'relu2_1', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2',
|
| 13 |
+
'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2',
|
| 14 |
+
'pool5'
|
| 15 |
+
],
|
| 16 |
+
'vgg13': [
|
| 17 |
+
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
|
| 18 |
+
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'pool4',
|
| 19 |
+
'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'pool5'
|
| 20 |
+
],
|
| 21 |
+
'vgg16': [
|
| 22 |
+
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
|
| 23 |
+
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2',
|
| 24 |
+
'relu4_2', 'conv4_3', 'relu4_3', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3',
|
| 25 |
+
'pool5'
|
| 26 |
+
],
|
| 27 |
+
'vgg19': [
|
| 28 |
+
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
|
| 29 |
+
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1',
|
| 30 |
+
'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1',
|
| 31 |
+
'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4', 'pool5'
|
| 32 |
+
]
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def insert_bn(names):
|
| 37 |
+
"""Insert bn layer after each conv.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
names (list): The list of layer names.
|
| 41 |
+
|
| 42 |
+
Returns:
|
| 43 |
+
list: The list of layer names with bn layers.
|
| 44 |
+
"""
|
| 45 |
+
names_bn = []
|
| 46 |
+
for name in names:
|
| 47 |
+
names_bn.append(name)
|
| 48 |
+
if 'conv' in name:
|
| 49 |
+
position = name.replace('conv', '')
|
| 50 |
+
names_bn.append('bn' + position)
|
| 51 |
+
return names_bn
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@ARCH_REGISTRY.register()
|
| 55 |
+
class VGGFeatureExtractor(nn.Module):
|
| 56 |
+
"""VGG network for feature extraction.
|
| 57 |
+
|
| 58 |
+
In this implementation, we allow users to choose whether use normalization
|
| 59 |
+
in the input feature and the type of vgg network. Note that the pretrained
|
| 60 |
+
path must fit the vgg type.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
layer_name_list (list[str]): Forward function returns the corresponding
|
| 64 |
+
features according to the layer_name_list.
|
| 65 |
+
Example: {'relu1_1', 'relu2_1', 'relu3_1'}.
|
| 66 |
+
vgg_type (str): Set the type of vgg network. Default: 'vgg19'.
|
| 67 |
+
use_input_norm (bool): If True, normalize the input image. Importantly,
|
| 68 |
+
the input feature must in the range [0, 1]. Default: True.
|
| 69 |
+
range_norm (bool): If True, norm images with range [-1, 1] to [0, 1].
|
| 70 |
+
Default: False.
|
| 71 |
+
requires_grad (bool): If true, the parameters of VGG network will be
|
| 72 |
+
optimized. Default: False.
|
| 73 |
+
remove_pooling (bool): If true, the max pooling operations in VGG net
|
| 74 |
+
will be removed. Default: False.
|
| 75 |
+
pooling_stride (int): The stride of max pooling operation. Default: 2.
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
def __init__(self,
|
| 79 |
+
layer_name_list,
|
| 80 |
+
vgg_type='vgg19',
|
| 81 |
+
use_input_norm=True,
|
| 82 |
+
range_norm=False,
|
| 83 |
+
requires_grad=False,
|
| 84 |
+
remove_pooling=False,
|
| 85 |
+
pooling_stride=2):
|
| 86 |
+
super(VGGFeatureExtractor, self).__init__()
|
| 87 |
+
|
| 88 |
+
self.layer_name_list = layer_name_list
|
| 89 |
+
self.use_input_norm = use_input_norm
|
| 90 |
+
self.range_norm = range_norm
|
| 91 |
+
|
| 92 |
+
self.names = NAMES[vgg_type.replace('_bn', '')]
|
| 93 |
+
if 'bn' in vgg_type:
|
| 94 |
+
self.names = insert_bn(self.names)
|
| 95 |
+
|
| 96 |
+
# only borrow layers that will be used to avoid unused params
|
| 97 |
+
max_idx = 0
|
| 98 |
+
for v in layer_name_list:
|
| 99 |
+
idx = self.names.index(v)
|
| 100 |
+
if idx > max_idx:
|
| 101 |
+
max_idx = idx
|
| 102 |
+
|
| 103 |
+
if os.path.exists(VGG_PRETRAIN_PATH):
|
| 104 |
+
vgg_net = getattr(vgg, vgg_type)(pretrained=False)
|
| 105 |
+
state_dict = torch.load(VGG_PRETRAIN_PATH, map_location=lambda storage, loc: storage)
|
| 106 |
+
vgg_net.load_state_dict(state_dict)
|
| 107 |
+
else:
|
| 108 |
+
vgg_net = getattr(vgg, vgg_type)(pretrained=True)
|
| 109 |
+
|
| 110 |
+
features = vgg_net.features[:max_idx + 1]
|
| 111 |
+
|
| 112 |
+
modified_net = OrderedDict()
|
| 113 |
+
for k, v in zip(self.names, features):
|
| 114 |
+
if 'pool' in k:
|
| 115 |
+
# if remove_pooling is true, pooling operation will be removed
|
| 116 |
+
if remove_pooling:
|
| 117 |
+
continue
|
| 118 |
+
else:
|
| 119 |
+
# in some cases, we may want to change the default stride
|
| 120 |
+
modified_net[k] = nn.MaxPool2d(kernel_size=2, stride=pooling_stride)
|
| 121 |
+
else:
|
| 122 |
+
modified_net[k] = v
|
| 123 |
+
|
| 124 |
+
self.vgg_net = nn.Sequential(modified_net)
|
| 125 |
+
|
| 126 |
+
if not requires_grad:
|
| 127 |
+
self.vgg_net.eval()
|
| 128 |
+
for param in self.parameters():
|
| 129 |
+
param.requires_grad = False
|
| 130 |
+
else:
|
| 131 |
+
self.vgg_net.train()
|
| 132 |
+
for param in self.parameters():
|
| 133 |
+
param.requires_grad = True
|
| 134 |
+
|
| 135 |
+
if self.use_input_norm:
|
| 136 |
+
# the mean is for image with range [0, 1]
|
| 137 |
+
self.register_buffer('mean', torch.Tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1))
|
| 138 |
+
# the std is for image with range [0, 1]
|
| 139 |
+
self.register_buffer('std', torch.Tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1))
|
| 140 |
+
|
| 141 |
+
def forward(self, x):
|
| 142 |
+
"""Forward function.
|
| 143 |
+
|
| 144 |
+
Args:
|
| 145 |
+
x (Tensor): Input tensor with shape (n, c, h, w).
|
| 146 |
+
|
| 147 |
+
Returns:
|
| 148 |
+
Tensor: Forward results.
|
| 149 |
+
"""
|
| 150 |
+
if self.range_norm:
|
| 151 |
+
x = (x + 1) / 2
|
| 152 |
+
if self.use_input_norm:
|
| 153 |
+
x = (x - self.mean) / self.std
|
| 154 |
+
output = {}
|
| 155 |
+
|
| 156 |
+
for key, layer in self.vgg_net._modules.items():
|
| 157 |
+
x = layer(x)
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
VQGAN code, adapted from the original created by the Unleashing Transformers authors:
|
| 3 |
+
https://github.com/samb-t/unleashing-transformers/blob/master/models/vqgan.py
|
| 4 |
+
|
| 5 |
+
'''
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
import copy
|
| 11 |
+
from basicsr.utils import get_root_logger
|
| 12 |
+
from basicsr.utils.registry import ARCH_REGISTRY
|
| 13 |
+
|
| 14 |
+
def normalize(in_channels):
|
| 15 |
+
return torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@torch.jit.script
|
| 19 |
+
def swish(x):
|
| 20 |
+
return x*torch.sigmoid(x)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Define VQVAE classes
|
| 24 |
+
class VectorQuantizer(nn.Module):
|
| 25 |
+
def __init__(self, codebook_size, emb_dim, beta):
|
| 26 |
+
super(VectorQuantizer, self).__init__()
|
| 27 |
+
self.codebook_size = codebook_size # number of embeddings
|
| 28 |
+
self.emb_dim = emb_dim # dimension of embedding
|
| 29 |
+
self.beta = beta # commitment cost used in loss term, beta * ||z_e(x)-sg[e]||^2
|
| 30 |
+
self.embedding = nn.Embedding(self.codebook_size, self.emb_dim)
|
| 31 |
+
self.embedding.weight.data.uniform_(-1.0 / self.codebook_size, 1.0 / self.codebook_size)
|
| 32 |
+
|
| 33 |
+
def forward(self, z):
|
| 34 |
+
# reshape z -> (batch, height, width, channel) and flatten
|
| 35 |
+
z = z.permute(0, 2, 3, 1).contiguous()
|
| 36 |
+
z_flattened = z.view(-1, self.emb_dim)
|
| 37 |
+
|
| 38 |
+
# distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
|
| 39 |
+
d = (z_flattened ** 2).sum(dim=1, keepdim=True) + (self.embedding.weight**2).sum(1) - \
|
| 40 |
+
2 * torch.matmul(z_flattened, self.embedding.weight.t())
|
| 41 |
+
|
| 42 |
+
mean_distance = torch.mean(d)
|
| 43 |
+
# find closest encodings
|
| 44 |
+
min_encoding_indices = torch.argmin(d, dim=1).unsqueeze(1)
|
| 45 |
+
# min_encoding_scores, min_encoding_indices = torch.topk(d, 1, dim=1, largest=False)
|
| 46 |
+
# [0-1], higher score, higher confidence
|
| 47 |
+
# min_encoding_scores = torch.exp(-min_encoding_scores/10)
|
| 48 |
+
|
| 49 |
+
min_encodings = torch.zeros(min_encoding_indices.shape[0], self.codebook_size).to(z)
|
| 50 |
+
min_encodings.scatter_(1, min_encoding_indices, 1)
|
| 51 |
+
|
| 52 |
+
# get quantized latent vectors
|
| 53 |
+
z_q = torch.matmul(min_encodings, self.embedding.weight).view(z.shape)
|
| 54 |
+
# compute loss for embedding
|
| 55 |
+
loss = torch.mean((z_q.detach()-z)**2) + self.beta * torch.mean((z_q - z.detach()) ** 2)
|
| 56 |
+
# preserve gradients
|
| 57 |
+
z_q = z + (z_q - z).detach()
|
| 58 |
+
|
| 59 |
+
# perplexity
|
| 60 |
+
e_mean = torch.mean(min_encodings, dim=0)
|
| 61 |
+
perplexity = torch.exp(-torch.sum(e_mean * torch.log(e_mean + 1e-10)))
|
| 62 |
+
# reshape back to match original input shape
|
| 63 |
+
z_q = z_q.permute(0, 3, 1, 2).contiguous()
|
| 64 |
+
|
| 65 |
+
return z_q, loss, {
|
| 66 |
+
"perplexity": perplexity,
|
| 67 |
+
"min_encodings": min_encodings,
|
| 68 |
+
"min_encoding_indices": min_encoding_indices,
|
| 69 |
+
"mean_distance": mean_distance
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
def get_codebook_feat(self, indices, shape):
|
| 73 |
+
# input indices: batch*token_num -> (batch*token_num)*1
|
| 74 |
+
# shape: batch, height, width, channel
|
| 75 |
+
indices = indices.view(-1,1)
|
| 76 |
+
min_encodings = torch.zeros(indices.shape[0], self.codebook_size).to(indices)
|
| 77 |
+
min_encodings.scatter_(1, indices, 1)
|
| 78 |
+
# get quantized latent vectors
|
| 79 |
+
z_q = torch.matmul(min_encodings.float(), self.embedding.weight)
|
| 80 |
+
|
| 81 |
+
if shape is not None: # reshape back to match original input shape
|
| 82 |
+
z_q = z_q.view(shape).permute(0, 3, 1, 2).contiguous()
|
| 83 |
+
|
| 84 |
+
return z_q
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class GumbelQuantizer(nn.Module):
|
| 88 |
+
def __init__(self, codebook_size, emb_dim, num_hiddens, straight_through=False, kl_weight=5e-4, temp_init=1.0):
|
| 89 |
+
super().__init__()
|
| 90 |
+
self.codebook_size = codebook_size # number of embeddings
|
| 91 |
+
self.emb_dim = emb_dim # dimension of embedding
|
| 92 |
+
self.straight_through = straight_through
|
| 93 |
+
self.temperature = temp_init
|
| 94 |
+
self.kl_weight = kl_weight
|
| 95 |
+
self.proj = nn.Conv2d(num_hiddens, codebook_size, 1) # projects last encoder layer to quantized logits
|
| 96 |
+
self.embed = nn.Embedding(codebook_size, emb_dim)
|
| 97 |
+
|
| 98 |
+
def forward(self, z):
|
| 99 |
+
hard = self.straight_through if self.training else True
|
| 100 |
+
|
| 101 |
+
logits = self.proj(z)
|
| 102 |
+
|
| 103 |
+
soft_one_hot = F.gumbel_softmax(logits, tau=self.temperature, dim=1, hard=hard)
|
| 104 |
+
|
| 105 |
+
z_q = torch.einsum("b n h w, n d -> b d h w", soft_one_hot, self.embed.weight)
|
| 106 |
+
|
| 107 |
+
# + kl divergence to the prior loss
|
| 108 |
+
qy = F.softmax(logits, dim=1)
|
| 109 |
+
diff = self.kl_weight * torch.sum(qy * torch.log(qy * self.codebook_size + 1e-10), dim=1).mean()
|
| 110 |
+
min_encoding_indices = soft_one_hot.argmax(dim=1)
|
| 111 |
+
|
| 112 |
+
return z_q, diff, {
|
| 113 |
+
"min_encoding_indices": min_encoding_indices
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class Downsample(nn.Module):
|
| 118 |
+
def __init__(self, in_channels):
|
| 119 |
+
super().__init__()
|
| 120 |
+
self.conv = torch.nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=0)
|
| 121 |
+
|
| 122 |
+
def forward(self, x):
|
| 123 |
+
pad = (0, 1, 0, 1)
|
| 124 |
+
x = torch.nn.functional.pad(x, pad, mode="constant", value=0)
|
| 125 |
+
x = self.conv(x)
|
| 126 |
+
return x
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
class Upsample(nn.Module):
|
| 130 |
+
def __init__(self, in_channels):
|
| 131 |
+
super().__init__()
|
| 132 |
+
self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1)
|
| 133 |
+
|
| 134 |
+
def forward(self, x):
|
| 135 |
+
x = F.interpolate(x, scale_factor=2.0, mode="nearest")
|
| 136 |
+
x = self.conv(x)
|
| 137 |
+
|
| 138 |
+
return x
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class ResBlock(nn.Module):
|
| 142 |
+
def __init__(self, in_channels, out_channels=None):
|
| 143 |
+
super(ResBlock, self).__init__()
|
| 144 |
+
self.in_channels = in_channels
|
| 145 |
+
self.out_channels = in_channels if out_channels is None else out_channels
|
| 146 |
+
self.norm1 = normalize(in_channels)
|
| 147 |
+
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
|
| 148 |
+
self.norm2 = normalize(out_channels)
|
| 149 |
+
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
|
| 150 |
+
if self.in_channels != self.out_channels:
|
| 151 |
+
self.conv_out = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
|
| 152 |
+
|
| 153 |
+
def forward(self, x_in):
|
| 154 |
+
x = x_in
|
| 155 |
+
x = self.norm1(x)
|
| 156 |
+
x = swish(x)
|
| 157 |
+
x = self.conv1(x)
|
| 158 |
+
x = self.norm2(x)
|
| 159 |
+
x = swish(x)
|
| 160 |
+
x = self.conv2(x)
|
| 161 |
+
if self.in_channels != self.out_channels:
|
| 162 |
+
x_in = self.conv_out(x_in)
|
| 163 |
+
|
| 164 |
+
return x + x_in
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
class AttnBlock(nn.Module):
|
| 168 |
+
def __init__(self, in_channels):
|
| 169 |
+
super().__init__()
|
| 170 |
+
self.in_channels = in_channels
|
| 171 |
+
|
| 172 |
+
self.norm = normalize(in_channels)
|
| 173 |
+
self.q = torch.nn.Conv2d(
|
| 174 |
+
in_channels,
|
| 175 |
+
in_channels,
|
| 176 |
+
kernel_size=1,
|
| 177 |
+
stride=1,
|
| 178 |
+
padding=0
|
| 179 |
+
)
|
| 180 |
+
self.k = torch.nn.Conv2d(
|
| 181 |
+
in_channels,
|
| 182 |
+
in_channels,
|
| 183 |
+
kernel_size=1,
|
| 184 |
+
stride=1,
|
| 185 |
+
padding=0
|
| 186 |
+
)
|
| 187 |
+
self.v = torch.nn.Conv2d(
|
| 188 |
+
in_channels,
|
| 189 |
+
in_channels,
|
| 190 |
+
kernel_size=1,
|
| 191 |
+
stride=1,
|
| 192 |
+
padding=0
|
| 193 |
+
)
|
| 194 |
+
self.proj_out = torch.nn.Conv2d(
|
| 195 |
+
in_channels,
|
| 196 |
+
in_channels,
|
| 197 |
+
kernel_size=1,
|
| 198 |
+
stride=1,
|
| 199 |
+
padding=0
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
def forward(self, x):
|
| 203 |
+
h_ = x
|
| 204 |
+
h_ = self.norm(h_)
|
| 205 |
+
q = self.q(h_)
|
| 206 |
+
k = self.k(h_)
|
| 207 |
+
v = self.v(h_)
|
| 208 |
+
|
| 209 |
+
# compute attention
|
| 210 |
+
b, c, h, w = q.shape
|
| 211 |
+
q = q.reshape(b, c, h*w)
|
| 212 |
+
q = q.permute(0, 2, 1)
|
| 213 |
+
k = k.reshape(b, c, h*w)
|
| 214 |
+
w_ = torch.bmm(q, k)
|
| 215 |
+
w_ = w_ * (int(c)**(-0.5))
|
| 216 |
+
w_ = F.softmax(w_, dim=2)
|
| 217 |
+
|
| 218 |
+
# attend to values
|
| 219 |
+
v = v.reshape(b, c, h*w)
|
| 220 |
+
w_ = w_.permute(0, 2, 1)
|
| 221 |
+
h_ = torch.bmm(v, w_)
|
| 222 |
+
h_ = h_.reshape(b, c, h, w)
|
| 223 |
+
|
| 224 |
+
h_ = self.proj_out(h_)
|
| 225 |
+
|
| 226 |
+
return x+h_
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
class Encoder(nn.Module):
|
| 230 |
+
def __init__(self, in_channels, nf, emb_dim, ch_mult, num_res_blocks, resolution, attn_resolutions):
|
| 231 |
+
super().__init__()
|
| 232 |
+
self.nf = nf
|
| 233 |
+
self.num_resolutions = len(ch_mult)
|
| 234 |
+
self.num_res_blocks = num_res_blocks
|
| 235 |
+
self.resolution = resolution
|
| 236 |
+
self.attn_resolutions = attn_resolutions
|
| 237 |
+
|
| 238 |
+
curr_res = self.resolution
|
| 239 |
+
in_ch_mult = (1,)+tuple(ch_mult)
|
| 240 |
+
|
| 241 |
+
blocks = []
|
| 242 |
+
# initial convultion
|
| 243 |
+
blocks.append(nn.Conv2d(in_channels, nf, kernel_size=3, stride=1, padding=1))
|
| 244 |
+
|
| 245 |
+
# residual and downsampling blocks, with attention on smaller res (16x16)
|
| 246 |
+
for i in range(self.num_resolutions):
|
| 247 |
+
block_in_ch = nf * in_ch_mult[i]
|
| 248 |
+
block_out_ch = nf * ch_mult[i]
|
| 249 |
+
for _ in range(self.num_res_blocks):
|
| 250 |
+
blocks.append(ResBlock(block_in_ch, block_out_ch))
|
| 251 |
+
block_in_ch = block_out_ch
|
| 252 |
+
if curr_res in attn_resolutions:
|
| 253 |
+
blocks.append(AttnBlock(block_in_ch))
|
| 254 |
+
|
| 255 |
+
if i != self.num_resolutions - 1:
|
| 256 |
+
blocks.append(Downsample(block_in_ch))
|
| 257 |
+
curr_res = curr_res // 2
|
| 258 |
+
|
| 259 |
+
# non-local attention block
|
| 260 |
+
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
| 261 |
+
blocks.append(AttnBlock(block_in_ch))
|
| 262 |
+
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
| 263 |
+
|
| 264 |
+
# normalise and convert to latent size
|
| 265 |
+
blocks.append(normalize(block_in_ch))
|
| 266 |
+
blocks.append(nn.Conv2d(block_in_ch, emb_dim, kernel_size=3, stride=1, padding=1))
|
| 267 |
+
self.blocks = nn.ModuleList(blocks)
|
| 268 |
+
|
| 269 |
+
def forward(self, x):
|
| 270 |
+
for block in self.blocks:
|
| 271 |
+
x = block(x)
|
| 272 |
+
|
| 273 |
+
return x
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
class Generator(nn.Module):
|
| 277 |
+
def __init__(self, nf, emb_dim, ch_mult, res_blocks, img_size, attn_resolutions):
|
| 278 |
+
super().__init__()
|
| 279 |
+
self.nf = nf
|
| 280 |
+
self.ch_mult = ch_mult
|
| 281 |
+
self.num_resolutions = len(self.ch_mult)
|
| 282 |
+
self.num_res_blocks = res_blocks
|
| 283 |
+
self.resolution = img_size
|
| 284 |
+
self.attn_resolutions = attn_resolutions
|
| 285 |
+
self.in_channels = emb_dim
|
| 286 |
+
self.out_channels = 3
|
| 287 |
+
block_in_ch = self.nf * self.ch_mult[-1]
|
| 288 |
+
curr_res = self.resolution // 2 ** (self.num_resolutions-1)
|
| 289 |
+
|
| 290 |
+
blocks = []
|
| 291 |
+
# initial conv
|
| 292 |
+
blocks.append(nn.Conv2d(self.in_channels, block_in_ch, kernel_size=3, stride=1, padding=1))
|
| 293 |
+
|
| 294 |
+
# non-local attention block
|
| 295 |
+
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
| 296 |
+
blocks.append(AttnBlock(block_in_ch))
|
| 297 |
+
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
| 298 |
+
|
| 299 |
+
for i in reversed(range(self.num_resolutions)):
|
| 300 |
+
block_out_ch = self.nf * self.ch_mult[i]
|
| 301 |
+
|
| 302 |
+
for _ in range(self.num_res_blocks):
|
| 303 |
+
blocks.append(ResBlock(block_in_ch, block_out_ch))
|
| 304 |
+
block_in_ch = block_out_ch
|
| 305 |
+
|
| 306 |
+
if curr_res in self.attn_resolutions:
|
| 307 |
+
blocks.append(AttnBlock(block_in_ch))
|
| 308 |
+
|
| 309 |
+
if i != 0:
|
| 310 |
+
blocks.append(Upsample(block_in_ch))
|
| 311 |
+
curr_res = curr_res * 2
|
| 312 |
+
|
| 313 |
+
blocks.append(normalize(block_in_ch))
|
| 314 |
+
blocks.append(nn.Conv2d(block_in_ch, self.out_channels, kernel_size=3, stride=1, padding=1))
|
| 315 |
+
|
| 316 |
+
self.blocks = nn.ModuleList(blocks)
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
def forward(self, x):
|
| 320 |
+
for block in self.blocks:
|
| 321 |
+
x = block(x)
|
| 322 |
+
|
| 323 |
+
return x
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
@ARCH_REGISTRY.register()
|
| 327 |
+
class VQAutoEncoder(nn.Module):
|
| 328 |
+
def __init__(self, img_size, nf, ch_mult, quantizer="nearest", res_blocks=2, attn_resolutions=[16], codebook_size=1024, emb_dim=256,
|
| 329 |
+
beta=0.25, gumbel_straight_through=False, gumbel_kl_weight=1e-8, model_path=None):
|
| 330 |
+
super().__init__()
|
| 331 |
+
logger = get_root_logger()
|
| 332 |
+
self.in_channels = 3
|
| 333 |
+
self.nf = nf
|
| 334 |
+
self.n_blocks = res_blocks
|
| 335 |
+
self.codebook_size = codebook_size
|
| 336 |
+
self.embed_dim = emb_dim
|
| 337 |
+
self.ch_mult = ch_mult
|
| 338 |
+
self.resolution = img_size
|
| 339 |
+
self.attn_resolutions = attn_resolutions
|
| 340 |
+
self.quantizer_type = quantizer
|
| 341 |
+
self.encoder = Encoder(
|
| 342 |
+
self.in_channels,
|
| 343 |
+
self.nf,
|
| 344 |
+
self.embed_dim,
|
| 345 |
+
self.ch_mult,
|
| 346 |
+
self.n_blocks,
|
| 347 |
+
self.resolution,
|
| 348 |
+
self.attn_resolutions
|
| 349 |
+
)
|
| 350 |
+
if self.quantizer_type == "nearest":
|
| 351 |
+
self.beta = beta #0.25
|
| 352 |
+
self.quantize = VectorQuantizer(self.codebook_size, self.embed_dim, self.beta)
|
| 353 |
+
elif self.quantizer_type == "gumbel":
|
| 354 |
+
self.gumbel_num_hiddens = emb_dim
|
| 355 |
+
self.straight_through = gumbel_straight_through
|
| 356 |
+
self.kl_weight = gumbel_kl_weight
|
| 357 |
+
self.quantize = GumbelQuantizer(
|
| 358 |
+
self.codebook_size,
|
| 359 |
+
self.embed_dim,
|
| 360 |
+
self.gumbel_num_hiddens,
|
| 361 |
+
self.straight_through,
|
| 362 |
+
self.kl_weight
|
| 363 |
+
)
|
| 364 |
+
self.generator = Generator(
|
| 365 |
+
self.nf,
|
| 366 |
+
self.embed_dim,
|
| 367 |
+
self.ch_mult,
|
| 368 |
+
self.n_blocks,
|
| 369 |
+
self.resolution,
|
| 370 |
+
self.attn_resolutions
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
if model_path is not None:
|
| 374 |
+
chkpt = torch.load(model_path, map_location='cpu')
|
| 375 |
+
if 'params_ema' in chkpt:
|
| 376 |
+
self.load_state_dict(torch.load(model_path, map_location='cpu')['params_ema'])
|
| 377 |
+
logger.info(f'vqgan is loaded from: {model_path} [params_ema]')
|
| 378 |
+
elif 'params' in chkpt:
|
| 379 |
+
self.load_state_dict(torch.load(model_path, map_location='cpu')['params'])
|
| 380 |
+
logger.info(f'vqgan is loaded from: {model_path} [params]')
|
| 381 |
+
else:
|
| 382 |
+
raise ValueError(f'Wrong params!')
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
def forward(self, x):
|
| 386 |
+
x = self.encoder(x)
|
| 387 |
+
quant, codebook_loss, quant_stats = self.quantize(x)
|
| 388 |
+
x = self.generator(quant)
|
| 389 |
+
return x, codebook_loss, quant_stats
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
# patch based discriminator
|
| 394 |
+
@ARCH_REGISTRY.register()
|
| 395 |
+
class VQGANDiscriminator(nn.Module):
|
| 396 |
+
def __init__(self, nc=3, ndf=64, n_layers=4, model_path=None):
|
| 397 |
+
super().__init__()
|
| 398 |
+
|
| 399 |
+
layers = [nn.Conv2d(nc, ndf, kernel_size=4, stride=2, padding=1), nn.LeakyReLU(0.2, True)]
|
| 400 |
+
ndf_mult = 1
|
| 401 |
+
ndf_mult_prev = 1
|
| 402 |
+
for n in range(1, n_layers): # gradually increase the number of filters
|
| 403 |
+
ndf_mult_prev = ndf_mult
|
| 404 |
+
ndf_mult = min(2 ** n, 8)
|
| 405 |
+
layers += [
|
| 406 |
+
nn.Conv2d(ndf * ndf_mult_prev, ndf * ndf_mult, kernel_size=4, stride=2, padding=1, bias=False),
|
| 407 |
+
nn.BatchNorm2d(ndf * ndf_mult),
|
| 408 |
+
nn.LeakyReLU(0.2, True)
|
| 409 |
+
]
|
| 410 |
+
|
| 411 |
+
ndf_mult_prev = ndf_mult
|
| 412 |
+
ndf_mult = min(2 ** n_layers, 8)
|
| 413 |
+
|
| 414 |
+
layers += [
|
| 415 |
+
nn.Conv2d(ndf * ndf_mult_prev, ndf * ndf_mult, kernel_size=4, stride=1, padding=1, bias=False),
|
| 416 |
+
nn.BatchNorm2d(ndf * ndf_mult),
|
| 417 |
+
nn.LeakyReLU(0.2, True)
|
| 418 |
+
]
|
| 419 |
+
|
| 420 |
+
layers += [
|
| 421 |
+
nn.Conv2d(ndf * ndf_mult, 1, kernel_size=4, stride=1, padding=1)] # output 1 channel prediction map
|
| 422 |
+
self.main = nn.Sequential(*layers)
|
| 423 |
+
|
| 424 |
+
if model_path is not None:
|
| 425 |
+
chkpt = torch.load(model_path, map_location='cpu')
|
| 426 |
+
if 'params_d' in chkpt:
|
| 427 |
+
self.load_state_dict(torch.load(model_path, map_location='cpu')['params_d'])
|
| 428 |
+
elif 'params' in chkpt:
|
| 429 |
+
self.load_state_dict(torch.load(model_path, map_location='cpu')['params'])
|
| 430 |
+
else:
|
| 431 |
+
raise ValueError(f'Wrong params!')
|
| 432 |
+
|
| 433 |
+
def forward(self, x):
|
| 434 |
+
return self.main(x)
|