我想問一下我的代碼是否有任何方法可以從包含大量水果影像的檔案夾中隨機選擇影像。這個想法是使用隨機影像測驗我的 CNN 模型。這是我嘗試過的代碼,但出現錯誤,如下所示。
from keras.preprocessing import image
import numpy as np
import os
import random
test_img = random.choice(os.listdir("drive/My Drive/HAZIQ/TESTTEST/MODELTEST/"))
img = image.load_img(test_img, target_size = (208,256))
img = image.img_to_array(img, dtype=np.uint8)
img = np.array(img)/255.0
prediction = model.predict(img[np.newaxis, ...])
print("Probability: ",np.max(prediction[0], axis=-1))
predicted_class = class_names[np.argmax(prediction[0], axis=-1)]
print("Classified: ",predicted_class,'\n')
plt.axis('off')
plt.imshow(img.squeeze())
plt.title("Loaded Image")
錯誤
() > 5 > 6 test_img = random.choice(os.listdir("drive/My Drive/HAZIQ/TESTTEST/MODELTEST/")) > ----> 7 img = image.load_img(test_img, target_size = (208,256)) > 8 img = image.img_to_array(img, dtype=np.uint8) > 9 img = np.array(img)/255.0 1 幀 /usr/local/lib/python3 .7/dist-packages/keras_preprocessing/image/utils.py > in load_img(path, grayscale, color_mode, target_size, interpolation) > 111 raise ImportError('Could not import PIL.Image.' > 112 '使用
load_img需要PIL .') > --> 113 with open(path, 'rb') as f: > 114 img = pil_image.open(io.BytesIO(f.read())) > 115 if color_mode == 'grayscale': FileNotFoundError :[Errno 2] 沒有這樣的檔案或目錄:'32660-3194-5469.jpg'
我可以確認“32660-3194-5469.jpg”在檔案夾中。我不知道為什么它說沒有這樣的檔案或目錄。
我希望它是這樣的

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475730.html
標籤:Python 张量流 喀拉斯 卷积神经网络 谷歌合作实验室
上一篇:ValueError:X有1個特征,但SVC期望3個特征作為輸入
下一篇:如何使用經過訓練的模型預測MNIST資料(輸入形狀的預期軸-1的值為784,但接收到的輸入形狀為(784,1))
