-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_imdb.m
150 lines (105 loc) · 3.64 KB
/
make_imdb.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
% load path
% Files start from index 4 and onwards
people_patches_dir = '../pixorama/patches_people/';
npeople_patches_dir = '../pixorama/patches_not_people/';
people_patches_folder = dir('../pixorama/patches_people/');
npeople_patches_folder = dir('../pixorama/patches_not_people/');
num_people = size(people_patches_folder,1) - 3;
num_not_people = size(npeople_patches_folder,1) - 3;
[pp_trainInd, pp_valInd, pp_testInd] = dividerand(num_people,0.7,0.15,0.15);
[npp_trainInd, npp_valInd, npp_testInd] = dividerand(num_not_people,0.7,0.15,0.15);
%% Training Data
% Create data struct
train_data = [];
for i = 1:size(pp_trainInd')
% File name
fname = people_patches_folder(3 + pp_trainInd(i)).name;
fpath = strcat(people_patches_dir,fname);
im = imread(fpath);
im = im2single(im);
train_data = cat(4,train_data,im);
end
for i = 1:size(npp_trainInd')
% Random index
%ind = randi([1 size(npp_trainInd',1)],1);
% File name
fname = npeople_patches_folder(3 + npp_trainInd(i)).name;
fpath = strcat(npeople_patches_dir,fname);
im = imread(fpath);
im = im2single(im);
train_data = cat(4,train_data,im);
end
% Randomize
indices = randperm(size(train_data,4));
train_data = train_data(:,:,:,indices);
train_labels = [ones(1,size(pp_trainInd',1)) 2*ones(1,size(npp_trainInd',1))];
train_labels = train_labels(indices);
%% Testing Data
% Create data struct
test_data = [];
for i = 1:size(pp_testInd')
% File name
fname = people_patches_folder(3 + pp_testInd(i)).name;
fpath = strcat(people_patches_dir,fname);
im = imread(fpath);
im = im2single(im);
test_data = cat(4,test_data,im);
end
for i = 1:size(npp_testInd')
% Random index
%ind = randi([1 size(npp_testInd',1)],1);
% File name
fname = npeople_patches_folder(3 + npp_testInd(i)).name;
fpath = strcat(npeople_patches_dir,fname);
im = imread(fpath);
im = im2single(im);
test_data = cat(4,test_data,im);
end
% Randomize
indices = randperm(size(test_data,4));
test_data = test_data(:,:,:,indices);
test_labels = [ones(1,size(pp_testInd',1)) 2*ones(1,size(npp_testInd',1))];
test_labels = test_labels(indices);
%% Validation data
% Create data struct
val_data = [];
for i = 1:size(pp_valInd')
% File name
fname = people_patches_folder(3 + pp_valInd(i)).name;
fpath = strcat(people_patches_dir,fname);
im = imread(fpath);
im = im2single(im);
val_data = cat(4,val_data,im);
end
for i = 1:size(npp_valInd')
% Random index
%ind = randi([1 size(npp_testInd',1)],1);
% File name
fname = npeople_patches_folder(3 + npp_valInd(i)).name;
fpath = strcat(npeople_patches_dir,fname);
im = imread(fpath);
im = im2single(im);
val_data = cat(4,val_data,im);
end
% Randomize
indices = randperm(size(val_data,4));
val_data = val_data(:,:,:,indices);
val_labels = [ones(1,size(pp_valInd',1)) 2*ones(1,size(npp_valInd',1))];
val_labels = val_labels(indices);
%% Concatenate
data = [];
data = cat(4,data,train_data, val_data, test_data);
labels = [train_labels, val_labels, test_labels];
%labels = single(labels);
set = [ones(1,size(train_data,4)), 2*ones(1,size(val_data,4)), 3*ones(1,size(test_data,4))];
set = int8(set);
%% Create imdb struct
imdb.images.data = data;
imdb.images.label = labels;
imdb.images.set = set;
imdb.meta.sets = cellstr({'train', 'val', 'test'});
imdb.meta.classes = cellstr({'people', 'non_people'}).';
imdb.meta.dataMean = mean(train_data,4);
% Normalize data
imdb.images.data = imdb.images.data - repmat(imdb.meta.dataMean,1,1,1,size(imdb.images.data,4));
save matlab/imdb.mat -struct imdb