关闭

容智睿软件科技

MNIST数据集手写数字识别(一)

2026-02-06 浏览:
浏览 awei 主页 关注 投稿量: 粉丝量: 关注量:

# coding: utf-8

try:

    import urllib.request

except ImportError:

    raise ImportError('You should use Python 3.x')

import os.path

import gzip

import pickle

import os

import numpy as np

 

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"

 

train_num = 60000

test_num = 10000

img_dim = (1, 28, 28)

img_size = 784

 

def _download(file_name):

    file_path = dataset_dir + "/" + file_name

    

    if os.path.exists(file_path):

        return

 

    print("Downloading " + file_name + " ... ")

    urllib.request.urlretrieve(url_base + file_name, file_path)

    print("Done")

    

def download_mnist():

    for v in key_file.values():

       _download(v)

        

def _load_label(file_name):

    file_path = dataset_dir + "/" + file_name

    

    print("Converting " + file_name + " to NumPy Array ...")

    with gzip.open(file_path, 'rb') as f:

            labels = np.frombuffer(f.read(), np.uint8, offset=8)

    print("Done")

    

    return labels

 

def _load_img(file_name):

    file_path = dataset_dir + "/" + file_name

    

    print("Converting " + file_name + " to NumPy Array ...")    

    with gzip.open(file_path, 'rb') as f:

            data = np.frombuffer(f.read(), np.uint8, offset=16)

    data = data.reshape(-1, img_size)#二维,和flatten一维有区别

    print("Done")

    

    return data

    

def _convert_numpy():

    dataset = {}

    dataset['train_img'] =  _load_img(key_file['train_img'])

    dataset['train_label'] = _load_label(key_file['train_label'])    

    dataset['test_img'] = _load_img(key_file['test_img'])

    dataset['test_label'] = _load_label(key_file['test_label'])

    

    return dataset

 

def init_mnist():

    download_mnist()

    dataset = _convert_numpy()

    print("Creating pickle file ...")

    with open(save_file, 'wb') as f:

        pickle.dump(dataset, f, -1)

    print("Done!")

 

def _change_one_hot_label(X):

    T = np.zeros((X.size, 10))

    for idx, row in enumerate(T):

        row[X[idx]] = 1

        

    return T

    

def load_mnist(normalize=True, flatten=True, one_hot_label=False):

    """读入MNIST数据集

    

    Parameters

    ----------

    normalize : 将图像的像素值正规化为0.0~1.0

    one_hot_label : 

        one_hot_label为True的情况下,标签作为one-hot数组返回

        one-hot数组是指[0,0,1,0,0,0,0,0,0,0]这样的数组

    flatten : 是否将图像展开为一维数组

    

    Returns

    -------

    (训练图像, 训练标签), (测试图像, 测试标签)

    """

    if not os.path.exists(save_file):

        init_mnist()

        

    with open(save_file, 'rb') as f:

        dataset = pickle.load(f)

    

    if normalize:

        for key in ('train_img', 'test_img'):

            dataset[key] = dataset[key].astype(np.float32)

            dataset[key] /= 255.0

            

    if one_hot_label:

        dataset['train_label'] = _change_one_hot_label(dataset['train_label'])

        dataset['test_label'] = _change_one_hot_label(dataset['test_label'])

    

    if not flatten:

         for key in ('train_img', 'test_img'):

            dataset[key] = dataset[key].reshape(-1, 1, 28, 28)

 

    return (dataset['train_img'], dataset['train_label']), (dataset['test_img'], dataset['test_label']) 

 

 

if __name__ == '__main__':

    init_mnist()

一键获取完整项目代码

python


————————————————

版权声明:本文为CSDN博主「寅恪光潜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_41896770/article/details/119576575

(x_train,t_train),(x_test,t_test)=load_mnist(flatten=True,normalize=False)

 

x_train.shape,t_train.shape,x_test.shape,t_test.shape

结果:((60000, 784), (60000,), (10000, 784), (10000,))

 

训练集图像是60000张,784(28*28)个像素/张,测试集图片是10000张,图片也是784(28*28)个像素/张

 

如果flatten为False,也就是不展开输入的图像(一维数组),那么图像就是1*28*28的三维数组

(x_train,t_train),(x_test,t_test)=load_mnist(flatten=False,normalize=False)

结果:((60000, 1, 28, 28), (60000,), (10000, 1, 28, 28), (10000,))

一键获取完整项目代码

python


————————————————

版权声明:本文为CSDN博主「寅恪光潜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_41896770/article/details/119576575

import sys,os

import numpy as np

from PIL import Image

os.chdir('D:\Anaconda3\TONYTEST')

sys.path.append('D:\Anaconda3\TONYTEST')

from dataset.mnist import load_mnist

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[4]#9

label=t_train[4]#9

print(label)

print(img.shape)#(784,)

img=img.reshape(28,28)

img_show(img)

 

将出现一个28*28像素的手写数字为9的图片

一键获取完整项目代码


————————————————

版权声明:本文为CSDN博主「寅恪光潜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_41896770/article/details/119576575


标签:
加载中~
智能客服
转人工 ×