# Image normalization data range

Hi, I would like to normalize the image data to the range [-1,1], Here is the code I used to do the transform.

``````compose_T1 = transforms.Compose([transforms.ToPILImage(),
transforms.Resize((128,128),interpolation=Image.NEAREST),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
``````

But after the transform, I find my data is in the range [-1,0.3412], checking the original data, I find out my orignal data range is [0,172], in this case, should I do the preprocess using data/172*255 in order to get the final data range [-1,1]?

It depends on how you read the image. Typically, torch dataset classes read the image and store in the range 0-1, hence using mean 0.5, std 0.5 makes the data in the range -1, 1. If RGB image from your dataset is with range 0-255, you can either divide it by 255 to keep it in consensus with typical torch approach or use mean 128, std 127.

About using a specific value 172, you may need to consider a larger use case rather than just the dataset that you have. If it’s RGB, ofcourse there could be test images with range more than 172.

Hope it helps.
{"ft_lang_label":"__label__en","ft_lang_prob":0.73343384,"math_prob":0.96957475,"size":553,"snap":"2022-05-2022-21","text_gpt3_token_len":162,"char_repetition_ratio":0.18397085,"word_repetition_ratio":0.0,"special_character_ratio":0.3381555,"punctuation_ratio":0.2589928,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96610886,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-18T14:26:14Z\",\"WARC-Record-ID\":\"<urn:uuid:3efd40ad-f2f4-4ef3-be83-a9329dd47e6e>\",\"Content-Length\":\"15183\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8e57f57b-a018-4a08-b018-62a769ddf64d>\",\"WARC-Concurrent-To\":\"<urn:uuid:bbac562b-935a-4e31-a66b-0ab45ecabf3e>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/image-normalization-data-range/44477\",\"WARC-Payload-Digest\":\"sha1:NDVMZBUB3Q4NYHVQKEQI4SZWABE4E53G\",\"WARC-Block-Digest\":\"sha1:IFKP4N5BDETZMSDPPGX2RTWXO3HJLHQJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662522270.37_warc_CC-MAIN-20220518115411-20220518145411-00256.warc.gz\"}"}