MNIST数据集格式转化
来源:互联网 发布:苹果版软件商店 编辑:程序博客网 时间:2024/05/20 06:10
MNIST数据集是ubyte格式存储的,现在转化为png格式:
训练集:
import numpy as npimport structfrom PIL import Imageimport osdata_file = 'train-images-idx3-ubyte'# It's 47040016B, but we should set to 47040000Bdata_file_size = 47040016data_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 = 'train-labels-idx1-ubyte'# It's 60008B, but we should set to 60000Blabel_file_size = 60008label_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 = '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)
- 测试集:
import numpy as npimport structfrom PIL import Imageimport osdata_file = 't10k-images-idx3-ubyte'# It's 7840016B, but we should set to 7840000Bdata_file_size = 7840016data_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 = 't10k-labels-idx1-ubyte'# It's 10008B, but we should set to 10000Blabel_file_size = 10008label_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 = '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)
0 0
- MNIST数据集格式转化
- 将MNIST手写数字数据集二进制格式转化为.jpg图片格式
- 图像转化为手写体mnist格式的数据
- Python解析lmdb格式mnist数据集
- MNIST数据集格式ubyte转png
- mnist数据集可视化(npy格式)
- MNIST数据集的格式转换
- 使用Python将MNIST数据集转化为图片
- Caffe下将mnist手写图片数据转化成lmdb格式
- mnist数据集格式转换---机器学习(0)
- mnist数据集常见格式(npz、gz等)简介
- json数据转化格式
- MNIST数据集规则化
- mnist手写数据集
- MNIST数据集分析
- 详解 MNIST 数据集
- tensorflow MNIST数据集
- MNIST数据集处理
- HaLoop——适用于迭代计算的Hadoop
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- 乘法逆元求法
- 环境配置六:Sublim Text文本编辑
- 程序员如何提高影响力2.0
- MNIST数据集格式转化
- Spring 学习笔记 整合 structs2
- dogse入门指南
- 读《失业的程序员》笔记-----中
- oracle11g 手工建库
- /sys/bus/usb/devices下的文件命名
- Springmvc json加载数据 前端拼接技术
- android在 Activity 之间传递参数
- libevent7