File size: 9,013 Bytes
01df1d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'''Module for making resnet encoders.

'''

import torch
import torch.nn as nn

from cortex_DIM.nn_modules.convnet import Convnet
from cortex_DIM.nn_modules.misc import Fold, Unfold, View


_nonlin_idx = 6


class ResBlock(Convnet):
    '''Residual block for ResNet

    '''

    def create_layers(self, shape, conv_args=None):
        '''Creates layers

        Args:
            shape: Shape of input.
            conv_args: Layer arguments for block.
        '''

        # Move nonlinearity to a separate step for residual.
        final_nonlin = conv_args[-1][_nonlin_idx]
        conv_args[-1] = list(conv_args[-1])
        conv_args[-1][_nonlin_idx] = None
        conv_args.append((None, 0, 0, 0, False, False, final_nonlin, None))

        super().create_layers(shape, conv_args=conv_args)

        if self.conv_shape != shape:
            dim_x, dim_y, dim_in = shape
            dim_x_, dim_y_, dim_out = self.conv_shape
            stride = dim_x // dim_x_
            next_x, _ = self.next_size(dim_x, dim_y, 1, stride, 0)
            assert next_x == dim_x_, (self.conv_shape, shape)

            self.downsample = nn.Sequential(
                nn.Conv2d(dim_in, dim_out, kernel_size=1, stride=stride, padding=0, bias=False),
                nn.BatchNorm2d(dim_out),
            )
        else:
            self.downsample = None

    def forward(self, x: torch.Tensor):
        '''Forward pass

        Args:
            x: Input.

        Returns:
            torch.Tensor or list of torch.Tensor.

        '''

        if self.downsample is not None:
            residual = self.downsample(x)
        else:
            residual = x

        x = self.conv_layers[-1](self.conv_layers[:-1](x) + residual)

        return x


class ResNet(Convnet):
    def create_layers(self, shape, conv_before_args=None, res_args=None, conv_after_args=None, fc_args=None):
        '''Creates layers

        Args:
            shape: Shape of the input.
            conv_before_args: Arguments for convolutional layers before residuals.
            res_args: Residual args.
            conv_after_args: Arguments for convolutional layers after residuals.
            fc_args: Fully-connected arguments.

        '''

        dim_x, dim_y, dim_in = shape
        shape = (dim_x, dim_y, dim_in)
        self.conv_before_layers, self.conv_before_shape = self.create_conv_layers(shape, conv_before_args)
        self.res_layers, self.res_shape = self.create_res_layers(self.conv_before_shape, res_args)
        self.conv_after_layers, self.conv_after_shape = self.create_conv_layers(self.res_shape, conv_after_args)

        dim_x, dim_y, dim_out = self.conv_after_shape
        dim_r = dim_x * dim_y * dim_out
        self.reshape = View(-1, dim_r)
        self.fc_layers, _ = self.create_linear_layers(dim_r, fc_args)

    def create_res_layers(self, shape, block_args=None):
        '''Creates a set of residual blocks.

        Args:
            shape: input shape.
            block_args: Arguments for blocks.

        Returns:
            nn.Sequential: sequence of residual blocks.

        '''

        res_layers = nn.Sequential()
        block_args = block_args or []

        for i, (conv_args, n_blocks) in enumerate(block_args):
            block = ResBlock(shape, conv_args=conv_args)
            res_layers.add_module('block_{}_0'.format(i), block)

            for j in range(1, n_blocks):
                shape = block.conv_shape
                block = ResBlock(shape, conv_args=conv_args)
                res_layers.add_module('block_{}_{}'.format(i, j), block)
            shape = block.conv_shape

        return res_layers, shape

    def forward(self, x: torch.Tensor, return_full_list=False):
        '''Forward pass

        Args:
            x: Input.
            return_full_list: Optional, returns all layer outputs.

        Returns:
            torch.Tensor or list of torch.Tensor.

        '''

        if return_full_list:
            conv_before_out = []
            for conv_layer in self.conv_before_layers:
                x = conv_layer(x)
                conv_before_out.append(x)
        else:
            conv_before_out = self.conv_layers(x)
            x = conv_before_out

        if return_full_list:
            res_out = []
            for res_layer in self.res_layers:
                x = res_layer(x)
                res_out.append(x)
        else:
            res_out = self.res_layers(x)
            x = res_out

        if return_full_list:
            conv_after_out = []
            for conv_layer in self.conv_after_layers:
                x = conv_layer(x)
                conv_after_out.append(x)
        else:
            conv_after_out = self.conv_after_layers(x)
            x = conv_after_out

        x = self.reshape(x)

        if return_full_list:
            fc_out = []
            for fc_layer in self.fc_layers:
                x = fc_layer(x)
                fc_out.append(x)
        else:
            fc_out = self.fc_layers(x)

        return conv_before_out, res_out, conv_after_out, fc_out


