如何使用Tensorflow預處理花卉訓練資料集?


可以使用Keras預處理API預處理花卉資料集。它有一個名為“image_dataset_from_directory”的方法,該方法接受驗證集、資料儲存的目錄以及其他引數來處理資料集。

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

我們將使用Keras Sequential API,它有助於構建一個順序模型,用於處理簡單的層堆疊,其中每一層都只有一個輸入張量和一個輸出張量。影像分類器是使用keras.Sequential模型建立的,資料是使用preprocessing.image_dataset_from_directory載入的。

資料有效地從磁碟載入。識別過擬合併應用緩解技術。這些技術包括資料增強和dropout。有3700朵花的影像。此資料集包含5個子目錄,每個類一個子目錄。它們是:雛菊、蒲公英、玫瑰、向日葵和鬱金香。

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

print("Pre-processing the dataset using keras.preprocessing")
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
   data_dir,
   validation_split=0.2,
   subset="validation",
   seed=123,
   image_size=(img_height, img_width),
   batch_size=batch_size)
class_names = train_ds.class_names
print("The class names are:")
print(class_names)

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

輸出

Pre-processing the dataset using keras.preprocessing
Found 3670 files belonging to 5 classes.
Using 734 files for validation.
The class names are:
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

解釋

  • 使用keras.preprocessing方法處理資料集。
  • 下一步是在控制檯上顯示類名。

更新於: 20-Feb-2021

125 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.