analogdevicesinc / ai8x-training

Model Training for ADI's MAX78000 and MAX78002 Edge AI Devices
Apache License 2.0
86 stars 76 forks source link

normalization range #228

Closed nikky4D closed 1 year ago

nikky4D commented 1 year ago

From the readme, normalization with ai8x.normalization should set the training/test data to range [-128, 128]. But, in some of the scripts eg datasets/imagenet.py, it says to map the data to this range [-128/128, +127/128]. Which one should we use?

Also, Is there a function to unnormalize this process so I can view the input image? I am using this: (128 + (img.detach().cpu().numpy()) + 1 ).astype(np.uint8) where my img is : img = ai8x.normalize(torch.transform.ToTensor(PIL.Image.open(img_path)))

alicangok commented 1 year ago

Hi,

Quoting from the "normalizing input data" section of the readme:

For training, input data is expected to be in the range [-128/128, +127/128] When evaluating quantized weights, or when running on hardware, input data is instead expected to be in the native MAX78000/MAX78002 range of [-128, +127] Conversely, the majority of PyTorch datasets are PIL images of range [0, 1] The respective data loaders therefore call the ai8x.normalize() function, which expects an input of 0 to 1 and normalizes the data, automatically switching between the two supported data ranges.

As you can see in ai8x.py, ai8x.normalize() sets the limits as explained, by checking if act_mode_8bit is set to True. This flag is set to True if evaluating on quantized network or running on hardware, and False during training. Thus, the function returns an image using one of the two possible normalization methods.

As for unnormalization, if you are in training mode (act_mode_8bit is set to False), then first multiplying the normalized image by 128, then adding 128 to this should return the original image.

I hope this clears your confusion. Let us know if there's any conflicting information in the readme.

nikky4D commented 1 year ago

I understand for evaluation. For training, I am unfamiliar with the notation so it confuses me a little. Is it right to say that during training image data is to be in the range [-1, +1 (approx)].

Then given that pytorch makes the conversion to [0, 1], ai8x.normalized() rescales from [0,1] --> [-1, 1]? Is this correct?

MaximGorkem commented 1 year ago

Correct. For training, you have to normalize your data first to [0, 1] and then call ai8x.normalize with act_mode_8bit=False. It rescales the data into [-1, 127/128].

nikky4D commented 1 year ago

Thanks. I understand