def step02_test(args): # list_all_images cars, notcars = list_all_images(args) # choose random car/notcar indices car_ind = np.random.randint(0, len(cars)) notcar_ind = np.random.randint(0, len(notcars)) # read in car / notcar images car_image = mpimg.imread(cars[car_ind]) notcar_image = mpimg.imread(notcars[notcar_ind]) # define feature parameters color_space = 'RGB' # can be RGB HSV LUV HLS YUV YCrCb orient = 6 pix_per_cell = 8 cell_per_block = 2 hog_channel = 0 # can be 0 1 2 or 'ALL' spatial_size = (16, 16) # spatial binning dimensions hist_bins = 16 # 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, car_hog_image = single_img_features( car_image, 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, vis=True) notcar_features, notcar_hog_image = single_img_features( notcar_image, 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, vis=True) images = [car_image, car_hog_image, notcar_image, notcar_hog_image] titles = ['car image', 'car HOG image', 'notcar_image', 'notcar HOG image'] #figure = plt.figure(figsize=(12, 3)) # , dpi=80) #visualize(figure, 1, 4, images, titles) figsize = (12, 3) visualize(figsize, 4, images, titles)
def step02_test(args, var, index=(0, 0), flag=True): # list_all_images cars, notcars = list_all_images(args) # choose random car/notcar indices if flag: car_ind = np.random.randint(0, len(cars)) notcar_ind = np.random.randint(0, len(notcars)) print('car_ind: {}, notcar_ind: {}'.format(car_ind, notcar_ind)) else: (car_ind, notcar_ind) = index # read in car / notcar images car_image = mpimg.imread(cars[car_ind]) notcar_image = mpimg.imread(notcars[notcar_ind]) # # define feature parameters # color_space = 'RGB' # can be RGB HSV LUV HLS YUV YCrCb # orient = 6 # pix_per_cell = 8 # cell_per_block = 2 # hog_channel = 0 # can be 0 1 2 or 'ALL' # spatial_size = (16, 16) # spatial binning dimensions # hist_bins = 16 # 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 # set feature parameters cell_per_block = var['cell_per_block'] # 4 color_space = var['color_space'] # 1 can be RGB HSV LUV HLS YUV YCrCb hist_bins = var['hist_bins'] # 7 number of histogram bins, 16 hist_feat = var['hist_feat'] # 9 histogram features on or off hog_channel = var['hog_channel'] # 5 can be 0 1 2 or 'ALL' hog_feat = var['hog_feat'] # 10 HOG features on or off orient = var['orient'] # 2 pix_per_cell = var['pix_per_cell'] # 3 #scale = var['scale'] spatial_feat = var['spatial_feat'] # 8 spatial features on or off spatial_size = var[ 'spatial_size'] # 6 spatial binning dimensions, (16, 16) #ystart = var['y_start_stop'][0] #ystop = var['y_start_stop'][1] car_features, car_hog_image = single_img_features( car_image, 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, vis=True) notcar_features, notcar_hog_image = single_img_features( notcar_image, 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, vis=True) images = [car_image, car_hog_image, notcar_image, notcar_hog_image] titles = ['car image', 'car HOG image', 'notcar_image', 'notcar HOG image'] #figure = plt.figure(figsize=(12, 3)) # , dpi=80) #visualize(figure, 1, 4, images, titles) figsize = (12, 3) visualize(figsize, 4, images, titles)
def classifier(args, var, to_print=True): if os.path.exists(args.pickled + 'svc.p'): # de-serialize/load X_scaler, scaled_X, svc: X_scaler = pickle.load(open(args.pickled + "X_scaler.p", "rb")) scaled_X = pickle.load(open(args.pickled + "scaled_X.p", "rb")) svc = pickle.load(open(args.pickled + "svc.p", "rb")) else: # list_all_images cars, notcars = list_all_images(args) # choose random car/notcar indices car_ind = np.random.randint(0, len(cars)) notcar_ind = np.random.randint(0, len(notcars)) # read in car / notcar images car_image = mpimg.imread(cars[car_ind]) notcar_image = mpimg.imread(notcars[notcar_ind]) t = time.time() n_samples = 1000 random_idxs = np.random.randint(0, len(cars), n_samples) test_cars = np.array(cars)[random_idxs] test_noncars = np.array(notcars)[random_idxs] car_features = extract_features(test_cars, color_space=var['color_space'], spatial_size=var['spatial_size'], hist_bins=var['hist_bins'], orient=var['orient'], pix_per_cell=var['pix_per_cell'], cell_per_block=var['cell_per_block'], hog_channel=var['hog_channel'], spatial_feat=var['spatial_feat'], hist_feat=var['hist_feat'], hog_feat=var['hog_feat']) notcar_features = extract_features(test_noncars, color_space=var['color_space'], spatial_size=var['spatial_size'], hist_bins=var['hist_bins'], orient=var['orient'], pix_per_cell=var['pix_per_cell'], cell_per_block=var['cell_per_block'], hog_channel=var['hog_channel'], spatial_feat=var['spatial_feat'], hist_feat=var['hist_feat'], hog_feat=var['hog_feat']) if to_print: print(time.time()-t, 'Seconds to compute features...') X = np.vstack((car_features, notcar_features)).astype(np.float64) # fit a per_column scaler X_scaler = StandardScaler().fit(X) # apply the scaler to X scaled_X = X_scaler.transform(X) # define the labels vector y = np.hstack(( np.ones(len(car_features)), np.zeros(len(notcar_features)) )) # split up data into randomized training and test sets rand_state = np.random.randint(0, 100) X_train, X_test, y_train, y_test = train_test_split(scaled_X, y, test_size=0.2, random_state=rand_state) # test_size=0.1 if to_print: print('Using:', var['orient'], 'orientations,', var['pix_per_cell'], 'pixels per cell,', var['cell_per_block'], 'cells per block,', var['hist_bins'], 'histogram bins, and', var['spatial_size'], 'spatial sampling') if to_print: print('Feature vector length:', len(X_train[0])) # use a linear SVC svc = LinearSVC() # check the training time for the SVC t = time.time() svc.fit(X_train, y_train) # https://stackoverflow.com/questions/40524790/valueerror-this-solver-needs-samples-of-at-least-2-classes-in-the-data-but-the if to_print: print(round(time.time()-t, 2), 'Seconds to train SVC...') # check the score of the SVC if to_print: print('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4)) if to_print: print('[SHAPE] Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4)) # serialize/store X_scaler, scaled_X, svc: pickle.dump(X_scaler, open(args.pickled + "X_scaler.p", 'wb')) pickle.dump(scaled_X, open(args.pickled + "scaled_X.p", 'wb')) pickle.dump(svc, open(args.pickled + "svc.p", 'wb')) return X_scaler, scaled_X, svc