4

I've searched for similar questions but found no solution to what i am trying to do. I have 16-bit grayscale images, and i am trying to fit them in keras ImageDataGenerator. When using functions like: flow_from_dataframe, it yields images all with the same pixel value (incorrect).

I tried using the keras preprocess_input, rescale to [0,1], to [-1,1] with a custom preprocessing function, but none of this worked. I also set the color_mode='grayscale' in ImageDataGenerator.

I further tested converting to 8-bit, and it worked. I triplicated the number of channels, and it was not the issue, since it still worked with 8-bit. I've read that keras in this case uses PIL library to read images, and since it doesn't treat 16-bit correctly, it returns that error. I saw that we could set a different library for loading images, but i don't know how to do that.

Doas anyone know an alternative for using 16-bit images? In the last case, i would try a custom generator, but i would really like to profit from already built and tested functions for this purpose.

I aim to use those images to fine-tune a pre-trained network, so i would like to standardize the type of input i use.

Thanks.

2 Answers2

3

You might want to write your own ImageDataGenerator and overwrite some methods to load your data as you expect it.

There are some great blog articles that explain everything you need.

You then have to make sure that the overwritten __data_generation method loads your .tiff images correctly into numpy arrays or you convert them beforehand and just load them.

It is also quite straightforward to test your generator (do it!) and check if it produces the output you expect.

53RT
  • 649
  • 3
  • 20
0

This is a bug in keras_preprocessing versions <= 1.1.0. Use the package directly from the source code on github to fix it. Type the following command to update your package:

pip install git+git://github.com/keras-team/keras-preprocessing.git --upgrade --no-deps

And, with color_mode='grayscale', your ImageDataGenerator will load 16-bit grayscale images correctly.

This bug was fixed by Rodrigo Agundez (see his post and the pull request for more details) and should be published in a next release. The problem comes from keras_preprocessing when loading an image:

# PIL library
img = pil_image.open(io.BytesIO(f.read()))

# This was the bug: converting with L mode will truncate all images to 8-bit.
# All values > 255 will be set to 255
if color_mode == 'grayscale':
    img = img.convert('L')

# This is the fix
if color_mode == 'grayscale':
    # if image is not already an 8-bit, 16-bit or 32-bit grayscale image
    # convert it to an 8-bit grayscale image.
    if img.mode not in ('L', 'I;16', 'I'):
        img = img.convert('L')

Be aware that if you load a 16-bit grayscale image with color_mode='rgb' (let's say if you want to convert your single grayscale channel into a 3 channel image) it wont work as the 16-bit grayscale image will be converted to 8-bit.