File size: 10,432 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
'''Convnet encoder module.

'''

import torch
import torch.nn as nn

#from cortex.built_ins.networks.utils import get_nonlinearity

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


def infer_conv_size(w, k, s, p):
    '''Infers the next size after convolution.

    Args:
        w: Input size.
        k: Kernel size.
        s: Stride.
        p: Padding.

    Returns:
        int: Output size.

    '''
    x = (w - k + 2 * p) // s + 1
    return x


class Convnet(nn.Module):
    '''Basic convnet convenience class.

    Attributes:
        conv_layers: nn.Sequential of nn.Conv2d layers with batch norm,
            dropout, nonlinearity.
        fc_layers: nn.Sequential of nn.Linear layers with batch norm,
            dropout, nonlinearity.
        reshape: Simple reshape layer.
        conv_shape: Shape of the convolutional output.

    '''

    def __init__(self, *args, **kwargs):
        super().__init__()
        self.create_layers(*args, **kwargs)

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

        conv_args are in format (dim_h, f_size, stride, pad, batch_norm, dropout, nonlinearity, pool)
        fc_args are in format (dim_h, batch_norm, dropout, nonlinearity)

        Args:
            shape: Shape of input.
            conv_args: List of tuple of convolutional arguments.
            fc_args: List of tuple of fully-connected arguments.
        '''

        self.conv_layers, self.conv_shape = self.create_conv_layers(shape, conv_args)

        dim_x, dim_y, dim_out = self.conv_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_conv_layers(self, shape, conv_args):
        '''Creates a set of convolutional layers.

        Args:
            shape: Input shape.
            conv_args: List of tuple of convolutional arguments.

        Returns:
            nn.Sequential: a sequence of convolutional layers.

        '''

        conv_layers = nn.Sequential()
        conv_args = conv_args or []

        dim_x, dim_y, dim_in = shape

        for i, (dim_out, f, s, p, batch_norm, dropout, nonlinearity, pool) in enumerate(conv_args):
            name = '({}/{})_{}'.format(dim_in, dim_out, i + 1)
            conv_block = nn.Sequential()

            if dim_out is not None:
                conv = nn.Conv2d(dim_in, dim_out, kernel_size=f, stride=s, padding=p, bias=not(batch_norm))
                conv_block.add_module(name + 'conv', conv)
                dim_x, dim_y = self.next_size(dim_x, dim_y, f, s, p)
            else:
                dim_out = dim_in

            if dropout:
                conv_block.add_module(name + 'do', nn.Dropout2d(p=dropout))
            if batch_norm:
                bn = nn.BatchNorm2d(dim_out)
                conv_block.add_module(name + 'bn', bn)

            if nonlinearity:
                nonlinearity = get_nonlinearity(nonlinearity)
                conv_block.add_module(nonlinearity.__class__.__name__, nonlinearity)

            if pool:
                (pool_type, kernel, stride) = pool
                Pool = getattr(nn, pool_type)
                conv_block.add_module(name + 'pool', Pool(kernel_size=kernel, stride=stride))
                dim_x, dim_y = self.next_size(dim_x, dim_y, kernel, stride, 0)

            conv_layers.add_module(name, conv_block)

            dim_in = dim_out

        dim_out = dim_in

        return conv_layers, (dim_x, dim_y, dim_out)

    def create_linear_layers(self, dim_in, fc_args):
        '''

        Args:
            dim_in: Number of input units.
            fc_args: List of tuple of fully-connected arguments.

        Returns:
            nn.Sequential.

        '''

        fc_layers = nn.Sequential()
        fc_args = fc_args or []

        for i, (dim_out, batch_norm, dropout, nonlinearity) in enumerate(fc_args):
            name = '({}/{})_{}'.format(dim_in, dim_out, i + 1)
            fc_block = nn.Sequential()

            if dim_out is not None:
                fc_block.add_module(name + 'fc', nn.Linear(dim_in, dim_out))
            else:
                dim_out = dim_in

            if dropout:
                fc_block.add_module(name + 'do', nn.Dropout(p=dropout))
            if batch_norm:
                bn = nn.BatchNorm1d(dim_out)
                fc_block.add_module(name + 'bn', bn)

            if nonlinearity:
                nonlinearity = get_nonlinearity(nonlinearity)
                fc_block.add_module(nonlinearity.__class__.__name__, nonlinearity)

            fc_layers.add_module(name, fc_block)

            dim_in = dim_out

        return fc_layers, dim_in

    def next_size(self, dim_x, dim_y, k, s, p):
        '''Infers the next size of a convolutional layer.

        Args:
            dim_x: First dimension.
            dim_y: Second dimension.
            k: Kernel size.
            s: Stride.
            p: Padding.

        Returns:
            (int, int): (First output dimension, Second output dimension)

        '''
        if isinstance(k, int):
            kx, ky = (k, k)
        else:
            kx, ky = k

        if isinstance(s, int):
            sx, sy = (s, s)
        else:
            sx, sy = s

        if isinstance(p, int):
            px, py = (p, p)
        else:
            px, py = p
        return (infer_conv_size(dim_x, kx, sx, px),
                infer_conv_size(dim_y, ky, sy, py))

    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_out = []
            for conv_layer in self.conv_layers:
                x = conv_layer(x)
                conv_out.append(x)
        else:
            conv_out = self.conv_layers(x)
            x = conv_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_out, fc_out


class FoldedConvnet(Convnet):
    '''Convnet with strided crop input.

    '''

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

        conv_args are in format (dim_h, f_size, stride, pad, batch_norm, dropout, nonlinearity, pool)
        fc_args are in format (dim_h, batch_norm, dropout, nonlinearity)

        Args:
            shape: Shape of input.
            crop_size: Size of crops
            conv_args: List of tuple of convolutional arguments.
            fc_args: List of tuple of fully-connected arguments.
        '''

        self.crop_size = crop_size

        dim_x, dim_y, dim_in = shape
        if dim_x != dim_y:
            raise ValueError('x and y dimensions must be the same to use Folded encoders.')

        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_layers, self.conv_shape = self.create_conv_layers(shape, conv_args)

        dim_x, dim_y, dim_out = self.conv_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_conv_layers(self, shape, conv_args):
        '''Creates a set of convolutional layers.

        Args:
            shape: Input shape.
            conv_args: List of tuple of convolutional arguments.

        Returns:
            nn.Sequential: A sequence of convolutional layers.

        '''

        conv_layers = nn.Sequential()
        conv_args = conv_args or []
        dim_x, dim_y, dim_in = shape

        for i, (dim_out, f, s, p, batch_norm, dropout, nonlinearity, pool) in enumerate(conv_args):
            name = '({}/{})_{}'.format(dim_in, dim_out, i + 1)
            conv_block = nn.Sequential()

            if dim_out is not None:
                conv = nn.Conv2d(dim_in, dim_out, kernel_size=f, stride=s, padding=p, bias=not(batch_norm))
                conv_block.add_module(name + 'conv', conv)
                dim_x, dim_y = self.next_size(dim_x, dim_y, f, s, p)
            else:
                dim_out = dim_in

            if dropout:
                conv_block.add_module(name + 'do', nn.Dropout2d(p=dropout))
            if batch_norm:
                bn = nn.BatchNorm2d(dim_out)
                conv_block.add_module(name + 'bn', bn)

            if nonlinearity:
                nonlinearity = get_nonlinearity(nonlinearity)
                conv_block.add_module(nonlinearity.__class__.__name__, nonlinearity)

            if pool:
                (pool_type, kernel, stride) = pool
                Pool = getattr(nn, pool_type)
                conv_block.add_module('pool', Pool(kernel_size=kernel, stride=stride))
                dim_x, dim_y = self.next_size(dim_x, dim_y, kernel, stride, 0)

            conv_layers.add_module(name, conv_block)

            dim_in = dim_out

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

            if dim_x == 1:
                dim_x = self.final_size
                dim_y = self.final_size

        dim_out = dim_in

        return conv_layers, (dim_x, dim_y, dim_out)

    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_out = []
        for conv_layer in self.conv_layers:
            x = conv_layer(x)
            if x.size(2) == 1:
                x = self.refold(x)
            conv_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_out = conv_out[-1]

        return conv_out, fc_out