如何用国外网站做头条,湖南网站建设开发,建湖做网站哪家好,wordpress自动标签添加内链插件前面的待补充
3.6 手写数字识别 3.6.1 MNIST 数据集 本书提供了便利的 Python 脚本 mnist.py #xff0c;该脚本支持从下载 MNIST 数据集到将这些数据转换成 NumPy 数组等处理#xff08;mnist.py 在 dataset 目录下#xff09;。 使用 mnist.py 时#xff0c;当前目录必须…前面的待补充
3.6 手写数字识别 3.6.1 MNIST 数据集 本书提供了便利的 Python 脚本 mnist.py 该脚本支持从下载 MNIST 数据集到将这些数据转换成 NumPy 数组等处理mnist.py 在 dataset 目录下。 使用 mnist.py 时当前目录必须是 ch01 、ch02 、ch03、…、ch08 目录中的一个。使用 mnist.py 中的 load_mnist() 函数就可以按下述方式轻松读入 MNIST 数据。 实际使用中是报错的提示urllib.request获取数据失败检查代码 from dataset.mnist import load_mnist 使用的是dataset目录下的mnist.py中的load_mnist函数 直接查看mnist.py源码
url_base http://yann.lecun.com/exdb/mnist/
key_file {train_img:train-images-idx3-ubyte.gz,train_label:train-labels-idx1-ubyte.gz,test_img:t10k-images-idx3-ubyte.gz,test_label:t10k-labels-idx1-ubyte.gz
}dataset_dir os.path.dirname(os.path.abspath(__file__))
save_file dataset_dir /mnist.pkl上面的代码是把从网站下载的4个文件放到mnist.pkl中保存。下载代码如下
def _download(file_name):file_path dataset_dir / file_nameif os.path.exists(file_path):returnurllib.request.urlretrieve(url_base file_name, file_path)直接运行下载这一步报错了。从浏览器直接下载试试
MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges
下载被拒绝网上搜了一下原因然后就更换为
GitHub - zalandoresearch/fashion-mnist: A MNIST-like fashion product database. Benchmark
下载OK了但里面的图明显不是0-9的数字库不太对,又找了一个
vision/torchvision/datasets/mnist.py at ddad38f3a84d4d87cbb389bc78e245920fe86f82 · pytorch/vision · GitHub
https://ossci-datasets.s3.amazonaws.com/mnist/
这个库也不太对里面显示的应该也是各种衬衫、裙子的识别估计和fashion-mnist是一样的。
最终在github上找到了原始的0-9数字数据集https://github.com/geektutu/tensorflow-tutorial-samples/blob/master/mnist/data_set/ 通过浏览器下载到dataset目录就可以了。 程序下载经常会失败原因众所周知国内访问github总是时灵时不灵用浏览器也要多刷几次才行骂一句万恶的墙啥时候才能拆了接轨世界。
插曲如果是内网python下载需要在程序中设置代理。 file_path dataset_dir / file_nameif os.path.exists(file_path):returnprint(set proxy...)# 设置代理proxies {http: http://proxy.xxx:80, https: https://proxy.xxx:80}proxy_handler urllib.request.ProxyHandler(proxies)# 创建Openeropener urllib.request.build_opener(proxy_handler)# 安装Openerurllib.request.install_opener(opener)print(Downloading file_name ... )urllib.request.urlretrieve(url_base file_name, file_path)print(Done)下载好了dataset就可以mnist_show.py了提示还有错误:
D:\python\test\ch03py mnist_show.py Traceback (most recent call last): File mnist_show.py, line 5, in module from PIL import Image ModuleNotFoundError: No module named PIL PIL 库没有py -m pip install Pillow --proxy http://proxy.xxx.cn:80 -i https://pypi.tuna.tsinghua.edu.cn/simple
Installing collected packages: Pillow Successfully installed Pillow-9.5.0 成功安装Pillow后问题解决。
mnist_show.py代码加了注释
import sys,os
sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定这里要用dataset目录下的mnist.py文件
import numpy as np
from dataset.mnist import load_mnist # 导入mnist.py文件中的load_mnist函数
from PIL import Image # 这个包需要单独安装py -m pip install Pillow --proxy http://proxy.xxx.cn:80 -i https://pypi.tuna.tsinghua.edu.cn/simple def img_show(img):pil_img Image.fromarray(np.uint8(img)) # 把保存为 NumPy 数组的图像数据转换为 PIL 用的数据对象pil_img.show()# 第一次加载需要几分钟load_mnist是从网上下载如前所述直接手工下载到dataset目录就行了不需要执行下载这样就快了
# load_mnist 函数以“( 训练图像, 训练标签 )( 测试图像, 测试标签 ) ”的形式返回读入的 MNIST 数据
(x_train, t_train),(x_test, t_test) load_mnist(flattenTrue, # flattenTrue 时读入的图像是以784个元素构成的一维数组的形式保存的。因此显示图像时需要把它变为原来的 28像素 × 28 像素的形状。normalizeFalse) # normalize 设置是否将输入图像正规化为 0.01.0 的值。如果将该参数设置为 False 则输入图像的像素会保持原来的 0255# 输出各数据的形状
print(x_train.shape) # (60000, 784)
print(t_train.shape) # (60000,)
print(x_test.shape) # (10000, 784)
print(t_test.shape) # (10000,)img x_train[0]
label t_train[0]
print(label)print(img.shape)
img img.reshape(28,28) # 通过 reshape() 方法的参数指定期望的形状更改 NumPy 数组的形状。
print(img.shape)img_show(img)
运行结果和书上是一样的标签是5图形也是5侧面证明和书上的数据集是一套。
D:\python\test\ch03py mnist_show.py (60000, 784) (60000,) (10000, 784) (10000,)5 (784,) (28, 28)
图形 其他内容待续