MCPcopy Create free account
hub / github.com/cvlab-epfl/MeshSDF / ResNet

Class ResNet

lib/models/encoder.py:133–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

131
132
133class ResNet(nn.Module):
134
135 def __init__(self, latent_size, depth, basicblock=False, norm_type="bin", inputchannels=4):
136 super(ResNet, self).__init__()
137 # Model type specifies number of layers for CIFAR-10 model
138 assert (depth - 2) % 6 == 0, 'depth should be 6n+2'
139 n = (depth - 2) // 6
140
141 if basicblock:
142 block = BasicBlock
143 else:
144 block = Bottleneck if depth >=44 else BasicBlock
145
146 if norm_type == 'bn':
147 from torch.nn import BatchNorm2d
148 self.normlayer = functools.partial(BatchNorm2d, affine=True)
149 elif norm_type == 'in':
150 from torch.nn import InstanceNorm2d
151 self.normlayer = functools.partial(InstanceNorm2d, affine=True)
152 elif norm_type == 'bin':
153 self.normlayer = functools.partial(BatchInstanceNorm2d, affine=True)
154 else:
155 raise ValueError('Normalization should be either of type')
156
157
158
159 self.inplanes = 64
160 self.conv1 = nn.Conv2d(inputchannels, 64, kernel_size=3, padding=1,
161 bias=False)
162 self.bn1 = self.normlayer(64)
163 self.relu = nn.ReLU(inplace=True)
164 self.layer1 = self._make_layer(block, 64, n)
165 self.layer2 = self._make_layer(block, 128, n, stride=2)
166 self.layer3 = self._make_layer(block, 256, n, stride=2)
167 self.layer4 = self._make_layer(block, 256, n, stride=2)
168
169 self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
170 self.fc = nn.Linear(256 * block.expansion, latent_size)
171
172 for m in self.modules():
173 if isinstance(m, nn.Conv2d):
174 n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
175 m.weight.data.normal_(0, math.sqrt(2. / n))
176 elif isinstance(m, self.normlayer.func):
177 m.weight.data.fill_(1)
178 m.bias.data.zero_()
179
180 def _make_layer(self, block, planes, blocks, stride=1):
181 downsample = None
182 if stride != 1 or self.inplanes != planes * block.expansion:
183 downsample = nn.Sequential(
184 nn.Conv2d(self.inplanes, planes * block.expansion,
185 kernel_size=1, stride=stride, bias=False),
186 self.normlayer(planes * block.expansion),
187 )
188
189 layers = []
190 layers.append(block(self.inplanes, planes, stride, downsample, normlayer=self.normlayer))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected