声明:此博客为个人笔记。参考书籍详见https://www.ituring.com.cn/book/1921
前言
本章用深度学习进行手写数字的识别。数据集采用MNIST。
MNIST下载地址:https://github.com/oreilly-japan/deep-learning-from-scratch
一、下载数据
1、在文件中打开cmd
2、输入以下命令,并按回车键
3、数据集下载完成
注意:mnist是上述参考书提供的python脚本,该脚本支持从下载MNIST数据到将这些数据集转换成NumPy数组等处理(mnist.py在dataset目录下)。使用mnist.py时,当前目录必须是ch01,ch02,ch03,…目录中的一个。使用mnist.py中的load_mnist()函数,可以轻松读入MNIST数据。
二、查看数据集
1、在ch03中打开cmd
2、在cmd面板中依次输入以下命令
activate geoscpt #geoscpt是我自己建立的虚拟环境
jupyter notebook
3、查看数据
import sys,os
sys.path.append(os.pardir) #为了导入父目录中的文件而进行的设定
from ch03.mnist import load_mnist
#(训练图像,训练标签)(测试图像,测试标签),normlize:是否正规化;flatten:是否展开输入图像,变为一维数组
(x_train,t_train),(x_test,t_test) = load_mnist(flatten=True,normalize=False)
print(x_train.shape)
print(t_train.shape)
print(x_test.shape)
print(t_test.shape)
结果如下:
(60000, 784)
(60000,)
(10000, 784)
(10000,)
这里解释一下784,MNIST是28像素乘28像素的灰度图像,展开一位数组后为784。
4、显示图像
import sys,os
sys.path.append(os.pardir) #为了导入父目录中的文件而进行的设定
from ch03.mnist import load_mnist
import numpy as np
from PIL import Image
def img_show(img):
pil_img = Image.fromarray(np.uint8(img))
pil_img.show()
(x_train,t_train),(x_test,t_test) = load_mnist(flatten=True,normalize=False)
img = x_train[0]
label = t_train[0]
print('要显示的数字的标签为',label)
print('一位数组的shape:',img.shape)
img = img.reshape(28,28) #把图像的形状变为原来的尺寸
print('二维数组的图像',img.shape)
#图像显示
img_show(img)
结果如下: