tensorflow 尝试评估CNN模型,但得到此错误

szqfcxe2  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(120)

下面是代码来评估模型运行历元后,我试图从驱动器中获取图像。我还试图从unsplash获取图像仍然相同的错误。

sad_face = "/content/drive/MyDrive/Dataset/images/images/validation/sad/10004.jpg"
sunflower_path = tf.keras.utils.get_file('Sad', origin=sad_face)

img = tf.keras.utils.load_img(
    sunflower_path, target_size=(img_height, img_width)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch

predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
)

这就是我得到的

UnidentifiedImageError                    Traceback (most recent call last)

<ipython-input-30-17ae3e24a3ca> in <cell line: 4>()
      2 sunflower_path = tf.keras.utils.get_file('Sad', origin=sad_face)
      3 
----> 4 img = tf.keras.utils.load_img(
      5     sunflower_path, target_size=(img_height, img_width)
      6 )

1 frames

/usr/local/lib/python3.9/dist-packages/PIL/Image.py in open(fp, mode, formats)
   3028     for message in accept_warnings:
   3029         warnings.warn(message)
-> 3030     raise UnidentifiedImageError(
   3031         "cannot identify image file %r" % (filename if filename else fp)
   3032     )

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f547c017680>
kuarbcqp

kuarbcqp1#

您可能需要在origin参数中使用完整的URL。我能够根据您的代码加载this图像。

相关问题