【问题标题】:Keras | Getting the Inception v3 example running喀拉斯 |让 Inception v3 示例运行
【发布时间】:2016-06-05 12:22:59
【问题描述】:

我正在尝试学习一些 Keras 语法并使用 Inception v3 example

我有一个 4 类多类分类玩具问题,所以我从示例中更改了以下几行:

NB_CLASS = 4  # number of classes
DIM_ORDERING = 'tf'  # 'th' (channels, width, height) or 'tf' (width, height, channels)

我的玩具数据集具有以下维度:

  • 包含所有图像的数组大小:(595, 299, 299, 3)
  • 包含训练图像的数组大小:(416, 299, 299, 3)
  • 包含训练标签的数组大小:(179, 4)
  • 包含测试图像的数组大小:(179, 299, 299, 3)
  • 包含测试标签的数组大小:(179, 4)

然后我尝试使用以下代码训练模型:

# fit the model on the batches generated by datagen.flow()
#  https://github.com/fchollet/keras/issues/1627
#    http://keras.io/models/sequential/#sequential-model-methods
checkpointer = ModelCheckpoint(filepath="/tmp/weights.hdf5", verbose=1, save_best_only=True)
model.fit_generator(datagen.flow(X_train, Y_train,
                batch_size=32),
                nb_epoch=10,
                samples_per_epoch=32,
                class_weight=None, #classWeights,
                verbose=2,
                validation_data=(X_test, Y_test),
                callbacks=[checkpointer])

然后我得到以下错误:

Exception: The model expects 2 input arrays, but only received one array. Found: array with shape (179, 4)`

这可能与此有关,因为 Inception 希望拥有 auxiliary classifiers (Szegedy et al., 2014)

model = Model(input=img_input, output=[preds, aux_preds])

我如何在 Keras 中为模型赋予两个标签,也不是高级 Python 程序员?

【问题讨论】:

  • ImageDataGenerator 默认输出一个标签。您可以扩展该类并覆盖flow 函数以生成两个输出。其他选项是单独执行增强并使用fit 函数。
  • 你试过'validation_data=(X_test, [Y_test, Y_test])'
  • 请发布您的完整型号代码 - 否则无法找到您的错误。
  • 根据 Marcin 的建议将 datagen.flow 更正为 (X_train, [Y_train,Y_train] 和验证数据。我认为这应该解决它

标签: python keras


【解决方案1】:

我建议您先尝试使用this tutorial。代码可以在here找到。

您将在第一部分中看到,它展示了如何使用以下方式从目录加载数据:

.flow_from_directory(
   train_data_dir,
   target_size=(img_width, img_height),
   batch_size=batch_size,
   class_mode='binary')

为了输入不同的类别,您必须将图像放在每个类别的一个文件夹中(请注意,可能还有另一种方法,通过传递标签)。另请注意,在您的情况下 class_mode 不能使用 'binary' (我认为您应该使用 'categorical'):

`"binary"`: binary targets (if there are only two classes),
`"categorical"`: categorical targets,

那么你就可以使用 Keras 中已有的 inceptionv3 模型了:

from keras.applications import InceptionV3    
cnn = InceptionV3(...)

另外请注意,你训练 InceptionV3 的示例太少,因为这个模型非常大(检查here 的大小)。在这种情况下,您可以做的是迁移学习,在 InceptionV3 上使用预训练的权重。请参阅the tutorial 中的使用预训练网络的瓶颈特征:一分钟内达到 90% 的准确率部分。

【讨论】:

    【解决方案2】:

    错误消息与validation_data 参数有关:当您使用model.fit_generator 时,验证数据也应该通过ImageDataGenerator 对象传递(就像您已经在为训练数据做的那样)。这与缺少辅助分类器无关 - 原始论文中 Keras does not implement the auxiliary classifier 中的 Inception v3 模型(这是尝试迁移学习而不是完整训练的另一个原因)。

    更新您的代码以使用生成器提供验证数据:

    datagen = ImageDataGenerator()
    model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
                    nb_epoch=10,
                    steps_per_epoch=len(X_train) / 32,
                    class_weight=None,
                    verbose=2,
                    validation_data=datagen.flow(X_test, Y_test, batch_size=32),
                    validation_steps=len(X_test) / 32,
                    callbacks=[checkpointer])
    

    请注意,我已将参数 samples_per_epoch 更新为较新的 steps_per_epoch

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 2018-01-22
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2021-07-31
      相关资源
      最近更新 更多