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

Class PatchEmbed

PATH/core/models/backbones/vitdet.py:286–313  ·  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):
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 self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
300
301 def forward(self, x, mask=None, **kwargs):
302 # FIXME look at relaxing size constraints
303 # assert H == self.img_size[0] and W == self.img_size[1], \
304 # f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
305 x = self.proj(x)
306 Hp, Wp = x.shape[2], x.shape[3]
307
308 x = x.flatten(2).transpose(1, 2)
309
310 if mask is not None:
311 mask = F.interpolate(mask[None].float(), size=(Hp, Wp)).to(torch.bool)[0]
312
313 return x, (Hp, Wp), mask
314
315
316class Norm2d(nn.Module):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected