我正在尝试在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(最近一次调用 最后)in() 12 model.add(卷积2D(128,3,3,激活=' 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, 层) 306 output_shapes = [self.outputs [0] ._ keras_shape]) 307其他: - > 308 output_tensor = layer(self.outputs [0]) 309如果type(output_tensor)是list: 310引发异常('顺序模型中的所有图层'
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in 致电(自我,x,面具) 512 if inbound_layers: 513#如果需要,这将调用layer.build() - > 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#创建节点会自动更新self.inbound_nodes 571#以及入站图层上的outbound_nodes。 - > 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如果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 in 呼(自我,x,面具) 160 strides = self.strides, 161 border_mode = self.border_mode, - > 162 dim_ordering = self.dim_ordering) 163返回输出 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输出= K.pool2d(输入,pool_size,步幅, - > 212 border_mode,dim_ordering,pool_mode =' max') 213返回输出 214
/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc 在pool2d(x,pool_size,strides,border_mode,dim_ordering,pool_mode)中 1699 1700如果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 在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 在_max_pool中(输入,ksize,strides,padding,data_format,name)
1593 result = _op_def_lib.apply_op(" MaxPool",input = input, ksize = ksize,1594 strides = strides, 填充=填充, - > 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中(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输出= op.outputs 751返回_Restructure(ops.convert_n_to_tensor(输出),
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc 在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 在set_shapes_for_outputs(op)1783引发RuntimeError(" No 为标准操作注册的形状函数:%s" 1784
%op.type) - > 1785个shapes = 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除了errors.InvalidArgumentError为错误: - > 596引发ValueError(err.message) 597 598#在output_shapes中转换TensorShapeProto值。
ValueError:从1减去2得到的负尺寸大小 ' MaxPool_7' (op:' MaxPool')输入形状:[?,1,112,128]。
答案 0 :(得分:21)
引用github中提到的答案,您需要指定维度排序:
Keras是Theano或Tensorflow库的包装器。 Keras使用设置变量image_dim_ordering
来确定输入层是Theano还是Tensorflow格式。此设置可以通过两种方式指定 -
'tf'
中指定'th'
或~/.keras/keras.json
,如此 - image_dim_ordering: 'th'
。注意:这是json file。image_dim_ordering
,如下所示:model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))
附录: image_dim_ordering
在'th'
模式下,频道维度(深度)位于索引1(例如3,256,256)。在'tf'
模式下它是索引3(例如256,256,3)。从评论中引用@naoko。
答案 1 :(得分:1)
您正在使用输入形状为(3,x,y)应将其更改为input_shape = x,y,3
答案 2 :(得分:1)
对于使用TensorFlow的keras,请尝试以下操作:
model.add(ZeroPadding2D((1, 1), input_shape=(img_rows, img_cols, channel)))
答案 3 :(得分:0)
我在加载经过培训的 VGG 模型时也遇到了同样的问题。所以我只是采取了测试图像的转置。实际命令如下:
addUIInterruptionMonitor
答案 4 :(得分:0)
接受的答案有效。但您也可以执行以下操作:
model.add(MaxPooling2D((2, 2), name='block1_pool', data_format='channels_last')
Keras假设TensorFlow后端的输入为(width, height, channels)
,Theano后端的输入为(channel, width, height)
。由于您的input_shape=(3,224,224)
,指定data_format='channels_last'
应该可以解决问题。
答案 5 :(得分:0)
为我添加dim_ordering
已解决的错误:
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))
答案 6 :(得分:0)
我遇到了同样的问题,我通过将padding:'valid'更改为padding:'SAME'来解决了这个问题:我猜它足以添加一个padding参数:'same'
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))