如何使用TensorFlow將花卉資料集拆分為訓練集和驗證集?
可以使用 Keras 預處理 API 將花卉資料集拆分為訓練集和驗證集,藉助“image_dataset_from_directory”函式,該函式需要驗證集的百分比拆分。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?
使用 keras.Sequential 模型建立影像分類器,並使用 **preprocessing.image_dataset_from_directory** 載入資料。資料可以有效地從磁碟載入。識別過擬合併應用技術來緩解它。這些技術包括資料增強和 dropout。有 3700 張花卉影像。此資料集包含 5 個子目錄,每個類有一個子目錄。它們是:雛菊、蒲公英、玫瑰、向日葵和鬱金香。
我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。
batch_size = 32
img_height = 180
img_width = 180
print("The data is being split into training and validation set")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)程式碼來源:https://www.tensorflow.org/tutorials/images/classification
輸出
The data is being split into training and validation set Found 3670 files belonging to 5 classes. Using 2936 files for training.
解釋
- 這些影像使用 image_dataset_from_directory 實用程式從磁碟載入。
- 這將從磁碟上的影像目錄轉換為 tf.data.Dataset。
- 下載資料後,將為載入程式定義一些引數。
- 資料被拆分為訓練集和驗證集。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP