簡體   English   中英

圖像預處理在 vgg16 中不起作用

[英]image preprocessing is not working in vgg16

我正在使用遷移學習(vgg16)學習圖像分類,我正在使用 keras 的內置時尚 mnist 數據集。

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

為了預處理 vgg16 的數據,我通過從 keras.applications.vgg16 導入 preprocess_input 使用了以下命令

X_train = preprocess_input(x_train)
X_test = preprocess_input(x_test)

train_features = vgg16.predict(np.array(X_train), batch_size=256, verbose=1)
test_features = vgg16.predict(np.array(X_test), batch_size=256, verbose=1)

但我收到以下錯誤

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (60000, 28, 28)

我正在使用 keras2.2.4,pip 19.0.3

Fashion mnist 數據集有灰度圖像,這意味着它只有一個深度通道,而VGG16是用 3 個深度通道的 RGB 圖像訓練的。 根據您的錯誤,您不能將 VGG16 與單通道輸入一起使用。 要將 VGG16 用於時尚 mnist 數據集,您必須將圖像讀取為三通道。 您可以使用np.stack如下進一步處理X_trainX_test

import numpy as np
X_train = np.stack((X_train,)*3, axis=-1)
X_test = np.stack((X_test,)*3, axis=-1)

VGG 接受最小 32 和最大 224,可以在這里看到,要重塑這個,我們可以做

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) # converting it to (,28x28x1)
x_train = np.pad(x_train, ((0,0),(2,2),(2,2),(0,0)), 'constant',constant_values=(0, 0)) # converting it to min (,32x32x1)
x_train = np.stack((x_train,)*3, axis=-1) # (,32,32,1,3)
x_train = x_train[:,:,:,0,:] # (,32,32,1)
y_train = keras.utils.to_categorical(y_train, num_classes)

這可以很容易地用於keras中的.fit()、.evaluate()和.predict(),而不需要將其轉換為張量數據並編寫生成器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM