In [1]:
%matplotlib inline
import numpy as np
import cv2
import os, time
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from skimage.feature import hog
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from helper import *
In [2]:
color_space = 'YCrCb' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
orient = 9 # HOG orientations
pix_per_cell = 16 # HOG pixels per cell
cell_per_block = 1 # HOG cells per block
hog_channel = 'ALL' # Can be 0, 1, 2, or "ALL"
spatial_size = (8, 8) # Spatial binning dimensions
hist_bins = 8 # Number of histogram bins
spatial_feat = True # Spatial features on or off
hist_feat = True # Histogram features on or off
hog_feat = True # HOG features on or off
In [3]:
vehicle_path = "./dataset/OwnCollection/vehicles/"
nonvehicle_path = "./dataset/OwnCollection/non-vehicles/"
vehicle_list = get_dataset(vehicle_path)
nonvehicle_list = get_dataset(nonvehicle_path)
In [4]:
## need to test SVC acc
## color space: RGB, LUV, HLS, YCrCb
## pix_per_cell: 8, 16
## cell_per_block: 1, 2
## others fixed
color_list = ['RGB', 'LUV','HLS','YCrCb']
orient_list = [9, 12]
pix_per_cell_list = [8, 16]
cell_per_block_list = [1, 2]
parameter_set = []
for cs in color_list:
for ppc in pix_per_cell_list:
for cpb in cell_per_block_list:
for orient in orient_list:
parameter_set.append([cs, ppc, cpb, orient])
parameter_set
Out[4]:
In [5]:
def get_classifier(vehicle_list, nonvehicle_list, parameters):
color_space = parameters[0] # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
orient = parameters[3] # HOG orientations
pix_per_cell = parameters[1] # HOG pixels per cell
cell_per_block = parameters[2] # HOG cells per block
hog_channel = 'ALL' # Can be 0, 1, 2, or "ALL"
spatial_size = (32, 32) # Spatial binning dimensions
hist_bins = 32 # Number of histogram bins
spatial_feat = True # Spatial features on or off
hist_feat = True # Histogram features on or off
hog_feat = True # HOG features on or off
car_features = extract_features(vehicle_list, 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)
notcar_features = extract_features(nonvehicle_list, 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)
# Standardized dataset
X = np.vstack((car_features, notcar_features)).astype(np.float64)
X_scaler = StandardScaler().fit(X)
scaled_X = X_scaler.transform(X)
y = np.hstack((np.ones(len(car_features)), np.zeros(len(notcar_features))))
X_train, X_test, y_train, y_test = train_test_split(scaled_X, y, test_size=0.2, random_state=101)
# Train SVM classifier
svc = LinearSVC()
t=time.time()
svc.fit(X_train, y_train)
t2=time.time()
print(parameters)
print(round(t2-t, 2), 'Seconds to train SVC...')
print('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))
print('\n')
In [6]:
for parameter in parameter_set:
get_classifier(vehicle_list=vehicle_list, nonvehicle_list=nonvehicle_list, parameters=parameter)
In [ ]: