[Bug] ImageDataGenerator: nonsensical warning re channels
See original GitHub issueFitting 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
- Ubuntu 14.04
- Python 2.7.6
- Keras 2.0.5
- Theano 0.9.0
- Tensorflow 1.2.0
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (1 by maintainers)
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.