tensorflow 无法从“keras.preprocessing.image”导入名称“load_img”

svmlkihl  于 2023-03-19  发布在  其他
关注(0)|答案(3)|浏览(292)

为什么我会遇到这个问题?我可以从kera.preprocessing导入图像模块,但是不能从目录. TF版本导入image_dataset_from_directory:2.9.1

# make a prediction for a new image.
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model

# load and prepare the image
def load_image(filename):
    # load the image
    img = load_img(filename, target_size=(224, 224))
    # convert to array
    img = img_to_array(img)
    # reshape into a single sample with 3 channels
    img = img.reshape(1, 224, 224, 3)
    # center pixel data
    img = img.astype('float32')
    img = img - [123.68, 116.779, 103.939]
    return img

# load an image and predict the class
def run_example():
    # load the image
    img = load_image('test.jpg')
    # load model
    model = load_model('final_model.h5')
    # predict the class
    result = model.predict(img)
    print(result[0])

# entry point, run the example
run_example()

误差

from keras.preprocessing.image import load_img
ImportError: cannot import name 'load_img' from 'keras.preprocessing.image'
7nbnzgx9

7nbnzgx91#

keras.preprocessing API在Tensorflow 2.9.1中已弃用。请改用tf.keras.utils导入load_img,如下所示:

from tensorflow.keras.utils import load_img

要从目录加载数据集,请使用tensorflow.keras.utils.image_dataset_from_directory。有关详细信息,请参阅此link

z5btuh9x

z5btuh9x2#

from tensorflow.keras.preprocessing.image import load_img
xj3cbfub

xj3cbfub3#

替换

from keras.preprocessing.image import load_img

from keras_preprocessing.image import load_img

相关问题