您的位置:首页 > 编程语言 > MATLAB

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

%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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Deep Learning 数据库