Mercurial Hosting > traffic-intelligence
comparison scripts/train-object-classification.py @ 614:5e09583275a4
Merged Nicolas/trafficintelligence into default
author | Mohamed Gomaa <eng.m.gom3a@gmail.com> |
---|---|
date | Fri, 05 Dec 2014 12:13:53 -0500 |
parents | ce40a89bd6ae |
children | da1352b89d02 |
comparison
equal
deleted
inserted
replaced
598:11f96bd08552 | 614:5e09583275a4 |
---|---|
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 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') | |
10 parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True) | |
11 parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = SVM_RBF, type = long) | |
12 parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) | |
13 parser.add_argument('-s', dest = 'rescaleSize', help = 'rescale size of image samples', default = 64, type = int) | |
14 parser.add_argument('-o', dest = 'nOrientations', help = 'number of orientations in HoG', default = 9, type = int) | |
15 parser.add_argument('-p', dest = 'nPixelsPerCell', help = 'number of pixels per cell', default = 8, type = int) | |
16 parser.add_argument('-c', dest = 'nCellsPerBlock', help = 'number of cells per block', default = 2, type = int) | |
17 args = parser.parse_args() | |
18 | |
19 rescaleSize = (args.rescaleSize, args.rescaleSize) | |
20 nPixelsPerCell = (args.nPixelsPerCell, args.nPixelsPerCell) | |
21 nCellsPerBlock = (args.nCellsPerBlock, args.nCellsPerBlock) | |
22 | |
23 imageDirectories = {'pedestrian': args.directoryName + "/Pedestrians/", | |
24 'bicycle': args.directoryName + "/Cyclists/", | |
25 'car': args.directoryName + "/Vehicles/"} | |
26 | |
27 #directory_model = args.directoryName | |
28 trainingSamplesPBV = {} | |
29 trainingLabelsPBV = {} | |
30 trainingSamplesBV = {} | |
31 trainingLabelsBV = {} | |
32 trainingSamplesPB = {} | |
33 trainingLabelsPB = {} | |
34 trainingSamplesPV = {} | |
35 trainingLabelsPV = {} | |
36 | |
37 for k, v in imageDirectories.iteritems(): | |
38 print('Loading {} samples'.format(k)) | |
39 trainingSamplesPBV[k], trainingLabelsPBV[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
40 if k != 'pedestrian': | |
41 trainingSamplesBV[k], trainingLabelsBV[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
42 if k != 'car': | |
43 trainingSamplesPB[k], trainingLabelsPB[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
44 if k != 'bicycle': | |
45 trainingSamplesPV[k], trainingLabelsPV[k] = cvutils.createHOGTrainingSet(v, moving.userType2Num[k], rescaleSize, args.nOrientations, nPixelsPerCell, nCellsPerBlock) | |
46 | |
47 # Training the Support Vector Machine | |
48 print "Training Pedestrian-Cyclist-Vehicle Model" | |
49 model = ml.SVM(args.svmType, args.kernelType) | |
50 model.train(np.concatenate(trainingSamplesPBV.values()), np.concatenate(trainingLabelsPBV.values())) | |
51 model.save(args.directoryName + "/modelPBV.xml") | |
52 | |
53 print "Training Cyclist-Vehicle Model" | |
54 model = ml.SVM(args.svmType, args.kernelType) | |
55 model.train(np.concatenate(trainingSamplesBV.values()), np.concatenate(trainingLabelsBV.values())) | |
56 model.save(args.directoryName + "/modelBV.xml") | |
57 | |
58 print "Training Pedestrian-Cyclist Model" | |
59 model = ml.SVM(args.svmType, args.kernelType) | |
60 model.train(np.concatenate(trainingSamplesPB.values()), np.concatenate(trainingLabelsPB.values())) | |
61 model.save(args.directoryName + "/modelPB.xml") | |
62 | |
63 print "Training Pedestrian-Vehicle Model" | |
64 model = ml.SVM(args.svmType, args.kernelType) | |
65 model.train(np.concatenate(trainingSamplesPV.values()), np.concatenate(trainingLabelsPV.values())) | |
66 model.save(args.directoryName + "/modelPV.xml") |