Fitting ImageDataGenerator insists on giving warning that channels should be in axis 1 (for Theano) or axis 3 (for Tensorflow), despite that being the case.
Reproducible code with MNIST data:
import os
os.environ["KERAS_BACKEND"] = "tensorflow"
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from keras import backend as K
K.set_image_dim_ordering('tf')
K.image_data_format() # 'channels_last'
(x_train, y_train), (x_test, y_test) = mnist.load_data()
img_rows, img_cols = 28, 28
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train.shape # (60000, 28, 28, 1)
datagen = ImageDataGenerator()
datagen.fit(x_train)
Warning:
/var/venv/tsats/local/lib/python2.7/site-packages/keras/preprocessing/image.py:648: UserWarning: Expected input to be images (as Numpy array) following the data format convention "channels_last" (channels on axis 3), i.e. expected either 1, 3 or 4 channels on axis 3. However, it was passed an array with shape (60000, 28, 28, 1) (1 channels).
' (' + str(x.shape[self.channel_axis]) + ' channels).')
despite the fact that channels are indeed on axis 3 with an admissible value of 1.
Changing the backend to Theano and image ordering to th, so that data format is now channels_first and x_train.shape is (60000, 1, 28, 28), gives a similarly nonsensical warning:
/var/venv/tsats/local/lib/python2.7/site-packages/keras/preprocessing/image.py:648: UserWarning: Expected input to be images (as Numpy array) following the data format convention "channels_first" (channels on axis 1), i.e. expected either 1, 3 or 4 channels on axis 1. However, it was passed an array with shape (60000, 1, 28, 28) (1 channels).
' (' + str(x.shape[self.channel_axis]) + ' channels).')
Tested with
I am also getting same issue on following:-
MacOS 10.12.6
Python 3.5.3
keras 2.0.6
tensorflow 0.12.1
This is fixed on master. Please update and close this issue.
This is now working with tensorflow 1.2.1 backend, and keras 2.0.6
I can still reproduce the problem on Ubuntu 14 (python 2.7.6) with the above mentioned versions
>>> keras.__version__
'2.0.6'
>>> tensorflow.__version__
'1.2.1'
Actually, this doesn't happen if you have booted freshly and not terminated any ipython kernel. But, it starts to happen again if some kernel is killed or restarted. It's a little weird.
Hi @dhingratul
Thanks for the reply. I checked the source code, it seems that source code is wrong:
line 646:
if x.shape[self.channel_axis] not in {3, 4}:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Most helpful comment
Hi @dhingratul
Thanks for the reply. I checked the source code, it seems that source code is wrong:
line 646:
if x.shape[self.channel_axis] not in {3, 4}: