-
Notifications
You must be signed in to change notification settings - Fork 12
/
runcnn.m
79 lines (61 loc) · 2.02 KB
/
runcnn.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
function y = runcnn(imgs_sample, imgs_sample_num, max_size)
path(path, 'DeepLearnToolbox-master/CNN/')
path(path, 'DeepLearnToolbox-master/util/')
% 网络训练集构造
[a, b] = buildtrainset_cnn(imgs_sample, imgs_sample_num);
% 16×16的原图片
cnn.layers = {
struct('type', 'i') %input layer
struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer
struct('type', 's', 'scale', 2) %sub sampling layer
struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer
struct('type', 's', 'scale', 2) %sub sampling layer
};
cnn = cnnsetup(cnn, a, b);
% 学习率
opts.alpha = 2;
% 每次挑出一个batchsize的batch来训练,也就是每用batchsize个样本就调整一次权值,而不是
% 把所有样本都输入了,计算所有样本的误差了才调整一次权值
opts.batchsize = size(a, 3);
% 训练次数,用同样的样本集。我训练的时候:
% 1的时候 11.41% error
% 5的时候 4.2% error
% 10的时候 2.73% error
opts.numepochs = 2000;
% cnn = cnntrain(cnn, a, b, opts);
load cnn_save cnn;
% 测试
image_dir=dir('image/*.jpg');
for i = 1: length(image_dir)
str_name = image_dir(i).name;
imgs_test{i} = str_name(1:4);
end
rightnum = 0;
sumnum = 0;
for i = 1 : length(imgs_test)
img_name = imgs_test{i};
imgs = cutting(imread(['image/',img_name,'.jpg']), false);
if (length(imgs) == length(img_name))
for j = 1 : length(img_name)
tmp_num = str2num(img_name(j)) + 1;
%% 等大小化
temp = zeros(max_size);
imgs_size = size(imgs{j});
temp(1:imgs_size(1,1), 1:imgs_size(1,2)) = imgs{j};
imgs{j} = temp;
input_size = size(temp);
testInput(:, :, j) = reshape(temp', input_size(1,1), input_size(1,2));
end
% 然后就用测试样本来测试
cnn = cnnff(cnn, testInput);
cnn.o
[~, mans] = max(cnn.o);
img_name
mans = mans-1
% [~, a] = max(y);
% bad = find(mans ~= a);
end
end
%plot mean squared error
plot(cnn.rL);
end