WZMIAOMIAO/deep-learning-for-image-processing · error · Exception
The input image should np.float32 in the range [0, 1]
Error message
The input image should np.float32 in the range [0, 1]
What it means
grad_cam's show_cam_on_image overlays a heatmap onto img, which must be a float32 array normalized to [0, 1]. It detects values > 1 (typically a uint8 0-255 image) and raises rather than producing a washed-out overlay. The check guards the additive blending cam = heatmap + img.
Source
Thrown at pytorch_classification/grad_cam/utils.py:198
use_rgb: bool = False,
colormap: int = cv2.COLORMAP_JET) -> np.ndarray:
""" This function overlays the cam mask on the image as an heatmap.
By default the heatmap is in BGR format.
:param img: The base image in RGB or BGR format.
:param mask: The cam mask.
:param use_rgb: Whether to use an RGB or BGR heatmap, this should be set to True if 'img' is in RGB format.
:param colormap: The OpenCV colormap to be used.
:returns: The default image with the cam overlay.
"""
heatmap = cv2.applyColorMap(np.uint8(255 * mask), colormap)
if use_rgb:
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)
heatmap = np.float32(heatmap) / 255
if np.max(img) > 1:
raise Exception(
"The input image should np.float32 in the range [0, 1]")
cam = heatmap + img
cam = cam / np.max(cam)
return np.uint8(255 * cam)
def center_crop_img(img: np.ndarray, size: int):
h, w, c = img.shape
if w == h == size:
return img
if w < h:
ratio = size / w
new_w = size
new_h = int(h * ratio)
else:View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Normalize before the call: img = np.float32(img) / 255 (and convert BGR->RGB if needed)
- Use cv2.cvtColor + astype(np.float32)/255.0 immediately after cv2.imread
- Clamp/verify range with assert np.max(img) <= 1 in your preprocessing
Example fix
// before rgb_img = cv2.imread(path) cv2.waitKey() show_cam_on_image(rgb_img, grayscale_cam) // after rgb_img = cv2.imread(path)[:, :, ::-1] rgb_img = np.float32(rgb_img) / 255 show_cam_on_image(rgb_img, grayscale_cam)
Defensive patterns
Strategy: type-guard
Validate before calling
img = cv2.imread(path)[:, :, ::-1] assert img.dtype == np.uint8 img = np.float32(img) / 255.0 assert 0.0 <= img.min() and img.max() <= 1.0
Type guard
def is_unit_float_image(img: 'np.ndarray') -> bool:
return img.dtype == np.float32 and img.max() <= 1.0 and img.min() >= 0.0 Try / catch
try:
visualization = show_cam_on_image(img, grayscale_cam)
except Exception as e:
if 'np.float32 in the range [0, 1]' in str(e):
img = np.float32(img) / 255.0
visualization = show_cam_on_image(img, grayscale_cam)
else:
raise Prevention
- Standardize a to_float_rgb() helper applied to every image before CAM utilities
- Never pass cv2.imread output directly to show_cam_on_image
- Assert dtype/float32 and range in your visualization pipeline
When it happens
Trigger: Calling show_cam_on_image(img, mask) with img read by cv2.imread or kept as np.uint8 in [0, 255]; also fires if img is float32 but scaled beyond 1.0.
Common situations: Forgetting the standard preprocessing img = np.float32(img) / 255 after cv2.imread; passing a PIL image converted via np.array without scaling; mixing BGR uint8 OpenCV reads with CAM utilities.
Related errors
- not support data format '{self.data_format}'
- image: {} isn't RGB mode.
- Transformer input dimension should be divisible by head dime
- image: {} isn't RGB mode.
- Embedding dim must be divisible by number of heads in {}. Go
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/1f30622fb01915e3.
Report an issue: GitHub.