I am a beginner working on a simple CNN to classify X-ray detector images. Due to source intensity, all images have different max values. I want to use ImageDataGenerator to rescale those images to be in range [0,1], but cant't find a way to do that for each individual image. As far as I understand it, people usually just divide by 255 because that is the RGB max, but in my case the max could be anything between 1 and serveral million. Does anyone have an idea how to do this within ImageDataGenerator? Thank you for your time!

Edit: I ran over my images and normalized all of them prior to feeding them to the generator. Inspecting them then showed that the generator had scaled them back to rgb values (I assume due to color_mode). I then used rescale with the standard 1./255 and got the expectd max value of 1. However, after adding back samplewise_std_normalization, I again am getting values larger than 1. I understand why this is happening, but I'm not sure which "rule" is more important to follow: normalize to [0,1] or use samplewise_std_normalization

  • $\begingroup$ First, have you make sure that every image possess similar characteristic as you mentioned? Second, do lookout what the max value might means and if possible find out the theoretical max value(e.g. 255 for normal image). If you still cant find out just calculate all channelwise max before proceeding. $\endgroup$ Dec 29, 2019 at 3:00
  • $\begingroup$ You could always write your own custom data generator in which you can preprocess and augment the different individual images as you like. $\endgroup$
    – Oxbowerce
    Dec 29, 2019 at 12:29
  • $\begingroup$ Thank you both. I have ammended my question $\endgroup$ Dec 29, 2019 at 14:38

1 Answer 1


Normalize to $[0, 1]$ is what I'd do to ensure that the model gets expected (sanitized) inputs.

Using the samplewise_std_normalization is something that I'd do inside the model to highlight features. E.g. a white pixel is more important in a mostly black image than in a noisy image.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.