使用Python将MNIST数据集转化为图片
2016-09-23 17:41
716 查看
1,对于训练集数据:
分别运行即可!
import numpy as np import struct from PIL import Image import os data_file = 'somePath/train-images.idx3-ubyte' #需要修改的路径 # It's 47040016B, but we should set to 47040000B data_file_size = 47040016 data_file_size = str(data_file_size - 16) + 'B' data_buf = open(data_file, 'rb').read() magic, numImages, numRows, numColumns = struct.unpack_from( '>IIII', data_buf, 0) datas = struct.unpack_from( '>' + data_file_size, data_buf, struct.calcsize('>IIII')) datas = np.array(datas).astype(np.uint8).reshape( numImages, 1, numRows, numColumns) label_file = 'somePath/train-labels.idx1-ubyte' #需要修改的路径 # It's 60008B, but we should set to 60000B label_file_size = 60008 label_file_size = str(label_file_size - 8) + 'B' label_buf = open(label_file, 'rb').read() magic, numLabels = struct.unpack_from('>II', label_buf, 0) labels = struct.unpack_from( '>' + label_file_size, label_buf, struct.calcsize('>II')) labels = np.array(labels).astype(np.int64) datas_root = '/somePath/mnist_train' #需要修改的路径 if not os.path.exists(datas_root): os.mkdir(datas_root) for i in range(10): file_name = datas_root + os.sep + str(i) if not os.path.exists(file_name): os.mkdir(file_name) for ii in range(numLabels): img = Image.fromarray(datas[ii, 0, 0:28, 0:28]) label = labels[ii] file_name = datas_root + os.sep + str(label) + os.sep + \ 'mnist_train_' + str(ii) + '.png' img.save(file_name)2,对于测试数据集:
import numpy as np import struct from PIL import Image import os data_file = 'somePath/t10k-images.idx3-ubyte' #需要修改的路径 # It's 7840016B, but we should set to 7840000B data_file_size = 7840016 data_file_size = str(data_file_size - 16) + 'B' data_buf = open(data_file, 'rb').read() magic, numImages, numRows, numColumns = struct.unpack_from( '>IIII', data_buf, 0) datas = struct.unpack_from( '>' + data_file_size, data_buf, struct.calcsize('>IIII')) datas = np.array(datas).astype(np.uint8).reshape( numImages, 1, numRows, numColumns) label_file = 'somePath/t10k-labels.idx1-ubyte'#需要修改的路径 # It's 10008B, but we should set to 10000B label_file_size = 10008 label_file_size = str(label_file_size - 8) + 'B' label_buf = open(label_file, 'rb').read() magic, numLabels = struct.unpack_from('>II', label_buf, 0) labels = struct.unpack_from( '>' + label_file_size, label_buf, struct.calcsize('>II')) labels = np.array(labels).astype(np.int64) datas_root = 'somePath/mnist_test' #需要修改的路径 if not os.path.exists(datas_root): os.mkdir(datas_root) for i in range(10): file_name = datas_root + os.sep + str(i) if not os.path.exists(file_name): os.mkdir(file_name) for ii in range(numLabels): img = Image.fromarray(datas[ii, 0, 0:28, 0:28]) label = labels[ii] file_name = datas_root + os.sep + str(label) + os.sep + \ 'mnist_test_' + str(ii) + '.png' img.save(file_name)
分别运行即可!
相关文章推荐
- mnist数据集转成图片 Python 实现
- 使用Python解析MNIST数据集(IDX文件格式)
- 使用OpenCV把二进制mnist数据集转换为图片
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- 使用PCA + KNN对MNIST数据集进行手写数字识别 python
- 【机器学习】MNIST数据集上的python读取和使用操作
- MNIST数据集转化为二维图片
- python使用opencv将灰度图人脸图片转化为RGB
- 神经网络与深度学习 1.6 使用Python实现基于梯度下降算法的神经网络和MNIST数据集的手写数字分类程序
- 使用python将自己的图片数据集准换成为cnn的数据集
- 使用python将任意字符串转化为列表
- 使用PyInstaller2将Python脚本转化为可执行文件(上-安装部分)(转载自博客园balian)
- [转]使用PyInstaller2将Python脚本转化为可执行文件(上-安装部分)
- 使用Python抓取网页图片[转载]
- 使用PyInstaller2将Python脚本转化为可执行文件(中-使用部分)
- 使用python生成excel格式的图片
- 使用Python编写客户端 上传文字or图片至新浪微博 by OAuth 2.0
- 使用PyInstaller2将Python脚本转化为可执行文件(下-进阶使用)
- [转]使用PyInstaller2将Python脚本转化为可执行文件(下-进阶使用)
- 在WIN7-64下使用PYTHON获得摄像头图片