如何修复它?模块'keras.preprocessing.image'没有属性'load_img'

bkhjykvo  于 2022-11-13  发布在  其他
关注(0)|答案(8)|浏览(642)
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'

当我试图运行时,错误是什么意思,我该如何修复它?帮助家伙:)我正在尝试学习我不知道哪一个是错误的

z8dt9xmd

z8dt9xmd1#

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

3vpjnl9f

3vpjnl9f2#

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

taor4pac

taor4pac3#

没有'load_img' https://github.com/keras-team/keras/blob/master/keras/preprocessing/image.py
我想您尝试使用keras.utils.image_utils的load_img

kx5bkwkv

kx5bkwkv4#

使用keras.utils.load_img

import keras
import tensorflow as tf

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

lndjwyie5#

我只是在keras前面加上了“tensorflow.”就像“tensorflow.keras”一样,它起作用了。

z31licg0

z31licg06#

首次导入

import tensorflow.compat.v2 as tf

然后

tf.keras.preprocessing.image.load_img
uxhixvfz

uxhixvfz7#

试试这个
更改此

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

ars1skjm

ars1skjm8#

替换:

from keras.preprocessing import image

用于:

import keras.utils as image

相关问题