如何使用 Keras 順序 API 探索 TensorFlow 中的花卉資料集?


藉助 'PIL' 包和 'Image.open' 方法,可以使用 Keras 順序 API 探索花卉資料集。不同的子目錄包含不同型別的花卉影像,這些影像可以被索引並在控制檯上顯示。

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

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

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

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

image_count = len(list(data_dir.glob('*/*.jpg')))
print("The number of images in the dataset is:")
print(image_count)
print("A glimpse of the dataset")
print("ROSES")
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[1]))
print("TULIPS")
tulips = list(data_dir.glob('tulips/*'))
PIL.Image.open(str(tulips[0]))

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

輸出

The number of images in the dataset is:
3670
A glimpse of the dataset
ROSES

TULIPS


解釋

  • 資料樣本顯示在控制檯上。

更新於: 2021年2月20日

95 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.