(1) Mnist数据集: 简介与读取

MNIST 数据集可在 http://yann.lecun.com/exdb/mnist/ 获取, 是 Yann Lecun 大佬整理的手写数字数据集,分为以下四个部分:

dataset name details
Training set images train-images-idx3-ubyte.gz 60,000 个样本的像素值
Training set labels train-labels-idx1-ubyte.gz 60,000 个标签
Test set images t10k-images-idx3-ubyte.gz 10,000 个样本的像素值
Test set labels t10k-labels-idx1-ubyte.gz 10,000 个标签

数据读取

import gzip
import struct

def read_data(label_url,image_url):
    with gzip.open(label_url) as flbl:
        magic, num = struct.unpack(">II",flbl.read(8))
        label = np.fromstring(flbl.read(),dtype=np.int8)
    with gzip.open(image_url,'rb') as fimg:
        magic, num, rows, cols = struct.unpack(">IIII",fimg.read(16))
        image = np.fromstring(fimg.read(),dtype=np.uint8).reshape(len(label),rows,cols)
    return (label, image)

获取Train和Test

输入是 ohe 标志,输出是像素值与标签值构成的 tuple

def get_train(ohe=True):
    (train_lbl, train_img) = read_data('DataSet/Mnist/train-labels-idx1-ubyte.gz','DataSet/Mnist/train-images-idx3-ubyte.gz')
    train_img = train_img.reshape((*train_img.shape, 1))  # 添加通道维度
    train_img = preprocessing_img(train_img)  # 归一化处理
    if ohe:
        class_num = len(np.unique(train_lbl))
        train_lbl = np_utils.to_categorical(train_lbl, num_classes=class_num)  # 对标签进行 one hot 编码
    return train_img, train_lbl

def get_test(ohe=True):
    (val_lbl, val_img) = read_data('DataSet/Mnist/t10k-labels-idx1-ubyte.gz','DataSet/Mnist/t10k-images-idx3-ubyte.gz')
    val_img = val_img.reshape((*val_img.shape, 1))  # 添加通道维度
    val_img = preprocessing_img(val_img)
    if ohe:
        class_num = len(np.unique(val_lbl))
        val_lbl = np_utils.to_categorical(val_lbl, num_classes=class_num)  # 对标签进行 one hot 编码
    return val_img, val_lbl
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这是一部被100%剧透,还是会看到哭成狗的电影。 因为这是一个关于狗的故事,很多人都听过的故事,忠犬八公的故事。 ...
    8分电影阅读 852评论 3 5
  • 小男孩的欢呼声,小狗狗摇着尾巴欢快的转着圈儿,不时的回头观望着它的小伙伴。 夏末的海边,丝丝凉意沁人心脾。 湿热、...
    雪韵_莲心阅读 253评论 21 25
  • 四种彩瓷的简单分辨 斗彩、五彩、粉彩和珐琅彩看起来差不多,该如何分辨呢? 不知道为什么人们总是喜欢用“四”来概...
    空森林阅读 501评论 0 6
  • 01 白楹与白夜所来自的辽国,是位于九州北方地区的四个国家之一,与大部分的国家不同,辽国没有下辖的附属村落,只有名...
    走不动游星阅读 675评论 0 4