MCPcopy Create free account
hub / github.com/lingeringlight/START / PatchEmbed

Class PatchEmbed

models_mamba.py:40–64  ·  view source on GitHub ↗

2D Image to Patch Embedding

Source from the content-addressed store, hash-verified

38
39
40class PatchEmbed(nn.Module):
41 """ 2D Image to Patch Embedding
42 """
43 def __init__(self, img_size=224, patch_size=16, stride=16, in_chans=3, embed_dim=768, norm_layer=None, flatten=True):
44 super().__init__()
45 img_size = to_2tuple(img_size)
46 patch_size = to_2tuple(patch_size)
47 self.img_size = img_size
48 self.patch_size = patch_size
49 self.grid_size = ((img_size[0] - patch_size[0]) // stride + 1, (img_size[1] - patch_size[1]) // stride + 1)
50 self.num_patches = self.grid_size[0] * self.grid_size[1]
51 self.flatten = flatten
52
53 self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=stride)
54 self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
55
56 def forward(self, x):
57 B, C, H, W = x.shape
58 assert H == self.img_size[0] and W == self.img_size[1], \
59 f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
60 x = self.proj(x)
61 if self.flatten:
62 x = x.flatten(2).transpose(1, 2) # BCHW -> BNC
63 x = self.norm(x)
64 return x
65
66
67class Block(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…