class FoldedResNet(ResNet):
    '''Resnet with strided crop input.

    '''

    def create_layers(self, shape, crop_size=8, conv_before_args=None, res_args=None,
                      conv_after_args=None, fc_args=None):
        '''Creates layers

        Args:
            shape: Shape of the input.
            crop_size: Size of the crops.
            conv_before_args: Arguments for convolutional layers before residuals.
            res_args: Residual args.
            conv_after_args: Arguments for convolutional layers after residuals.
            fc_args: Fully-connected arguments.

        '''
        self.crop_size = crop_size

        dim_x, dim_y, dim_in = shape
        self.final_size = 2 * (dim_x // self.crop_size) - 1

        self.unfold = Unfold(dim_x, self.crop_size)
        self.refold = Fold(dim_x, self.crop_size)

        shape = (self.crop_size, self.crop_size, dim_in)
        self.conv_before_layers, self.conv_before_shape = self.create_conv_layers(shape, conv_before_args)

        self.res_layers, self.res_shape = self.create_res_layers(self.conv_before_shape, res_args)
        self.conv_after_layers, self.conv_after_shape = self.create_conv_layers(self.res_shape, conv_after_args)
        self.conv_after_shape = self.res_shape

        dim_x, dim_y, dim_out = self.conv_after_shape
        dim_r = dim_x * dim_y * dim_out
        self.reshape = View(-1, dim_r)
        self.fc_layers, _ = self.create_linear_layers(dim_r, fc_args)

    def create_res_layers(self, shape, block_args=None):
        '''Creates a set of residual blocks.

        Args:
            shape: input shape.
            block_args: Arguments for blocks.

        Returns:
            nn.Sequential: sequence of residual blocks.

        '''

        res_layers = nn.Sequential()
        block_args = block_args or []

        for i, (conv_args, n_blocks) in enumerate(block_args):
            block = ResBlock(shape, conv_args=conv_args)
            res_layers.add_module('block_{}_0'.format(i), block)

            for j in range(1, n_blocks):
                shape = block.conv_shape
                block = ResBlock(shape, conv_args=conv_args)
                res_layers.add_module('block_{}_{}'.format(i, j), block)
            shape = block.conv_shape
            dim_x, dim_y = shape[:2]

            if dim_x != dim_y:
                raise ValueError('dim_x and dim_y do not match.')

            if dim_x == 1:
                shape = (self.final_size, self.final_size, shape[2])

        return res_layers, shape

    def forward(self, x: torch.Tensor, return_full_list=False):
        '''Forward pass

        Args:
            x: Input.
            return_full_list: Optional, returns all layer outputs.

        Returns:
            torch.Tensor or list of torch.Tensor.

        '''
        x = self.unfold(x)

        conv_before_out = []
        for conv_layer in self.conv_before_layers:
            x = conv_layer(x)
            if x.size(2) == 1:
                x = self.refold(x)
            conv_before_out.append(x)

        res_out = []
        for res_layer in self.res_layers:
            x = res_layer(x)
            res_out.append(x)

        if x.size(2) == 1:
            x = self.refold(x)
            res_out[-1] = x

        conv_after_out = []
        for conv_layer in self.conv_after_layers:
            x = conv_layer(x)
            if x.size(2) == 1:
                x = self.refold(x)
            conv_after_out.append(x)

        x = self.reshape(x)

        if return_full_list:
            fc_out = []
            for fc_layer in self.fc_layers:
                x = fc_layer(x)
                fc_out.append(x)
        else:
            fc_out = self.fc_layers(x)

        if not return_full_list:
            conv_before_out = conv_before_out[-1]
            res_out = res_out[-1]
            conv_after_out = conv_after_out[-1]

        return conv_before_out, res_out, conv_after_out, fc_out