Mercurial Hosting > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 921:630934595871
work in progress with prototype class
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 05 Jul 2017 18:01:43 -0400 |
parents | 499154254f37 |
children | acb5379c5fd7 |
comparison
equal
deleted
inserted
replaced
920:499154254f37 | 921:630934595871 |
---|---|
3 import sys, argparse | 3 import sys, argparse |
4 | 4 |
5 #import matplotlib.pyplot as plt | 5 #import matplotlib.pyplot as plt |
6 import numpy as np | 6 import numpy as np |
7 | 7 |
8 import ml, utils, storage | 8 import ml, utils, storage, moving |
9 | 9 |
10 parser = argparse.ArgumentParser(description='The program learns prototypes for the motion patterns') #, epilog = '' | 10 parser = argparse.ArgumentParser(description='The program learns prototypes for the motion patterns') #, epilog = '' |
11 #parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') | 11 #parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') |
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) | 12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) |
13 parser.add_argument('-r', dest = 'initialPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') | 13 parser.add_argument('-o', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes') |
14 parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') | |
14 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') | 15 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') |
15 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1) | 16 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1) |
16 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) | 17 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) |
17 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) | 18 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) |
18 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance | 19 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance |
32 # 2. load proto, load objects, update proto, save proto | 33 # 2. load proto, load objects, update proto, save proto |
33 # 3. assign objects from one db to proto | 34 # 3. assign objects from one db to proto |
34 # 4. load objects from several files, save in another -> see metadata: site with view and times | 35 # 4. load objects from several files, save in another -> see metadata: site with view and times |
35 # 5. keep prototypes, with positions/velocities, in separate db (keep link to original data through filename, type and index) | 36 # 5. keep prototypes, with positions/velocities, in separate db (keep link to original data through filename, type and index) |
36 | 37 |
37 # TODO add possibility to cluter with velocities | 38 # TODO add possibility to clutesr with velocities |
38 # TODO add possibility to start with saved prototypes so that one can incrementally learn from several databases | 39 # TODO add possibility to start with saved prototypes so that one can incrementally learn from several databases |
39 # save the objects that match the prototypes | 40 # save the objects that match the prototypes |
40 # write an assignment function for objects | 41 # write an assignment function for objects |
41 | 42 |
42 trajectoryType = args.trajectoryType | 43 trajectoryType = args.trajectoryType |
52 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] | 53 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] |
53 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) | 54 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) |
54 else: | 55 else: |
55 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) | 56 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) |
56 | 57 |
58 if args.inputPrototypeDatabaseFilename is not None: | |
59 prototypeIndices, dbFilenames, trajectoryTypes, nMatchings, prototypes = storage.loadPrototypesFromSqlite(args.inputPrototypeDatabaseFilename, True) | |
60 | |
57 trajectories = [o.getPositions().asArray().T for o in objects] | 61 trajectories = [o.getPositions().asArray().T for o in objects] |
58 | 62 |
59 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | 63 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
60 nTrajectories = len(trajectories) | 64 nTrajectories = len(trajectories) |
61 | 65 |
64 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, None) # this line can be called again without reinitializing similarities | 68 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, None) # this line can be called again without reinitializing similarities |
65 | 69 |
66 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 70 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
67 print(clusterSizes) | 71 print(clusterSizes) |
68 | 72 |
69 prototypes = [objects[i] for i in prototypeIndices] | 73 |
70 storage.savePrototypesToSqlite(args.databaseFilename, [p.getNum() for p in prototypes], prototypeType, [clusterSizes[i] for i in prototypeIndices]) # if saving filenames, add for example [objects[i].dbFilename for i in prototypeIndices] | 74 prototypes = [moving.Prototype(objects[i].getNum(), args.databaseFilename, prototypeType, clusterSizes[i]) for i in prototypeIndices] |
75 if args.outputPrototypeDatabaseFilename is None: | |
76 outputPrototypeDatabaseFilename = args.databaseFilename | |
77 else: | |
78 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | |
79 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) | |
71 | 80 |
72 if args.saveSimilarities: | 81 if args.saveSimilarities: |
73 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') | 82 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') |
74 | 83 |
75 # if args.saveMatches: | 84 # if args.saveMatches: |