TF09——CIFAR10数据集


TF09——CIFAR10数据集

Cifar10数据集

  • Cifar10数据集一共有6万张彩色图片,每张图片有32行32列像素点的红绿蓝三通道数据
    • 提供5万张32*32像素点的十分类彩色图片和标签,用于训练。
    • 提供1万张32*32像素点的十分类彩色图片和标签,用于测试。

image-20220320114924843

标签依次对应0-9

导入cifar10数据集

可以只用load_data()函数直接从cifar10数据集中读取训练集和测试集 ,给(x_train,y_train),(x_test,y_test)赋值

cifar10=tf.keras.datasets.cifar10
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
  • 可以使用以下两句话,把训练集中的第一个样本x_train[0]可视化出来
plt.imshow(x_train[0]) #绘制图片
plt.show()

程序运行结果:

image-20220320115431182

  • 用print函数把训练集中第一个样本的输入特征打印出来
print("x_train[0]:\n",x_train[0])

程序运行结果:

是一个32行32列三通道的三维数组,是这个青蛙图片32行32列像素点的RGB值

image-20220320115635877

  • 用print函数把训练集中第一个样本的标签打印出来
print("y_train[0]:",y_train[0])

程序运行结果:

image-20220320115834557

  • 还可以用print函数打印出测试集的形状
print("x_test.shape:",x_test.shape)

程序运行结果:

一共10000个32行32列的RGB三通道数据,维度是4

image-20220320120034067

源码:P24_cifar10_datasets.py

import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np

np.set_printoptions(threshold=np.inf)

cifar10 = tf.keras.datasets.cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# 可视化训练集输入特征的第一个元素
plt.imshow(x_train[0])  # 绘制图片
plt.show()

# 打印出训练集输入特征的第一个元素
print("x_train[0]:\n", x_train[0])
# 打印出训练集标签的第一个元素
print("y_train[0]:\n", y_train[0])

# 打印出整个训练集输入特征形状
print("x_train.shape:\n", x_train.shape)
# 打印出整个训练集标签的形状
print("y_train.shape:\n", y_train.shape)
# 打印出整个测试集输入特征的形状
print("x_test.shape:\n", x_test.shape)
# 打印出整个测试集标签的形状
print("y_test.shape:\n", y_test.shape)

程序运行结果:

image-20220320130423229