Exemplo n.º 1
0
	def train_classifier(self):
		vehicles = glob.glob('./Data/vehicles/**/*.png',recursive=True)
		non_vehicles = glob.glob('./Data/non-vehicles/**/*.png',recursive=True)
		print("Number of images in vehicle dataset:",len(vehicles))
		print("Number of images in non-vehicle dataset:",len(non_vehicles))


		#Read training data
		vehicle_features 			= extract_features(vehicles, 
																				color_space=self.color_space, spatial_size=self.spatial_size,
                        								hist_bins=self.hist_bins, orient=self.orient, 
                        								pix_per_cell=self.pix_per_cell, cell_per_block=self.cell_per_block, 
                        								hog_channel= self.hog_channel,
                        								spatial_feat=self.spatial_feat, hist_feat=self.hist_feat, 
                        								hog_feat=self.hog_feat)
		non_vehicle_features 	= extract_features(non_vehicles, 
																				color_space=self.color_space, spatial_size=self.spatial_size,
                        								hist_bins=self.hist_bins, orient=self.orient, 
                        								pix_per_cell=self.pix_per_cell, cell_per_block=self.cell_per_block, 
                        								hog_channel= self.hog_channel,
                        								spatial_feat=self.spatial_feat, hist_feat=self.hist_feat, 
                        								hog_feat=self.hog_feat)

		#Define labels vectors bases on features
		feature_labels = np.hstack((np.ones(len(vehicles)),np.zeros(len(non_vehicles))))
		X_train_initial = np.vstack((vehicle_features,non_vehicle_features))
		y_train_initial = feature_labels
		
		#Normalize/scale features
		feature_scaler = StandardScaler()
		X_train_scaled = feature_scaler.fit_transform(X_train_initial)

		X_train_final = X_train_scaled
		y_train_final = y_train_initial

		#Train the classifier
		from sklearn.model_selection import train_test_split
		rand_state = np.random.randint(0, 100)
		X_train,X_test,y_train,y_test = train_test_split(X_train_final,y_train_final,test_size=0.2,
                                                 random_state = rand_state)

		from sklearn.svm import LinearSVC
		clf = LinearSVC()
		clf.fit(X_train,y_train)
		#Print accuracy of SVC
		print("End of Training Classifier")
		print('Test Accuracy of SVC = ', clf.score(X_test, y_test))

		
		#Save the classifier
		self.clf = clf
		self.feature_scaler = feature_scaler
		print("Saving Classifier and Feature Scaler to file")
		joblib.dump(clf,"./dependencies/car_classifier.pkl")
		joblib.dump(feature_scaler,"./dependencies/car_feature_scaler.pkl")

		return 
