MNIST数据库处理--matlab生成mnist_uint8.mat
2016-12-31 12:09
555 查看
我的上一篇博客CNN卷积神经网络手写数字识别实例及代码详解中使用了mnist_uint8.mat数据,
但我们最初下载的数据一般是从http://yann.lecun.com/exdb/mnist/index.html下载得到的2进制文件。
本文讲解如何将下载得到的2进制文件作成mnist_uint8,mat。最后生成的数据集存放连接:http://download.csdn.net/download/fuwenyan/9726703
本文作者:非文艺小燕儿,VivienFu,
欢迎大家转载适用,请注明出处。http://blog.csdn.net/fuwenyan?viewmode=contents http://blog.csdn.net/fuwenyan?viewmode=contents
代码是自己编写的,如果有疏漏之处,请大家帮忙指正。
mnist_uint8.m
decodefile.m
但我们最初下载的数据一般是从http://yann.lecun.com/exdb/mnist/index.html下载得到的2进制文件。
本文讲解如何将下载得到的2进制文件作成mnist_uint8,mat。最后生成的数据集存放连接:http://download.csdn.net/download/fuwenyan/9726703
本文作者:非文艺小燕儿,VivienFu,
欢迎大家转载适用,请注明出处。http://blog.csdn.net/fuwenyan?viewmode=contents http://blog.csdn.net/fuwenyan?viewmode=contents
代码是自己编写的,如果有疏漏之处,请大家帮忙指正。
mnist_uint8.m
%Create database mnist_uint8 by the original dix3-ubyte file %MNIST源文件下载地址http://yann.lecun.com/exdb/mnist/index.html clear all close all train_x_file=char('train-images.idx3-ubyte');%得到vector形式 test_x_file=char('t10k-images.idx3-ubyte');%得到vector形式 train_y_file=char('train-labels.idx1-ubyte');%得到vector形式 test_y_file=char('t10k-labels.idx1-ubyte');%得到vector形式 train_x=decodefile(train_x_file,'image'); test_x=decodefile(test_x_file,'image'); train_y=decodefile(train_y_file,'label'); test_y=decodefile(test_y_file,'label'); save('mnist_uint8.mat','train_x','train_y','test_x','test_y');%以上代码已经实现功能要求。 % 如果想检验转化是否正确,可执行以下代码。 train_x_matrix=reshape(train_x,28,28,60000);%reshape后的图像是放倒的 train_x_matrix=permute(train_x_matrix,[2 1 3]);%对每张图像进行行列的转置处理 test_x_matrix=reshape(test_x,28,28,10000);%reshape后的图像是放倒的 test_x_matrix=permute(test_x_matrix,[2 1 3]);%对每张图像进行行列的转置处理 figure; for m=1:5 subplot(2,5,m),imshow(train_x_matrix(:,:,m)); title(num2str(train_y(m))); end for m=1:5 subplot(2,5,m+5),imshow(test_x_matrix(:,:,m)); title(num2str(test_y(m))); end
decodefile.m
%MNIST源文件下载地址http://yann.lecun.com/exdb/mnist/index.html %功能:将下载得到的二进制文件转换为10进制数据,提取像素数据和标签数据 %适用:仅适用于MNIST数据集,修改后可适用于其他 function output=decodefile(filename,type) %数据介绍如下,参考网址http://yann.lecun.com/exdb/mnist/index.html % TRAINING SET LABEL FILE (train-labels-idx1-ubyte): % % [offset] [type] [value] [description] % 0000 32 bit integer 0x00000801(2049) magic number (MSB first) % 0004 32 bit integer 60000 number of items % 0008 unsigned byte ?? label % 0009 unsigned byte ?? label % ........ % xxxx unsigned byte ?? label % The labels values are 0 to 9. % TRAINING SET IMAGE FILE (train-images-idx3-ubyte): % % [offset] [type] [value] [description] % 0000 32 bit integer 0x00000803(2051) magic number % 0004 32 bit integer 60000 number of images % 0008 32 bit integer 28 number of rows % 0012 32 bit integer 28 number of columns % 0016 unsigned byte ?? pixel % 0017 unsigned byte ?? pixel % ........ % xxxx unsigned byte ?? pixel % TEST SET LABEL FILE (t10k-labels-idx1-ubyte): % % [offset] [type] [value] [description] % 0000 32 bit integer 0x00000801(2049) magic number (MSB first) % 0004 32 bit integer 10000 number of items % 0008 unsigned byte ?? label % 0009 unsigned byte ?? label % ........ % xxxx unsigned byte ?? label % The labels values are 0 to 9. % % TEST SET IMAGE FILE (t10k-images-idx3-ubyte): % % [offset] [type] [value] [description] % 0000 32 bit integer 0x00000803(2051) magic number % 0004 32 bit integer 10000 number of images % 0008 32 bit integer 28 number of rows % 0012 32 bit integer 28 number of columns % 0016 unsigned byte ?? pixel % 0017 unsigned byte ?? pixel % ........ % xxxx unsigned byte ?? pixel fio=fopen(filename,'r');%原始文件中数据是以2进制存储的。 a = fread(fio,'uint8');%以8进制方式读取源文件。虽然前几项是32bit的,但是图像像素数据是8bit的,所以此处用8bit处理。 if strcmp(type,'image') % magic_num=a(1)*256^3+a(2)*256^2+a(3)*256+a(4); % image_num=a(5)*256^3+a(6)*256^2+a(7)*256+a(8); % image_rows=a(9)*256^3+a(10)*256^2+a(11)*256+a(12);%默认训练和测试图像都已经reshape到一个size % image_cols=a(13)*256^3+a(14)*256^2+a(15)*256+a(16); output=a(17:end);%提取像素数据 else if strcmp(type,'label') % magic_num=a(1)*256^3+a(2)*256^2+a(3)*256+a(4); % image_num=a(5)*256^3+a(6)*256^2+a(7)*256+a(8); output=a(9:end); end end
相关文章推荐
- MATLAB自动识别MNIST手写数字数据库
- MATLAB自动识别MNIST手写数字数据库
- 【matlab图像处理】图片生成浮雕效果
- JDBC调用存储过程、JDBC事务处理、数据库自动生成的主键
- 【matlab图像处理】图片生成伪色彩处理
- Mybatis自动生成插件对数据库类型为text的处理
- MNIST手写数字数据库保存为*.bmp图像之MATLAB源程序
- C#中使用SQL语句生成DataTable,对不同的数据库,处理方式不同,返回的结果属性不同
- resty.upload 处理上传的图片 并把生成的url保存到数据库中
- entity framework codefirst 用户代码未处理DataException,InnerException基础提供程序在open上失败,数据库生成失败
- mssql 数据库“查询处理器用尽了内部资源,无法生成查询计划。”问题的处理
- 【matlab图像处理】图片生成手绘效果
- matlab生成dll时子函数处理方法
- Yale人脸数据库、PCA算法实现Matlab中特征脸生成和简单的人脸识别
- 【matlab图像处理】生成棋方格,五子棋标定板(设置个数,尺寸,打印)
- MATLAB如何生成、读取.mat数据库文件?
- Matlab Worldmape 地图生成与处理
- 【matlab图像处理】图片生成油画效果(选择路径-交互)
- JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理
- 【caffe-Windows】以mnist为例的hdf5单标签处理——matlab实现