一、获取SVNH数据
数据集集地址-http://ufldl.stanford.edu/housenumbers/
提供两种格式的数据:
1.Format 1,图像形式,压缩包
2.Format 2, .mat格式的数据
·10 classes, 1 for each digit. Digit '1' has label 1, '9' has label 9 and '0' has label 10.
·73257 digits for training, 26032 digits for testing, and 531131 additional, somewhat less difficult samples, to use as extra training data
·Comes in two formats:
1. Original images with character level bounding boxes.
2. MNIST-like 32-by-32 images centered around a single character (many of the images do contain some distractors at the sides).
二、将.mat形式数据转换为'.png'图像
图像分别存在1-10命名的10个文件夹中,并以原始的序号命名
注:这步的操作主要是为了方便后续dataset.ImageFolder处理,其要求数据存储的文件路径格式为
├── svhn
├── trainset
│ ├── subfolders for 1 ~ 10
├── testset
具体转换的matlab代码如下:
%将SVHN数据的.mat形式转换为图像并分别存在trainset和testset文件夹下
%这两个文件夹中分别包含1-10命名的10个文件夹,其中存图像
clear;clc;
%1.创建trainset和testset文件夹
train_filename = 'trainset';
if exist(train_filename)==0 %该文件夹不存在,则直接创建
mkdir(train_filename);
end
test_filename = 'trainset';
if exist(test_filename)==0 %该文件夹不存在,则直接创建
mkdir(test_filename);
end
% mkdir('trainset');
% mkdir('testset');
%2.分别在trainset和testset文件夹下创建1-10命名的文件夹,用于存图
load train_32x32.mat;
labels = unique(y);
savepath = './trainset';
fileNums = length(labels);
makefile(fileNums, savepath);
savepath = './testset';
makefile(fileNums, savepath);
% 3.分别在对应标签的文件夹下存图
% for traindata
load train_32x32.mat;
savepath = './trainset/';
img_num = length(y);%size(X,4);
for i = 1:img_num
img = X(:,:,:,i);
imshow(img);
n = y(i);
choosensaveimg(img,n,i,savepath);
end
% for testdata
load test_32x32.mat;
savepath = './testset/';
img_num = length(y);%size(X,4);
for i = 1:img_num
img = X(:,:,:,i);
imshow(img);
n = y(i);
choosensaveimg(img,n,i,savepath);
end
% fun2:在文件1-10保存图片
function choosensaveimg(img,n,i,savepath)
switch n
case 1
imwrite(img,strcat(savepath,'1/',num2str(i),'.png'));
case 2
imwrite(img,strcat(savepath,'2/',num2str(i),'.png'));%该数据集本身是.png.mnist是'.jpg'
case 3
imwrite(img,strcat(savepath,'3/',num2str(i),'.png'));
case 4
imwrite(img,strcat(savepath,'4/',num2str(i),'.png'));
case 5
imwrite(img,strcat(savepath,'5/',num2str(i),'.png'));
case 6
imwrite(img,strcat(savepath,'6/',num2str(i),'.png'));
case 7
imwrite(img,strcat(savepath,'7/',num2str(i),'.png'));
case 8
imwrite(img,strcat(savepath,'8/',num2str(i),'.png'));
case 9
imwrite(img,strcat(savepath,'9/',num2str(i),'.png'));
case 10
imwrite(img,strcat(savepath,'10/',num2str(i),'.png'));
end
end
% fun1:创建1-10的文件夹
function makefile(fileNums, savepath)
for i=1:fileNums
file_name = sprintf('%s',num2str(i));
file_path_name_ = strcat(savepath,'/',file_name,'');
if exist(file_path_name_)==0 %该文件夹不存在,则直接创建
mkdir(file_path_name_);
else %该文件夹存在,则先删除再创建
rmdir(file_path_name_, 's'); %该文件夹中有没有文件均可
mkdir(file_path_name_);
end
end
end