python将ubyte格式的MNIST数据集转成jpg图片格式并保存

       我们去官网下载的MNIST数据集一般是ubyte格式的,这样的格式对于计算机来说可以正常读取使用,但是对我们人来说并不友好,有时候我们希望得到一张一张的图片,便于我们观察和测试等。下面给出将ubyte格式的MNIST数据集转成jpg图片格式的代码,以及一个下载链接,下载的文件中包括原始的ubyte格式的MNIST数据集和转换为jpg格式的数据集。

百度网盘链接:https://pan.baidu.com/s/1n7j36Qpa3GL17xjq7yHb4Q 
提取码:xov3

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 10 13:58:57 2022
@author: 2540817538(有问题请联系此QQ)
python3.8.8
"""
import numpy as np
import cv2
import os
import struct
 
def trans(image, label, save):#image位置,label位置和转换后的数据保存位置
    if 'train' in os.path.basename(image):
        prefix = 'train'
    else:
        prefix = 'test'
 
    labelIndex = 0
    imageIndex = 0
    i = 0
    lbdata = open(label, 'rb').read()
    magic, nums = struct.unpack_from(">II", lbdata, labelIndex)
    labelIndex += struct.calcsize('>II')
 
    imgdata = open(image, "rb").read()
    magic, nums, numRows, numColumns = struct.unpack_from('>IIII', imgdata, imageIndex)
    imageIndex += struct.calcsize('>IIII')
 
    for i in range(nums):
        label = struct.unpack_from('>B', lbdata, labelIndex)[0]
        labelIndex += struct.calcsize('>B')
        im = struct.unpack_from('>784B', imgdata, imageIndex)
        imageIndex += struct.calcsize('>784B')
        im = np.array(im, dtype='uint8')
        img = im.reshape(28, 28)
        save_name = os.path.join(save, '{}_{}_{}.jpg'.format(prefix, i, label))
        cv2.imwrite(save_name, img)
 
if __name__ == '__main__':
    #需要更改的文件路径!!!!!!
    #此处是原始数据集位置
    train_images = 'C:/Users/25408/Desktop/fashion/train-images.idx3.ubyte'
    train_labels = 'C:/Users/25408/Desktop/fashion/train-labels.idx1.ubyte'
    test_images ='C:/Users/25408/Desktop/fashion/t10k-images.idx3.ubyte'
    test_labels = 'C:/Users/25408/Desktop/fashion/t10k-labels.idx1.ubyte'
    #此处是我们将转化后的数据集保存的位置
    save_train ='C:/Users/25408/Desktop/fashion/MNIST_data/train_images/'
    save_test ='C:/Users/25408/Desktop/fashion/MNIST_data/test_images/'
    
    if not os.path.exists(save_train):
        os.makedirs(save_train)
    if not os.path.exists(save_test):
        os.makedirs(save_test)
 
    trans(test_images, test_labels, save_test)
    trans(train_images, train_labels, save_train)

转化后的数据集内容如下:

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是将MNIST数据集转换为JSON格式的步骤: 1. 下载MNIST数据集并将其解压缩。 2. 创建一个Python脚本,并导入必要的模块,如NumPy、json和gzip。 3. 使用NumPy模块加载MNIST数据集并将其转换为NumPy数组。 4. 将NumPy数组转换为Python列表。 5. 将Python列表转换为JSON格式。 6. 将JSON格式的数据写入文件。 下面是一个示例代码: ```python import numpy as np import json import gzip # 加载MNIST数据集 def load_mnist(path, kind='train'): labels_path = path + '/' + kind + '-labels-idx1-ubyte.gz' images_path = path + '/' + kind + '-images-idx3-ubyte.gz' with gzip.open(labels_path, 'rb') as lbpath: labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8) with gzip.open(images_path, 'rb') as imgpath: images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784) return images, labels # 转换为JSON格式 def mnist_to_json(images, labels, outfile): mnist_data = {"data": [], "labels": []} for i in range(len(labels)): mnist_data["data"].append(images[i].tolist()) mnist_data["labels"].append(int(labels[i])) with open(outfile, 'w') as f: json.dump(mnist_data, f) # 测试 X_train, y_train = load_mnist('mnist', kind='train') mnist_to_json(X_train, y_train, 'mnist_train.json') ``` 此代码将MNIST数据集的训练集转换为JSON格式,并将其写入名为'mnist_train.json'的文件中。您可以根据需要修改代码以转换测试集或将数据写入不同的文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值