MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / PatchEmbed

Class PatchEmbed

PATH/core/models/backbones/vit.py:286–314  ·  view source on GitHub ↗

Image to Patch Embedding

Source from the content-addressed store, hash-verified

284
285
286class PatchEmbed(nn.Module):
287 """ Image to Patch Embedding
288 """
289
290 def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768, proj_padding=False):
291 super().__init__()
292 img_size = to_2tuple(img_size)
293 patch_size = to_2tuple(patch_size)
294 self.patch_shape = (img_size[0] // patch_size[0], img_size[1] // patch_size[1]) # could be dynamic
295 self.num_patches = self.patch_shape[0] * self.patch_shape[1] # could be dynamic
296 self.img_size = img_size
297 self.patch_size = patch_size
298
299 if proj_padding:
300 self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size, padding=2)
301 else:
302 self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
303
304 def forward(self, x, **kwargs):
305 B, C, H, W = x.shape
306
307 # FIXME look at relaxing size constraints
308 # assert H == self.img_size[0] and W == self.img_size[1], \
309 # f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
310 x = self.proj(x)
311 Hp, Wp = x.shape[2], x.shape[3]
312
313 x = x.flatten(2).transpose(1, 2)
314 return x, (Hp, Wp)
315
316
317class HybridEmbed(nn.Module):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected