Spaces:
Running
Running
File size: 4,922 Bytes
f2fa83b |
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 |
import torch.nn as nn
import torch.nn.functional as F
from utils.common import initialize_weights
from .layers import LayerNorm2d, get_norm
class DownConv(nn.Module):
def __init__(self, channels, bias=False):
super(DownConv, self).__init__()
self.conv1 = SeparableConv2D(channels, channels, stride=2, bias=bias)
self.conv2 = SeparableConv2D(channels, channels, stride=1, bias=bias)
def forward(self, x):
out1 = self.conv1(x)
out2 = F.interpolate(x, scale_factor=0.5, mode='bilinear')
out2 = self.conv2(out2)
return out1 + out2
class UpConv(nn.Module):
def __init__(self, channels, bias=False):
super(UpConv, self).__init__()
self.conv = SeparableConv2D(channels, channels, stride=1, bias=bias)
def forward(self, x):
out = F.interpolate(x, scale_factor=2.0, mode='bilinear')
out = self.conv(out)
return out
class UpConvLNormLReLU(nn.Module):
"""Upsample Conv block with Layer Norm and Leaky ReLU"""
def __init__(self, in_channels, out_channels, norm_type="instance", bias=False):
super(UpConvLNormLReLU, self).__init__()
self.conv_block = ConvBlock(
in_channels,
out_channels,
kernel_size=3,
norm_type=norm_type,
bias=bias,
)
def forward(self, x):
out = F.interpolate(x, scale_factor=2.0, mode='bilinear')
out = self.conv_block(out)
return out
class SeparableConv2D(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, bias=False):
super(SeparableConv2D, self).__init__()
self.depthwise = nn.Conv2d(in_channels, in_channels, kernel_size=3,
stride=stride, padding=1, groups=in_channels, bias=bias)
self.pointwise = nn.Conv2d(in_channels, out_channels,
kernel_size=1, stride=1, bias=bias)
# self.pad =
self.ins_norm1 = nn.InstanceNorm2d(in_channels)
self.activation1 = nn.LeakyReLU(0.2, True)
self.ins_norm2 = nn.InstanceNorm2d(out_channels)
self.activation2 = nn.LeakyReLU(0.2, True)
initialize_weights(self)
def forward(self, x):
out = self.depthwise(x)
out = self.ins_norm1(out)
out = self.activation1(out)
out = self.pointwise(out)
out = self.ins_norm2(out)
return self.activation2(out)
class ConvBlock(nn.Module):
"""Stack of Conv2D + Norm + LeakyReLU"""
def __init__(
self,
channels,
out_channels,
kernel_size=3,
stride=1,
groups=1,
padding=1,
bias=False,
norm_type="instance"
):
super(ConvBlock, self).__init__()
# if kernel_size == 3 and stride == 1:
# self.pad = nn.ReflectionPad2d((1, 1, 1, 1))
# elif kernel_size == 7 and stride == 1:
# self.pad = nn.ReflectionPad2d((3, 3, 3, 3))
# elif stride == 2:
# self.pad = nn.ReflectionPad2d((0, 1, 1, 0))
# else:
# self.pad = None
self.pad = nn.ReflectionPad2d(padding)
self.conv = nn.Conv2d(
channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
groups=groups,
padding=0,
bias=bias
)
self.ins_norm = get_norm(norm_type, out_channels)
self.activation = nn.LeakyReLU(0.2, True)
# initialize_weights(self)
def forward(self, x):
if self.pad is not None:
x = self.pad(x)
out = self.conv(x)
out = self.ins_norm(out)
out = self.activation(out)
return out
class InvertedResBlock(nn.Module):
def __init__(
self,
channels=256,
out_channels=256,
expand_ratio=2,
norm_type="instance",
):
super(InvertedResBlock, self).__init__()
bottleneck_dim = round(expand_ratio * channels)
self.conv_block = ConvBlock(
channels,
bottleneck_dim,
kernel_size=1,
padding=0,
norm_type=norm_type,
bias=False
)
self.conv_block2 = ConvBlock(
bottleneck_dim,
bottleneck_dim,
groups=bottleneck_dim,
norm_type=norm_type,
bias=True
)
self.conv = nn.Conv2d(
bottleneck_dim,
out_channels,
kernel_size=1,
padding=0,
bias=False
)
self.norm = get_norm(norm_type, out_channels)
def forward(self, x):
out = self.conv_block(x)
out = self.conv_block2(out)
# out = self.activation(out)
out = self.conv(out)
out = self.norm(out)
if out.shape[1] != x.shape[1]:
# Only concate if same shape
return out
return out + x
|