Keras Maxpooling2d层给出ValueError
deep-learning
keras
neural-network
python
5
0

我正在尝试在keras中复制VGG16模型,以下是我的代码:

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(最近一次通话最后一次)()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在add(self,layer)306 output_shapes = [self.outputs [0] ._ keras_shape])307其他:-> 308 output_tensor = layer(self.outputs [0])309如果type(output_tensor)被列出:310 throw Exception(“顺序模型中的所有层”

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc在调用中 (self,x,mask)512如果inbound_layers:513#必要时将调用layer.build()-> 514 self.add_inbound_node(inbound_layers,node_indices,tensor_indices)515 input_added = True 516

在add_inbound_node(self,inbound_layers,node_indices,tensor_indices)中的/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc 570#创建节点会自动更新self.inbound_nodes 571#以及outbound_nodes入站层。 -> 572 Node.create_node(self,inbound_layers,node_indices,tensor_indices)573574 def get_output_shape_for(self,input_shape):

create_node(cls,outbound_layer,inbound_layers,node_indices,tensor_indices)中的/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc 147148如果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:尝试自动推断形状,如果get_output_shape_for引发异常

/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.pyc在调用中(self,x,mask)160 strides = self.strides,161 border_mode = self.border_mode,-> 162 dim_ordering = self.dim_ordering)163返回输出164

_pooling_function中的/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.pyc(自身,输入,pool_size,步幅,border_mode,dim_ordering)210 border_mode,dim_ordering):211输出= K.pool2d(输入,pool_size,步幅,-> 212 border_mode,dim_ordering,pool_mode ='max')213返回输出214

pool2d中的/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc(x、pool_size、stride、border_mode、dim_ordering、pool_mode)1699 1700如果pool_mode =='max':-> 1701 x = tf.nn.max_pool(x,pool_size,步幅,填充=填充)1702 elif pool_mode =='avg':1703
x = tf.nn.avg_pool(x,pool_size,步幅,填充=填充)

max_pool中的/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.pyc(值,ksize,跨度,填充,数据格式,名称)1391 padding = padding,1392
data_format =数据格式,-> 1393 name = name)1394 1395

_max_pool中的/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.pyc(输入,ksize,跨距,填充,data_format,名称)
1593结果= _op_def_lib.apply_op(“ MaxPool”,输入=输入,ksize = ksize,1594步幅=步幅,填充=填充,-> 1595 data_format = data_format,name = name)1596返回结果1597

/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.pyc在apply_op(自己,op_type_name,名称,**关键字)中747 op = g.create_op(op_type_name,输入,output_types,名称=范围,748个input_types = input_types,attrs = attr_protos,-> 749 op_def = op_def)750个输出= op.outputs 751 return _Restructure(ops.convert_n_to_tensor(outputs),

/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc在create_op(自身,op_type,输入,dtypes,input_types,名称,attrs,op_def,compute_shapes,compute_device)2388
original_op = self._default_original_op,op_def = op_def)2389如果compute_shapes:-> 2390 set_shapes_for_outputs(ret)2391 self._add_op(ret)2392
self._record_op_seen_by_control_dependencies(ret)

set_shapes_for_outputs(op)中的/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc 1783提高RuntimeError(“没有为标准op注册形状函数:%s” 1784
%op.type)-> 1785形状= shape_func(op)1786如果形状为None:1787提高RuntimeError(

/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.pyc在call_cpp_shape_fn(op,input_tensors_needed,debug_python_shape_fn)594状态)中除错误以外595.InvalidArgumentError为错误:-> 596引发ValueError (err.message)597 598#在output_shapes中转换TensorShapeProto值。

ValueError:负尺寸大小是由于输入形状为[?,1,112,128]的'MaxPool_7'(op:'MaxPool')从1中减去2引起的。

参考资料:
Stack Overflow
收藏
评论
共 1 个回答
高赞 时间 活跃

引用github中提到的答案,您需要指定维度顺序:

Keras是Theano或Tensorflow库的包装器。 image_dim_ordering使用设置变量image_dim_ordering来确定输入图层是Theano还是Tensorflow格式。可以通过两种方式指定此设置-

  1. ~/.keras/keras.json指定'tf''th' ,例如image_dim_ordering: 'th' 。注意:这是一个json文件
  2. 或像这样在模型中指定image_dim_orderingmodel.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))

附录: image_dim_ordering'th'模式下,通道尺寸(深度)在索引1(例如image_dim_ordering )处。在'tf'模式下,它位于索引3(例如256、256、3)。在评论中引用@naoko。

收藏
评论
新手导航
  • 社区规范
  • 提出问题
  • 进行投票
  • 个人资料
  • 优化问题
  • 回答问题

关于我们

常见问题

内容许可

联系我们

@2020 AskGo
京ICP备20001863号