如何使用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://tensorflowcn.cn/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 之间。

更新于:2021年2月20日

164 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.