This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOG_train.m
76 lines (63 loc) · 1.98 KB
/
HOG_train.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
function [net] = HOG_train(nbins)
%HOG_TRAIN Summary of this function goes here
% Detailed explanation goes here
nshapes = 3+1; % + 1 for training negative images
files = dir('images/train/tringles/*.jpg')';
nnmat = zeros(nbins,nshapes*length(files));
T = zeros(nshapes*length(files)+1,nshapes)';
count = 1;
for file = files
% file.name
img = rgb2gray(imread(['images/train/tringles/' file.name]));
[hog1, ~] = extractHOGFeatures(img);
[histFreq, ~] = hist(hog1, nbins);
histFreq = histFreq/sum(histFreq);
nnmat(:,count) = histFreq';
target = -1*ones(1,nshapes);
target(1,1) = 1;
T(:,count) = target;
count = count + 1;
end
files = dir('images/train/Ricktangles/*.jpg')';
for file = files
img = rgb2gray(imread(['images/train/Ricktangles/' file.name]));
[hog1, ~] = extractHOGFeatures(img);
[histFreq, ~] = hist(hog1,nbins);
histFreq = histFreq/sum(histFreq);
nnmat(:,count) = histFreq';
target = -1*ones(1,nshapes);
target(1,2) = 1;
T(:,count) = target';
count = count + 1;
end
files = dir('images/train/Circles/*.jpg')';
for file = files
img = rgb2gray(imread(['images/train/Circles/' file.name]));
[hog1, ~] = extractHOGFeatures(img);
[histFreq, ~] = hist(hog1,nbins);
histFreq = histFreq/sum(histFreq);
nnmat(:,count) = histFreq';
target = -1*ones(1,nshapes);
target(1,3) = 1;
T(:,count) = target';
count = count + 1;
end
% train negative case
files = dir('negative/*.png')';
for file = files
img = rgb2gray(imread(['negative/' file.name]));
[hog1, ~] = extractHOGFeatures(img);
[histFreq, ~] = hist(hog1,nbins);
histFreq = histFreq/sum(histFreq);
nnmat(:,count) = histFreq';
target = -2*ones(1,nshapes);
target(1,4) = 1;
T(:,count) = target';
count = count + 1;
end
nhid = 100; % number of hidden nuerons
net = feedforwardnet(nhid);
net=init(net);
net.trainparam.epochs=250;
net.trainparam.goal=0.001;
net=train(net,nnmat,T);