简体   繁体   English

如何解决? AttributeError: 模块 'keras.preprocessing.image' 没有属性 'load_img'

[英]HOW TO FIX IT? AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'

    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')

output:输出:


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

when I try to run.当我尝试跑步时。 What does the error mean and how can I fix it?该错误是什么意思,我该如何解决? help guys:) I'm trying to learn I don't know anymore which one is wrong帮助大家:)我正在努力学习我不知道哪个是错的

Replace:代替:

from keras.preprocessing import image

for:为了:

import keras.utils as image

I'm facing the same problem today.我今天面临同样的问题。 You can try using tensorflow 2.8.0 to fix it or try tf.keras.utils.load_img instead of image.load_img .您可以尝试使用 tensorflow 2.8.0 来修复它或尝试tf.keras.utils.load_img而不是image.load_img

there is no 'load_img' https://github.com/keras-team/keras/blob/master/keras/preprocessing/image.py没有'load_img' https://github.com/keras-team/keras/blob/master/keras/preprocessing/image.py

I suppose you trying to use load_img of keras.utils.image_utils我想你试图使用 keras.utils.image_utils 的 load_img

use keras.utils.load_img使用keras.utils.load_img

import keras
import tensorflow as tf

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

I just added 'tensorflow.'我刚刚添加了“张量流”。 infront of the keras like 'tensorflow.keras.'像'tensorflow.keras'这样的keras。 and it worked.它奏效了。

I also face the same error.我也面临同样的错误。 I used from tensorflow.keras.utils import load_img, img_to_array and it work for me.我使用from tensorflow.keras.utils import load_img, img_to_array它对我有用。

first import首先导入

import tensorflow.compat.v2 as tf

then然后

tf.keras.preprocessing.image.load_img

Try this out试试这个

change this改变这个

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

To this对此

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)

reff:- https://keras.io/api/data_loading/image/参考资料:- https://keras.io/api/data_loading/image/

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

I too had this and fixed我也有这个并修好了

changes in import进口变化

  1. " from keras.utils import load_img, img_to_array instead " of " from keras.preprocessing import image " from keras.utils import load_img, img_to_array 而不是from keras.preprocessing import image

and change并改变

  1. " img = image.load_img(path, target_size=(150,150)) " to "l oad_img(path, target_size=(150,150)) " img = image.load_img(path, target_size=(150,150)) ”到“l oad_img(path, target_size=(150,150))

  2. " x = image.img_to_array(img) " to " x = img_to_array(img) " x = image.img_to_array(img) ”到“ x = img_to_array(img)

The "AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'" occurs because the keras preprocessing API has been deprecated.出现“AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'”是因为 keras 预处理 API 已被弃用。 To solve the error, import the load_img() function from tensorflow.keras.utils.load_img.要解决该错误,请从 tensorflow.keras.utils.load_img 中导入 load_img() 函数。

I too faced the same problem.我也面临同样的问题。 I used from tensorflow.keras.utils import load_img, img_to_array .我使用from tensorflow.keras.utils import load_img, img_to_array Problem got solved.问题得到了解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 模块 'keras.preprocessing.image' 没有属性 'img_to_array' - AttributeError: module 'keras.preprocessing.image' has no attribute 'img_to_array' 无法从“keras.preprocessing.image”导入名称“load_img” - cannot import name 'load_img' from 'keras.preprocessing.image' load_img , keras.preprocessing.image ,TypeError: an integer is required (got type str) - load_img , keras.preprocessing.image ,TypeError: an integer is required (got type str) 使用 keras.preprocessing.image load_img 时图像背景变黑 - Background of image turning black when using keras.preprocessing.image load_img 如何修复 AttributeError:'JpegImageFile' object 没有属性 'load_img' - How to Fix AttributeError: 'JpegImageFile' object has no attribute 'load_img' Keras 预处理在使用 load_img() 导入图像时旋转图像 - Keras Preprocessing Rotates Image While Importing It With load_img() 模块'keras.preprocessing.image'没有属性'ndim'-自定义resnet50转移学习 - module 'keras.preprocessing.image' has no attribute 'ndim' - custom resnet50 transfer learning AttributeError:模块'keras.api._v2.keras.preprocessing.image'没有属性'as_numpy_iterator' - AttributeError: module 'keras.api._v2.keras.preprocessing.image' has no attribute 'as_numpy_iterator' 这是什么错误“ AttributeError:'numpy.ndarray'对象没有属性'load_img'”? - What is this error “AttributeError: 'numpy.ndarray' object has no attribute 'load_img'”? 从 keras.preprocessing.image import img_to_array ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image - from keras.preprocessing.image import img_to_array ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM