试图标准化/标准化数据以进入CNN,但有问题

问题描述 投票:0回答:1

我有以下代码段,我正在尝试在训练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的解决方案,但是我不确定如何实现。谢谢!

python tensorflow deep-learning conv-neural-network normalization
1个回答
0
投票

您需要应用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)
© www.soinside.com 2019 - 2024. All rights reserved.