如何使用TensorFlow和Python下載和準備CIFAR資料集?
CIFAR 資料集可以使用 `datasets` 模組中的 `load_data` 方法下載。下載後,資料會被分成訓練集和驗證集。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協同建立神經網路?
我們將使用 Keras Sequential API,它有助於構建一個順序模型,用於處理簡單的層堆疊,其中每一層只有一個輸入張量和一個輸出張量。
包含至少一層卷積層的神經網路稱為卷積神經網路。卷積神經網路通常由以下幾種層的組合構成:
- 卷積層
- 池化層
- 密集層
卷積神經網路已被用於解決特定型別的問題,例如影像識別。
我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 基於 Jupyter Notebook 構建。
import tensorflow as tf from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt print("The CIFAR dataset is being downloaded") (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data() print("The pixel values are normalized to be between 0 and 1") train_images, test_images = train_images / 255.0, test_images / 255.0 class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer','dog', 'frog', 'horse', 'ship', 'truck']
程式碼來源:https://www.tensorflow.org/tutorials/images/cnn
輸出
The CIFAR dataset is being downloaded Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 170500096/170498071 [==============================] - 11s 0us/step The pixel values are normalized to be between 0 and 1
解釋
- CIFAR-10 資料集包含 60,000 張 10 個類別的彩色影像,每個類別有 6,000 張影像。
- 該資料集分為 50,000 張訓練影像和 10,000 張測試影像。
- 這些類別互斥,彼此之間沒有重疊。
- 此資料集已下載,資料已歸一化到 0 到 1 之間。
廣告