Fix the uint8 wrap model for pixel normalization
Your task
An AI normalized uint8 image pixels for a CNN, subtracting an integer mean while the array was still uint8. In unsigned 8-bit arithmetic, a pixel below the mean wraps around to a large value instead of going negative, with no error, and training stalled on a plateau. Your team models the normalization both ways to compare them.
An assistant wrote normalize_pixel_batch(pixels, mean_value, scale, wrap_uint8). It is in the editor. It has a bug. Find it and fix it.
Input and output:
pixels: pixel values from 0 to 255.mean_value: the mean to subtract.scale: the number to divide by.wrap_uint8: true to model the uint8 subtraction, false for the float path.- Return the normalized values, in order.
Rules the tests check:
- If
scaleis 0, return an empty list. - For each pixel, take the difference: the pixel minus
mean_value. - When
wrap_uint8is true, replace the difference with the difference modulo 256, a whole number from 0 to 255, the way uint8 subtraction wraps. So 5 minus 10 becomes 251. - Then divide by
scale. - Answers within 0.000001 of the expected values pass.
Examples
normalize_pixel_batch(pixels: list[int], mean_value: int, scale: float, wrap_uint8: bool) -> list[float]Example 1
Input
normalize_pixel_batch([5, 200, 255], 10, 1, True)Output
[251, 190, 245]Example 2
Input
normalize_pixel_batch([5, 200, 255], 10, 1, False)Output
[-5, 190, 245]