Convert (C, H, W) image to (num_patches, patch_dim) patches. Patches are extracted in row-major order (left-to-right, top-to-bottom).
(image: np.ndarray)
| 111 | |
| 112 | |
| 113 | def _patchify(image: np.ndarray) -> np.ndarray: |
| 114 | """Convert (C, H, W) image to (num_patches, patch_dim) patches. |
| 115 | |
| 116 | Patches are extracted in row-major order (left-to-right, top-to-bottom). |
| 117 | """ |
| 118 | c, h, w = image.shape |
| 119 | ph = h // PATCH_SIZE |
| 120 | pw = w // PATCH_SIZE |
| 121 | # (C, ph, patch_size, pw, patch_size) -> (ph, pw, patch_size, patch_size, C) -> (ph*pw, patch_dim) |
| 122 | patches = image.reshape(c, ph, PATCH_SIZE, pw, PATCH_SIZE) |
| 123 | patches = patches.transpose(1, 3, 2, 4, 0) |
| 124 | patches = patches.reshape(ph * pw, -1) |
| 125 | return patches |
| 126 | |
| 127 | |
| 128 | def _compute_position_ids(h_patches: int, w_patches: int) -> np.ndarray: |