4

I have a problem and at the same time a question. I want to make an image classifier with Keras using Theano as Backend and a Sequential model.

>>> keras.__version__
'2.0.1'  
>>> theano.__version__
'0.9.0'

My input shape: INPUT_SHAPE = (3, 28, 28) #depth, size, size

Let's come to my problem. If I run my script at Windows 7 32 Bit, it gives me the error below out:

ValueError: ('The specified size contains a dimension with value <= 0', (-1024, 512))

If run it with the the input shape: INPUT_SHAPE = (28, 28, 3) #size, size, depth
It gives me out this error below:

ValueError: Error when checking model input: expected conv2d_1_input to have shape (None, 48, 48, 3) but got array with shape (1000, 3, 48, 48)

If I run the code on Elementary OS 64 Bit, it runs without any problems (INPUT_SHAPE = (3, 28, 28)).

My keras.json file for windows is:

{
  "backend": "theano",
  "epsilon": 1e-07,
  "floatx": "float32",
  "image_dim_ordering": "tf"
}

So, my question is: Is there such a big difference between different Operating Systems or where is my mistake? Just to remind, I used exactly the same code for both systems.

3 Answers 3

16

If your problem is not solved yet try using: from keras import backend as K K.set_image_dim_ordering('th') This would work good if you wish to use theano backend and have to use channels first configuration for image dimension ordering.

Sign up to request clarification or add additional context in comments.

Comments

4

The problem you are having has to do with the expected dimension ordering.

  • Tensorflow ordering (tf): Shapes are expected to be (size_lines,size_columns,channel)
  • Theano ordering (th): Shapes are expected to be (channel,size_lines,size_columns)

If you change the ordering line in the keras.json file to "image_dim_ordering": "th" it should work. (i'd bet that's what's in your Elementary OS keras.json).

1 Comment

Thanks for the answer and sorry for my late response. You're still right, but I recognized that my .keras file doesn't change anything. I installed keras without any envs and one time trought anaconda. After setting the image_data_format to 'channels_first' in my code it works without any issues. So it was totally my fault. :D But thanks for your time.
1

For switching to another backend you can change the configuration file located in:

  • Linux: $HOME/.keras/keras.json
  • Windows: %USER_PROFILE%/.keras/keras.json

This is the keras.json file for theano backend:

{
    "floatx": "float32",
    "epsilon": 1e-07,
    "backend": "theano",
    "image_data_format": "channels_first"
}

This is the keras.json file for tensorflow backend:

{
    "floatx": "float32",
    "epsilon": 1e-07,
    "backend": "tensorflow",
    "image_data_format": "channels_last"
}

This is what the documentation https://keras.io/backend/ says about the image_data_format property:

image_data_format: string, either "channels_last" or "channels_first". It specifies which data format convention Keras will follow. (keras.backend.image_data_format() returns it.)

For 2D data (e.g. image), "channels_last" assumes (rows, cols, channels) while "channels_first" assumes (channels, rows, cols).

For 3D data, "channels_last" assumes (conv_dim1, conv_dim2, conv_dim3, channels) while "channels_first" assumes (channels, conv_dim1, conv_dim2, conv_dim3).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.