简体   繁体   English

如何根据 Keras 中保存的模型进行预测?

[英]How to predict from saved model in Keras ?

I have trained an image classifier using keras and it gave a very good accuracy.我已经使用keras训练了一个图像分类器,它提供了非常好的准确性。 I've saved the model using the save() and saved it using the h5 format.我已经使用save()保存了模型并使用h5格式保存了它。 How can I make a prediction using the model?如何使用模型进行预测?

The code is:代码是:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

classifier = Sequential()

classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation =   'relu'))

classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 5,
validation_data = test_set,
validation_steps = 2000)
classifier.save('classifier.h5')

Thanks in Advance..!!提前致谢..!!

The first step is to import your model using load_model method.第一步是使用load_model方法导入模型。

from keras.models import load_model
model = load_model('my_model.h5')

Then you have to compile the model in order to make predictions.然后,您必须编译模型才能进行预测。

model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

Now you can predict results for a new entry image.现在,您可以predict新条目图像的结果。

from keras.preprocessing import image

test_image = image.load_img(imagePath, target_size = (64, 64)) 
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)

#predict the result
result = model.predict(test_image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何加载用 saved_model 保存的 tensorflow keras model 以使用预测 function? - How to load a tensorflow keras model saved with saved_model to use the predict function? 如何从 Keras 的已保存 model 生成精度? - How to generate accuracy from a saved model of Keras? 如何从HKL或其他任何已保存的keras配置中重建keras模型? - How to rebuild a keras model from a saved keras config in a hkl or any? 如何使用从 save_weights 保存的张量流模型加载和预测? - How to load and predict with a tensorflow model saved from save_weights? 如何在 Keras、.h5 保存文件中使用训练有素的 model 预测输入图像? - How to predict input image using trained model in Keras, .h5 saved file? 如何从 keras 中预训练的 model 预测图像 - How to predict image from a pre-trained model in keras 如何使用多个保存的模型进行预测? - How to predict using multiple saved model? 如何使用保存在JSON文件中的权重重建keras模型? - How to rebuild keras model from weights that are saved in a JSON file? 如何从 Colaboratory 中保存的检查点加载 TensorFlow Keras model? - How to load TensorFlow Keras model from a saved checkpoint in Colaboratory? 如何在Keras中使用训练模型预测输入图像? - How to predict input image with trained model in Keras?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM