TypeError: __array__() 采用 1 个位置参数,但给出了 2 个(图像分类 Keras)

Nom*_*ems 3 python numpy python-imaging-library keras tensorflow

如何解决这个问题?我尝试过dtype=Noneimage.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]
Run Code Online (Sandbox Code Playgroud)

这会引发以下错误:

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
Run Code Online (Sandbox Code Playgroud)

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

Kav*_*veh 8

Pillow 8.3.0此错误有时是由于此处的错误所致。(您可能不会import PIL直接在代码中使用,但是有些库例如在内部tf.keras.preprocessing.image.load_img使用PIL

因此,从 降级PIL 8.3.08.2.0可能会起作用。

检查PIL版本:

import PIL
print(PIL.__version__)
Run Code Online (Sandbox Code Playgroud)

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

!pip install pillow==8.2.0
Run Code Online (Sandbox Code Playgroud)