2017-04-08 192 views
0

我使用flow_from_directory從目錄上傳圖片。
它是這樣完成的:Keras和ImageGenerator輸出黑色圖像

from keras.preprocessing.image import ImageDataGenerator 

train_datagen = ImageDataGenerator(rescale=1./255) 


train_generator = train_datagen.flow_from_directory(
          'Dataset/train', 
          target_size=(image_rows, image_cols), 
          batch_size=batch_size, 
          color_mode='grayscale', 
          class_mode='categorical', 
          classes=classes) 

然後我檢查加載的圖像是這樣的:

import matplotlib.pyplot as plt 

p = train_generator.next() 
plt.imshow(p[0][0][:,:,0], cmap='gray') 
plt.show() 

enter image description here

好了,但我看過Udacity課程約tensorflow和理解通過減去均值和除以標準偏差來集中輸入數據非常有用。
所以我修改

train_datagen = ImageDataGenerator(rescale=1./255) 

train_datagen = ImageDataGenerator(samplewise_center=True, 
            samplewise_std_normalization=True, 
            rescale=1./255) 

這裏是輸出:
enter image description here

出於某種原因,我裝都是黑色的所有圖像,他們numpy的陣列只是零。我的問題是:
爲什麼?

+0

有兩個問題:1.圖像的大小是多少? 2.如果是三通道圖像,是否所有通道都相同(我強烈懷疑是這種情況)? –

+0

尺寸爲64 * 160,圖像灰度並有1個通道。 flow_from_directory中的Colormode也設置爲「灰度」。 –

回答

1

由於您設置了color_mode = grayscale,因此圖片尺寸將爲64 x 160 x 1(請參閱here)。當您執行採樣定位時,它將沿着通道軸執行(請參見here)。平均矩陣將與圖像相同,因爲平均值是沿通道軸計算的。您可以在Python如下測試:

img = np.random.randint(0,10,(5,5,1)) 
print np.squeeze(np.mean(img, axis=2, keepdims=True)) # channels last 
img -= np.mean(img, axis=2, keepdims=True) 
print np.squeeze(img) # should be all zeros 

此之後,你做樣本式均值減,這不過是將每個像素與少數(1e-7)零,以避免分裂。這仍然保持img中的所有元素爲零。

可以做些什麼來解決這個問題?

而不是做樣本化規範化,考慮做整個數據集。您可以通過將featurewise_centerfeaturewise_std_normalization設置爲true來完成此操作。