line 596, in call_cpp_shape_fn raise ValueError(err.message) ValueError: Negative dimension size caused by subtracting 3 from 1

line 596, in call_cpp_shape_fn raise ValueError(err.message) ValueError: Negative dimension size caused by subtracting 3 from 1

我正在尝试通过示例学习 Keras,并在 Kaggle 上找到了这段代码。如何使用 TensorFlow 后端让 Keras 工作?

mona@pascal:~/computer_vision/kaggle_distracted_driver$ python run_keras_simple.py 
/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
Using TensorFlow backend.
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so.5.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so.8.0 locally
Read train images
Load folder c0
Load folder c1
Load folder c2
Load folder c3
Load folder c4
Load folder c5
Load folder c6
Load folder c7
Load folder c8
Load folder c9
Directory doesnt exists
('Train shape:', (0, 1, 96, 128))
(0, 'train samples')
('Split train: ', 0)
('Split valid: ', 0)
('Split holdout: ', 0)
Traceback (most recent call last):
  File "run_keras_simple.py", line 189, in <module>
    input_shape=(1, img_rows, img_cols)))
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 294, in add
    layer.create_input_layer(batch_input_shape, input_dtype)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 398, in create_input_layer
    self(x)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 569, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 632, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 164, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/convolutional.py", line 442, in call
    filter_shape=self.W_shape)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2206, in conv2d
    x = tf.nn.conv2d(x, kernel, strides, padding=padding)
  File "/home/mona/tensorflow/_python_build/tensorflow/python/ops/gen_nn_ops.py", line 394, in conv2d
    data_format=data_format, name=name)
  File "/home/mona/tensorflow/_python_build/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
    op_def=op_def)
  File "/home/mona/tensorflow/_python_build/tensorflow/python/framework/ops.py", line 2390, in create_op
    set_shapes_for_outputs(ret)
  File "/home/mona/tensorflow/_python_build/tensorflow/python/framework/ops.py", line 1785, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/mona/tensorflow/_python_build/tensorflow/python/framework/common_shapes.py", line 596, in call_cpp_shape_fn
    raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 3 from 1

你的问题出在这里:

input_shape=(1, img_rows, img_cols)

这个输入形状是 theano 格式,在 Tensorflow 中,通道维度在最后。您需要将输入形状更改为:

input_shape=(img_rows, img_cols, 1)

还要确保输入数据(训练和验证)采用相同的格式,通道维度位于形状元组的末尾。