我目前正在研究图像分类器项目。在测试预测函数期间,我收到错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:'flowers / test / 1 / image_06760'
文件路径正确。 您可以在这里找到整个笔记本: https://github.com/MartinTschendel/image-classifier-1/blob/master/190829-0510-Image%20Classifier%20Project.ipynb
这是相应的预测功能和该功能的检验:
def predict(image_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
#loading model
loaded_model = load_checkpoint(model)
# implement the code to predict the class from an image file
img = Image.open(image_name)
img = process_image(img)
# convert 2D image to 1D vector
img = np.expand_dims(img, 0)
img = torch.from_numpy(img)
#set model to evaluation mode and turn off gradients
loaded_model.eval()
with torch.no_grad():
#run image through network
output = loaded_model.forward(img)
#calculate probabilities
probs = torch.exp(output)
probs_top = probs.topk(topk)[0]
index_top = probs.topk(topk)[1]
# Convert probabilities and outputs to lists
probs_top_list = np.array(probs_top)[0]
index_top_list = np.array(index_top[0])
#load index and class mapping
loaded_model.class_to_idx = train_data.class_to_idx
#invert index-class dictionary
idx_to_class = {x: y for y, x in class_to_idx.items()}
#convert index list to class list
classes_top_list = []
for index in index_top_list:
classes_top_list += [idx_to_class[index]]
return probs_top_list, classes_top_list
# test predict function
# inputs equal paths to saved model and test image
model_path = '190824_checkpoint.pth'
image_name = data_dir + '/test' + '/1/' + 'image_06760'
probs, classes = predict(image_name, model_path, topk=5)
print(probs)
print(classes)
这是我收到的错误:
FileNotFoundError: [Errno 2] No such file or directory: 'flowers/test/1/image_06760'