File size: 7,284 Bytes
35188e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Donny You([email protected])


import torch.nn as nn
from networks.resnet_models import *


class NormalResnetBackbone(nn.Module):
    def __init__(self, orig_resnet):
        super(NormalResnetBackbone, self).__init__()

        self.num_features = 2048
        # take pretrained resnet, except AvgPool and FC
        self.prefix = orig_resnet.prefix
        self.maxpool = orig_resnet.maxpool
        self.layer1 = orig_resnet.layer1
        self.layer2 = orig_resnet.layer2
        self.layer3 = orig_resnet.layer3
        self.layer4 = orig_resnet.layer4

    def get_num_features(self):
        return self.num_features

    def forward(self, x):
        tuple_features = list()
        x = self.prefix(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        tuple_features.append(x)
        x = self.layer2(x)
        tuple_features.append(x)
        x = self.layer3(x)
        tuple_features.append(x)
        x = self.layer4(x)
        tuple_features.append(x)

        return tuple_features


class DilatedResnetBackbone(nn.Module):
    def __init__(self, orig_resnet, dilate_scale=8, multi_grid=(1, 2, 4)):
        super(DilatedResnetBackbone, self).__init__()

        self.num_features = 2048
        from functools import partial

        if dilate_scale == 8:
            orig_resnet.layer3.apply(partial(self._nostride_dilate, dilate=2))
            if multi_grid is None:
                orig_resnet.layer4.apply(partial(self._nostride_dilate, dilate=4))
            else:
                for i, r in enumerate(multi_grid):
                    orig_resnet.layer4[i].apply(partial(self._nostride_dilate, dilate=int(4 * r)))

        elif dilate_scale == 16:
            if multi_grid is None:
                orig_resnet.layer4.apply(partial(self._nostride_dilate, dilate=2))
            else:
                for i, r in enumerate(multi_grid):
                    orig_resnet.layer4[i].apply(partial(self._nostride_dilate, dilate=int(2 * r)))

        # Take pretrained resnet, except AvgPool and FC
        self.prefix = orig_resnet.prefix
        self.maxpool = orig_resnet.maxpool
        self.layer1 = orig_resnet.layer1
        self.layer2 = orig_resnet.layer2
        self.layer3 = orig_resnet.layer3
        self.layer4 = orig_resnet.layer4

    def _nostride_dilate(self, m, dilate):
        classname = m.__class__.__name__
        if classname.find('Conv') != -1:
            # the convolution with stride
            if m.stride == (2, 2):
                m.stride = (1, 1)
                if m.kernel_size == (3, 3):
                    m.dilation = (dilate // 2, dilate // 2)
                    m.padding = (dilate // 2, dilate // 2)
            # other convoluions
            else:
                if m.kernel_size == (3, 3):
                    m.dilation = (dilate, dilate)
                    m.padding = (dilate, dilate)

    def get_num_features(self):
        return self.num_features

    def forward(self, x):
        tuple_features = list()

        x = self.prefix(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        tuple_features.append(x)
        x = self.layer2(x)
        tuple_features.append(x)
        x = self.layer3(x)
        tuple_features.append(x)
        x = self.layer4(x)
        tuple_features.append(x)

        return tuple_features


def ResNetBackbone(backbone=None, width_multiplier=1.0, pretrained=None, multi_grid=None, norm_type='batchnorm'):
    arch = backbone

    if arch == 'resnet18':
        orig_resnet = resnet18(pretrained=pretrained)
        arch_net = NormalResnetBackbone(orig_resnet)
        arch_net.num_features = 512

    elif arch == 'resnet18_dilated8':
        orig_resnet = resnet18(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=8, multi_grid=multi_grid)
        arch_net.num_features = 512

    elif arch == 'resnet34':
        orig_resnet = resnet34(pretrained=pretrained)
        arch_net = NormalResnetBackbone(orig_resnet)
        arch_net.num_features = 512

    elif arch == 'resnet34_dilated8':
        orig_resnet = resnet34(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=8, multi_grid=multi_grid)
        arch_net.num_features = 512

    elif arch == 'resnet34_dilated16':
        orig_resnet = resnet34(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=16, multi_grid=multi_grid)
        arch_net.num_features = 512

    elif arch == 'resnet50':
        orig_resnet = resnet50(pretrained=pretrained, width_multiplier=width_multiplier)
        arch_net = NormalResnetBackbone(orig_resnet)

    elif arch == 'resnet50_dilated8':
        orig_resnet = resnet50(pretrained=pretrained, width_multiplier=width_multiplier)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=8, multi_grid=multi_grid)

    elif arch == 'resnet50_dilated16':
        orig_resnet = resnet50(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=16, multi_grid=multi_grid)

    elif arch == 'deepbase_resnet50':
        if pretrained:
            pretrained = 'models/backbones/pretrained/3x3resnet50-imagenet.pth'
        orig_resnet = deepbase_resnet50(pretrained=pretrained)
        arch_net = NormalResnetBackbone(orig_resnet)

    elif arch == 'deepbase_resnet50_dilated8':
        if pretrained:
            pretrained = 'models/backbones/pretrained/3x3resnet50-imagenet.pth'
            # pretrained = "/home/gishin/Projects/DeepLearning/Oxford/cct/models/backbones/pretrained/3x3resnet50-imagenet.pth"
        orig_resnet = deepbase_resnet50(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=8, multi_grid=multi_grid)

    elif arch == 'deepbase_resnet50_dilated16':
        orig_resnet = deepbase_resnet50(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=16, multi_grid=multi_grid)

    elif arch == 'resnet101':
        orig_resnet = resnet101(pretrained=pretrained)
        arch_net = NormalResnetBackbone(orig_resnet)

    elif arch == 'resnet101_dilated8':
        orig_resnet = resnet101(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=8, multi_grid=multi_grid)

    elif arch == 'resnet101_dilated16':
        orig_resnet = resnet101(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=16, multi_grid=multi_grid)

    elif arch == 'deepbase_resnet101':
        orig_resnet = deepbase_resnet101(pretrained=pretrained)
        arch_net = NormalResnetBackbone(orig_resnet)

    elif arch == 'deepbase_resnet101_dilated8':
        if pretrained:
            pretrained = 'backbones/backbones/pretrained/3x3resnet101-imagenet.pth'
        orig_resnet = deepbase_resnet101(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=8, multi_grid=multi_grid)

    elif arch == 'deepbase_resnet101_dilated16':
        orig_resnet = deepbase_resnet101(pretrained=pretrained)
        arch_net = DilatedResnetBackbone(orig_resnet, dilate_scale=16, multi_grid=multi_grid)

    else:
        raise Exception('Architecture undefined!')

    return arch_net