深度学习
文章目录
- 深度学习
- 下载数据集
下载数据集
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 Imageclass 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_numberfmt_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,labelsdef 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格式图片保存在