(
self,
*,
image_size,
patch_size,
num_classes,
dim,
depth,
heads,
mlp_dim,
channels=3,
dim_head=64
)
| 108 | """ |
| 109 | |
| 110 | def __init__( |
| 111 | self, |
| 112 | *, |
| 113 | image_size, |
| 114 | patch_size, |
| 115 | num_classes, |
| 116 | dim, |
| 117 | depth, |
| 118 | heads, |
| 119 | mlp_dim, |
| 120 | channels=3, |
| 121 | dim_head=64 |
| 122 | ): |
| 123 | super().__init__() |
| 124 | image_height, image_width = pair(image_size) |
| 125 | patch_height, patch_width = pair(patch_size) |
| 126 | |
| 127 | assert ( |
| 128 | image_height % patch_height == 0 and image_width % patch_width == 0 |
| 129 | ), "Image dimensions must be divisible by the patch size." |
| 130 | |
| 131 | patch_dim = channels * patch_height * patch_width |
| 132 | |
| 133 | self.to_patch_embedding = nn.Sequential( |
| 134 | Rearrange( |
| 135 | "b c (h p1) (w p2) -> b (h w) (p1 p2 c)", |
| 136 | p1=patch_height, |
| 137 | p2=patch_width, |
| 138 | ), |
| 139 | nn.LayerNorm(patch_dim), |
| 140 | BitLinear(patch_dim, dim), |
| 141 | nn.LayerNorm(dim), |
| 142 | ) |
| 143 | |
| 144 | self.pos_embedding = posemb_sincos_2d( |
| 145 | h=image_height // patch_height, |
| 146 | w=image_width // patch_width, |
| 147 | dim=dim, |
| 148 | ) |
| 149 | |
| 150 | self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim) |
| 151 | |
| 152 | self.pool = "mean" |
| 153 | self.to_latent = nn.Identity() |
| 154 | |
| 155 | self.linear_head = nn.LayerNorm(dim) |
| 156 | |
| 157 | def forward(self, img): |
| 158 | device = img.device |
nothing calls this directly
no test coverage detected