question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] ImageDataGenerator: nonsensical warning re channels

See original GitHub issue

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

  • Ubuntu 14.04
  • Python 2.7.6
  • Keras 2.0.5
  • Theano 0.9.0
  • Tensorflow 1.2.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jiayiliucommented, Aug 22, 2017

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}:

1reaction
stale[bot]commented, Nov 20, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - error ': Expected input to be images (as Numpy array ...
Close inspection of the warning reveals that it is nonsensical: it asks that the channels be on axis 1 (they are) and that...
Read more >
(PDF) Deep Learning with Python | Joaquim Carreira - Academia.edu
There- fore, the central problem in machine learning and deep learning is to meaningfully transform data: in other words, to learn useful representations...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found