Keras ValueError Input Form
I have a problem and a question at the same time. I want to make an image classifier with Keras using Theano as Backend and Sequential.
>>> keras.__version__
'2.0.1'
>>> theano.__version__
'0.9.0'
My input form: INPUT_SHAPE = (3, 28, 28) #depth, size, size
Let's go to my problem. If I run my script on Windows 7 32 Bit it gives me below error:
ValueError: ('The specified size contains a dimension with value <= 0', (-1024, 512))
If I run it with an input form: INPUT_SHAPE = (28, 28, 3) #size, size, depth
It gives me 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 works without issue ( INPUT_SHAPE = (3, 28, 28)
).
My keras.json file for windows:
{
"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.
source to share
The problem you are having is with the expected ordering of sizes.
- Tensorflow order (tf): There are expected to be shapes (size_lines, size_columns, channel )
- Theano ordering (th): Shapes are expected ( channel , size_lines, size_columns)
If you change the order line in your keras.json file to "image_dim_ordering": "th" it should work. (I would bet that in your Elementary OS keras.json).
source to share
To switch to another server, you can change the configuration file located in the folder
- Linux:
$HOME/.keras/keras.json
- Windows:
%USER_PROFILE%/.keras/keras.json
This is the file keras.json
for the backend theano
:
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "theano",
"image_data_format": "channels_first"
}
This is the file keras.json
for the 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 property image_data_format
:
image_data_format
:string
, either"channels_last"
, or"channels_first"
. It specifies which format the Keras data format will be used for. (keras.backend.image_data_format()
returns it.)For two-dimensional data (eg, an 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)
but"channels_first"
takes(channels, conv_dim1, conv_dim2, conv_dim3)
.
source to share