|
| 1 | +import torch |
| 2 | +from torch import nn |
| 3 | +import torch.nn.functional as F |
| 4 | +from einops import repeat |
| 5 | + |
| 6 | +class SimMIM(nn.Module): |
| 7 | + def __init__( |
| 8 | + self, |
| 9 | + *, |
| 10 | + encoder, |
| 11 | + masking_ratio = 0.5 |
| 12 | + ): |
| 13 | + super().__init__() |
| 14 | + assert masking_ratio > 0 and masking_ratio < 1, 'masking ratio must be kept between 0 and 1' |
| 15 | + self.masking_ratio = masking_ratio |
| 16 | + |
| 17 | + # extract some hyperparameters and functions from encoder (vision transformer to be trained) |
| 18 | + |
| 19 | + self.encoder = encoder |
| 20 | + num_patches, encoder_dim = encoder.pos_embedding.shape[-2:] |
| 21 | + self.to_patch, self.patch_to_emb = encoder.to_patch_embedding[:2] |
| 22 | + pixel_values_per_patch = self.patch_to_emb.weight.shape[-1] |
| 23 | + |
| 24 | + # simple linear head |
| 25 | + |
| 26 | + self.mask_token = nn.Parameter(torch.randn(encoder_dim)) |
| 27 | + self.to_pixels = nn.Linear(encoder_dim, pixel_values_per_patch) |
| 28 | + |
| 29 | + def forward(self, img): |
| 30 | + device = img.device |
| 31 | + |
| 32 | + # get patches |
| 33 | + |
| 34 | + patches = self.to_patch(img) |
| 35 | + batch, num_patches, *_ = patches.shape |
| 36 | + |
| 37 | + # for indexing purposes |
| 38 | + |
| 39 | + batch_range = torch.arange(batch, device = device)[:, None] |
| 40 | + |
| 41 | + # get positions |
| 42 | + |
| 43 | + pos_emb = self.encoder.pos_embedding[:, 1:(num_patches + 1)] |
| 44 | + |
| 45 | + # patch to encoder tokens and add positions |
| 46 | + |
| 47 | + tokens = self.patch_to_emb(patches) |
| 48 | + tokens = tokens + pos_emb |
| 49 | + |
| 50 | + # prepare mask tokens |
| 51 | + |
| 52 | + mask_tokens = repeat(self.mask_token, 'd -> b n d', b = batch, n = num_patches) |
| 53 | + mask_tokens = mask_tokens + pos_emb |
| 54 | + |
| 55 | + # calculate of patches needed to be masked, and get positions (indices) to be masked |
| 56 | + |
| 57 | + num_masked = int(self.masking_ratio * num_patches) |
| 58 | + masked_indices = torch.rand(batch, num_patches, device = device).topk(k = num_masked, dim = -1).indices |
| 59 | + masked_bool_mask = torch.zeros((batch, num_patches), device = device).scatter_(-1, masked_indices, 1).bool() |
| 60 | + |
| 61 | + # mask tokens |
| 62 | + |
| 63 | + tokens = torch.where(masked_bool_mask[..., None], mask_tokens, tokens) |
| 64 | + |
| 65 | + # attend with vision transformer |
| 66 | + |
| 67 | + encoded = self.encoder.transformer(tokens) |
| 68 | + |
| 69 | + # get the masked tokens |
| 70 | + |
| 71 | + encoded_mask_tokens = encoded[batch_range, masked_indices] |
| 72 | + |
| 73 | + # small linear projection for predicted pixel values |
| 74 | + |
| 75 | + pred_pixel_values = self.to_pixels(encoded_mask_tokens) |
| 76 | + |
| 77 | + # get the masked patches for the final reconstruction loss |
| 78 | + |
| 79 | + masked_patches = patches[batch_range, masked_indices] |
| 80 | + |
| 81 | + # calculate reconstruction loss |
| 82 | + |
| 83 | + recon_loss = F.l1_loss(pred_pixel_values, masked_patches) / num_masked |
| 84 | + return recon_loss |
0 commit comments