-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathexercise4.m
169 lines (127 loc) · 4.87 KB
/
exercise4.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
function exercise4(varargin)
% EXERCISE4 Part 4 of the VGG CNN practical
setup ;
% -------------------------------------------------------------------------
% Part 4.1: prepare the data
% -------------------------------------------------------------------------
% Load character dataset
imdb = load('data/charsdb.mat') ;
% Visualize some of the data
figure(10) ; clf ; colormap gray ;
subplot(1,2,1) ;
vl_imarraysc(imdb.images.data(:,:,imdb.images.label==1 & imdb.images.set==1)) ;
axis image off ;
title('training chars for ''a''') ;
subplot(1,2,2) ;
vl_imarraysc(imdb.images.data(:,:,imdb.images.label==1 & imdb.images.set==2)) ;
axis image off ;
title('validation chars for ''a''') ;
% -------------------------------------------------------------------------
% Part 4.2: initialize a CNN architecture
% -------------------------------------------------------------------------
net = initializeCharacterCNN() ;
% -------------------------------------------------------------------------
% Part 4.3: train and evaluate the CNN
% -------------------------------------------------------------------------
trainOpts.batchSize = 100 ;
trainOpts.numEpochs = 15 ;
trainOpts.continue = true ;
trainOpts.gpus = [] ;
trainOpts.learningRate = 0.001 ;
trainOpts.expDir = 'data/chars-experiment' ;
trainOpts = vl_argparse(trainOpts, varargin);
% Take the average image out
imdb = load('data/charsdb.mat') ;
imageMean = mean(imdb.images.data(:)) ;
imdb.images.data = imdb.images.data - imageMean ;
% Convert to a GPU array if needed
if numel(trainOpts.gpus) > 0
imdb.images.data = gpuArray(imdb.images.data) ;
end
% Call training function in MatConvNet
[net,info] = cnn_train(net, imdb, @getBatch, trainOpts) ;
% Move the CNN back to the CPU if it was trained on the GPU
if numel(trainOpts.gpus) > 0
net = vl_simplenn_move(net, 'cpu') ;
end
% Save the result for later use
net.layers(end) = [] ;
net.imageMean = imageMean ;
save('data/chars-experiment/charscnn.mat', '-struct', 'net') ;
% -------------------------------------------------------------------------
% Part 4.4: visualize the learned filters
% -------------------------------------------------------------------------
figure(2) ; clf ; colormap gray ;
vl_imarraysc(squeeze(net.layers{1}.weights{1}),'spacing',2)
axis equal ; title('filters in the first layer') ;
% -------------------------------------------------------------------------
% Part 4.5: apply the model
% -------------------------------------------------------------------------
% Load the CNN learned before
net = load('data/chars-experiment/charscnn.mat') ;
%net = load('data/chars-experiment/charscnn-jit.mat') ;
% Load the sentence
[im,cmap] = imread('data/sentence-lato.png') ;
if isempty(cmap)
im = im2single(im) ;
else
im = im2single(ind2gray(p,cmap)) ;
end
im = 256 * (im - net.imageMean) ;
% Apply the CNN to the larger image
res = vl_simplenn(net, im) ;
% Visualize the results
figure(3) ; clf ;
decodeCharacters(net, imdb, im, res) ;
% -------------------------------------------------------------------------
% Part 4.6: train with jitter
% -------------------------------------------------------------------------
trainOpts.batchSize = 100 ;
trainOpts.numEpochs = 15 ;
trainOpts.continue = true ;
trainOpts.learningRate = 0.001 ;
trainOpts.expDir = 'data/chars-jit-experiment' ;
% Initlialize a new network
net = initializeCharacterCNN() ;
% Call training function in MatConvNet
[net,info] = cnn_train(net, imdb, @getBatchWithJitter, trainOpts) ;
% Move the CNN back to CPU if it was trained on GPU
if numel(trainOpts.gpus) > 0
net = vl_simplenn_move(net, 'cpu') ;
end
% Save the result for later use
net.layers(end) = [] ;
net.imageMean = imageMean ;
save('data/chars-experiment/charscnn-jit.mat', '-struct', 'net') ;
% Visualize the results on the sentence
figure(4) ; clf ;
decodeCharacters(net, imdb, im, vl_simplenn(net, im)) ;
% --------------------------------------------------------------------
function [im, labels] = getBatch(imdb, batch)
% --------------------------------------------------------------------
im = imdb.images.data(:,:,batch) ;
im = 256 * reshape(im, 32, 32, 1, []) ;
labels = imdb.images.label(1,batch) ;
% --------------------------------------------------------------------
function [im, labels] = getBatchWithJitter(imdb, batch)
% --------------------------------------------------------------------
im = imdb.images.data(:,:,batch) ;
labels = imdb.images.label(1,batch) ;
n = numel(batch) ;
train = find(imdb.images.set == 1) ;
sel = randperm(numel(train), n) ;
im1 = imdb.images.data(:,:,sel) ;
sel = randperm(numel(train), n) ;
im2 = imdb.images.data(:,:,sel) ;
ctx = [im1 im2] ;
ctx(:,17:48,:) = min(ctx(:,17:48,:), im) ;
dx = randi(11) - 6 ;
im = ctx(:,(17:48)+dx,:) ;
sx = (17:48) + dx ;
dy = randi(5) - 2 ;
sy = max(1, min(32, (1:32) + dy)) ;
im = ctx(sy,sx,:) ;
% Visualize the batch:
% figure(100) ; clf ;
% vl_imarraysc(im) ;
im = 256 * reshape(im, 32, 32, 1, []) ;