-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcnn_signature_independent.m
executable file
·206 lines (185 loc) · 9.56 KB
/
cnn_signature_independent.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 [net, stats] = cnn_writer_independent(varargin)
run '../matlab/vl_setupnn.m'
% Parameter defaults.
opts.train.batchSize = 32 ;
opts.train.numEpochs = 50 ;
opts.train.continue = true ;
opts.train.gpus = 1 ;
opts.train.learningRate = 0.001 ;
opts.expDir = fullfile(vl_rootnn, 'signature-verification', 'data', 'CEDAR-adam') ;
opts.dataDir = fullfile(vl_rootnn, 'signature-verification', 'data', 'D_set.mat');
[opts, varargin] = vl_argparse(opts, varargin) ;
opts = vl_argparse(opts, varargin) ;
% --------------------------------------------------------------------
% Load data
% --------------------------------------------------------------------
load(opts.dataDir);
imdb = D;
clear D;
%---------------------------------------------------------------------
% NETWORK
%---------------------------------------------------------------------
f = 1/100;
net.layers = {} ;
% BLOCK 1 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,1,64, 'single'), zeros(1, 64, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,64,64, 'single'), zeros(1, 64, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2);
% BLOCK 2 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,64,128, 'single'), zeros(1, 128, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,128,128, 'single'), zeros(1, 128, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2);
% BLOCK 3 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,128,256, 'single'), zeros(1, 256, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,256,256, 'single'), zeros(1, 256, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,256,256, 'single'), zeros(1, 256, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2);
% BLOCK 4 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,256,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,512,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,512,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2);
% BLOCK 5 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,512,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,512,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,3,512,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 1);
net.layers{end+1} = struct('type','relu');
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2);
net.layers{end+1} = struct('type', 'pool', ...
'method', 'avg', ...
'pool', [7 11], ...
'stride', [7 11], ...
'pad', 0);
% FC-1024 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,512,1024, 'single'), zeros(1, 1024, 'single')}}, ...
'stride', 1, ...
'pad', 0);
net.layers{end+1} = struct('type','relu');
% FC-1024 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,1024,1024, 'single'), zeros(1, 1024, 'single')}}, ...
'stride', 1, ...
'pad', 0);
net.layers{end+1} = struct('type','relu');
% FC- 512 ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,1024,512, 'single'), zeros(1, 512, 'single')}}, ...
'stride', 1, ...
'pad', 0);
net.layers{end+1} = struct('type','relu');
% SOFTMAX ---------------------------------------
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,512,2, 'single'), zeros(1, 2, 'single')}}, ...
'stride', 1, ...
'pad', 0);
net.layers{end+1} = struct('type', 'loss');
% Fill in any values we didn't specify explicitly
net.meta.inputSize = [224 352 1];
net.meta.trainOpts.learningRate = 0.001; %[0.3*ones(1,20) 0.01*ones(1,20) 0.001*ones(1,20) 0.0001*ones(1,20)];
net.meta.trainOpts.weightDecay = 0.0005;
net.meta.trainOpts.momentum = 0.3;
net.meta.trainOpts.numEpochs = 50;
net.meta.trainOpts.batchSize = 32;
net = vl_simplenn_tidy(net) ;
% --------------------------------------------------------------------
% Train
% --------------------------------------------------------------------
use_gpu = ~isempty(opts.train.gpus) ;
% Start training
[net, stats] = cnn_train(net, imdb, @(imdb, batch) getBatch(imdb, batch, use_gpu), ...
'train', find(imdb.images.set == 1), 'val', find(imdb.images.set == 2), opts.train) ;
%---------------------------------------------------------------------
% Visualize the learned filters
%---------------------------------------------------------------------
% figure(2); vl_tshow(net.layers{1}.weights{1}); title('Conv1 filters');
% figure(3); vl_tshow(net.layers{3}.weights{1}); title('Conv2 filters');
% figure(4); vl_tshow(net.layers{5}.weights{1}); title('Conv3 filters');
% figure(5); vl_tshow(net.layers{8}.weights{1}); title('Conv4 filters');
% figure(6); vl_tshow(net.layers{11}.weights{1}); title('Conv5 filters');
% figure(7); vl_tshow(net.layers{14}.weights{1}); title('Conv6 filters');
% figure(8); vl_tshow(net.layers{16}.weights{1}); title('Conv7 filters');
% --------------------------------------------------------------------
function [images, labels] = getBatch(imdb, batch, use_gpu)
% --------------------------------------------------------------------
% This is where we return a given set of images (and their labels) from
% our imdb structure.
% If the dataset was too large to fit in memory, getBatch could load images
% from disk instead (with indexes given in 'batch').
images = imdb.images.data(:,:,:,batch) ;
images = images - imdb.images.data_mean;
% For signature dependent
%labels = imdb.images.labels(batch) ;
% For signature independent
labels = imdb.images.identities(batch);
labels(labels == 1) = 0; % original 0
labels(labels == 3) = 1; % forgeries 1
if use_gpu
images = gpuArray(images) ;
end