MCPcopy Create free account
hub / github.com/drinkingcoder/FlowFormer-Official / BasicEncoder

Class BasicEncoder

core/extractor.py:118–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116 return self.relu(x+y)
117
118class BasicEncoder(nn.Module):
119 def __init__(self, output_dim=128, norm_fn='batch', dropout=0.0):
120 super(BasicEncoder, self).__init__()
121 self.norm_fn = norm_fn
122
123 if self.norm_fn == 'group':
124 self.norm1 = nn.GroupNorm(num_groups=8, num_channels=64)
125
126 elif self.norm_fn == 'batch':
127 self.norm1 = nn.BatchNorm2d(64)
128
129 elif self.norm_fn == 'instance':
130 self.norm1 = nn.InstanceNorm2d(64)
131
132 elif self.norm_fn == 'none':
133 self.norm1 = nn.Sequential()
134
135 self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
136 self.relu1 = nn.ReLU(inplace=True)
137
138 self.in_planes = 64
139 self.layer1 = self._make_layer(64, stride=1)
140 self.layer2 = self._make_layer(96, stride=2)
141 self.layer3 = self._make_layer(128, stride=2)
142
143 # output convolution
144 self.conv2 = nn.Conv2d(128, output_dim, kernel_size=1)
145
146 self.dropout = None
147 if dropout > 0:
148 self.dropout = nn.Dropout2d(p=dropout)
149
150 for m in self.modules():
151 if isinstance(m, nn.Conv2d):
152 nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
153 elif isinstance(m, (nn.BatchNorm2d, nn.InstanceNorm2d, nn.GroupNorm)):
154 if m.weight is not None:
155 nn.init.constant_(m.weight, 1)
156 if m.bias is not None:
157 nn.init.constant_(m.bias, 0)
158
159 def _make_layer(self, dim, stride=1):
160 layer1 = ResidualBlock(self.in_planes, dim, self.norm_fn, stride=stride)
161 layer2 = ResidualBlock(dim, dim, self.norm_fn, stride=1)
162 layers = (layer1, layer2)
163
164 self.in_planes = dim
165 return nn.Sequential(*layers)
166
167
168 def forward(self, x):
169
170 # if input is list, combine batch dimension
171 is_list = isinstance(x, tuple) or isinstance(x, list)
172 if is_list:
173 batch_dim = x[0].shape[0]
174 x = torch.cat(x, dim=0)
175

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected