Skip to content

Commit e23f951

Browse files
authored
Arm backend: Fix conversion from uint8 to fp32 for img_class application (pytorch#18607)
1 parent 3a6314e commit e23f951

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

examples/arm/image_classification_example_ethos_u/runtime/rgb_to_array.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -37,7 +37,11 @@ def convert_image_to_c_array(
3737
img = resize_and_crop_center(img, image_size)
3838
# NumPy arrays are stored in channels-last format. Convert to channels-first.
3939
img_channels_first = np.transpose(img, (2, 0, 1))
40-
data = np.array(img_channels_first, dtype=np.float32) / 255.0
40+
# The PIL Image function returns the image represented in the range [0;255].
41+
# However, the NN was trained on fp32 numbers in the range [-1;1].
42+
# To pass from [0;255] to [-1;1], we divide by 127.5(the middle of the range)
43+
# and subtract 1.
44+
data = np.array(img_channels_first, dtype=np.float32) / 127.5 - 1
4145
data = data.flatten()
4246
# Format as C array
4347
array_lines = []

0 commit comments

Comments
 (0)