ValueError: Negative dimension size caused by subtracting 3 from 1 for 'pool1/MaxPool'

在定义LetNet5网络模型时候,遇到了下述问题
此为图片通道数据格式问题

def LeNet5(w_path=None):
	input_shape = (1, img_rows, img_cols)
	img_input = Input(shape=input_shape)
	
	x = Conv2D(32, (3, 3), activation="relu", padding="same", name="conv1")	(img_input)
	x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)
		x = Conv2D(64, (3, 3), activation="relu", padding='same', name='conv2')(x)
	x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x)
	x = Dropout(0.25)(x)
	
	x = Flatten(name='flatten')(x)
	
	x = Dense(128, activation='relu', name='fc1')(x)
	x = Dropout(0.5)(x)
	x = Dense(128, activation='relu', name='fc2')(x)
	x = Dropout(0.5)(x)
	x = Dense(10, activation='softmax', name='predictions')(x)
	
	model = Model(img_input, x, name='LeNet5')
	if(w_path): model.load_weights(w_path)
	
	return model
	
lenet5 = LeNet5()
print('Model loaded.')
lenet5.summary()

ValueError: Negative dimension size caused by subtracting 3 from 1 for ‘pool1/MaxPool’ (op: ‘MaxPool’) with input shapes: [?,1,28,32].

改正:

 x = Conv2D(32, (3, 3), activation="relu", padding="same", name="conv1",data_format = 'channels_first')(img_input)

结果:
在这里插入图片描述
可以看到上面只修改第一个卷积层输入图片通道数据格式模型输出的模型不对,需要修改整个模型中的图片通道数据格式

x = Conv2D(32, (3, 3), activation="relu", padding="same", name="conv1",data_format = 'channels_first')(img_input)
x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1',data_format = 'channels_first')(x)
x = Conv2D(64, (3, 3), activation="relu", padding='same', name='conv2',data_format = 'channels_first')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2',data_format = 'channels_first')(x)

得到正确结果:
在这里插入图片描述
添加链接描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值