tensorflow.python.framework.errors_impl.InvalidArgumentError:通过从< max_pooling2d_1 / MaxPool' max_pooling2d_1 / MaxPool'中减去2而导致的负尺寸大小

时间:2017-07-08 12:21:50

标签: machine-learning tensorflow neural-network deep-learning keras

根据教程from here,我正在尝试使用Keras和Tensorflow实现用于图像分类的神经网络。

我添加了以下代码:

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)))

然而,问题是我得到了:

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].

之后,我看了at a possible answer并将最后一行更改为:

model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf"))

但是在这次改变之后我得到了同样的错误。

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

您提供的代码是在假设您的后端为Theano的情况下编写的。如果是Tensorflow,您应该将输入更改为(width, height, channels)形状,以便更改此行:

model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))

您的问题来自于卷积后(使用valid填充),您的输出具有(1, 148, 32)形状,因此无法使用步幅MaxPooling2D来应用(2, 2)这是一个默认值。