图像尺寸为32 * 32 * 3
model = Sequential() #- Sequential container.
model.add(Convolution2D(6, 5, 5, #-- 6 outputs (6 filters), 5x5 convolution kernel
border_mode='valid',
input_shape=(3, img_rows, img_cols))) #-- 3 input depth (RGB)
model.add(Activation('relu')) #-- ReLU non-linearity
model.add(MaxPooling2D(pool_size=(2, 2))) #-- A max-pooling on 2x2 windows
model.add(Convolution2D(16, 5, 5)) #-- 16 outputs (16 filters), 5x5 convolution kernel
model.add(Activation('relu')) #-- ReLU non-linearity
model.add(MaxPooling2D(pool_size=(2, 2))) #-- A max-pooling on 2x2 windows
model.add(Flatten()) #-- eshapes a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
model.add(Dense(120)) #-- 120 outputs fully connected layer
model.add(Activation('relu')) #-- ReLU non-linearity
model.add(Dense(84)) #-- 84 outputs fully connected layer
model.add(Activation('relu')) #-- ReLU non-linearity
model.add(Dense(num_classes)) #-- 10 outputs fully connected layer (one for each class)
model.add(Activation('softmax')) #-- converts the output to a log-probability. Useful for classification problems
代码中的错误:
Traceback (most recent call last):
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 670, in _call_cpp_shape_fn_impl
status)
File "/home/saurabh/anaconda3/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 5 from 3 for 'Conv2D' (op: 'Conv2D') with input shapes: [?,3,32,32], [5,5,32,6].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "convert.py", line 141, in <module>
input_shape=(3, img_rows, img_cols))) #-- 3 input depth (RGB)
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/models.py", line 299, in add
layer.create_input_layer(batch_input_shape, input_dtype)
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 401, in create_input_layer
self(x)
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 572, in __call__
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 635, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 166, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/layers/convolutional.py", line 475, in call
filter_shape=self.W_shape)
File "/home/saurabh/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 2627, in conv2d
x = tf.nn.conv2d(x, kernel, strides, padding=padding)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 396, in conv2d
data_format=data_format, name=name)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
op_def=op_def)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2242, in create_op
set_shapes_for_outputs(ret)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs
shapes = shape_func(op)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/home/saurabh/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting
答案 0 :(得分:1)
我认为您使用“tf”dim_ordering。在keras.json中将其更改为“th”,因此第二个参数是过滤器大小而不是最后一个。
此外,您的问题部分归因于'border_mode='valid'
的使用。使用border_mode='same'
保留卷积的维度。
答案 1 :(得分:0)
我已经复制并粘贴了您错误代码的相关部分。
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 5 from 3 for 'Conv2D' (op: 'Conv2D') with input shapes: [?,3,32,32], [5,5,32,6]
之后的部分是由此引起的,一旦解决了这个问题就会消失。 但是,没有看到任何代码,我无法告诉你究竟是什么导致了这一点。