MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / SmallEncoder

Class SmallEncoder

core/extractor.py:195–267  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

193
194
195class SmallEncoder(nn.Module):
196 def __init__(self, output_dim=128, norm_fn='batch', dropout=0.0):
197 super(SmallEncoder, self).__init__()
198 self.norm_fn = norm_fn
199
200 if self.norm_fn == 'group':
201 self.norm1 = nn.GroupNorm(num_groups=8, num_channels=32)
202
203 elif self.norm_fn == 'batch':
204 self.norm1 = nn.BatchNorm2d(32)
205
206 elif self.norm_fn == 'instance':
207 self.norm1 = nn.InstanceNorm2d(32)
208
209 elif self.norm_fn == 'none':
210 self.norm1 = nn.Sequential()
211
212 self.conv1 = nn.Conv2d(3, 32, kernel_size=7, stride=2, padding=3)
213 self.relu1 = nn.ReLU(inplace=True)
214
215 self.in_planes = 32
216 self.layer1 = self._make_layer(32, stride=1)
217 self.layer2 = self._make_layer(64, stride=2)
218 self.layer3 = self._make_layer(96, stride=2)
219
220 self.dropout = None
221 if dropout > 0:
222 self.dropout = nn.Dropout2d(p=dropout)
223
224 self.conv2 = nn.Conv2d(96, output_dim, kernel_size=1)
225
226 for m in self.modules():
227 if isinstance(m, nn.Conv2d):
228 nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
229 elif isinstance(m, (nn.BatchNorm2d, nn.InstanceNorm2d, nn.GroupNorm)):
230 if m.weight is not None:
231 nn.init.constant_(m.weight, 1)
232 if m.bias is not None:
233 nn.init.constant_(m.bias, 0)
234
235 def _make_layer(self, dim, stride=1):
236 layer1 = BottleneckBlock(self.in_planes, dim, self.norm_fn, stride=stride)
237 layer2 = BottleneckBlock(dim, dim, self.norm_fn, stride=1)
238 layers = (layer1, layer2)
239
240 self.in_planes = dim
241 return nn.Sequential(*layers)
242
243
244 def forward(self, x):
245
246 # if input is list, combine batch dimension
247 is_list = isinstance(x, tuple) or isinstance(x, list)
248 if is_list:
249 batch_dim = x[0].shape[0]
250 x = torch.cat(x, dim=0)
251
252 x = self.conv1(x)

Callers 2

__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected