在定义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)