ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image'

ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image'

我是新来的。我对这段代码有疑问,

#Library
import numpy as np
import pickle
import cv2
from os import listdir
from sklearn.preprocessing import LabelBinarizer
from keras.models import Sequential
from keras.layers import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation, Flatten, Dropout, Dense
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.preprocessing import image
#from tensorflow.keras.preprocessing.image import img_to_array
from keras.preprocessing.image import img_to_array
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

我收到一个错误

此代码来自github link

我正在使用

  1. python 3.7.13
  2. 张量流 2.9
  3. opencv 4.5.5
  4. 喀拉斯 2.9.0

Keras Documentation V2.9.0,

tf 版本 2.9.0 中 img_to_array 移动到 utlis

代替

from keras.preprocessing.image import img_to_array

试试这个,

from tensorflow.keras.utils import img_to_array