下载EMNIST数据库
01 EMNIST数据库
一、前言
EMNIST 数据库中包含有数字和字母多个数据集合。 数据文件与MNIST兼容, 可以直接替换原来的MNIST数据集合对网络进行训练。 下面从 PyTorch 图像库中下载该数据库, 查看一下具体的数据库文件的结构。
▲ 图1.1.1 DIagram of the conversion process used to convert the NIST dataset
▲ 图1.1.2 Structure and Orgranizaiton of the EMNIST datasets
二、下载数据库
使用 torch vision 中的 datasets。 下载其中的 EMNIST数据库。 下载了562M字节的压缩文件。 原以为这个命令只是下载其中的 MNIST数据库, 实际上, 它将 EMNIST中所有的数据文件都下载下来了。 下载文件存储在 E 盘下的对应的子目录中。
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
root = r'e:\aidataset\emnist'
data = datasets.EMNIST(root,
split='mnist',
train=True,
download=True)
可以看到在 E盘数据目录中, 包括有24个解压缩后的数据文件。 分别属于 EMNIST六个子集, 每个子集包括有四个数据文件, 分别是训练集合与测试集合, 图片以及对应的标号。 下面检查一下这些数据文件的内容和格式。
三、MNIST数据集合
使用昨天读取FashionMNIST程序, 读取 MNIST数据集合, 会发现, 图像有 90°的旋转。 将图像数据矩阵进行转置, 图像显示正常了。
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST2.PY -- by Dr. ZhuoQing 2024-12-26
#
# Note:
#============================================================
from headm import *
fimage = r'E:\aidataset\emnist\EMNIST\raw\emnist-mnist-train-images-idx3-ubyte'
flabel = r'E:\aidataset\emnist\EMNIST\raw\emnist-mnist-train-labels-idx1-ubyte'
#------------------------------------------------------------
with open(flabel,'rb') as f:
flabeldim = f.read(100+8)[8:]
with open(fimage, 'rb') as f:
fimagedim = f.read(28*28*100+16)[16:]
#------------------------------------------------------------
from PIL import Image
nn = 28*28
cols, rows = 5,5
for i in range(cols):
for j in range(rows):
n = i * cols + j
imgdata = frombuffer(fimagedim[n*nn:(n+1)*nn], uint8).reshape(28,28)
img = Image.fromarray(imgdata.T)
plt.subplot(cols, rows, n+1)
plt.imshow(img, cmap='gray')
id = flabeldim[n]
plt.title(f"{id}")
plt.axis('off')
plt.show()
#------------------------------------------------------------
# END OF FILE : TEST2.PY
#============================================================
四、LETTER图像
下面, 检查一下数据路中的字母数据集合的图像。 通常对读取的字母数据矩阵进行转置, 对应的标号是从 1到26表示字符的种类。 显示了数据鲁中 前面25个字符的结果。
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST2.PY -- by Dr. ZhuoQing 2024-12-26
#
# Note:
#============================================================
from headm import *
fimage = r'E:\aidataset\emnist\EMNIST\raw\emnist-letters-train-images-idx3-ubyte'
flabel = r'E:\aidataset\emnist\EMNIST\raw\emnist-letters-train-labels-idx1-ubyte'
#------------------------------------------------------------
with open(flabel,'rb') as f:
flabeldim = f.read(100+8)[8:]
with open(fimage, 'rb') as f:
fimagedim = f.read(28*28*100+16)[16:]
#------------------------------------------------------------
from PIL import Image
nn = 28*28
cols, rows = 5,5
for i in range(cols):
for j in range(rows):
n = i * cols + j
imgdata = frombuffer(fimagedim[n*nn:(n+1)*nn], uint8).reshape(28,28)
img = Image.fromarray(imgdata.T)
plt.subplot(cols, rows, n+1)
plt.imshow(img, cmap='gray')
id = flabeldim[n]
plt.title(f"%c"%(id+ord('A')-1))
plt.axis('off')
plt.show()
#------------------------------------------------------------
# END OF FILE : TEST2.PY
#============================================================
※ 总 结 ※
本文从 PyTorch 图像库中下载了 EMNIST 数据库。 利用这些数据库可以为后面手写数字识别算法的训练和改进提供基础数据库的支持。
■ 相关文献链接:
● 相关图表链接: