Keras Xception模型输入形状混乱

时间:2018-12-17 21:33:45

标签: python-3.x tensorflow keras

我正在阅读文档here,其中说如果我指定(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)

1 个答案:

答案 0 :(得分:0)

来自Keras文档:

此模型的默认输入大小为299x299,input_shape默认为“无”。 如果include_top为True,则可以使用宽度和高度小于71的数据。