【问题标题】:Keras Xception model input shape confusionKeras Xception 模型输入形状混淆
【发布时间】: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


    【解决方案1】:

    来自 Keras 文档:

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

    【讨论】:

    • 我不认为它是这么说的。它说如果include_top=True它必须是(299,299,3),否则它必须大于或等于(71,71,3)
    • 在我看来,如果 include_top=False 输入形状必须是 (299, 299, 3).... 根据,“include_top: 是否在网络的顶端。”
    • only to be specified if include_top is False (otherwise the input shape has to be (299, 299, 3) 表示指定是否为 False,否则(如果为 True)不指定,因为假定为 (299,299,3)。
    【解决方案2】:

    keras 文档很清楚:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2019-12-12
      • 2020-09-26
      • 2022-10-18
      • 2017-12-24
      • 2016-11-09
      相关资源
      最近更新 更多