Spaces:
Runtime error
Runtime error
File size: 4,717 Bytes
a856109 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvLayer(nn.Module):
def __init__(
self,
in_channels,
out_channels,
kernel_size,
stride,
dilation=1,
bias=True,
groups=1,
norm="in",
nonlinear="relu",
):
super(ConvLayer, self).__init__()
reflection_padding = (kernel_size + (dilation - 1) * (kernel_size - 1)) // 2
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
self.conv2d = nn.Conv2d(
in_channels,
out_channels,
kernel_size,
stride,
groups=groups,
bias=bias,
dilation=dilation,
)
self.norm = norm
self.nonlinear = nonlinear
if norm == "bn":
self.normalization = nn.BatchNorm2d(out_channels)
elif norm == "in":
self.normalization = nn.InstanceNorm2d(out_channels, affine=False)
else:
self.normalization = None
if nonlinear == "relu":
self.activation = nn.ReLU(inplace=True)
elif nonlinear == "leakyrelu":
self.activation = nn.LeakyReLU(0.2)
elif nonlinear == "PReLU":
self.activation = nn.PReLU()
else:
self.activation = None
def forward(self, x):
out = self.conv2d(self.reflection_pad(x))
if self.normalization is not None:
out = self.normalization(out)
if self.activation is not None:
out = self.activation(out)
return out
class Aggreation(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3):
super(Aggreation, self).__init__()
self.attention = SelfAttention(in_channels, k=8, nonlinear="relu")
self.conv = ConvLayer(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=1,
dilation=1,
nonlinear="leakyrelu",
norm=None,
)
def forward(self, x):
return self.conv(self.attention(x))
class SelfAttention(nn.Module):
def __init__(self, channels, k, nonlinear="relu"):
super(SelfAttention, self).__init__()
self.channels = channels
self.k = k
self.nonlinear = nonlinear
self.linear1 = nn.Linear(channels, channels // k)
self.linear2 = nn.Linear(channels // k, channels)
self.global_pooling = nn.AdaptiveAvgPool2d((1, 1))
if nonlinear == "relu":
self.activation = nn.ReLU(inplace=True)
elif nonlinear == "leakyrelu":
self.activation = nn.LeakyReLU(0.2)
elif nonlinear == "PReLU":
self.activation = nn.PReLU()
else:
raise ValueError
def attention(self, x):
N, C, H, W = x.size()
out = torch.flatten(self.global_pooling(x), 1)
out = self.activation(self.linear1(out))
out = torch.sigmoid(self.linear2(out)).view(N, C, 1, 1)
return out.mul(x)
def forward(self, x):
return self.attention(x)
class SPP(nn.Module):
def __init__(
self, in_channels, out_channels, num_layers=4, interpolation_type="bilinear"
):
super(SPP, self).__init__()
self.conv = nn.ModuleList()
self.num_layers = num_layers
self.interpolation_type = interpolation_type
for _ in range(self.num_layers):
self.conv.append(
ConvLayer(
in_channels,
in_channels,
kernel_size=1,
stride=1,
dilation=1,
nonlinear="leakyrelu",
norm=None,
)
)
self.fusion = ConvLayer(
(in_channels * (self.num_layers + 1)),
out_channels,
kernel_size=3,
stride=1,
norm="False",
nonlinear="leakyrelu",
)
def forward(self, x):
N, C, H, W = x.size()
out = []
for level in range(self.num_layers):
out.append(
F.interpolate(
self.conv[level](
F.avg_pool2d(
x,
kernel_size=2 * 2 ** (level + 1),
stride=2 * 2 ** (level + 1),
padding=2 * 2 ** (level + 1) % 2,
)
),
size=(H, W),
mode=self.interpolation_type,
)
)
out.append(x)
return self.fusion(torch.cat(out, dim=1))
|