【发布时间】:2018-12-17 21:33:44
【问题描述】:
我正在阅读文档here,它说如果我指定include_top=True,输入形状必须是(299,299,3)。但是,如果我设置input_shape=None(输入形状实际上是(32,32,3)),模型就会训练。那么为什么这是有效的呢?
input_shape:可选的形状元组,仅在 include_top 时指定 是 False (否则输入形状必须是 (299, 299, 3)。它应该 正好有 3 个输入通道,宽度和高度应该是 no 小于 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.x tensorflow keras