首页 > 解决方案 > Keras Xception 模型输入形状混淆

问题描述

我正在阅读此处(299,299,3)的文档,其中说如果我指定输入形状必须是include_top=True. 但是,如果我设置input_shape=None(输入形状实际上是(32,32,3))模型训练。那么为什么这是有效的呢?

input_shape:可选的形状元组,仅在 include_top 为 False 时指定(否则输入形状必须为 (299, 299, 3)。它应该有正好 3 个输入通道,并且宽度和高度应该不小于 71。例如(150, 150, 3) 将是一个有效值。

最小的例子:

batch_size = 32
epochs = 2

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.xception import Xception


NUM_CLASSES = 10

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)
y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)

print("x_train shape:", x_train.shape) # (50000, 32, 32, 3)


"""
Why does this work when input_shape=None, when the documentation specifies that
input_shape for this model must be greater than 71x71?
"""
model = Xception(weights=None, include_top=True, classes=NUM_CLASSES, input_shape=None)


model.compile(loss='categorical_crossentropy', 
              optimizer=keras.optimizers.RMSprop(lr=0.0001, decay=1e-6), metrics=['accuracy'])


train_datagen = ImageDataGenerator()

train_datagen.fit(x_train)

model.fit_generator(train_datagen.flow(x_train, y_train, batch_size=batch_size),
                    steps_per_epoch=len(x_train) / batch_size, epochs=epochs, verbose=1)

标签: python-3.xtensorflowkeras

解决方案


keras 文档非常清楚:

input_shape:可选的形状元组,仅在 include_top 为 False 时指定(否则输入形状必须为 (299, 299, 3)。它应该有正好 3 个输入通道,并且宽度和高度应该不小于 71。例如(150, 150, 3) 将是一个有效值。


推荐阅读