MNIST数据集提取图片和标注信息

本文介绍如何处理MNIST手写数字数据集,包括训练集和测试集的图片及标签提取,并提供Python代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MNIST数据集

  • 简介

    MNIST数据集(http://yann.lecun.com/exdb/mnist/)是著名的手写数字分类数据集,主要由一下四部分组成:

    • 训练集图片:train-images.idx3-ubyte,处理后能得到60000个训练图片
    • 训练街标签:train-labels.idx1-ubyte,处理后能得到60000个训练标签
    • 测试集图片:t10k-images.idx3-ubyte,处理后能得到10000个测试图片
    • 测试集标签:t10k-labels.idx1-ubyte,处理后能得到10000个测试标签
  • 处理

    初始接触MNIST数据集时,需要将对应存储格式的数据信息转换为对应的图片和标签,具体处理过程如下:

    # -*- coding: utf-8 -*-
    """
    Created on Sat Aug 11 10:06:46 2018
    
    @author: yzhang
    """
    
    import os
    import struct
    import numpy as np
    import matplotlib.pyplot as plt
    from PIL import Image
    
    def load_mnist_image(path, filename, type = 'train'):
        full_name = os.path.join(path, filename)
        fp = open(full_name, 'rb')
        buf = fp.read()
        index = 0;
        magic, num, rows, cols = struct.unpack_from('>IIII', buf, index)
        index += struct.calcsize('>IIII')
    
        for image in range(0, num):
            im = struct.unpack_from('>784B', buf, index)
            index += struct.calcsize('>784B')
            im = np.array(im, dtype = 'uint8')
            im = im.reshape(28, 28)
            im = Image.fromarray(im)
            if (type == 'train'):
                isExists = os.path.exists('./train')
                if not isExists:
                    os.mkdir('./train')
                im.save('./train/train_%s.bmp' %image, 'bmp')
            if (type == 'test'):
                isExists = os.path.exists('./test')
                if not isExists:
                    os.mkdir('./test')
                im.save('./test/test_%s.bmp' %image, 'bmp')
    
    def load_mnist_label(path, filename, type = 'train'):
        full_name = os.path.join(path, filename)
        fp = open(full_name, 'rb')
        buf = fp.read()
        index = 0;
        magic, num = struct.unpack_from('>II', buf, index)
        index += struct.calcsize('>II')
        Labels = np.zeros(num)
    
        for i in range(num):
            Labels[i] = np.array(struct.unpack_from('>B', buf, index))
            index += struct.calcsize('>B')
    
        if (type == 'train'):
            np.savetxt('./train_labels.csv', Labels, fmt='%i', delimiter=',')
        if (type == 'test'):
            np.savetext('./test_labels.csv', Labels, fmt='%i', delimiter=',')
    
        return Labels
    
    if __name__ == '__main__':
        path = 'D:/Project/an_python/Minst/'
        train_images = 'train-images.idx3-ubyte'
        load_mnist_image(path, train_images, 'train')
        train_labels = 'train-labels.idx1-ubyte'
        load_mnist_label(path, train_labels, 'train')
        test_images = 't10k-images.idx3-ubyte'
        load_mnist_image(path, test_images, 'test')
        test_labels = 't10k-labels.idx1-ubyte'
        load_mnist_label(path, test_labels, 'test')
    

    执行上述Python程序后,便能够在D:/Project/an_python/Minst目录下得到提取出来的图片和标注信息。

### 如何下载 NLST 数据集 #### 下载方法概述 NLST(National Lung Screening Trial)是一个重要的医疗影像数据集,主要用于研究肺癌筛查的相关技术。然而,由于其涉及隐私保护以及伦理审查等问题,访问数据集通常受到严格控制。 为了合法合规地获得 NLST 数据集,需遵循特定流程: 1. **注册并申请权限** 需要前往 The Cancer Imaging Archive (TCIA) 官方网站完成账户注册,并提交数据访问请求。这一过程可能需要填写详细的科研计划书或说明具体用途[^3]。 2. **通过审核机制** 提交的数据访问请求会经过 TCIA 的内部评审团队评估。只有当申请人能够证明自己具备合理的研究目的及足够的安全保障措施时,才会被授予进一步操作资格。 3. **安装专用工具** 获得批准之后,建议下载并配置官方推荐的客户端软件——TCIA Download Manager 来高效管理整个传输环节中的各项参数设置工作。 4. **实际执行下载命令** 利用已安装好的管理器连接至服务器端指定路径位置处实施批量拉动作即可顺利完成最终目标文件集合本地化存储任务。 以下是基于 Python 编写的简易脚本用于自动化处理部分常规事务场景下的示范代码片段: ```python import subprocess def download_nlst_data(output_directory): command = [ "java", "-jar", "/path/to/tcia-download-manager.jar", "--collection", "NLST", "--outputDirectory", output_directory, "--apiKey", "<your_api_key_here>" ] result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if result.returncode != 0: raise Exception(f"Error downloading data: {result.stderr.decode('utf-8')}") download_nlst_data("/desired/local/path") ``` 请注意替换上述模板里的占位符变量值为自己环境实际情况适配后的有效字符串表达形式后再运行测试验证效果如何! ---
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值