【问题标题】:How to select a random image from a folder for CNN test process?如何从文件夹中选择随机图像用于 CNN 测试过程?
【发布时间】:2022-12-24 01:12:18
【问题描述】:

我想问一下我的代码是否有任何方法可以从包含大量水果图像的文件夹中随机选择图像。我的想法是使用随机图像测试我的 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")

错误

FileNotFoundError Traceback (most recent call > last) in () > 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 '使用@987654323 @ 需要 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”在文件夹中。我不知道为什么它说没有这样的文件或目录。

我希望它是这样的

enter image description here

任何帮助都会很棒。

谢谢!

【问题讨论】:

    标签: python tensorflow keras conv-neural-network google-colaboratory


    【解决方案1】:

    代码

    os.listdir("drive/My Drive/HAZIQ/TESTTEST/MODELTEST/")
    

    将返回文件名但不是文件的完整路径,因此找不到文件。你需要做的是

    sdir=r'drive/My Drive/HAZIQ/TESTTEST/MODELTEST/'
    flist=os.listdir(sdir)
    test_img=random.choice(flist)
    test_img=os.path.join(sdir, test_img)
    

    【讨论】:

    • 谢谢你,先生。有用。现在我明白出了什么问题 :)
    【解决方案2】:

    命令os.listdir(mydir) 返回一个列表,其中包含条目在目录中。

    1. 在随机选择列表中的一个元素之前,必须确保这个元素实际上是一个图像文件myimage
    2. 你的问题在这里: 重要的是,你必须设置完整路径:myfilepath = os.path.join(mydir, myimage)

      或者你可以使用glob,它允许通配符,比如jpeg图像:

      import glob
      image_list = glob.glob(os.path.join(mydir, '*.jpg'))
      

    【讨论】:

      【解决方案3】:

      给大家介绍一下,数据集更适合做预处理数据的转换和后续处理的转换。

      [ 样本 ]:

      """""""""""""""""""""""""""""""""""""""""""""""""""""""""
      DataSet
      """""""""""""""""""""""""""""""""""""""""""""""""""""""""
      dataset = tf.data.Dataset.from_tensor_slices((tf.constant(tf.cast(list_file, dtype=tf.int64), shape=(33, 1, 32, 32, 4), dtype=tf.int64),tf.constant(list_label, shape=(33, 1, 1), dtype=tf.int64)))
      dataset = tf.data.Dataset.range(33)
      dataset = dataset.shuffle(10, reshuffle_each_iteration=True)
      

      【讨论】:

        猜你喜欢
        • 2020-01-07
        • 1970-01-01
        • 2012-06-13
        • 2014-07-29
        • 2020-06-01
        • 2012-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多