MNIST数据集下载与保存为图片格式

深度学习



下载数据集

https://github.com/geektutu/tensorflow-tutorial-samples/tree/master/mnist/data_set
t10k-images-idx3-ubyte.gz
t10k-labels-idx1-ubyte.gz
train-images-idx3-ubyte.gz
train-labels-idx1-ubyte.gz
解压后,使用以下脚本导出图片

import struct
import numpy as np
from PIL import Image

class MnistDataParser:
   # 加载图像
   def load_image(self, file_path):

       # 读取二进制数据
       binary = open(file_path,'rb').read()

       # 读取头文件
       fmt_head = '>iiii'
       offset = 0

       # 读取头文件
       magic_number,images_number,rows_number,columns_number = struct.unpack_from(fmt_head,binary,offset)

       # 打印头文件信息
       print('图片数量:%d,图片行数:%d,图片列数:%d'%(images_number,rows_number,columns_number))

       # 处理数据
       image_size = rows_number * columns_number
       fmt_data = '>'+str(image_size)+'B'
       offset = offset + struct.calcsize(fmt_head)

       # 读取数据
       images = np.empty((images_number,rows_number,columns_number))
       for i in range(images_number):
           images[i] = np.array(struct.unpack_from(fmt_data, binary, offset)).reshape((rows_number, columns_number))
           offset = offset + struct.calcsize(fmt_data)
           # 每1万张打印一次信息
           if (i+1) % 10000 == 0:
               print('> 已读取:%d张图片'%(i+1))

       # 返回数据
       return images_number,rows_number,columns_number,images

   # 加载标签
   def load_labels(self, file_path):
       # 读取数据
       binary = open(file_path,'rb').read()

       # 读取头文件
       fmt_head = '>ii'
       offset = 0

       # 读取头文件
       magic_number,items_number = struct.unpack_from(fmt_head,binary,offset)

       # 打印头文件信息
       print('标签数:%d'%(items_number))

       # 处理数据
       fmt_data = '>B'
       offset = offset + struct.calcsize(fmt_head)

       # 读取数据
       labels = np.empty((items_number))
       for i in range(items_number):
           labels[i] = struct.unpack_from(fmt_data, binary, offset)[0]
           offset = offset + struct.calcsize(fmt_data)
           # 每1万张打印一次信息
           if (i+1)%10000 == 0:
               print('> 已读取:%d个标签'%(i+1))

       return items_number,labels

   def visualaztion(self, images, labels, path):
       d = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
       for i in range(images.__len__()):
            im = Image.fromarray(np.uint8(images[i]))
            im.save(path + "%d_%d.png"%(labels[i], d[labels[i]]))
            d[labels[i]] += 1
            # im.show()
            
            if (i+1)%10000 == 0:
               print('> 已保存:%d个图片'%(i+1))
               
def change_and_save():
    mnist =  MnistDataParser()

    trainImageFile = './t10k-images.idx3-ubyte'
    _, _, _, images = mnist.load_image(trainImageFile)
    trainLabelFile = './t10k-labels.idx1-ubyte'
    _, labels = mnist.load_labels(trainLabelFile)
    mnist.visualaztion(images, labels, "./images/train/")

    testImageFile = './train-images.idx3-ubyte'
    _, _, _, images = mnist.load_image(testImageFile)
    testLabelFile = './train-labels.idx1-ubyte'
    _, labels = mnist.load_labels(testLabelFile)
    mnist.visualaztion(images, labels, "./images/test/")

if __name__ == '__main__':
    change_and_save()

导出的png格式图片保存在

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值