如何使用 Keras 順序 API 和 TensorFlow 下載花卉資料集?
可以使用 Keras 順序 API 和 Google API(儲存資料集的 API)下載花卉資料集。使用帶有 API(URL)的 'get_file' 方法獲取資料集並將其儲存在記憶體中。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 一起建立神經網路?
包含至少一層卷積層的神經網路稱為卷積神經網路。卷積神經網路已被用於針對特定型別的問題(例如影像識別)產生良好的結果。
使用 keras.Sequential 模型建立影像分類器,並使用 preprocessing.image_dataset_from_directory 載入資料。資料從磁碟高效載入。識別過擬合併應用緩解技術。這些技術包括資料增強和 dropout。有 3700 張花卉影像。此資料集包含 5 個子目錄,每個類有一個子目錄。它們是:雛菊、蒲公英、玫瑰、向日葵和鬱金香。
我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,並且無需任何配置即可免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。
import matplotlib.pyplot as plt import numpy as np import os import PIL import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.models import Sequential import pathlib print("Required pakcages imported") dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz" data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True) data_dir = pathlib.Path(data_dir) print("Data has been downloaded")
程式碼來源:https://www.tensorflow.org/tutorials/images/classification
輸出
Required pakcages imported Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz 228818944/228813984 [==============================] - 5s 0us/step Data has been downloaded
解釋
- 匯入所需的包。
- 從 API 下載資料。
廣告