Keras | 让Inception v3示例运行

Pje*_*eri 16 python keras

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

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

NB_CLASS = 4  # number of classes
DIM_ORDERING = 'tf'  # 'th' (channels, width, height) or 'tf' (width, height, channels)
Run Code Online (Sandbox Code Playgroud)

我的玩具数据集具有以下尺寸:

  • 包含所有图像的数组大小:(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])
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误:

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

这可能与此有关,因为Inception希望有辅助分类器(Szegedy等,2014):

model = Model(input=img_input, output=[preds, aux_preds])
Run Code Online (Sandbox Code Playgroud)

如何将这两个标签赋予 Keras 的模型不是高级Python程序员?

Mar*_*ols 1

我建议您先尝试本教程。代码可以在这里找到。

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

.flow_from_directory(
   train_data_dir,
   target_size=(img_width, img_height),
   batch_size=batch_size,
   class_mode='binary')
Run Code Online (Sandbox Code Playgroud)

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

`"binary"`: binary targets (if there are only two classes),
`"categorical"`: categorical targets,
Run Code Online (Sandbox Code Playgroud)

然后您可以使用 Keras 中已有的 inceptionv3 模型:

from keras.applications import InceptionV3    
cnn = InceptionV3(...)
Run Code Online (Sandbox Code Playgroud)

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