我在基本的Kaggle Cat vs Dog数据(https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data)上使用一些Keras示例拼凑了一个简单的神经网络。我能够使用
训练和保存模型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文件,每个Training set cat和dog目录中有11500个标记为.jpgs,每个验证目录中有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))
答案 0 :(得分: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)