annotate scripts/train-object-classification.py @ 1222:69b531c7a061

added methods to reset trajectories and change object coordinates (including features)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 20 Jun 2023 15:42:19 -0400
parents 933670761a57
children d478d3122804
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
1 #! /usr/bin/env python3
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
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
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 812
diff changeset
4 import argparse
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 963
diff changeset
5 from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE # row_sample for layout in cv2.ml.SVM_load
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
6
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
7 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
8
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 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
10 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
11 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
12 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
13 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
14 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
15 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
16 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
17 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
18 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
19 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the classifier configuration file', required = True)
963
2757efeabbb4 minor renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 961
diff changeset
20 parser.add_argument('--confusion-matrix', dest = 'computeConfusionMatrix', help = 'compute the confusion matrix on the training data', action = 'store_true')
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
21
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
22 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
23 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
24
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
25 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
26 '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
27 '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
28
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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
38 for k, v in imageDirectories.items():
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
39 print('Loading {} samples'.format(k))
923
238008f81c16 corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
40 trainingSamples, trainingLabels = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogBlockNorm, classifierParams.hogNCellsPerBlock)
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
41 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
42 if k != 'pedestrian':
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
43 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
44 if k != 'car':
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
45 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
46 if k != 'bicycle':
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 522
diff changeset
47 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
48
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
49 # Training the Support Vector Machine
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
50 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
51 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
52 classifications = model.train(np.concatenate(list(trainingSamplesPBV.values())), ROW_SAMPLE, np.concatenate(list(trainingLabelsPBV.values())), True)
963
2757efeabbb4 minor renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 961
diff changeset
53 if args.computeConfusionMatrix:
961
ec1682ed999f added computation of confusion matrix and improved default parameter for block normalization for SVM classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 923
diff changeset
54 print(classifications)
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
55 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
56
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
57 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
58 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
59 classifications = model.train(np.concatenate(list(trainingSamplesBV.values())), ROW_SAMPLE, np.concatenate(list(trainingLabelsBV.values())), True)
963
2757efeabbb4 minor renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 961
diff changeset
60 if args.computeConfusionMatrix:
961
ec1682ed999f added computation of confusion matrix and improved default parameter for block normalization for SVM classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 923
diff changeset
61 print(classifications)
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
62 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
63
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
64 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
65 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
66 classifications = model.train(np.concatenate(list(trainingSamplesPB.values())), ROW_SAMPLE, np.concatenate(list(trainingLabelsPB.values())), True)
963
2757efeabbb4 minor renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 961
diff changeset
67 if args.computeConfusionMatrix:
961
ec1682ed999f added computation of confusion matrix and improved default parameter for block normalization for SVM classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 923
diff changeset
68 print(classifications)
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
69 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
70
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
71 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
72 model = ml.SVM(args.svmType, args.kernelType, args.degree, args.gamma, args.coef0, args.cvalue, args.nu, args.svmP)
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
73 classifications = model.train(np.concatenate(list(trainingSamplesPV.values())), ROW_SAMPLE, np.concatenate(list(trainingLabelsPV.values())), True)
963
2757efeabbb4 minor renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 961
diff changeset
74 if args.computeConfusionMatrix:
961
ec1682ed999f added computation of confusion matrix and improved default parameter for block normalization for SVM classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 923
diff changeset
75 print(classifications)
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")