Spaces:
Sleeping
Sleeping
File size: 4,493 Bytes
c20a1af |
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 |
# Copyright (C) 2021 * Ltd. All rights reserved.
# author : Sanghyeon Jo <[email protected]>
import torch
import torch.nn as nn
import torch.nn.functional as F
class ASPPModule(nn.Module):
def __init__(self, inplanes, planes, kernel_size, padding, dilation, norm_fn=None):
super().__init__()
self.atrous_conv = nn.Conv2d(inplanes, planes, kernel_size=kernel_size, stride=1, padding=padding, dilation=dilation, bias=False)
self.bn = norm_fn(planes)
self.relu = nn.ReLU(inplace=True)
self.initialize([self.atrous_conv, self.bn])
def forward(self, x):
x = self.atrous_conv(x)
x = self.bn(x)
return self.relu(x)
def initialize(self, modules):
for m in modules:
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
class ASPP(nn.Module):
def __init__(self, output_stride, norm_fn):
super().__init__()
inplanes = 2048
if output_stride == 16:
dilations = [1, 6, 12, 18]
elif output_stride == 8:
dilations = [1, 12, 24, 36]
self.aspp1 = ASPPModule(inplanes, 256, 1, padding=0, dilation=dilations[0], norm_fn=norm_fn)
self.aspp2 = ASPPModule(inplanes, 256, 3, padding=dilations[1], dilation=dilations[1], norm_fn=norm_fn)
self.aspp3 = ASPPModule(inplanes, 256, 3, padding=dilations[2], dilation=dilations[2], norm_fn=norm_fn)
self.aspp4 = ASPPModule(inplanes, 256, 3, padding=dilations[3], dilation=dilations[3], norm_fn=norm_fn)
self.global_avg_pool = nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)),
nn.Conv2d(inplanes, 256, 1, stride=1, bias=False),
norm_fn(256),
nn.ReLU(inplace=True),
)
self.conv1 = nn.Conv2d(1280, 256, 1, bias=False)
self.bn1 = norm_fn(256)
self.relu = nn.ReLU(inplace=True)
self.dropout = nn.Dropout(0.5)
self.initialize([self.conv1, self.bn1] + list(self.global_avg_pool.modules()))
def forward(self, x):
x1 = self.aspp1(x)
x2 = self.aspp2(x)
x3 = self.aspp3(x)
x4 = self.aspp4(x)
x5 = self.global_avg_pool(x)
x5 = F.interpolate(x5, size=x4.size()[2:], mode='bilinear', align_corners=True)
x = torch.cat((x1, x2, x3, x4, x5), dim=1)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.dropout(x)
return x
def initialize(self, modules):
for m in modules:
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
class Decoder(nn.Module):
def __init__(self, num_classes, low_level_inplanes, norm_fn):
super().__init__()
self.conv1 = nn.Conv2d(low_level_inplanes, 48, 1, bias=False)
self.bn1 = norm_fn(48)
self.relu = nn.ReLU(inplace=True)
self.classifier = nn.Sequential(
nn.Conv2d(304, 256, kernel_size=3, stride=1, padding=1, bias=False),
norm_fn(256),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
norm_fn(256),
nn.ReLU(inplace=True),
nn.Dropout(0.1),
nn.Conv2d(256, num_classes, kernel_size=1, stride=1)
)
self.initialize([self.conv1, self.bn1] + list(self.classifier.modules()))
def forward(self, x, x_low_level):
x_low_level = self.conv1(x_low_level)
x_low_level = self.bn1(x_low_level)
x_low_level = self.relu(x_low_level)
x = F.interpolate(x, size=x_low_level.size()[2:], mode='bilinear', align_corners=True)
x = torch.cat((x, x_low_level), dim=1)
x = self.classifier(x)
return x
def initialize(self, modules):
for m in modules:
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_() |