首页 文章

Keras Maxpooling2d层给出了ValueError

提问于
浏览
19

我想在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(最近一次调用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如果type(output_tensor)是list:310 raise Exception('all layers in一个顺序模型'/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in call(self,x,mask)512 if inbound_layers:513#这将调用layer.build()if必要 - > 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):create_node中的/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc(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 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/ _pooling_function中的dist-packages / keras / layers / pooling.pyc(self,inputs,pool_size,strides,border_mode,dim_ordering)210 border_mode,dim_ordering):211 output = K.pool2d(inputs,pool_siz) e,strides, - > 212 border_mode,dim_ordering,pool_mode ='max')213在pool2d(x,pool_size)中返回输出214 /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc ,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返回结果1597 /usr/local/lib/python2.7/dist-packages apply_op中的/tensorflow/python/framework/op_def_library.pyc(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在create_op中返回_Restructure(ops.convert_n_to_tensor(outputs),/ usr / local / lib / python2.7 /dist-packages /tensorflow / python / framework /ops.pyc (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引发RuntimeError(“没有为标准操作注册形状函数:% s“1784%op.type” - > 1785个shapes = shape_func(op)1786如果形状为None:1787引发RuntimeError(/ usr / local / lib / pytho call_cpp_shape_fn中的n2.7 / dist-packages / tensorflow / python / framework / common_shapes.pn(op,input_tensors_needed,debug_python_shape_fn)594 status)595除了errors.InvalidArgumentError为错误: - > 596引发ValueError(err.message)597 598 #在output_shapes中转换TensorShapeProto值 . ValueError:通过输入形状为“MaxPool_7”(op:'MaxPool')从1减1而导致的负尺寸大小:[?,1,112,128] .

6 回答

  • 19

    您使用输入形状为(3,x,y)应将其更改为input_shape = x,y,3

  • 1

    对于使用TensorFlow的keras,请尝试以下操作:

    model.add(ZeroPadding2D((1, 1), input_shape=(img_rows, img_cols, channel)))
    
  • 0

    我在加载经过训练的 VGG 模型时也遇到了同样的问题 . 所以我只是对测试图像进行了转置 . 实际命令如下:

    kerasImage = kerasImage.transpose(1,2,0)

  • 0

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

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

    • ~/.keras/keras.json 中指定 'tf''th' ,如此 - image_dim_ordering: 'th' . 注意:这是json file .

    • 或在模型中指定 image_dim_ordering ,如下所示: model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))

    Appendix: image_dim_ordering'th' 模式中,通道维度(深度)位于索引1(例如3,256,256) . 在 'tf' 模式中它是索引3(例如256,256,3) . 从评论中引用@naoko .

  • 1

    为我添加 dim_ordering 已解决的错误:

    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))
    
  • 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' 应该可以解决问题 .

相关问题