MCPcopy Create free account
hub / github.com/pytorch/executorch / _patchify

Function _patchify

examples/models/gemma4/image_utils.py:113–125  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

111
112
113def _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
128def _compute_position_ids(h_patches: int, w_patches: int) -> np.ndarray:

Callers 1

preprocess_imageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected