huggingface/pytorch-image-models · error · ValueError
output_fmt='NCHW' requires a raw image (B, C, H, W) input.
Error message
output_fmt='NCHW' requires a raw image (B, C, H, W) input.
What it means
Gemma4VitEncoder.forward_intermediates can reshape intermediate token outputs into spatial NCHW maps, but the grid can only be recovered from position_ids derived from a raw image input. If reshape=True (output_fmt='NCHW') and the input was pre-patchified or otherwise not a 4D raw image, the reshape is impossible and ValueError is raised.
Source
Thrown at timm/models/gemma4_vit.py:960
def _cb(i: int, y: torch.Tensor) -> None:
if i in take_indices:
intermediates.append(y)
max_block_index = None
if stop_early and not torch.jit.is_scripting():
max_block_index = max_index
x = self._encode(
x,
position_ids,
padding_positions,
block_callback=_cb,
max_block_index=max_block_index,
)
if reshape:
if raw_input_ndim != 4:
raise ValueError("output_fmt='NCHW' requires a raw image (B, C, H, W) input.")
# Recover grid from internal (x, y) position_ids max.
B = position_ids.shape[0]
pW = int(position_ids[..., 0].max().item()) + 1
pH = int(position_ids[..., 1].max().item()) + 1
intermediates = [y.reshape(B, pH, pW, -1).permute(0, 3, 1, 2).contiguous() for y in intermediates]
if output_dict:
result_dict: Dict[str, Any] = {'image_intermediates': intermediates}
if not intermediates_only:
result_dict['image_features'] = x
result_dict['patch_valid'] = ~padding_positions
return result_dict
if intermediates_only:
return intermediates
return x, intermediates
View on GitHub (pinned to 9a5261e31b)
Solutions
- Pass a raw 4D image when you need output_fmt='NCHW'
- Or use output_fmt='NLC' for pre-patchified input and reshape externally using your known grid (from patch_coord maxima)
- Compute the grid yourself: pH/pW = position_ids.max()+1 per axis, then reshape tokens to (B, pH, pW, C) and permute
Example fix
# before feats = enc.forward_intermediates(patches, patch_coord=c, output_fmt='NCHW') # after feats = enc.forward_intermediates(patches, patch_coord=c, output_fmt='NLC') maps = [y.reshape(B, pH, pW, -1).permute(0, 3, 1, 2) for y in feats]
Defensive patterns
Strategy: validation
Validate before calling
raw = x.ndim == 4 and patch_coord is None fmt = 'NCHW' if raw else 'NLC' feats = enc.forward_intermediates(x, patch_coord=patch_coord, output_fmt=fmt)
Type guard
def can_use_nchw_output(x: torch.Tensor) -> bool:
return x.ndim == 4 Try / catch
try:
feats = enc.forward_intermediates(x, patch_coord=c, output_fmt='NCHW')
except ValueError as e:
if 'NCHW' in str(e):
feats = enc.forward_intermediates(x, patch_coord=c, output_fmt='NLC')
else:
raise Prevention
- Request NCHW only when feeding raw 4D images
- Keep a grid-recovery helper that reshapes NLC using patch_coord maxima
- Test feature-extraction paths for both raw and pre-patchified inputs
When it happens
Trigger: Calling enc.forward_intermediates(patches, patch_coord=..., output_fmt='NCHW') with a 3D/5D pre-patchified input instead of a raw (B,C,H,W) image.
Common situations: Reusing the same forward_intermediates call for both raw-image and cached-token paths with a fixed output_fmt; writing generic feature-extraction utilities that always request NCHW.
Related errors
- Expected input ndim in (3, 4, 5); got {x.ndim}.
- patch_coord is required for pre-patchified input.
- Cannot pool {N} tokens with k={k}: N must be divisible by k^
- Image size ({H}, {W}) must be divisible by (patch_size * poo
- Gemma4VitEncoder does not support classification use cases.
AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27).
Data as JSON: /api/errors/9275252cf0670fe7.
Report an issue: GitHub.