annotate scripts/train-object-classification.py @ 812:21f10332c72b

moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 10 Jun 2016 17:07:36 -0400
parents 52aa03260f03
children 85b81c46c526
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 import numpy as np
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4 import sys, argparse
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
5 from cv2 import SVM_RBF, SVM_C_SVC
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
6 #from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE # row_sample for layout in cv2.ml.SVM_load
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
7
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
8 import cvutils, moving, ml, storage
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene')
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11 parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True)
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
12 parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = SVM_RBF, type = long)
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13 parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long)
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
14 parser.add_argument('--deg', dest = 'degree', help = 'SVM degree', default = 0, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
15 parser.add_argument('--gamma', dest = 'gamma', help = 'SVM gamma', default = 1, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
16 parser.add_argument('--coef0', dest = 'coef0', help = 'SVM coef0', default = 0, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
17 parser.add_argument('--cvalue', dest = 'cvalue', help = 'SVM Cvalue', default = 1, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
18 parser.add_argument('--nu', dest = 'nu', help = 'SVM nu', default = 0, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
19 parser.add_argument('--svmp', dest = 'svmP', help = 'SVM p', default = 0, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
20 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the classifier configuration file', required = True)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
21 # parser.add_argument('-s', dest = 'rescaleSize', help = 'rescale size of image samples', default = 64, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
22 # parser.add_argument('-o', dest = 'nOrientations', help = 'number of orientations in HoG', default = 9, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
23 # parser.add_argument('-p', dest = 'nPixelsPerCell', help = 'number of pixels per cell', default = 8, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
24 # parser.add_argument('-c', dest = 'nCellsPerBlock', help = 'number of cells per block', default = 2, type = int)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
25
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26 args = parser.parse_args()
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
27 classifierParams = storage.ClassifierParameters(args.configFilename)
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
29 # rescaleSize = (args.rescaleSize, args.rescaleSize)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
30 # nPixelsPerCell = (args.nPixelsPerCell, args.nPixelsPerCell)
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
31 # nCellsPerBlock = (args.nCellsPerBlock, args.nCellsPerBlock)
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
32
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
33 imageDirectories = {'pedestrian': args.directoryName + "/Pedestrians/",
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
34 'bicycle': args.directoryName + "/Cyclists/",
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
35 'car': args.directoryName + "/Vehicles/"}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
36
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
37 trainingSamplesPBV = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
38 trainingLabelsPBV = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
39 trainingSamplesBV = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
40 trainingLabelsBV = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
41 trainingSamplesPB = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
42 trainingLabelsPB = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
43 trainingSamplesPV = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
44 trainingLabelsPV = {}
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
45
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
46 for k, v in imageDirectories.iteritems():
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
47 print('Loading {} samples'.format(k))
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
48 trainingSamples, trainingLabels = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock)
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
49 trainingSamplesPBV[k], trainingLabelsPBV[k] = trainingSamples, trainingLabels
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
50 if k != 'pedestrian':
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
51 trainingSamplesBV[k], trainingLabelsBV[k] = trainingSamples, trainingLabels
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
52 if k != 'car':
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
53 trainingSamplesPB[k], trainingLabelsPB[k] = trainingSamples, trainingLabels
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
54 if k != 'bicycle':
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
55 trainingSamplesPV[k], trainingLabelsPV[k] = trainingSamples, trainingLabels
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
56
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57 # Training the Support Vector Machine
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
58 print "Training Pedestrian-Cyclist-Vehicle Model"
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
59 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
60 model.train(np.concatenate(trainingSamplesPBV.values()), np.concatenate(trainingLabelsPBV.values()))
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
61 model.save(args.directoryName + "/modelPBV.xml")
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
62
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
63 print "Training Cyclist-Vehicle Model"
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
64 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
65 model.train(np.concatenate(trainingSamplesBV.values()), np.concatenate(trainingLabelsBV.values()))
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66 model.save(args.directoryName + "/modelBV.xml")
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
67
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
68 print "Training Pedestrian-Cyclist Model"
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
69 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
70 model.train(np.concatenate(trainingSamplesPB.values()), np.concatenate(trainingLabelsPB.values()))
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71 model.save(args.directoryName + "/modelPB.xml")
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
72
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
73 print "Training Pedestrian-Vehicle Model"
812
21f10332c72b moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
74 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
75 model.train(np.concatenate(trainingSamplesPV.values()), np.concatenate(trainingLabelsPV.values()))
519
4ad5123d969e added script to train HoG-SVM classifiers for object classification (based on a script by Sohail Zangenehpour, PhD student at McGill)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
76 model.save(args.directoryName + "/modelPV.xml")