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

相关推荐
啊吧怪不啊吧30 分钟前
LangChain之模型调用
python·langchain
溪语流沙32 分钟前
Django + Vue电商项目第001讲:开篇|注册登录加增删改查,那不是电商
redis·python·mysql·docker·typescript·django·vue
夜雪一千41 分钟前
如何使用BeautifulSoup库来提取HTML中的特定元素
python
IT毕设梦工厂1 小时前
计算机毕业设计选题推荐:基于大数据的城市空气污染物浓度数据分析与可视化|毕业设计选题|计算机毕设|选题推荐|毕设指导|项目定制|源码|高质量项目
大数据·python·信息可视化·数据挖掘·数据分析·课程设计·数据可视化
程序员阿黄1 小时前
基于 Django 与 Vue 3 的智能实验室预约系统设计与实现
后端·python·mysql·django·vue·毕设
用户298698530141 小时前
Python 如何实现 Word 与 RTF 文档互转
后端·python·api
程序猿在线码字1 小时前
深度学习(一)
python·深度学习·神经网络·机器学习
打工仔折腾 AI1 小时前
用 Shell 脚本自动部署 mysqld_exporter 并接入 Prometheus 远程抓取
人工智能·后端·python·性能优化·ai agent 实战
梦想不只是梦与想1 小时前
环境管理:Conda 与 venv
python·conda·uv
Yolanda_20221 小时前
6.pytorch加载数据初认识
python