matlab mnist uint8,MNIST數據庫處理--matlab生成mnist_uint8.mat

我的上一篇博客CNN卷積神經網絡手寫數字識別實例及代碼詳解中使用了mnist_uint8.mat數據,

本文講解如何將下載得到的2進制文件作成mnist_uint8,mat。

代碼是自己編寫的,如果有疏漏之處,請大家幫忙指正。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值