类型错误:__array__() 需要 1 个位置参数,但给出了 2 个(图像分类 Keras) [英] TypeError: __array__() takes 1 positional argument but 2 were given (Image classification Keras)

查看:48
本文介绍了类型错误:__array__() 需要 1 个位置参数,但给出了 2 个(图像分类 Keras)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决这个问题?我试过在 image.img_to_array 方法 中设置 dtype=None.

How to troubleshoot this? I've tried setting dtype=None in the image.img_to_array method.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from keras.preprocessing import image

image_size = (180, 180)
batch_size = 32


model = keras.models.load_model('best_model.h5')

img = keras.preprocessing.image.load_img(
    "GarnetCreek_7-15-2019.jpeg", target_size=image_size
)

img_array = image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)  # Create batch axis

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

这会引发以下错误:

Traceback (most recent call last):
img_array = image.img_to_array(img, dtype=None)
return image.img_to_array(img, data_format=data_format, **kwargs)
x = np.asarray(img, dtype=dtype)
    return array(a, dtype, copy=False, order=order)
TypeError: __array__() takes 1 positional argument but 2 were given

有人见过这个吗?非常感谢!

Has anyone seen this before? Many thanks!

推荐答案

此错误有时是由于 Pillow 8.3.0 中的错误造成的,因为它是 此处.(您不能直接在代码中使用 import PIL,但是一些库,例如 tf.keras.preprocessing.image.load_img 内部使用 PIL)

This error sometimes is due to a bug in Pillow 8.3.0 as it is here. (You may not use import PIL directly in your code, however some libraries such as tf.keras.preprocessing.image.load_img use PIL internally)

因此,从 PIL 8.3.0 降级到 8.2.0 是可行的.

So, downgrading from PIL 8.3.0 to 8.2.0 may work.

检查PIL版本:

import PIL
print(PIL.__version__)

如果是8.3.0,那么你可以降级到8.2.0:

If it is 8.3.0, then you may downgrade to 8.2.0:

!pip install pillow==8.2.0

这篇关于类型错误:__array__() 需要 1 个位置参数,但给出了 2 个(图像分类 Keras)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