The "Invalid Photograph" error is a
Invalid Photograph error for about 15% of the uploaded claim images. The frustrating part is that these images were standard JPEGs and PNGs, and they passed every basic validation check in my preprocessing script before hitting the embedding layer.The error looked like this in my logs:
RuntimeError: Tensor shape mismatch in vision_encoder.
Error Code: 0x8821 - Invalid Photograph: Input dimensions exceed maximum allowed tensor size or contain NaN values.At first, I thought it was a corrupted file issue. I ran a loop to check for truncated files or header corruption using PIL, but everything came back clean. The weird thing was that the "invalid" photos were mostly high-resolution shots from newer iPhones—the kind of images that are technically valid but have massive dimensions.
I spent three hours digging through the model's documentation, which was uselessly vague about the "Invalid Photograph" trigger. I decided to intercept the tensors right before they hit the encoder. I wrote a quick debug wrapper to print the tensor shapes and check for NaNs.
import torch
def debug_tensor(tensor):
if torch.isnan(tensor).any():
print("NaN detected!")
print(f"Tensor Shape: {tensor.shape}")
print(f"Max Value: {torch.max(tensor)}")
print(f"Min Value: {torch.min(tensor)}")It turned out that the preprocessing library I was using for normalization was occasionally producing Inf or NaN values when it encountered specific EXIF orientation tags combined with extremely high resolution. The model wasn't actually complaining that the "photograph" was invalid in a human sense; it was crashing because the normalization math exploded on specific pixel distributions, resulting in a tensor that the C++ backend of the model couldn't handle.
The "Invalid Photograph" error is essentially a catch-all for "the tensor resulting from this image is garbage."
I solved it by forcing a hard resize to 1024px on the longest edge and stripping EXIF data before the normalization step. Here is the fix that stopped the crashes:
Image Preprocessing Fix:
- Resize first: Use
ImageOps.exif_transposeto handle rotation correctly. - Downscale: Clamp the max dimension to 1024px to prevent memory spikes.
- Explicit Cast: Ensure the image is converted to
RGBto avoid alpha channel issues with PNGs that were triggering weird normalization offsets.
from PIL import Image, ImageOps
def sanitize_image(img_path):
with Image.open(img_path) as img:
img = ImageOps.exif_transpose(img)
img = img.convert("RGB")
img.thumbnail((1024, 1024), Image.Resampling.LANCZOS)
return imgIf you're seeing this error with a multimodal model, don't waste time checking if the file is "corrupt." Check your tensor values for NaNs and look at your image dimensions. The error message is a lie; it's a math problem, not a file format problem.
All Replies (0)
No replies yet — be the first!
