Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 878:8e8ec4ece66e
minor + bug corrected in motion pattern learning
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 10 Mar 2017 15:31:26 -0500 |
| parents | 1535251a1f40 |
| children | 9fd7b18f75b4 |
comparison
equal
deleted
inserted
replaced
| 877:d1ff6917d082 | 878:8e8ec4ece66e |
|---|---|
| 8 import ml, utils, storage | 8 import ml, utils, storage |
| 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('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') | 13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') |
| 14 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 3) | 14 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 3) |
| 15 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) | 15 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) |
| 16 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) | 16 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) |
| 17 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance | 17 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance |
| 18 parser.add_argument('-s', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True) | 18 parser.add_argument('-s', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True) |
| 26 | 26 |
| 27 # TODO parameters (random init?) and what to learn from: objects, features, longest features from objects | 27 # TODO parameters (random init?) and what to learn from: objects, features, longest features from objects |
| 28 # TODO add possibility to cluter with velocities | 28 # TODO add possibility to cluter with velocities |
| 29 | 29 |
| 30 trajectoryType = args.trajectoryType | 30 trajectoryType = args.trajectoryType |
| 31 prototypeType = args.trajectoryType | |
| 31 if args.trajectoryType == 'objectfeatures': | 32 if args.trajectoryType == 'objectfeatures': |
| 32 trajectoryType = 'object' | 33 trajectoryType = 'object' |
| 34 prototypeType = 'feature' | |
| 33 | 35 |
| 34 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType) | 36 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType) |
| 35 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) | 37 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) |
| 36 | 38 |
| 37 if args.trajectoryType == 'objectfeatures': | 39 if args.trajectoryType == 'objectfeatures': |
| 38 features = [] | 40 features = [] |
| 39 for o in objects: | 41 for o in objects: |
| 40 o.getNLongestFeatures(args.maxNObjectFeatures) | 42 features += o.getNLongestFeatures(args.maxNObjectFeatures) |
| 41 objects = features | 43 objects = features |
| 42 | 44 |
| 43 trajectories = [o.getPositions().asArray().T for o in objects] | 45 trajectories = [o.getPositions().asArray().T for o in objects] |
| 44 | 46 |
| 45 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | 47 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
| 50 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.randomInitialization) # this line can be called again without reinitializing similarities | 52 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.randomInitialization) # this line can be called again without reinitializing similarities |
| 51 | 53 |
| 52 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 54 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
| 53 print(clusterSizes) | 55 print(clusterSizes) |
| 54 | 56 |
| 55 storage.savePrototypesToSqlite(args.databaseFilename, [objects[i].getNum() for i in prototypeIndices], args.trajectoryType, [clusterSizes[i] for i in prototypeIndices]) # if saving filenames, add for example [objects[i].dbFilename for i in prototypeIndices] | 57 storage.savePrototypesToSqlite(args.databaseFilename, [objects[i].getNum() for i in prototypeIndices], prototypeType, [clusterSizes[i] for i in prototypeIndices]) # if saving filenames, add for example [objects[i].dbFilename for i in prototypeIndices] |
| 56 | 58 |
| 57 if args.saveSimilarities: | 59 if args.saveSimilarities: |
| 58 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') | 60 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') |
| 59 | 61 |
| 60 if args.display: | 62 if args.display: |
