File size: 6,774 Bytes
162943d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# -*- coding: utf-8 -*-

# Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is
# holder of all proprietary rights on this computer program.
# You can only use this computer program if you have closed
# a license agreement with MPG or you get the right to use the computer
# program from someone who is authorized to grant you that right.
# Any use of the computer program without a valid license is prohibited and
# liable to prosecution.
#
# Copyright©2019 Max-Planck-Gesellschaft zur Förderung
# der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute
# for Intelligent Systems. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de


import torch.nn as nn
import pytorch_lightning as pl


class BaseNetwork(pl.LightningModule):
    def __init__(self):
        super(BaseNetwork, self).__init__()

    def init_weights(self, init_type='xavier', gain=0.02):
        '''
        initializes network's weights
        init_type: normal | xavier | kaiming | orthogonal
        https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/9451e70673400885567d08a9e97ade2524c700d0/models/networks.py#L39
        '''
        def init_func(m):
            classname = m.__class__.__name__
            if hasattr(m, 'weight') and (classname.find('Conv') != -1
                                         or classname.find('Linear') != -1):
                if init_type == 'normal':
                    nn.init.normal_(m.weight.data, 0.0, gain)
                elif init_type == 'xavier':
                    nn.init.xavier_normal_(m.weight.data, gain=gain)
                elif init_type == 'kaiming':
                    nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
                elif init_type == 'orthogonal':
                    nn.init.orthogonal_(m.weight.data, gain=gain)

                if hasattr(m, 'bias') and m.bias is not None:
                    nn.init.constant_(m.bias.data, 0.0)

            elif classname.find('BatchNorm2d') != -1:
                nn.init.normal_(m.weight.data, 1.0, gain)
                nn.init.constant_(m.bias.data, 0.0)

        self.apply(init_func)


class Residual3D(BaseNetwork):
    def __init__(self, numIn, numOut):
        super(Residual3D, self).__init__()
        self.numIn = numIn
        self.numOut = numOut
        self.with_bias = True
        # self.bn = nn.GroupNorm(4, self.numIn)
        self.bn = nn.BatchNorm3d(self.numIn)
        self.relu = nn.ReLU(inplace=True)
        self.conv1 = nn.Conv3d(self.numIn,
                               self.numOut,
                               bias=self.with_bias,
                               kernel_size=3,
                               stride=1,
                               padding=2,
                               dilation=2)
        # self.bn1 = nn.GroupNorm(4, self.numOut)
        self.bn1 = nn.BatchNorm3d(self.numOut)
        self.conv2 = nn.Conv3d(self.numOut,
                               self.numOut,
                               bias=self.with_bias,
                               kernel_size=3,
                               stride=1,
                               padding=1)
        # self.bn2 = nn.GroupNorm(4, self.numOut)
        self.bn2 = nn.BatchNorm3d(self.numOut)
        self.conv3 = nn.Conv3d(self.numOut,
                               self.numOut,
                               bias=self.with_bias,
                               kernel_size=3,
                               stride=1,
                               padding=1)

        if self.numIn != self.numOut:
            self.conv4 = nn.Conv3d(self.numIn,
                                   self.numOut,
                                   bias=self.with_bias,
                                   kernel_size=1)
        self.init_weights()

    def forward(self, x):
        residual = x
        # out = self.bn(x)
        # out = self.relu(out)
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        # out = self.conv3(out)
        # out = self.relu(out)

        if self.numIn != self.numOut:
            residual = self.conv4(x)

        return out + residual


class VolumeEncoder(BaseNetwork):
    """CycleGan Encoder"""

    def __init__(self, num_in=3, num_out=32, num_stacks=2):
        super(VolumeEncoder, self).__init__()
        self.num_in = num_in
        self.num_out = num_out
        self.num_inter = 8
        self.num_stacks = num_stacks
        self.with_bias = True

        self.relu = nn.ReLU(inplace=True)
        self.conv1 = nn.Conv3d(self.num_in,
                               self.num_inter,
                               bias=self.with_bias,
                               kernel_size=5,
                               stride=2,
                               padding=4,
                               dilation=2)
        # self.bn1 = nn.GroupNorm(4, self.num_inter)
        self.bn1 = nn.BatchNorm3d(self.num_inter)
        self.conv2 = nn.Conv3d(self.num_inter,
                               self.num_out,
                               bias=self.with_bias,
                               kernel_size=5,
                               stride=2,
                               padding=4,
                               dilation=2)
        # self.bn2 = nn.GroupNorm(4, self.num_out)
        self.bn2 = nn.BatchNorm3d(self.num_out)

        self.conv_out1 = nn.Conv3d(self.num_out,
                                   self.num_out,
                                   bias=self.with_bias,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1,
                                   dilation=1)
        self.conv_out2 = nn.Conv3d(self.num_out,
                                   self.num_out,
                                   bias=self.with_bias,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1,
                                   dilation=1)

        for idx in range(self.num_stacks):
            self.add_module("res" + str(idx),
                            Residual3D(self.num_out, self.num_out))

        self.init_weights()

    def forward(self, x, intermediate_output=True):
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out_lst = []
        for idx in range(self.num_stacks):
            out = self._modules["res" + str(idx)](out)
            out_lst.append(out)

        if intermediate_output:
            return out_lst
        else:
            return [out_lst[-1]]