Reputation: 20222
I am trying to implement a neural network used for image classification with Keras and Tensorflow, according to the tutorial from here.
I added the following code:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
However, the problem is that I am getting:
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 671, in _call_cpp_shape_fn_impl
16.4s
3
input_tensors_as_shapes, status)
File "/opt/conda/lib/python3.6/contextlib.py", line 89, in __exit__
next(self.gen)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,148,32].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "../src/script.py", line 49, in <module>
model.add(MaxPooling2D(pool_size=(2, 2)))
File "/opt/conda/lib/python3.6/site-packages/Keras-2.0.5-py3.6.egg/keras/models.py", line 469, in add
File "/opt/conda/lib/python3.6/site-packages/Keras-2.0.5-py3.6.egg/keras/engine/topology.py", line 596, in __call__
File "/opt/conda/lib/python3.6/site-packages/Keras-2.0.5-py3.6.egg/keras/layers/pooling.py", line 154, in call
File "/opt/conda/lib/python3.6/site-packages/Keras-2.0.5-py3.6.egg/keras/layers/pooling.py", line 217, in _pooling_function
File "/opt/conda/lib/python3.6/site-packages/Keras-2.0.5-py3.6.egg/keras/backend/tensorflow_backend.py", line 3378, in pool2d
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1769, in max_pool
16.4s
4
name=name)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1605, in _max_pool
data_format=data_format, name=name)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
set_shapes_for_outputs(ret)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
shapes = shape_func(op)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,148,32].
After that, I looked at a possible answer and I changed the last line to this:
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf"))
But after this change I getting the same error.
Any idea what could be wrong?
Upvotes: 1
Views: 3028
Reputation: 40516
The code you provided was written with assuming that your backend is Theano
. In case of Tensorflow
you should change your input to have shape (width, height, channels)
so you should change this line:
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
Your problem comes from that after the convolution (with valid
padding) your output has shape(1, 148, 32)
so it's impossible to apply MaxPooling2D
with stride (2, 2)
which is a default value.
Upvotes: 2