NameError: name 'classifier' is not defined
NameError: name 'classifier' is not defined
我是机器学习的新手。我试图在数据集上进行预测,但是当我 运行 程序时,它给我以下错误:
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
没有定义。这就是错误所在。
要解决此问题,您必须拥有针对您的特定问题进行训练的已保存 keras 模型。如果你有,你可以加载它并进行预测。
下面的代码显示了如何加载模型。
from keras.models import load_model
classifier = load_model('path_to_your_model')
加载模型后,您可以使用它像您一样进行预测。
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'
在开始将层添加到模型之前,您必须指定 'empty' 版本。
您只需在代码上方添加以下行即可修复此错误:
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
#empty model
classifier = Sequential()
然后继续指定:
#add layers, start with hidden layer and first deep layer
classifier.add(Dense(output_dim=15, init="uniform", activation='relu',input_dim = 15))
classifier.add(Dropout(rate=0.1))
我是机器学习的新手。我试图在数据集上进行预测,但是当我 运行 程序时,它给我以下错误:
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
没有定义。这就是错误所在。
要解决此问题,您必须拥有针对您的特定问题进行训练的已保存 keras 模型。如果你有,你可以加载它并进行预测。
下面的代码显示了如何加载模型。
from keras.models import load_model
classifier = load_model('path_to_your_model')
加载模型后,您可以使用它像您一样进行预测。
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'
在开始将层添加到模型之前,您必须指定 'empty' 版本。
您只需在代码上方添加以下行即可修复此错误:
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
#empty model
classifier = Sequential()
然后继续指定:
#add layers, start with hidden layer and first deep layer
classifier.add(Dense(output_dim=15, init="uniform", activation='relu',input_dim = 15))
classifier.add(Dropout(rate=0.1))