cancel
Showing results for 
Search instead for 
Did you mean: 

RGB Value Normalization

pmiracle
Associate II

Hey,

Looking at the image classification application example, I couldn't find any part of the code that handles the normalization of pixel value from [0,255] to [-1,1] or [0,1] that I would expect from a CV model of that kind. 

Do you mind explaining how this implementation that is based on the mobilenet_v2_0 skips the need for normalization?

Thanks.

 

5 REPLIES 5
Julian E.
ST Employee

Hello @pmiracle,

 

In the example you linked, the deployed model is a model from model zoo: 

mobilenet_v2_0.35_128_fft_int8.tflite

 

This model takes a float32 input [-1,1] and contains the scale and offset:

JulianE_0-1758528831057.png

 

The yaml from model zoo only indicate the scripts how to edit the getting started application for model zoo to deploy it on the stm32. ie:

JulianE_0-1758529015167.png

If you run the deployment yaml throught the stm32ai_main.py, you can look at the edited and deployed application here:  /application_code/image_classification/STM32N6/

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Thanks for the reply,

This doesn't makes sense to me, because the camera on the DevKit creates RGB values in the range of 0-255 and there is no code in the example project that I have seen that translates these values to -1,1.

Any explanation?

Hello @pmiracle,

 

You're right, I misread. 

The float32[-1:1] is the output (look at the image).

The input is uint8[128,128,3] and most likely RGB values in the range of 0-255 as you pointed out.

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

But MobileNet expects values [0,1], so what am I missing?

Hello @pmiracle,

There are two different cases depending on whether you’re talking about the float model or the quantized model.

 

Case 1:  Float (FP32 / FP16) MobileNetV2

  • The original MobileNetV2 models trained on ImageNet usually expect input normalized to:
    • either [0,1] (just divide by 255)
    • or sometimes normalized further with mean/std (e.g. PyTorch convention: (x/255 - mean) / std).
  • In TensorFlow Lite’s reference MobileNetV2 float models, the convention is simply [0,1].

So if you are using the non-quantized version, the comment “MobileNet expects values [0,1]” is correct. 

 

Case 2: Quantized (INT8 / UINT8) MobileNetV2

  • When you quantize the model, the input is no longer float but integers.
  • The quantization metadata (scale + zero_point) encodes how to map from uint8 [0,255] to the internal float domain.
  • In your case: float = 0.0078431377 * (q - 127) which corresponds to an internal domain of [-1, +1].
    • q = 0 → float ≈ -1

    • q = 127 → float = 0

    • q = 255 → float ≈ +1

  • But the TFLite/STM32 runtime expects you to feed uint8 [0,255], and it applies the scaling automatically.

 

So for quantized models, you do not normalize to [0,1] yourself. You just give raw 8-bit pixels, and the model’s quantization parameters handle the mapping.

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.