Open bittergourd1224 opened 5 years ago
Hi @bittergourd1224 I made this minimal reproducible example
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
x = np.random.randint(0, 255, size=(1, 10, 10, 3))
g = ImageDataGenerator()
x_g = next(g.flow(x, batch_size=1))
print((x - x_g).sum())
>>> 0.0
The pixel values remain the same. BTW please always submit a minimal reproducible example that supports your claim, thanks.
@rragundez Thanks very much for your reply! I see that the generator returns the same image. But my problem still exists, maybe there's something wrong with the saving or loading process? Here's my code and for rookie like me, I've tried my best to make it short:
`import numpy as np from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array import glob
image1 = np.random.randint(0,255,size=(100,100,3)) datagen = ImageDataGenerator() next(datagen.flow(np.expand_dims(image1,axis=0), batch_size=1, save_to_dir='./', save_prefix='test', saveformat='png')) file=glob.glob("./test*.png") image2=load_img(file[0]) image2=img_to_array(image2) print((image1-image2).sum())`
The result is -127.0 (each time different, but never zero). Could you help me with that?
You're actually right. good catch @bittergourd1224 . When you save images to disk, it does so by scaling the pixel values between [0, 255].
At the moment there is no way to turn that scaling off directly from flow
. I have to say I also find it a bit strange that the image that is generated as a batch can be different from the one saved to file.
I am unaware of the reason for this decision, perhaps @Dref360 can help us with that...
@rragundez A lot of thanks for confirming my guess! I'm glad that I didn't ask a silly question, but a true issue.
did you search in the issues history for something similar? perhaps it has been already asked and answered.
@rragundez Yes, I searched for issues concerning ImageDataGenerator but didn't find a similar one.
Hi @bittergourd1224 do you mind checking #244 and commenting in the PR if this solve your problem. I think it does.
@rragundez Thanks for raising this PR. Though not totally understanding your conversation, I've left a comment there.
I found a way around this. I had the same problem, that when we save to disk after we datagen the images are scale automatically between [0,1] so if your maximum value in an image is 155/255 it will be converted to 1 in the range between 0 and 1. This is obviously an issue... To go around is to keep track of the maximum pixel value, and after you generate the images, get the images again and rescale them appropriately. This solved my issue even though it is a convoluted solution.
Hey guys, I'll just state out the core problem. If I use ImageDataGenerator to read the images and then output without any change, the output image still looks different to the original ones. Here's my code:
img=load_img("anyimage.jpg") img=img_to_array(img) img=np.expand_dims(img,axis=0) datagen = ImageDataGenerator() k=0 for batch in datagen.flow(img, batch_size=1, save_to_dir='', save_prefix='', save_format='jpg'): k=k+1 if k==1: break
Since all the parameters in ImageDataGenerator are set to false by default, the output image should be exactly the same as the input image. But actually the output image looks "brighter", and that can be confirmed by examining the pixel values. Could anyone help me with that? I'm greatly thankful here.
You may wonder why I use ImageDataGenerator to do "nothing". Actually I simplify the problem. While the whole issue is, I use the ImageDataGenerator to generate "rotation, fliping and horizontal & vertical shifting" images, but the colour of all the images generated are also changed which I don't expect. So after training with these images, all the generated images pass the neural network, but the original images fail a lot. The colour change really confuses me.
Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or on the Keras Slack channel instead of opening a GitHub issue.
Thank you!
[x] Check that you are up-to-date with the master branch of keras-preprocessing. You can update with:
pip install git+git://github.com/keras-team/keras-preprocessing.git --upgrade --no-deps
[X] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).