Skip to content

Commit 0f60098

Browse files
pierrot0The TensorFlow Datasets Authors
authored and
The TensorFlow Datasets Authors
committed
fix apply_colormap to use with numpy2.
PiperOrigin-RevId: 668451921
1 parent 858fbe5 commit 0f60098

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

tensorflow_datasets/core/utils/image_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ def get_colormap() -> np.ndarray:
127127
"""
128128
colormap_path = resource_utils.tfds_path() / 'core/utils/colormap.csv'
129129
with colormap_path.open() as f:
130-
return np.array(list(csv.reader(f)), dtype=np.uint8)
130+
colormap = np.array(list(csv.reader(f)), dtype=np.uint8)
131+
assert colormap.shape == (256, 3)
132+
return colormap
131133

132134

133135
def apply_colormap(image: np.ndarray) -> np.ndarray:
134136
"""Apply colormap from grayscale (h, w, 1) to colored (h, w, 3) image."""
135137
image = image.squeeze(axis=-1) # (h, w, 1) -> (h, w)
136138
cmap = get_colormap() # Get the (256, 3) colormap
137-
# Normalize uint16 and convert each value to a unique color
138-
return cmap[image % len(cmap)]
139+
# Convert the image to uint64 first to avoid overflow.
140+
return cmap[(image.astype(np.int64) % 256)]
139141

140142

141143
# Visualization single image

0 commit comments

Comments
 (0)