Exemplo n.º 2
0
def train_with_grid_search(color_space='YUV',
                           spatial_size=(32, 32),
                           hist_bins=32,
                           orient=11,
                           pix_per_cell=16,
                           cell_per_block=2,
                           hog_channel='ALL',
                           spatial_feat=False,
                           hist_feat=False,
                           hog_feat=True):
    parameters = {'C': [1, 100, 1000, 10000]}
    svc = LinearSVC()
    clf = GridSearchCV(svc, parameters, n_jobs=3, verbose=5)

    cars, notcars = get_data()

    print('Extracting features')
    car_features = lesson_functions.extract_features(
        cars,
        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 = lesson_functions.extract_features(
        notcars,
        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)

    # Create an array stack of feature vectors
    X = np.vstack((car_features, notcar_features)).astype(np.float64)

    # Define the labels vector
    y = np.hstack((np.ones(len(car_features)), np.zeros(len(notcar_features))))

    print('Starting to fit model')
    clf.fit(X, y)
    print('Best parameters found: ' + str(clf.best_params_))
 def _extract_features(self, file_names):
     return lesson_functions.extract_features(
         file_names,
         color_space=self.color_space,
         spatial_size=self.spatial_size,
         hist_bins=self.hist_bins,
         orient=self.orient,
         pix_per_cell=self.pix_per_cell,
         cell_per_block=self.cell_per_block,
         hog_channel=self.hog_channel,
         spatial_feat=self.spatial_feat,
         hist_feat=self.hist_feat,
         hog_feat=self.hog_feat)
Exemplo n.º 4
0
    def train(self):
        vehicles = glob.glob('./vehicles/**/*.png', recursive=True)
        non_vehicles = glob.glob('./non-vehicles/**/*.png', recursive=True)
        print("Length of Vehicle Dataset:", len(vehicles))
        print("Length of Non-Vehicle Dataset:", len(non_vehicles))

        # Extract Features from Vehicles/Non-Vehilces
        vehicle_features = extract_features(vehicles,
                                            color_space=self.color_space,
                                            spatial_size=self.spatial_size,
                                            hist_bins=self.hist_bins,
                                            orient=self.orient,
                                            pix_per_cell=self.pix_per_cell,
                                            cell_per_block=self.cell_per_block,
                                            hog_channel=self.hog_channel,
                                            spatial_feat=self.spatial_feat,
                                            hist_feat=self.hist_feat,
                                            hog_feat=self.hog_feat)

        non_vehicle_features = extract_features(
            non_vehicles,
            color_space=self.color_space,
            spatial_size=self.spatial_size,
            hist_bins=self.hist_bins,
            orient=self.orient,
            pix_per_cell=self.pix_per_cell,
            cell_per_block=self.cell_per_block,
            hog_channel=self.hog_channel,
            spatial_feat=self.spatial_feat,
            hist_feat=self.hist_feat,
            hog_feat=self.hog_feat)

        # Normalize Features
        X_scaler, X_train_final, y_train_final = self.normalize_features(
            vehicle_features, non_vehicle_features, vehicles, non_vehicles)

        # Split Data
        rand_state = np.random.randint(0, 100)
        X_train, X_test, y_train, y_test = train_test_split(
            X_train_final,
            y_train_final,
            test_size=0.2,
            random_state=rand_state)

        # Train Car Classifier
        t = time.time()
        clf = LinearSVC()
        clf.fit(X_train, y_train)
        t2 = time.time()
        print(round(t2 - t, 2), 'Seconds to train SVC...')

        # Check the Score of the SVC
        print('Test Accuracy of SVC = ', clf.score(X_test, y_test))

        # Save the Classifier
        self.clf = clf
        self.feature_scaler = X_scaler
        print("Saving Classifier and Feature Scaler.")
        joblib.dump(clf, "./dependencies/car_classifier.pkl")
        joblib.dump(X_scaler, "./dependencies/car_feature_scaler.pkl")

        return
Exemplo n.º 5
0
def train(vehicle_files,
          non_vehicle_files,
          color_space='YCrCb',
          orient=9,
          hog_channel='ALL',
          pix_per_cell=8,
          cell_per_block=2,
          hist_bins=32,
          spatial_size=32,
          spatial_feat=True,
          hist_feat=True,
          hog_feat=True):

    #print ("get vehicle feautures")
    vehicle_features = extract_features(vehicle_files,
                                        color_space,
                                        spatial_size=(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)

    #print ("get non vehicle feautures")
    non_vehicle_features = extract_features(non_vehicle_files,
                                            color_space,
                                            spatial_size=(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)

    # Create an array stack, NOTE: StandardScaler() expects np.float64
    X = np.vstack((vehicle_features, non_vehicle_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(vehicle_features)), np.zeros(len(non_vehicle_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, train_size=1000, test_size=100, random_state=rand_state)
        scaled_X,
        y,
        test_size=0.2,
        shuffle=True,
        random_state=rand_state)

    t = time.time()
    #parameters = {'kernel': ['rbf'], 'C':[0.001, 0.01, 0.1, 1, 10], 'gamma':[0.001, 0.01, 0.1, 1]}
    #svc = svm.SVC(verbose=True, cache_size=2000)
    #clf = GridSearchCV(svc, parameters, verbose=True, n_jobs=6)
    clf = svm.LinearSVC()
    clf.fit(X_train, y_train)
    #print('Best params = ', clf.best_params_)
    t2 = time.time()
    print(round(t2 - t, 2), 'Seconds to train SVC...')
    # Check the score of the SVC
    accu = round(clf.score(X_test, y_test), 4)
    #print('Test Accuracy of SVC = ', accu)

    # Check the prediction time for a single sample
    t = time.time()
    return clf, X_scaler, accu, len(vehicle_features[0])
Exemplo n.º 6
0
def training(color_space='YCrCb',
             spatial_size=(16, 16),
             hist_bins=32,
             orient=16,
             pix_per_cell=16,
             cell_per_block=2,
             hog_channel='ALL',
             spatial_feat=True,
             hist_feat=True,
             hog_feat=True):
    '''
    color_space Can be RGB, HSV, LUV, HLS, YUV, YCrCb
    hog_channel Can be 0, 1, 2, or "ALL"
    '''
    cars, notcars = get_data()
    print('Num cars: {} Num non-cars: {}'.format(len(cars), len(notcars)))

    t = time.time()
    car_features = lesson_functions.extract_features(
        cars,
        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 = lesson_functions.extract_features(
        notcars,
        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)
    t2 = time.time()
    print(round(t2 - t, 2), 'Seconds to extract HOG features...')

    # Create an array stack of feature vectors
    X = np.vstack((car_features, notcar_features)).astype(np.float64)

    # 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(
        X, y, test_size=0.2, random_state=rand_state)

    # Fit a per-column scaler
    X_scaler = StandardScaler().fit(X_train)
    # Apply the scaler to X
    X_train = X_scaler.transform(X_train)
    X_test = X_scaler.transform(X_test)

    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()
    # 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('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 = 10
    print('My SVC predicts: ', svc.predict(X_test[0:n_predict]))
    print('For these', n_predict, 'labels: ', y_test[0:n_predict])
    t2 = time.time()
    print(round(t2 - t, 5), 'Seconds to predict', n_predict, 'labels with SVC')
    return svc, X_scaler
Exemplo n.º 7
0
def train(save=False,
          save_path=MODEL_PICKLE,
          tuning=False,
          sample_size=SAMPLE_SIZE,
          data_path=DATA_PATH,
          color_space=COLOR_SPACE,
          orient=ORIENT,
          pix_per_cell=PIX_PER_CELL,
          cell_per_block=CELL_PER_BLOCK,
          hog_channel=HOG_CHANNEL,
          spatial_size=SPATIAL_SIZE,
          hist_bins=HIST_BINS,
          spatial_feat=SPATIAL_FEAT,
          hist_feat=HIST_FEAT,
          hog_feat=HOG_FEAT):

    cars = glob.glob(data_path + '/vehicles/*/*.png')
    notcars = glob.glob(data_path + '/non-vehicles/*/*.png')

    random.shuffle(cars)
    random.shuffle(notcars)
    if sample_size != "ALL":
        cars = cars[0:sample_size]
        notcars = notcars[0:sample_size]

    print("Number of car images:", len(cars))
    print("Number of non-car images", len(notcars))

    car_features = extract_features(cars,
                                    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(notcars,
                                       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)

    # Create an array stack of feature vectors
    X = np.vstack((car_features, notcar_features)).astype(np.float64)

    # 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(
        X, y, test_size=0.2, random_state=rand_state)

    # Fit a per-column scaler
    X_scaler = StandardScaler().fit(X_train)
    X_train = X_scaler.transform(X_train)
    X_test = X_scaler.transform(X_test)

    print('Using:', color_space, 'color space,', orient, 'orientations,',
          pix_per_cell, 'pixels per cell,', cell_per_block,
          'and cells per block')
    print('Feature vector length:', len(X_train[0]))

    # TRAIN a new model
    if tuning:
        parameters = {
            'kernel': ('linear', 'rbf'),
            'C': [0.1, 1, 10],
            'gamma': [0.1, 1, 10]
        }
        print("Tuning SVM parameters using GridSearchCV() with: \n\t",
              parameters)
        svr = SVC()
        svc = GridSearchCV(svr, parameters)
    else:
        svc = LinearSVC()

    t_start = time.time()
    svc.fit(X_train, y_train)
    t_end = time.time()
    print(round(t_end - t_start, 2), 'Seconds to train SVM...')

    if save:
        print("Saving trained model to:", save_path)
        model = {
            "svc": svc,
            "scaler": X_scaler,
            "color_space": color_space,
            "orient": orient,
            "pix_per_cell": pix_per_cell,
            "cell_per_block": cell_per_block,
            "hog_channel": hog_channel,
            "spatial_size": spatial_size,
            "hist_bins": hist_bins,
            "spatial_feat": spatial_feat,
            "hist_feat": hist_feat,
            "hog_feat": hog_feat
        }
        pickle.dump(model, open(save_path, "wb"))

    # Check the score of the SVC
    print('Test Accuracy of SVC =', round(svc.score(X_test, y_test), 4))
    return svc
def Training_Classifier_Pipeline(data,
                                 output_name='Svm.pkl',
                                 color_space='YUV',
                                 spatial_size=(32, 32),
                                 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):
    # Read data
    cars, notcars = Read_Data(data)
    # Reduce the sample size because HOG features are slow to compute
    # The quiz evaluator times out after 13s of CPU time
    #     sample_size = 5000
    #     cars = cars[0:sample_size]
    #     notcars = notcars[0:sample_size]
    # Get feature in data images
    t = time.time()
    car_features = extract_features(cars,
                                    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(notcars,
                                       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)
    t2 = time.time()
    print(round(t2 - t, 2), 'Seconds to extract HOG features...')
    # Create an array stack of feature vectors
    X = np.vstack((car_features, notcar_features)).astype(np.float64)
    # 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(
        X, y, test_size=0.2, random_state=rand_state)

    # Fit a per-column scaler
    X_scaler = StandardScaler().fit(X_train)
    # Apply the scaler to X
    X_train = X_scaler.transform(X_train)
    X_test = X_scaler.transform(X_test)

    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()
    # 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('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))
    # Check the prediction time for a single sample
    t = time.time()
    # Test the model
    n_predict = 10
    print('My SVC predicts: ', svc.predict(X_test[0:n_predict]))
    print('For these', n_predict, 'labels: ', y_test[0:n_predict])
    t2 = time.time()
    print(round(t2 - t, 5), 'Seconds to predict', n_predict, 'labels with SVC')
    # save model
    SaveToPickle(output_name, svc, X_scaler, orient, pix_per_cell,
                 cell_per_block, spatial_size, hist_bins)
    return output_name
Exemplo n.º 9
0
#######################
# Train
#######################

if train_model == 1:
    # Create the training data
    cars, notcars = detect.make_learnlist(sample_size)
    # Extract the features
    car_features_spat = lesson_functions.extract_features(
        cars,
        color_space,
        spatial_size,
        hist_bins,
        orient,
        pix_per_cell,
        cell_per_block,
        hog_channel,
        spatial_feat,
        False,
        False,
    )
    notcar_features_spat = lesson_functions.extract_features(
        notcars,
        color_space,
        spatial_size,
        hist_bins,
        orient,
        pix_per_cell,
        cell_per_block,
        hog_channel,