-
Notifications
You must be signed in to change notification settings - Fork 1
/
csae_caltech101_64.m
207 lines (163 loc) · 6.3 KB
/
csae_caltech101_64.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
function ConvSparse_vgg_Caltech101_simple(varargin)
close all
clc;
%% SET DIRECTORY
addpath(genpath('./utils'));
addpath('./dictionary');
addpath(genpath('./data'));
addpath(genpath('./Caltech'));
addpath('./CNN');
run(fullfile(fileparts(mfilename('fullpath')), './MatConvNet/vl_setupnn.m')) ;
%%% original data path
Caltech101Dir = '/home/luowei/Datasets/Caltech101';
%%% sava data path
modelPath = './model';
dataPath = './data';
dictionaryPath = './dictionary';
resultPath = './result';
featurePath = './feature';
%%% processing dataset
dataDir = Caltech101Dir;
datasetName = 'Caltech101';
patchNameSize = '96';
%%% number of classes
numClass = 102;
%%%
if gpuDeviceCount
gpud = gpuDevice(2);
fprintf('success loading GPU.\n');
end
%%%
InitialCNN = 1; % 1, initialize new CNN, need regenerate first layer features, very time comsuming!
convsparsetrain = 0; % 1, learn 1st layer decoders
InitialFirLayerWeights = 1; % 1, initialize 1st layer weight with unsupervised learned features
InitCALTECHparams
%% PRE-PROCESSING DATA
load Caltech101_whitened
xtrain = single(xtrain);
ytrain = single(ytrain);
numTrains = size(xtrain,2);
if gpuDeviceCount
xtrain = gpuArray(xtrain);
ytrain = gpuArray(ytrain);
end
acttype = 'max';
imgsize = [96 96 1];
%% convolutional sparse learning first layer feature
str = sprintf('covkernel_%s_%s_imgsz%s_mdsz%s-%s_ksz%s-%s_pool%s-%s_vneigb%s', acttype, datasetName, patchNameSize,...
num2str(numfeatures1), num2str(numfeatures2), num2str(kernelsize1), num2str(kernelsize2),...
num2str(poolsize1), num2str(poolsize2), num2str(vneighbors1));
if convsparsetrain
ConvSparseLearning;
else
load([dictionaryPath filesep str]);
end
%% Network initialization
str = sprintf('convnets1_%s_imgsz%s_mdsz%s-%s_ksz%s-%s_pool%s-%s_vneigb%d', datasetName, patchNameSize,...
num2str(numfeatures1), num2str(numfeatures2), num2str(kernelsize1), num2str(kernelsize2),...
num2str(poolsize1), num2str(poolsize2), ...
vneighbors1);
if InitialCNN
net = initializeNetwork_simple;
save([modelPath filesep str '.mat'], 'net')
else
load([modelPath filesep str '.mat']);
end
%% Reinitialize the weights of the first layer
if InitialFirLayerWeights
if gpuDeviceCount
net.layers{1}.filters = gpuArray(single(reshape(flipud(kernels), size(net.layers{1}.filters))));
net.layers{1}.biases = gpuArray(single(hbias'));
else
net.layers{1}.filters = single(reshape(flipud(kernels), size(net.layers{1}.filters)));
net.layers{1}.biases = single(hbias');
end
%%% save model
save([modelPath filesep str], 'net');
fprintf('Initialize the 1st layer weighs of CNN complete!\n');
end
clear kernels
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CLASSIFICATION
trNum = 30;
ntimes = 5;
error = zeros(ntimes,1);
top5error = zeros(ntimes,1);
objective = zeros(ntimes,1);
% error(1) = 0.433055091819700;
% top5error(1) = 0.239732888146912;
% objective(1) = 1.901318014205398;
opts.dataDir = 'data/Caltech101' ;
opts.expBaseDir = 'data/Caltech101-baseline' ;
opts.expDir = 'data/Caltech101-baseline/pre1' ;
opts.imdbPath = fullfile(opts.expBaseDir, 'imdb.mat');
opts.lite = false ;
opts.numFetchThreads = 0 ;
opts.train.batchSize = 50 ;
opts.train.numEpochs = 50 ;
opts.train.weightDecay = 0.0005 ;
opts.train.momentum = 0.9 ;
opts.train.continue = false ;
opts.train.useGpu = false ;
opts.train.prefetch = false ;
opts.train.learningRate = [0.01*ones(1, 30) 0.001*ones(1, 15) 0.0001*ones(1,5)] ;
opts.train.expDir = opts.expDir ;
opts.train.errorType = 'multiclass' ;
opts.datainfo.datasetName = datasetName;
opts.datainfo.numClass = numClass;
opts.datainfo.imgsize = imgsize;
opts.datainfo.trNum = trNum;
[opts, varargin] = vl_argparse(opts, varargin) ;
[~, ytrain] = max(ytrain, [], 1);
backnet = net;
for ltimes = 1:ntimes;
%%% prepare data for each trial
indstr = ['data_index_' num2str(ltimes) '.mat'];
if exist([opts.expDir filesep indstr])
load([opts.expDir filesep indstr])
trdata = xtrain(:,trindex);
vldata = xtrain(:,vlindex);
tsdata = xtrain(:,tsindex);
trlabel = ytrain(trindex);
vllabel = ytrain(vlindex);
tslabel = ytrain(tsindex);
else
PrepareTrainTestData
save([opts.expDir filesep 'data_index_' num2str(ltimes) '.mat'], 'trindex', 'vlindex', 'tsindex');
end
%%% training and validataion
if ltimes == 1
[net,info] = cnn_trainval(net, trdata, trlabel, vldata, vllabel,...
opts.train, 'conserveMemory', true, 'datainfo', opts.datainfo);
end
%%% train on all training data
net = backnet;
opts.train.numEpochs = 50 ;
opts.train.batchSize = 50 ;
opts.train.continue = false ;
opts.train.learningRate = [0.01*ones(1, 30) 0.001*ones(1, 15) 0.0001*ones(1,5)] ;
[net,info] = cnn_train(net, [trdata vldata], [trlabel vllabel],...
opts.train, 'conserveMemory', true, 'datainfo', opts.datainfo, 'times', ltimes);
save([opts.expDir filesep 'net-1layer-1-final-' num2str(ltimes) '.mat'],'net', 'info');
%%% testing
% load([opts.expDir filesep 'net-1layer-1-final-' num2str(ltimes) '.mat'], 'net');
opts.train.batchSize = 100 ;
info = cnn_test(net, tsdata, tslabel, opts.train, 'imgsize', [96 96 1]);
save([opts.expDir filesep 'Result-1layer-1-' num2str(ltimes) '.mat'], 'info');
error(ltimes) = info.error;
top5error(ltimes) = info.topFiveError;
objective(ltimes) = info.objective;
fprintf('Classification accuracy: %f \n', 1-error(ltimes));
end
Ravg = mean(1-error);
Rstd = std(1-error);
top5Ravg = mean(1-top5error);
top5Rstd = std(1-top5error);
save([opts.expDir filesep 'Performance-1layer-1.mat'], 'Ravg', 'Rstd', 'top5Ravg', 'top5Rstd');
fprintf('===============================================\n');
fprintf('Average classification accuracy: %f\n', Ravg);
fprintf('Standard deviation: %f\n', Rstd);
fprintf('\n');
fprintf('Average top 5 classification accuracy: %f\n', top5Ravg);
fprintf('Top 5 standard deviation: %f\n', top5Rstd);
fprintf('===============================================');