File size: 3,755 Bytes
984cdba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models.resnet import resnet50, ResNet50_Weights

def resnet50_multispectral(in_channels, use_pretrained=True):
    weights = ResNet50_Weights.IMAGENET1K_V1 if use_pretrained else None
    model = resnet50(weights=weights)

    old_conv = model.conv1
    model.conv1 = nn.Conv2d(
        in_channels,
        64,
        kernel_size=7,
        stride=2,
        padding=3,
        bias=False
    )

    if use_pretrained:
        with torch.no_grad():
            if in_channels >= 3:
                model.conv1.weight[:, :3] = old_conv.weight
                if in_channels > 3:
                    model.conv1.weight[:, 3:] = old_conv.weight.mean(dim=1, keepdim=True)
            else:
                model.conv1.weight = old_conv.weight[:, :in_channels]

    return model



class ASPP(nn.Module):
    def __init__(self, in_channels, out_channels=256):
        super().__init__()

        self.conv1 = nn.Conv2d(in_channels, out_channels, 1, bias=False)
        self.conv2 = nn.Conv2d(in_channels, out_channels, 3, padding=6, dilation=6, bias=False)
        self.conv3 = nn.Conv2d(in_channels, out_channels, 3, padding=12, dilation=12, bias=False)
        self.conv4 = nn.Conv2d(in_channels, out_channels, 3, padding=18, dilation=18, bias=False)

        self.global_pool = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            nn.Conv2d(in_channels, out_channels, 1, bias=False)
        )

        self.project = nn.Conv2d(out_channels * 5, out_channels, 1, bias=False)

    def forward(self, x):
        h, w = x.shape[2:]
        p1 = self.conv1(x)
        p2 = self.conv2(x)
        p3 = self.conv3(x)
        p4 = self.conv4(x)

        gp = self.global_pool(x)
        gp = F.interpolate(gp, size=(h, w), mode="bilinear", align_corners=False)

        x = torch.cat([p1, p2, p3, p4, gp], dim=1)
        return self.project(x)


class Decoder(nn.Module):
    def __init__(self, low_level_channels, num_classes):
        super().__init__()

        self.low_proj = nn.Conv2d(low_level_channels, 48, 1, bias=False)

        self.output = nn.Sequential(
            nn.Conv2d(304, 256, 3, padding=1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, num_classes, 1)
        )

    def forward(self, x, low_level):
        low_level = self.low_proj(low_level)
        x = F.interpolate(x, size=low_level.shape[2:], mode="bilinear", align_corners=False)
        x = torch.cat([x, low_level], dim=1)
        return self.output(x)


class model(nn.Module):
    def __init__(self,
                 in_channels,
                 num_classes,
                 freeze_encoder=False,):
        super().__init__()

        backbone = resnet50_multispectral(in_channels)

        self.layer0 = nn.Sequential(
            backbone.conv1,
            backbone.bn1,
            backbone.relu,
            backbone.maxpool
        )

        if freeze_encoder:
            for param in backbone.parameters():
                param.requires_grad = False

        self.layer1 = backbone.layer1  # low-level features
        self.layer2 = backbone.layer2
        self.layer3 = backbone.layer3
        self.layer4 = backbone.layer4  # high-level features

        self.aspp = ASPP(2048)
        self.decoder = Decoder(256, num_classes)

    def forward(self, x):
        input_size = x.shape[2:]

        x = self.layer0(x)
        low_level = self.layer1(x)

        x = self.layer2(low_level)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.aspp(x)
        x = self.decoder(x, low_level)

        x = F.interpolate(x, size=input_size, mode="bilinear", align_corners=False)
        return x