我試圖複製VGG16模型keras,下面是我的代碼:Keras Maxpooling2d層給ValueError異常
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2))) ###This line gives error
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
的maxpooling2d層在其評論
錯誤說行給出了一個錯誤:
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'MaxPool_7' (op: 'MaxPool') with input shapes: [?,1,112,128].
背後是什麼原因?如何解決這個問題?
編輯: 更詳細的錯誤日誌:
ValueError Traceback (most recent call last) in() 12 model.add(Convolution2D(128, 3, 3, activation='relu')) 13 ---> 14 model.add(MaxPooling2D((2,2), strides=(2,2))) 15 16 model.add(ZeroPadding2D((1,1)))
/usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self, layer) 306 output_shapes=[self.outputs[0]._keras_shape]) 307 else: --> 308 output_tensor = layer(self.outputs[0]) 309 if type(output_tensor) is list: 310 raise Exception('All layers in a Sequential model '
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in call(self, x, mask) 512 if inbound_layers: 513 # this will call layer.build() if necessary --> 514 self.add_inbound_node(inbound_layers, node_indices, tensor_indices) 515 input_added = True 516
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices) 570 # creating the node automatically updates self.inbound_nodes 571 # as well as outbound_nodes on inbound layers. --> 572 Node.create_node(self, inbound_layers, node_indices, tensor_indices) 573 574 def get_output_shape_for(self, input_shape):
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices) 147 148 if len(input_tensors) == 1: --> 149 output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0])) 150 output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0])) 151 # TODO: try to auto-infer shape if exception is raised by get_output_shape_for
/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.pyc in call(self, x, mask) 160 strides=self.strides, 161 border_mode=self.border_mode, --> 162 dim_ordering=self.dim_ordering) 163 return output 164
/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.pyc in _pooling_function(self, inputs, pool_size, strides, border_mode, dim_ordering) 210 border_mode, dim_ordering): 211 output = K.pool2d(inputs, pool_size, strides, --> 212 border_mode, dim_ordering, pool_mode='max') 213 return output 214
/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in pool2d(x, pool_size, strides, border_mode, dim_ordering, pool_mode) 1699 1700 if pool_mode == 'max': -> 1701 x = tf.nn.max_pool(x, pool_size, strides, padding=padding) 1702 elif pool_mode == 'avg': 1703
x = tf.nn.avg_pool(x, pool_size, strides, padding=padding)/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.pyc in max_pool(value, ksize, strides, padding, data_format, name) 1391 padding=padding, 1392
data_format=data_format, -> 1393 name=name) 1394 1395/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.pyc in _max_pool(input, ksize, strides, padding, data_format, name)
1593 result = _op_def_lib.apply_op("MaxPool", input=input, ksize=ksize, 1594 strides=strides, padding=padding, -> 1595 data_format=data_format, name=name) 1596 return result 1597/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords) 747 op = g.create_op(op_type_name, inputs, output_types, name=scope, 748 input_types=input_types, attrs=attr_protos, --> 749 op_def=op_def) 750 outputs = op.outputs 751 return _Restructure(ops.convert_n_to_tensor(outputs),
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device) 2388
original_op=self._default_original_op, op_def=op_def) 2389 if compute_shapes: -> 2390 set_shapes_for_outputs(ret) 2391 self._add_op(ret) 2392
self._record_op_seen_by_control_dependencies(ret)/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op) 1783 raise RuntimeError("No shape function registered for standard op: %s" 1784
% op.type) -> 1785 shapes = shape_func(op) 1786 if shapes is None: 1787 raise RuntimeError(/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.pyc in call_cpp_shape_fn(op, input_tensors_needed, debug_python_shape_fn) 594 status) 595 except errors.InvalidArgumentError as err: --> 596 raise ValueError(err.message) 597 598 # Convert TensorShapeProto values in output_shapes.
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'MaxPool_7' (op: 'MaxPool') with input shapes: [?,1,112,128].
這個問題是由我只開了,反正感謝。 –
@PranayMathur沒有注意到! –
注意:'dim_ordering',在''th''模式下,通道維度(深度)在索引1處,在'tf''模式下處於索引3處。 對於tensorflow 0.10將使用「tf」(默認),0.11會使用「th」。你可以在'〜/ .keras/keras.json'中設置這個行爲 – naoko