MATLAB实例:读取Fashion MNIST数据,保存为.mat文件,并展示部分样例
作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/
Fashion MNIST数据来源:https://github.com/zalandoresearch/fashion-mnist
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程序同目录下。
每一类取了10个样例来展示。
参考
[1] GitHub - zalandoresearch/fashion-mnist: A MNIST-like fashion product database. Benchmark
[2] MATLAB小函数:展示灰度图像数据集的部分样例 - 凯鲁嘎吉 - 博客园
[3]【机器学习】MATLAB读取mnist数据库_心所愿,力必坚!-CSDN博客
注意:传统的MNIST数据也可以采用相同的方式进行转化成.mat文件,只需把路径改一下,换成MNIST的路径即可。