如何使用TensorFlow標準化花卉資料集?


資料標準化是指將資料集縮放至一個級別,以便所有特徵都可以使用等效單位表示。縮放層是使用Keras模組中的“Rescaling”方法構建的。該層使用“map”方法應用於整個資料集。

閱讀更多: 什麼是TensorFlow以及Keras如何與TensorFlow一起建立神經網路?

我們將使用花卉資料集,其中包含數千張花的影像。它包含5個子目錄,每個類都有一個子目錄。

我們使用Google Colaboratory執行以下程式碼。Google Colab或Colaboratory有助於透過瀏覽器執行Python程式碼,無需任何配置,並且可以免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。

from tensorflow.keras import layers
print("Standardizing the data using a rescaling layer")
normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)

print("This layer can be applied by calling the map function on the dataset")
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
print(np.min(first_image), np.max(first_image))

程式碼來源:https://www.tensorflow.org/tutorials/load_data/images

輸出

Standardizing the data using a rescaling layer
This layer can be applied by calling the map function on the dataset
0.0 0.96902645

解釋

  • RGB通道值範圍為0到255。
  • 這對神經網路來說並不理想。
  • 我們的目標是使輸入資料儘可能小。
  • 影像中的值已標準化為0到1的範圍。
  • 這是藉助縮放層實現的。
  • 另一種方法是在模型定義中包含此縮放層,這將簡化部署。

更新於:2021年2月19日

瀏覽量:115

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告