python:mnist 数据集下载,parse

pip install mnist

pip show mnist

Name: mnist

Version: 0.2.2

Summary: Python utilities to download and parse the MNIST dataset

Home-page: https://github.com/datapythonista/mnist

Author: Marc Garcia

Author-email: garcia.marc@gmail.com

License: BSD

Location: d:\python39\lib\site-packages

Requires: numpy

https://opendatalab.com/MNIST/

复制代码
D:\python>TREE /F data

D:\PYTHON\DATA
└─MNIST
    └─raw
            t10k-images-idx3-ubyte
            t10k-images-idx3-ubyte.gz
            t10k-labels-idx1-ubyte
            t10k-labels-idx1-ubyte.gz
            train-images-idx3-ubyte
            train-images-idx3-ubyte.gz
            train-labels-idx1-ubyte
            train-labels-idx1-ubyte.gz

修改 D:\Python39\Lib\site-packages\mnist\init.py

找到第143行:fname = download_file(fname, target_dir=target_dir, force=force) 注释掉,修改为:

python 复制代码
def download_and_parse_mnist_file(fname, target_dir=None, force=False):
    """Download the IDX file named fname from the URL specified in dataset_url
    and return it as a numpy array.

    Parameters
    ----------
    fname : str
        File name to download and parse

    target_dir : str
        Directory where to store the file

    force : bool
        Force downloading the file, if it already exists

    Returns
    -------
    data : numpy.ndarray
        Numpy array with the dimensions and the data in the IDX file
    """
    #fname = download_file(fname, target_dir=target_dir, force=force)
    if target_dir == None:
        target_dir = './data/MNIST/raw'
    fname = os.path.join(target_dir, fname)
        
    fopen = gzip.open if os.path.splitext(fname)[1] == '.gz' else open
    with fopen(fname, 'rb') as fd:
        return parse_idx(fd)

编写一个测试脚本 mnist_test.py 如下

python 复制代码
import mnist

print('mnist train shape:')
train_X = mnist.train_images()
train_y = mnist.train_labels()
print(train_X.shape, train_y.shape)

print('mnist test shape:')
test_X = mnist.test_images()
test_y = mnist.test_labels()
print(test_X.shape, test_y.shape)

运行 cmd

python mnist_test.py

python 复制代码
D:\python> python mnist_test.py
mnist train shape:
(60000, 28, 28) (60000,)
mnist test shape:
(10000, 28, 28) (10000,)

编写一个显示手写数字图像的脚本 mnist_image1.py 如下

python 复制代码
from PIL import Image
import numpy as np
import mnist

images = mnist.train_images()
img = Image.fromarray(images[0]).resize((280, 280), Image.Resampling.NEAREST)
#img.save('mnist_img1.jpg') # 可选:保存图片
img.show() # 可选:显示图片 

python mnist_image1.py

相关推荐
全栈老石20 分钟前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
梨落秋霜28 分钟前
Python入门篇【模块/包】
python
阔皮大师2 小时前
INote轻量文本编辑器
java·javascript·python·c#
小法师爱分享2 小时前
StickyNotes,简单便签超实用
java·python
深蓝电商API2 小时前
处理字体反爬:woff字体文件解析实战
爬虫·python
开源技术2 小时前
Claude Opus 4.6 发布,100万上下文窗口,越贵越好用
人工智能·python
张3蜂2 小时前
深入理解 Python 的 frozenset:为什么要有“不可变集合”?
前端·python·spring
皮卡丘不断更2 小时前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
爱打代码的小林2 小时前
基于 MediaPipe 实现实时面部关键点检测
python·opencv·计算机视觉
极客小云3 小时前
【ComfyUI API 自动化利器:comfyui_xy Python 库使用详解】
网络·python·自动化·comfyui