python-3.x - 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)
解决方案
keras 文档非常清楚:
input_shape:可选的形状元组,仅在 include_top 为 False 时指定(否则输入形状必须为 (299, 299, 3)。它应该有正好 3 个输入通道,并且宽度和高度应该不小于 71。例如(150, 150, 3) 将是一个有效值。
推荐阅读
- c - 哪种方法更适合在 c 中的两个进程之间进行对话?
- python - 如何通过多处理模块创建的工作进程在python中组合动态生成的测试套件
- laravel - 如何使表单中的动态字段得到验证和要求?
- nlp - spaCy:使用依赖解析在句子中查找根词的问题
- swift - Swift Realm 备份和恢复领域数据
- r - 总结变量随时间的发展
- android - ZXing 在导航页面的第一页上不起作用
- php - 如果没有什么不同,更新不起作用
- sql - 在 PL/SQL 中运行项目(多个 SQL)的正确方法
- c++ - consteval 函数返回具有非平凡 constexpr 析构函数的对象