Mercurial Hosting > traffic-intelligence
comparison scripts/train-object-classification.py @ 519:4ad5123d969e
added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 18 Jun 2014 00:53:39 -0400 |
parents | |
children | ce40a89bd6ae |
comparison
equal
deleted
inserted
replaced
518:0c86c73f3c09 | 519:4ad5123d969e |
---|---|
1 #! /usr/bin/env python | |
2 | |
3 import numpy as np | |
4 import sys, argparse | |
5 from cv2 import SVM_RBF, SVM_C_SVC | |
6 | |
7 import cvutils, moving, ml | |
8 | |
9 | |
10 # todo update with argparse | |
11 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') | |
12 parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True) | |
13 parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = SVM_RBF, type = long) | |
14 parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) | |
15 parser.add_argument('-s', dest = 'rescaleSize', help = 'rescale size of image samples', default = 64, type = int) | |
16 parser.add_argument('-o', dest = 'nOrientations', help = 'number of orientations in HoG', default = 9, type = int) | |
17 parser.add_argument('-p', dest = 'nPixelsPerCell', help = 'number of pixels per cell', default = 8, type = int) | |
18 parser.add_argument('-c', dest = 'nCellsPerBlock', help = 'number of cells per block', default = 2, type = int) | |
19 args = parser.parse_args() | |
20 | |
21 rescaleSize = (args.rescaleSize, args.rescaleSize) | |
22 nPixelsPerCell = (args.nPixelsPerCell, args.nPixelsPerCell) | |
23 nCellsPerBlock = (args.nCellsPerBlock, args.nCellsPerBlock) | |
24 | |
25 imageDirectories = {'pedestrian': args.directoryName + "/Pedestrians/", | |
26 'bicycle': args.directoryName + "/Cyclists/", | |
27 'car': args.directoryName + "/Vehicles/"} | |
28 | |
29 #directory_model = args.directoryName | |
30 trainingSamplesPBV = {} | |
31 trainingLabelsPBV = {} | |
32 trainingSamplesBV = {} | |
33 trainingLabelsBV = {} | |
34 trainingSamplesPB = {} | |
35 trainingLabelsPB = {} | |
36 trainingSamplesPV = {} | |
37 trainingLabelsPV = {} | |
38 | |
39 for k, v in imageDirectories.iteritems(): | |
40 print('Loading {} samples'.format(k)) | |
41 trainingSamplesPBV[k], trainingLabelsPBV[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
42 if k != 'pedestrian': | |
43 trainingSamplesBV[k], trainingLabelsBV[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
44 if k != 'car': | |
45 trainingSamplesPB[k], trainingLabelsPB[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
46 if k != 'bicycle': | |
47 trainingSamplesPV[k], trainingLabelsPV[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
48 | |
49 # Training the Support Vector Machine | |
50 print "Training Pedestrian-Cyclist-Vehicle Model" | |
51 model = ml.SVM(args.svmType, args.kernelType) | |
52 model.train(np.concatenate(trainingSamplesPBV.values()), np.concatenate(trainingLabelsPBV.values())) | |
53 model.save(args.directoryName + "/modelPBV.xml") | |
54 | |
55 print "Training Cyclist-Vehicle Model" | |
56 model = ml.SVM(args.svmType, args.kernelType) | |
57 model.train(np.concatenate(trainingSamplesBV.values()), np.concatenate(trainingLabelsBV.values())) | |
58 model.save(args.directoryName + "/modelBV.xml") | |
59 | |
60 print "Training Pedestrian-Cyclist Model" | |
61 model = ml.SVM(args.svmType, args.kernelType) | |
62 model.train(np.concatenate(trainingSamplesPB.values()), np.concatenate(trainingLabelsPB.values())) | |
63 model.save(args.directoryName + "/modelPB.xml") | |
64 | |
65 print "Training Pedestrian-Vehicle Model" | |
66 model = ml.SVM(args.svmType, args.kernelType) | |
67 model.train(np.concatenate(trainingSamplesPV.values()), np.concatenate(trainingLabelsPV.values())) | |
68 model.save(args.directoryName + "/modelPV.xml") |