如何使用Tensorflow將花卉資料集下載到環境中?


花卉資料集可以使用一個Google API下載,該API基本上鍊接到花卉資料集。“get_file”方法可以用來傳遞API作為引數。完成此操作後,資料將下載到環境中。

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

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

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

import numpy as np
import os
import PIL
import PIL.Image
import tensorflow as tf
import tensorflow_datasets as tfds
print("The Tensorflow version is :")
print(tf.__version__)

import pathlib
print("The dataset is being downloaded")
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file(origin=dataset_url,
fname='flower_photos',
untar=True)
data_dir = pathlib.Path(data_dir)

print("The number of images in the dataset")
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)

print("Sample data in the dataset")
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[2]))

print("Some more sample data from the dataset")
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[4]))

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

輸出

The Tensorflow version is :
2.4.0
The dataset is being downloaded
Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz
228818944/228813984 [==============================] - 4s 0us/step
The number of images in the dataset
3670
Sample data in the dataset
Some more sample data from the dataset

解釋

  • 下載所需軟體包。
  • 在控制檯上顯示Tensorflow的版本。
  • 使用API下載資料。
  • 在控制檯上還顯示影像示例。

更新於: 2021年2月19日

341 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.