forked from vedaldi/practical-cnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1.m
73 lines (55 loc) · 2 KB
/
exercise1.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
setup ;
% -------------------------------------------------------------------------
% Part 1.1: Linear convolution
% -------------------------------------------------------------------------
% Read an example image
x = imread('peppers.png') ;
% Convert to single format
x = im2single(x) ;
% Visualize the input x
figure(1) ; clf ; imagesc(x) ;
% Create a bank of linear filters
w = randn(5,5,3,10,'single') ;
% Apply the convolutional operator
y = vl_nnconv(x, w, []) ;
% Visualize the output y
figure(2) ; clf ; vl_imarraysc(y) ; colormap gray ;
% Try again, downsampling the output
y_ds = vl_nnconv(x, w, [], 'stride', 16) ;
figure(3) ; clf ; vl_imarraysc(y_ds) ; colormap gray ;
% Try padding
y_pad = vl_nnconv(x, w, [], 'pad', 4) ;
figure(4) ; clf ; vl_imarraysc(y_pad) ; colormap gray ;
% Manually design a filter
w = [0 1 0 ;
1 -4 1 ;
0 1 0 ] ;
w = single(repmat(w, [1, 1, 3])) ;
y_lap = vl_nnconv(x, w, []) ;
figure(5) ; clf ; colormap gray ;
subplot(1,2,1) ; imagesc(y_lap) ; title('filter output') ;
subplot(1,2,2) ; imagesc(-abs(y_lap)) ; title('- abs(filter output)') ;
% -------------------------------------------------------------------------
% Part 1.2: Non-linear gating (ReLU)
% -------------------------------------------------------------------------
w = single(repmat([1 0 -1], [1, 1, 3])) ;
w = cat(4, w, -w) ;
y = vl_nnconv(x, w, []) ;
z = vl_nnrelu(y) ;
figure(6) ; clf ; colormap gray ;
subplot(1,2,1) ; vl_imarraysc(y) ;
subplot(1,2,2) ; vl_imarraysc(z) ;
% -------------------------------------------------------------------------
% Part 1.2: Pooling
% -------------------------------------------------------------------------
y = vl_nnpool(x, 15) ;
figure(7) ; clf ; imagesc(y) ;
% -------------------------------------------------------------------------
% Part 1.3: Normalization
% -------------------------------------------------------------------------
rho = 5 ;
kappa = 0 ;
alpha = 1 ;
beta = 0.5 ;
y_nrm = vl_nnnormalize(x, [rho kappa alpha beta]) ;
figure(8) ; clf ; imagesc(y_nrm) ;