-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHOG_Classify.py
302 lines (245 loc) · 10.4 KB
/
HOG_Classify.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import cv2
import glob
import time
import pickle
import sklearn.svm as svm
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
from skimage.feature import hog
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn.externals import joblib
from lesson_functions import *
# Load the training validation and test data
data_file = 'data.p'
with open(data_file, mode='rb') as f:
data = pickle.load(f)
cars_train = data['cars_train']
cars_val = data['cars_val']
cars_test = data['cars_test']
notcars_train = data['notcars_train']
notcars_val = data['notcars_val']
notcars_test = data['notcars_test']
#helper function to extract features from files
def get_features(files, color_space='RGB', spatial_size=(32, 32),
hist_bins=32, orient=9,
pix_per_cell=8, cell_per_block=2, hog_channel=0,
spatial_feat=True, hist_feat=True, hog_feat=True):
features = []
for file in files:
img = mpimg.imread(file)
img_features = single_img_features(img, color_space=color_space, spatial_size=spatial_size,
hist_bins=hist_bins, orient=orient,
pix_per_cell=pix_per_cell, cell_per_block=cell_per_block, hog_channel=hog_channel,
spatial_feat=spatial_feat, hist_feat=hist_feat, hog_feat=hog_feat)
features.append(img_features)
return features
color_space = 'HLS' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
spatial_size = (16, 16)
hist_bins = 32
orient = 9
pix_per_cell = 8
cell_per_block = 2
hog_channel = 'ALL'
spatial_feat = True
hist_feat = True
hog_feat = True
t=time.time()
cars_train_feat = get_features(cars_train,color_space, spatial_size,hist_bins, orient,
pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat)
cars_val_feat = get_features(cars_val,color_space, spatial_size,hist_bins, orient,
pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat)
cars_test_feat = get_features(cars_test,color_space, spatial_size,hist_bins, orient,
pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat)
notcars_train_feat = get_features(notcars_train,color_space, spatial_size,hist_bins, orient,
pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat)
notcars_val_feat = get_features(notcars_val,color_space, spatial_size,hist_bins, orient,
pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat)
notcars_test_feat = get_features(notcars_test,color_space, spatial_size,hist_bins, orient,
pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat)
t2 = time.time()
print(round(t2-t, 2), 'Seconds to extract HOG,spatial and color features...')
# Create an array stack of feature vectors
X = np.vstack((cars_train_feat,cars_val_feat,cars_test_feat,
notcars_train_feat,notcars_val_feat,notcars_test_feat)).astype(np.float64)
# Fit a per-column scaler
X_scaler = StandardScaler().fit(X)
# Apply the scaler to X
scaled_X = X_scaler.transform(X)
cars_ntrain=len(cars_train_feat)
cars_nval=len(cars_val_feat)
cars_ntest=len(cars_test_feat)
ncars_ntrain=len(notcars_train_feat)
ncars_nval=len(notcars_val_feat)
ncars_ntest=len(notcars_test_feat)
i1 = cars_ntrain
i2 = i1 + cars_nval
i3 = i2 + cars_ntest
i4 = i3 + ncars_ntrain
i5 = i4 + ncars_nval
cars_train_feat,cars_val_feat,cars_test_feat = scaled_X[:i1],scaled_X[i1:i2],scaled_X[i2:i3]
notcars_train_feat,notcars_val_feat,notcars_test_feat = scaled_X[i3:i4],scaled_X[i4:i5],scaled_X[i5:]
y_train = np.hstack((np.ones(cars_ntrain), np.zeros(ncars_ntrain)))
y_val = np.hstack((np.ones(cars_nval), np.zeros(ncars_nval)))
y_test = np.hstack((np.ones(cars_ntest), np.zeros(ncars_ntest)))
X_train = np.vstack((scaled_X[:i1],scaled_X[i3:i4]))
X_val = np.vstack((scaled_X[i1:i2],scaled_X[i4:i5]))
X_test = np.vstack((scaled_X[i2:i3],scaled_X[i5:]))
X_train,y_train = shuffle(X_train,y_train,random_state=42)
X_val,y_val = shuffle(X_val,y_val,random_state=42)
X_test,y_test = shuffle(X_test,y_test,random_state=42)
print('Using:',orient,'orientations',pix_per_cell,'pixels per cell and', cell_per_block,'cells per block')
print('Feature vector length:', len(X_train[0]))
# Use a linear SVC
svc = LinearSVC()
# use of the rbf kernel improves the accuracy by about another percent,
# but increases the prediction time up to 1.7s(!) for 100 labels. Too slow.
#svc = svm.SVC(kernel='rbf')
# Check the training time for the SVC
t=time.time()
svc.fit(X_train, y_train)
t2 = time.time()
print(round(t2-t, 2), 'Seconds to train SVC...')
# Check the score of the SVC
print('Validation Accuracy of SVC = ', round(svc.score(X_val, y_val), 4))
print('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))
# Check the prediction time for a single sample
t=time.time()
n_predict = 100
print('My SVC predicts: ', svc.predict(X_val[0:n_predict]))
print('For these',n_predict, 'labels: ', y_val[0:n_predict])
t2 = time.time()
print(round(t2-t, 5), 'Seconds to predict', n_predict,'labels with SVC')
font_size=15
f, axarr = plt.subplots(4, 7,figsize=(20,10))
f.subplots_adjust(hspace=0.2, wspace=0.05)
colorspace = cv2.COLOR_RGB2HLS
#colorspace=cv2.COLOR_RGB2HSV
#colorspace=cv2.COLOR_RGB2YCrCb
i1,i2=22,4000
for ind,j in enumerate([i1,i2]):
image = plt.imread(cars_train[j])
feature_image = cv2.cvtColor(image, colorspace)
axarr[ind,0].imshow(image)
axarr[ind,0].set_xticks([])
axarr[ind,0].set_yticks([])
title = "car {0}".format(j)
axarr[ind,0].set_title(title, fontsize=font_size)
for channel in range(3):
axarr[ind,channel+1].imshow(feature_image[:,:,channel],cmap='gray')
title = "ch {0}".format(channel)
axarr[ind,channel+1].set_title(title, fontsize=font_size)
axarr[ind,channel+1].set_xticks([])
axarr[ind,channel+1].set_yticks([])
for channel in range(3):
features,hog_image = get_hog_features(feature_image[:,:,channel], orient, pix_per_cell,
cell_per_block, vis=True, feature_vec=True)
axarr[ind,channel+4].imshow(hog_image,cmap='gray')
title = "HOG ch {0}".format(channel)
axarr[ind,channel+4].set_title(title, fontsize=font_size)
axarr[ind,channel+4].set_xticks([])
axarr[ind,channel+4].set_yticks([])
for indn,j in enumerate([i1,i2]):
ind=indn+2
image = plt.imread(notcars_train[j])
feature_image = cv2.cvtColor(image, colorspace)
axarr[ind,0].imshow(image)
axarr[ind,0].set_xticks([])
axarr[ind,0].set_yticks([])
title = "not car {0}".format(j)
axarr[ind,0].set_title(title, fontsize=font_size)
for channel in range(3):
axarr[ind,channel+1].imshow(feature_image[:,:,channel],cmap='gray')
title = "ch {0}".format(channel)
axarr[ind,channel+1].set_title(title, fontsize=font_size)
axarr[ind,channel+1].set_xticks([])
axarr[ind,channel+1].set_yticks([])
for channel in range(3):
features,hog_image = get_hog_features(feature_image[:,:,channel], orient, pix_per_cell,
cell_per_block, vis=True, feature_vec=True)
axarr[ind,channel+4].imshow(hog_image,cmap='gray')
title = "HOG ch {0}".format(channel)
axarr[ind,channel+4].set_title(title, fontsize=font_size)
axarr[ind,channel+4].set_xticks([])
axarr[ind,channel+4].set_yticks([])
plt.show()
#plt.savefig('./images/HOG_features_HLS.png')
#plt.savefig('./images/HOG_features_YCrCb.png')
# plot false positives/negatives
font_size=15
preds = svc.predict(cars_val_feat)
misclassifieds = np.array(preds != np.ones(cars_nval))
inds = np.where(preds != np.ones(cars_nval))
inds = np.ravel(inds)
misclassifieds = [ cars_val[i] for i in inds]
fig, axes = plt.subplots(2,10,figsize=(20,5))
fig.subplots_adjust(hspace=0.2, wspace=0.05)
for i, ax in enumerate(axes.flat):
ax.imshow(plt.imread(misclassifieds[i]))
xlabel = "false neg {0}".format(i)
ax.set_xlabel(xlabel)
ax.set_xticks([])
ax.set_yticks([])
plt.show()
print('number of misclassified car images',len(misclassifieds))
#plt.savefig('./images/false_negatives.png')
fig, axes = plt.subplots(2,10,figsize=(20,5))
fig.subplots_adjust(hspace=0.2, wspace=0.05)
preds = svc.predict(notcars_val_feat)
inds = np.where(preds != np.zeros(ncars_nval))
inds = np.ravel(inds)
misclassifieds = [ notcars_val[i] for i in inds]
for i, ax in enumerate(axes.flat):
ax.imshow(plt.imread(misclassifieds[i]))
xlabel = "false pos {0}".format(i)
ax.set_xlabel(xlabel)
ax.set_xticks([])
ax.set_yticks([])
plt.show()
print('number of misclassified notcar images',len(misclassifieds))
#plt.savefig('./images/false_positives.png')
# Save the data for easy access
pickle_file = 'ProcessedData.p'
print('Saving data to pickle file...')
try:
with open(pickle_file, 'wb') as pfile:
pickle.dump(
{
'X_train': X_train,
'X_val': X_val,
'X_test': X_test,
'y_train': y_train,
'y_val': y_val,
'y_test': y_test
},
pfile, pickle.HIGHEST_PROTOCOL)
except Exception as e:
print('Unable to save data to', pickle_file, ':', e)
raise
print('Data cached in pickle file.')
pickle_file = 'ClassifierData.p'
print('Saving data to pickle file...')
try:
with open(pickle_file, 'wb') as pfile:
pickle.dump(
{ 'svc':svc,
'X_scaler': X_scaler,
'color_space': color_space,
'spatial_size': spatial_size,
'hist_bins': hist_bins,
'orient': orient,
'pix_per_cell': pix_per_cell,
'cell_per_block': cell_per_block,
'hog_channel': hog_channel,
'spatial_feat': spatial_feat,
'hist_feat': hist_feat,
'hog_feat':hog_feat
},
pfile, pickle.HIGHEST_PROTOCOL)
except Exception as e:
print('Unable to save data to', pickle_file, ':', e)
raise
print('Data cached in pickle file.')