Spaces:
Runtime error
Runtime error
File size: 8,793 Bytes
ce190ee |
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 |
"""
https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/modeling/backbone/resnet.py
"""
from pathlib import Path
import torch
import torch.nn as nn
import torch.nn.functional as F
from climategan.deeplab.mobilenet_v3 import SeparableConv2d
from climategan.utils import find_target_size
class _DeepLabHead(nn.Module):
def __init__(
self, nclass, c1_channels=256, c4_channels=2048, norm_layer=nn.BatchNorm2d
):
super().__init__()
last_channels = c4_channels
# self.c1_block = _ConvBNReLU(c1_channels, 48, 1, norm_layer=norm_layer)
# last_channels += 48
self.block = nn.Sequential(
SeparableConv2d(
last_channels, 256, 3, norm_layer=norm_layer, relu_first=False
),
SeparableConv2d(256, 256, 3, norm_layer=norm_layer, relu_first=False),
nn.Conv2d(256, nclass, 1),
)
def forward(self, x, c1=None):
return self.block(x)
class ConvBNReLU(nn.Module):
"""
https://github.com/CoinCheung/DeepLab-v3-plus-cityscapes/blob/master/models/deeplabv3plus.py
"""
def __init__(
self, in_chan, out_chan, ks=3, stride=1, padding=1, dilation=1, *args, **kwargs
):
super().__init__()
self.conv = nn.Conv2d(
in_chan,
out_chan,
kernel_size=ks,
stride=stride,
padding=padding,
dilation=dilation,
bias=True,
)
self.bn = nn.BatchNorm2d(out_chan)
self.init_weight()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return x
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if ly.bias is not None:
nn.init.constant_(ly.bias, 0)
class ASPPv3Plus(nn.Module):
"""
https://github.com/CoinCheung/DeepLab-v3-plus-cityscapes/blob/master/models/deeplabv3plus.py
"""
def __init__(self, backbone, no_init):
super().__init__()
if backbone == "mobilenet":
in_chan = 320
else:
in_chan = 2048
self.with_gp = False
self.conv1 = ConvBNReLU(in_chan, 256, ks=1, dilation=1, padding=0)
self.conv2 = ConvBNReLU(in_chan, 256, ks=3, dilation=6, padding=6)
self.conv3 = ConvBNReLU(in_chan, 256, ks=3, dilation=12, padding=12)
self.conv4 = ConvBNReLU(in_chan, 256, ks=3, dilation=18, padding=18)
if self.with_gp:
self.avg = nn.AdaptiveAvgPool2d((1, 1))
self.conv1x1 = ConvBNReLU(in_chan, 256, ks=1)
self.conv_out = ConvBNReLU(256 * 5, 256, ks=1)
else:
self.conv_out = ConvBNReLU(256 * 4, 256, ks=1)
if not no_init:
self.init_weight()
def forward(self, x):
H, W = x.size()[2:]
feat1 = self.conv1(x)
feat2 = self.conv2(x)
feat3 = self.conv3(x)
feat4 = self.conv4(x)
if self.with_gp:
avg = self.avg(x)
feat5 = self.conv1x1(avg)
feat5 = F.interpolate(feat5, (H, W), mode="bilinear", align_corners=True)
feat = torch.cat([feat1, feat2, feat3, feat4, feat5], 1)
else:
feat = torch.cat([feat1, feat2, feat3, feat4], 1)
feat = self.conv_out(feat)
return feat
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if ly.bias is not None:
nn.init.constant_(ly.bias, 0)
class Decoder(nn.Module):
"""
https://github.com/CoinCheung/DeepLab-v3-plus-cityscapes/blob/master/models/deeplabv3plus.py
"""
def __init__(self, n_classes):
super(Decoder, self).__init__()
self.conv_low = ConvBNReLU(256, 48, ks=1, padding=0)
self.conv_cat = nn.Sequential(
ConvBNReLU(304, 256, ks=3, padding=1),
ConvBNReLU(256, 256, ks=3, padding=1),
)
self.conv_out = nn.Conv2d(256, n_classes, kernel_size=1, bias=False)
def forward(self, feat_low, feat_aspp):
H, W = feat_low.size()[2:]
feat_low = self.conv_low(feat_low)
feat_aspp_up = F.interpolate(
feat_aspp, (H, W), mode="bilinear", align_corners=True
)
feat_cat = torch.cat([feat_low, feat_aspp_up], dim=1)
feat_out = self.conv_cat(feat_cat)
logits = self.conv_out(feat_out)
return logits
"""
https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/modeling/deeplab.py
"""
class DeepLabV3Decoder(nn.Module):
def __init__(
self,
opts,
no_init=False,
freeze_bn=False,
):
super().__init__()
num_classes = opts.gen.s.output_dim
self.backbone = opts.gen.deeplabv3.backbone
self.use_dada = ("d" in opts.tasks) and opts.gen.s.use_dada
if self.backbone == "resnet":
self.aspp = ASPPv3Plus(self.backbone, no_init)
self.decoder = Decoder(num_classes)
self.freeze_bn = freeze_bn
else:
self.head = _DeepLabHead(num_classes, c4_channels=320)
self._target_size = find_target_size(opts, "s")
print(
" - {}: setting target size to {}".format(
self.__class__.__name__, self._target_size
)
)
if not no_init:
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode="fan_out")
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.zeros_(m.bias)
self.load_pretrained(opts)
def load_pretrained(self, opts):
assert opts.gen.deeplabv3.backbone in {"resnet", "mobilenet"}
assert Path(opts.gen.deeplabv3.pretrained_model.resnet).exists()
if opts.gen.deeplabv3.backbone == "resnet":
std = torch.load(opts.gen.deeplabv3.pretrained_model.resnet)
self.aspp.load_state_dict(
{
k.replace("aspp.", ""): v
for k, v in std.items()
if k.startswith("aspp.")
}
)
self.decoder.load_state_dict(
{
k.replace("decoder.", ""): v
for k, v in std.items()
if k.startswith("decoder.")
and not (len(v.shape) > 0 and v.shape[0] == 19)
},
strict=False,
)
print(
"- Loaded pre-trained DeepLabv3+ (Resnet) Decoder & ASPP as Seg Decoder"
)
else:
std = torch.load(opts.gen.deeplabv3.pretrained_model.mobilenet)
self.load_state_dict(
{
k: v
for k, v in std.items()
if k.startswith("head.")
and not (len(v.shape) > 0 and v.shape[0] == 19)
},
strict=False,
)
print(
" - Loaded pre-trained DeepLabv3+ (MobileNetV2) Head as Seg Decoder"
)
def set_target_size(self, size):
"""
Set final interpolation's target size
Args:
size (int, list, tuple): target size (h, w). If int, target will be (i, i)
"""
if isinstance(size, (list, tuple)):
self._target_size = size[:2]
else:
self._target_size = (size, size)
def forward(self, z, z_depth=None):
assert isinstance(z, (tuple, list))
if self._target_size is None:
error = "self._target_size should be set with self.set_target_size()"
error += "to interpolate logits to the target seg map's size"
raise ValueError(error)
z_high, z_low = z
if z_depth is not None and self.use_dada:
z_high = z_high * z_depth
if self.backbone == "resnet":
z_high = self.aspp(z_high)
s = self.decoder(z_high, z_low)
else:
s = self.head(z_high)
s = F.interpolate(
s, size=self._target_size, mode="bilinear", align_corners=True
)
return s
def freeze_bn(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
|