【问题标题】:Loading a pre trained Keras model and predicting加载预训练的 Keras 模型并进行预测
【发布时间】:2017-07-16 15:06:18
【问题描述】:

我使用一些 Keras 示例拼凑了一个简单的神经网络,基于基本的 Kaggle Cat vs Dog 数据 (https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data)。我能够使用

训练和保存模型
model.fit_generator(
         #train_generator,
         #samples_per_epoch=2000,
         #nb_epoch=50,
         #validation_data=validation_generator,
         #nb_val_samples=800)
model.save('first_model.h5')

但是当我尝试加载模型进行预测时,我得到了

Traceback (most recent call last):
  File "/Users/me/PycharmProjects/CatVsDog/SampleML.py", line 48, in <module>
    print(saved_model.predict_generator(test_generator, 12500))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/models.py", line 1012, in predict_generator
    pickle_safe=pickle_safe)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/engine/training.py", line 1763, in predict_generator
    outs = self.predict_on_batch(x)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/engine/training.py", line 1371, in predict_on_batch
    self.internal_input_shapes)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/engine/training.py", line 85, in standardize_input_data
    'Found: ' + str(data)[:200] + '...')
TypeError: Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: None...
Exception ignored in: <bound method Session.__del__ of <tensorflow.python.client.session.Session object at 0x10c7586d8>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 581, in __del__
UnboundLocalError: local variable 'status' referenced before assignment

这是我的 PyCharm 解决方案中保存图像的位置。 Test 目录中有 1-12500 个 .jpg 文件,每个训练集 cat 和 dog 目录中有 11500 个标记为 .jpgs,每个 validate 目录中有 1000 个标记为 .jpgs。

这是我的代码

from __future__ import print_function
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.models import load_model

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        'train',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        'validate',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

test_generator = train_datagen.flow_from_directory(
         'test',
         target_size=(64, 64),
         batch_size=32,
         class_mode='binary')


nb_filters = 32
kernel_size = (3,3)
pool_size = (2, 2)
nb_classes = 2
input_shape = (64, 64, 3)


saved_model = load_model('first_model.h5')
score = saved_model.evaluate_generator(validation_generator, 2000)


print('Test score:', score[0])
print('Test accuracy:', score[1])
print(saved_model.predict_generator(test_generator, 12500))

【问题讨论】:

    标签: python-3.x machine-learning keras conv-neural-network


    【解决方案1】:

    我相信您提供给predict_generator 的内容格式不正确。

    当您预测值时,与训练和评估相反,您不想提供标签。 因此,我会尝试将您的 test_generator 更改为:

    test_generator = train_datagen.flow_from_directory(
         'test',
         target_size=(64, 64),
         batch_size=32,
         #This will not output the targets. 
         class_mode=None)
    

    您可以找到documentation about the ImageDataGenerator here

    【讨论】:

    • 那行得通,我还需要为我的测试数据添加另一个子目录。 Keras 喜欢 test 子目录中的测试数据。看似人为,但确实有效!
    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 2017-05-29
    • 2020-02-25
    • 2020-07-14
    • 2020-09-07
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多