如何解决?AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'

25
    import numpy as np
    from keras.preprocessing import image
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    
    
    %matplotlib inline
    
    
    
    path = './test/paper2.png'
    
    img = image.load_img(path, target_size=(150,150))
    imgplot = plt.imshow(img)
    x = image.img_to_array(img)
    img_test = np.expand_dims(x, axis=0)
    
    classes = model.predict(img_test, batch_size=10)
    
    print(classes)
    paper, rock, scissors = classes[0]
    
    if paper==1.:
        print('paper')
    elif rock==1.:
        print('rock')
    else:
        print('scissors')

输出:


AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'

当我尝试运行时,出现了错误。这个错误是什么意思,我该如何修复呢?帮帮我吧 :) 我正在努力学习, 我不知道哪一个是错误的。


请尝试使用 from tensorflow.keras.preprocessing import image,如此处所述 @Almaz Fazulzyanov。 - Elias
14个回答

23

替换:

from keras.preprocessing import image

for:

import keras.utils as image

18

今天我也遇到了同样的问题。 你可以尝试使用tensorflow 2.8.0来解决它,或者尝试使用tf.keras.utils.load_img代替image.load_img


3

我也遇到了这个问题并已经解决了

导入模块发生了改变:

  1. "from keras.utils import load_img, img_to_array" 替换为 "from keras.preprocessing import image"

并且需要做出如下修改:

  1. "img = image.load_img(path, target_size=(150,150))" 修改为 "load_img(path, target_size=(150,150))"

  2. "x = image.img_to_array(img)" 修改为 "x = img_to_array(img)"


你能分享一下你正在使用的Keras版本作为参考吗?谢谢! - Ben Saunders
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

2

尝试这个

更改这个

from keras.preprocessing import image
test_image = image.load_img('$PATH', target_size = (64, 64))
test_image =  image.img_to_array(test_image)

对于这个问题

from keras.utils import load_img, img_to_array
test_image = load_img('$PATH', target_size = (64, 64))
test_image = img_to_array(test_image)

参考文献:- https://keras.io/api/data_loading/image/

来源:- https://github.com/keras-team/keras/blob/v2.10.0/keras/utils/image_utils.py#L364

这是关于it技术方面的内容。

2

我也遇到同样的错误。 我使用了from tensorflow.keras.utils import load_img, img_to_array,并且它对我起作用了。


2

出现“AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'”的原因是keras预处理API已经被弃用。要解决此错误,请从tensorflow.keras.utils.load_img导入load_img()函数。


这是一个重复的答案。 - jgmh

2

首先使用这个

from keras.utils import load_img, img_to_array

然后像这样使用加载图像

train_image = []

# ...

# Assume `train` is each batch X output in the form of dictionary
for i in tqdm(range(train.shape[0])):
    img = load_img('path to folder' + train['Name'][i], target_size=(400, 400, 3))
    img = img_to_array(img)
    img = img/255
    train_image.append(img)

X = np.array(train_image)

0

首先导入

import tensorflow.compat.v2 as tf

那么

tf.keras.preprocessing.image.load_img

0

使用keras.utils.load_img

import keras
import tensorflow as tf

image = keras.utils.load_img('path_to_image', target_size=(img_size, img_size))

0
我遇到了类似的错误,我添加了以下内容:
import keras.utils as image 然后它就正常工作了。

这个问题已经在现有的答案中得到了解答。请不要重复回答或将“谢谢”作为答案。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接