NameError:未定义名称“分类器" [英] NameError: name 'classifier' is not defined
问题描述
我是机器学习的新手.我试图对数据集进行预测,但是当我运行程序时,它给了我以下错误:
I am new to machine learning. I was trying to predict on a dataset but when I run the program it give me following error:
NameError: name 'classifier' is not defined
这是我的代码:
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'nsfw'
else:
prediction = 'sfw'
推荐答案
您正在使用classifier
进行预测.但是classifier
没有定义.那就是错误所在.
You are using classifier
to make predictions. But the classifier
is not defined. That is what the error is.
要解决此问题,您必须具有已保存的keras模型,该模型已针对您的特定问题进行了培训.如果有的话,您可以加载它并进行预测.
To solve this, You must have the saved keras model that is trained for your specific problem with it. If you have that, you can load it and make predictions.
下面的代码显示了如何加载模型.
Below code shows how you can load the model.
from keras.models import load_model
classifier = load_model('path_to_your_model')
在加载模型后,您可以像使用模型那样进行预测.
After the model is loaded you can use that to make predictions like you do.
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'nsfw'
else:
prediction = 'sfw'
这篇关于NameError:未定义名称“分类器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!