MCPcopy Create free account
hub / github.com/SooLab/CGFormer / PatchEmbed

Class PatchEmbed

model/backbone.py:291–331  ·  view source on GitHub ↗

Image to Patch Embedding Args: patch_size (int): Patch token size. Default: 4. in_chans (int): Number of input image channels. Default: 3. embed_dim (int): Number of linear projection output channels. Default: 96. norm_layer (nn.Module, optional): Normalization

Source from the content-addressed store, hash-verified

289
290
291class PatchEmbed(nn.Module):
292 """ Image to Patch Embedding
293
294 Args:
295 patch_size (int): Patch token size. Default: 4.
296 in_chans (int): Number of input image channels. Default: 3.
297 embed_dim (int): Number of linear projection output channels. Default: 96.
298 norm_layer (nn.Module, optional): Normalization layer. Default: None
299 """
300
301 def __init__(self, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None):
302 super().__init__()
303 patch_size = to_2tuple(patch_size)
304 self.patch_size = patch_size
305
306 self.in_chans = in_chans
307 self.embed_dim = embed_dim
308
309 self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
310 if norm_layer is not None:
311 self.norm = norm_layer(embed_dim)
312 else:
313 self.norm = None
314
315 def forward(self, x):
316 """Forward function."""
317 # padding
318 _, _, H, W = x.size()
319 if W % self.patch_size[1] != 0:
320 x = F.pad(x, (0, self.patch_size[1] - W % self.patch_size[1]))
321 if H % self.patch_size[0] != 0:
322 x = F.pad(x, (0, 0, 0, self.patch_size[0] - H % self.patch_size[0]))
323
324 x = self.proj(x) # B C Wh Ww
325 if self.norm is not None:
326 Wh, Ww = x.size(2), x.size(3)
327 x = x.flatten(2).transpose(1, 2)
328 x = self.norm(x)
329 x = x.transpose(1, 2).view(-1, self.embed_dim, Wh, Ww)
330
331 return x
332
333
334class MultiModalSwinTransformer(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected