MATLAB實例:讀取Fashion MNIST數據,保存爲.mat文件,並展現部分樣例

MATLAB實例:讀取Fashion MNIST數據,保存爲.mat文件,並展現部分樣例

做者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/html

Fashion MNIST數據來源:https://github.com/zalandoresearch/fashion-mnistgit

Name Content Examples Size Link MD5 Checksum
train-images-idx3-ubyte.gz training set images 60,000 26 MBytes Download 8d4fb7e6c68d591d4c3dfef9ec88bf0d
train-labels-idx1-ubyte.gz training set labels 60,000 29 KBytes Download 25c81989df183df01b3e8a0aad5dffbe
t10k-images-idx3-ubyte.gz test set images 10,000 4.3 MBytes Download bef4ecab320f06d8554ea6380940ec79
t10k-labels-idx1-ubyte.gz test set labels 10,000 5.1 KBytes Download bb300cfdad3c16e7a12a480ee83cd310

MATLAB程序

demo.m

clear
clc
% Author:kailugaji https://www.cnblogs.com/kailugaji/
filename_data='E:\database\MNIST\Fashion MNIST\t10k-images-idx3-ubyte\t10k-images-idx3-ubyte'; %自行修改路徑
data = loadMNISTImages(filename_data);
data=data';
filename_label='E:\database\MNIST\Fashion MNIST\t10k-labels-idx1-ubyte\t10k-labels-idx1-ubyte'; %自行修改路徑
real_label = loadMNISTLabels(filename_label);
%     標籤	所表明的意思
%     0	短袖圓領T恤
%     1	褲子
%     2	套衫
%     3	連衣裙
%     4	外套
%     5	涼鞋
%     6	襯衫
%     7	運動鞋
%     8	包
%     9	短靴
%  real_label(real_label==0)=10;
save fashion_MNIST data real_label

Image_samples=Image_integration(data, real_label, 10);
A=mat2gray(Image_samples);
figure(1)
imshow(A, 'Border','tight');
print(gcf,'-r1000','-djpeg','My_Fashion_MNIST.jpg');

loadMNISTImages.m

function images = loadMNISTImages(filename)
%load MNIST Images returns a 28x28x[number of MNIST images] matrix containing
% 原連接:https://blog.csdn.net/tracer9/article/details/51253604
%the raw MNIST images
 
fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);
 
magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2051, ['Bad magic number in ', filename, '']);
 
numImages = fread(fp, 1, 'int32', 0, 'ieee-be');
numRows = fread(fp, 1, 'int32', 0, 'ieee-be');
numCols = fread(fp, 1, 'int32', 0, 'ieee-be');
 
images = fread(fp, inf, 'unsigned char');
images = reshape(images, numCols, numRows, numImages);
images = permute(images,[2 1 3]);
 
fclose(fp);
 
% Reshape to #pixels x #examples
images = reshape(images, size(images, 1) * size(images, 2), size(images, 3));
% Convert to double and rescale to [0,1]
images = double(images) / 255;

loadMNISTLabels.m

function labels = loadMNISTLabels(filename)
% load MNIST Labels returns a [number of MNIST images]x1 matrix containing
% 原連接:https://blog.csdn.net/tracer9/article/details/51253604
% the labels for the MNIST images
fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);
magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2049, ['Bad magic number in ', filename, '']);
numLabels = fread(fp, 1, 'int32', 0, 'ieee-be');
labels = fread(fp, inf, 'unsigned char');
assert(size(labels,1) == numLabels, 'Mismatch in label count');
fclose(fp);

Image_integration.m

function Image_samples=Image_integration(data, real_label, N_samples)
% Gray image integration
% This code only applies to square matrices
% Input:
% data: dataset. N*Dim
% real_label: GroundTruth. N*1
% N_samples: number of selected samples
% Output:
% Image_samples:Integrated image
% Author: kailugaji https://www.cnblogs.com/kailugaji/
[~, Dim]=size(data);
[real_label, b]=sort(real_label);
data=data(b, :);
K=length(unique(real_label)); % number of cluster
[~, ID]=unique(real_label);
ID=ID-1;
image_10=cell(N_samples, K);
temp=cell(N_samples, K);
Image_samples=[];
for i=1:N_samples
    for j=1:K
        temp{i, j}=reshape(data(ID(j)+i, :), sqrt(Dim), sqrt(Dim)); % you can change its size
        image_10{i, j}=[image_10{i, j}, temp{i, j}];
    end
    Image_samples=[Image_samples; image_10{i, :}];
end

結果

數據已經轉換成.mat格式,同時保存在和MATLAB程序同目錄下。github

每一類取了10個樣例來展現。web

參考

[1] GitHub - zalandoresearch/fashion-mnist: A MNIST-like fashion product database. Benchmark 數據庫

[2] MATLAB小函數:展現灰度圖像數據集的部分樣例 - 凱魯嘎吉 - 博客園 app

[3]【機器學習】MATLAB讀取mnist數據庫_心所願,力必堅!-CSDN博客機器學習

注意:傳統的MNIST數據也能夠採用相同的方式進行轉化成.mat文件,只需把路徑改一下,換成MNIST的路徑便可。函數

相關文章
相關標籤/搜索