keras-team/keras-preprocessing

How to make keras ImageGenerator load float32 images

sam6485 opened this issue · 1 comments

For an image to image regression task, I have 4 gray scale images with continues pixel ranges in float32 data type. The data ranges are: [0 to 790.65],[ -2.74174 to 2.4126 ],[ 150.87 to 260.45],[ -32.927 to 69.333].
The first three images were already stacked to form a color_mode=’rgb’ and the last image will be used as response image based on color_mode=’grayscale’. I am trying to open the image using ImageDataGenerator and flow_from_directory using Keras.

The order of folders inside the train folder is:
image>img
label>img

Here is the code for feeding the predictor and response images using ImageDataGenerator:

batch_size = 2
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator()
img_train_generator = train_datagen.flow_from_directory(
"train/image/",
target_size=(128,128),
batch_size=batch_size,
class_mode= None,
color_mode='rgb',
shuffle=False)
label_train_generator = train_datagen.flow_from_directory(
"train/label/",
target_size=(128,128),
batch_size=batch_size,
class_mode=None,
shuffle=False,
color_mode='grayscale')

For this initial test the result is:
Found 22 images belonging to 1 classes.
Found 22 images belonging to 1 classes.

After this step, I use the following code to finalize the preparation of X_train, y_train:

X_train, y_train=next(train_generator)
X_train.shape, y_train.shape

However, this code returns error related to “UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x00000251E837DAF0>” and the error log can be seen below.

I have looked at the #242, but as I am new in working with keras, I am struggling to solve the problem. For this regression task, it is important to work with images with decimal places and float32 data type. Could anyone please help to resolve this issue or suggest how I can use another generator to load color_mode of ’grayscale’ and ‘rgb’ with float32 data type?

To check if the master branch of keras-preprocessing is up-to-date, I already run the following code in Anaconda Prompt and there was no error: pip install git+git://github.com/keras-team/keras-preprocessing.git --upgrade --no-deps

Thanks in advance!


UnidentifiedImageError Traceback (most recent call last)
in
----> 1 X_train, y_train=next(train_generator)
2 X_train.shape, y_train.shape

~.conda\envs\myenv-36\lib\site-packages\keras_preprocessing\image\iterator.py in next(self, *args, **kwargs)
102
103 def next(self, *args, **kwargs):
--> 104 return self.next(*args, **kwargs)
105
106 def next(self):

~.conda\envs\myenv-36\lib\site-packages\keras_preprocessing\image\iterator.py in next(self)
114 # The transformation of images is not under thread lock
115 # so it can be done in parallel
--> 116 return self._get_batches_of_transformed_samples(index_array)
117
118 def _get_batches_of_transformed_samples(self, index_array):

~.conda\envs\myenv-36\lib\site-packages\keras_preprocessing\image\iterator.py in _get_batches_of_transformed_samples(self, index_array)
228 color_mode=self.color_mode,
229 target_size=self.target_size,
--> 230 interpolation=self.interpolation)
231 x = img_to_array(img, data_format=self.data_format)
232 # Pillow images should be closed after load_img,

~.conda\envs\myenv-36\lib\site-packages\keras_preprocessing\image\utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
112 'The use of load_img requires PIL.')
113 with open(path, 'rb') as f:
--> 114 img = pil_image.open(io.BytesIO(f.read()))
115 if color_mode == 'grayscale':
116 # if image is not already an 8-bit, 16-bit or 32-bit grayscale image

~.conda\envs\myenv-36\lib\site-packages\PIL\Image.py in open(fp, mode, formats)
2957 warnings.warn(message)
2958 raise UnidentifiedImageError(
-> 2959 "cannot identify image file %r" % (filename if filename else fp)
2960 )
2961

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x00000251E837DAF0>

Hello,
We only support 32-bits integers images, in your cases, the images are floats I think.

If you need more flexibility, maybe tf.data will help you?