尝试将 standardize/normalize 数据输入 CNN,但遇到问题

Trying to standardize/normalize data to go into CNN, but having issues

我有以下代码片段,我正在尝试 standardize/normalize 训练我的 CNN 之前的数据。

X = [] # Image data
y = [] # Labels

datagen = ImageDataGenerator(samplewise_center=True)

Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
  img = cv2.imread(path) # Reads image and returns np.array
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
  img = cv2.resize(img, (200, 200)) 
  img = datagen.standardize(img) #ERROR POINTING HERE
  X.append(img)

....

但是,当 运行 这时,我得到以下错误提示,指向我在上面评论的行:

UFuncTypeError: Cannot cast ufunc 'subtract' output from dtype('float64') to dtype('uint8') with casting rule 'same_kind' 

知道我在标准化方面哪里出了问题吗?或者有没有更简单的方法让我标准化?我看到一些解决方案将人们除以 255,但我不确定具体如何实施。谢谢!

您需要应用img_to_array方法:

from keras.preprocessing.image import img_to_array

img = cv2.resize(img, (200, 200)) 
img = img_to_array(img)
img = datagen.standardize(img)