Last active
June 23, 2023 16:36
-
-
Save antoinebrl/cd4f0d431ff1cebf53eb7d7622eff1f1 to your computer and use it in GitHub Desktop.
Rounding error when normalizing color intensity with float16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Images have color pixels encoded as uint8. In computer | |
# vision and deep learning the images is represented with | |
# floatingpoints with value in the range [0,1]. This is | |
# obtained by dividing by 255, the highest color intensity. | |
# However, reduced precision training an inference become | |
# the new standard. | |
# | |
# What is the magnitude of the rouding error when normalizing | |
# an image by 255 rather than 256 (256=2^8)? | |
import numpy as np | |
colors_u8 = np.arange(1, 256).astype(np.uint8) | |
# FP32 precision | |
colors_fp32 = colors_u8.astype(np.float32) | |
colors_fp32_01_255 = colors_fp32 / 255 | |
colors_fp32_01_256 = colors_fp32 / 256 | |
# FP16 precsion | |
colors_fp16 = colors_u8.astype(np.float16) | |
colors_fp16_01_255 = colors_fp16 / 255 | |
colors_fp16_01_256 = colors_fp16 / 256 | |
# Diffenrece | |
relative_diff_255 = (colors_fp32_01_255 - colors_fp16_01_255).astype(np.float64) / colors_fp32_01_255 | |
diff256 = colors_fp32_01_256 - colors_fp16_01_256 | |
print("/255 relative error:", np.max(np.abs(relative_diff_255))) | |
print("/256 maximum erro:", np.max(np.abs(diff256))) | |
# >>> /255 relative error: 0.00042345138386973345 | |
# >>> /256 maximum erro: 0.0 | |
# The rounding error is below 0.05% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment