【发布时间】:2021-09-15 04:01:39
【问题描述】:
如何解决这个问题?我尝试在image.img_to_array method 中设置dtype=None。
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
有人见过这个吗?非常感谢!
【问题讨论】:
-
img的类型和形状是什么?请告诉我PIL的版本。import PIL然后PIL.__version__。并尝试用from tensorflow.keras.preprocessing import image替换from keras.preprocessing import image -
图像形状为 (686, 1140, 3)。 PIL 版本是 8.3.0。我确实尝试按照建议替换导入,但问题仍然存在。谢谢!
-
将枕头从 8.3.0 降级到 8.2 有时可以。试试吧。将 PIL 降级到 8.2.0。
-
哇,好用!感激不尽
标签: python numpy tensorflow keras python-imaging-library