-
Notifications
You must be signed in to change notification settings - Fork 0
/
head_train_svm.py
174 lines (130 loc) · 5 KB
/
head_train_svm.py
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
import os
import sys
import cv2
import glob
import joblib
import numpy as np
import random as rand
import sklearn.linear_model as svm
from skimage.feature import hog
from sklearn.utils import shuffle
# General configurations
MAX_HARD_NEGATIVES = 20000
# Model configuration
orientations = 8 # Ped = 9; Heads = 11
pixels_per_cell = (16, 16) # Ped = 8; Heads = 6
cells_per_block = (1, 1) # Ped = 3; Heads = 2
block_norm = "L2"
feature_vector = True
train_data = []
train_labels = []
# General dataset
pos_img_dir = r"images/pos_head"
neg_img_dir = r"images/train_64x128_H96/neg"
hnm_img_dir = r"images/train_64x128_H96/neg"
# Util functions
def sliding_window(image, win_size, step_size):
for y in range(0, image.shape[0] - 50, step_size[1]):
for x in range(0, image.shape[1] - 50, step_size[0]):
yield x, y, image[y:y + win_size[1], x:x + win_size[0]]
def ten_random_windows(img_file):
h, w = img_file.shape
if h < 50 or w < 50:
return []
h = h - 50
w = w - 50
win_array = []
for i in range(0, 10):
x = rand.randint(0, w)
y = rand.randint(0, h)
win_array.append(img_file[y:y + 50, x:x + 50])
return win_array
# Hard Negative Mining function
def hard_negative_mine(f_neg, win_size, win_stride):
hard_negatives = []
hard_negative_labels = []
count = 0
num = 0
for imgfile in f_neg:
hnm_img = cv2.imread(os.path.join(hnm_img_dir, imgfile), cv2.IMREAD_GRAYSCALE)
for (x, y, im_window) in sliding_window(hnm_img, win_size, win_stride):
features = hog(im_window,
orientations=orientations,
pixels_per_cell=pixels_per_cell,
cells_per_block=cells_per_block,
block_norm=block_norm,
feature_vector=feature_vector)
if clf1.predict([features]) == 1:
hard_negatives.append(features)
hard_negative_labels.append(0)
count = count + 1
if count == MAX_HARD_NEGATIVES:
return np.array(hard_negatives), np.array(hard_negative_labels)
num = num + 1
sys.stdout.write("\r" + "\tHard Negatives Mined: " + str(count) + "\tCompleted: " + str(
round((count / float(MAX_HARD_NEGATIVES)) * 100, 4)) + " %")
sys.stdout.flush()
return np.array(hard_negatives), np.array(hard_negative_labels)
# Main Program
print("Reading Images")
pos_num_files = 0
neg_num_files = 0
# Load the positive features
for filename in glob.glob(os.path.join(pos_img_dir, "*.png")):
fd = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
fd = hog(fd,
orientations=orientations,
pixels_per_cell=pixels_per_cell,
cells_per_block=cells_per_block,
block_norm=block_norm,
feature_vector=feature_vector)
train_data.append(fd)
train_labels.append(1)
pos_num_files += 1
print("Total Positive Images : " + str(pos_num_files))
# Load the negative features
for filename in glob.glob(os.path.join(neg_img_dir, "*.png")):
img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
windows = ten_random_windows(img)
for win in windows:
fd = hog(win,
orientations=orientations,
pixels_per_cell=pixels_per_cell,
cells_per_block=cells_per_block,
block_norm=block_norm,
feature_vector=feature_vector)
train_data.append(fd)
train_labels.append(0)
neg_num_files += 1
print("Total Negative Images : " + str(neg_num_files))
train_data = np.array(train_data)
train_labels = np.array(train_labels)
train_data, train_labels = shuffle(train_data, train_labels, random_state=0) # Needed?
print("Images read and shuffled")
print("===========================")
print("Training Started")
clf1 = svm.LogisticRegression(C=0.05, dual=True, random_state=rand.randint(1, 100), solver='liblinear', max_iter=10000)
clf1.fit(train_data, train_labels)
joblib.dump(clf1, r'models/head_raw.model')
print("Training Completed")
print("===========================")
print("Hard Negative Mining")
# Configuration
window_stride = (8, 8)
window_size = (50, 50)
print("Maximum Hard Negatives to mine: " + str(MAX_HARD_NEGATIVES))
neg_img_files = []
for (dirpath, dirnames, filenames) in os.walk(hnm_img_dir):
neg_img_files.extend(filenames)
break
hnm_data, hnm_labels = hard_negative_mine(neg_img_files, window_size, window_stride)
sys.stdout.write("\n")
hnm_data = np.concatenate((hnm_data, train_data), axis=0)
hnm_labels = np.concatenate((hnm_labels, train_labels), axis=0)
hnm_data, hnm_labels = shuffle(hnm_data, hnm_labels, random_state=0)
print("Final Samples dims: " + str(hnm_data.shape))
print("Retraining the classifier with final data")
clf2 = svm.LogisticRegression(C=0.05, dual=True, random_state=rand.randint(1, 100), solver='liblinear', max_iter=10000)
clf2.fit(hnm_data, hnm_labels)
print("Trained and dumping")
joblib.dump(clf2, r'models/head_final.model')