Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 907:9fd7b18f75b4
re arranged motion pattern learning
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 23 Jun 2017 23:50:02 -0400 |
| parents | 8e8ec4ece66e |
| children | b297525b2cbf |
comparison
equal
deleted
inserted
replaced
| 906:a57e6fbcd8e3 | 907:9fd7b18f75b4 |
|---|---|
| 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) |
| 19 parser.add_argument('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = None) | 19 parser.add_argument('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = None) |
| 20 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') | 20 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') |
| 21 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int, default = None) | 21 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) |
| 22 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') | 22 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') |
| 23 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') | 23 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') |
| 24 #parser.add_argument('--save-matches', dest = 'saveMatches', help = 'save the matched prototype information', action = 'store_true') | |
| 24 | 25 |
| 25 args = parser.parse_args() | 26 args = parser.parse_args() |
| 26 | 27 |
| 27 # TODO parameters (random init?) and what to learn from: objects, features, longest features from objects | 28 # use cases |
| 29 # 1. learn proto from one file, save in same or another (with traj) | |
| 30 # 2. load proto, load objects, update proto, save proto | |
| 31 # 3. assign objects from one db to proto | |
| 32 # 4. load objects from several files, save in another | |
| 33 | |
| 28 # TODO add possibility to cluter with velocities | 34 # TODO add possibility to cluter with velocities |
| 35 # TODO add possibility to start with saved prototypes so that one can incrementally learn from several databases | |
| 36 # save prototypes with database name, add option to keep trajectory along: if saved in same db, no need | |
| 37 # load proto must load the movingobject | |
| 38 # save the objects that match the prototypes | |
| 39 # write an assignment function for objects | |
| 29 | 40 |
| 30 trajectoryType = args.trajectoryType | 41 trajectoryType = args.trajectoryType |
| 31 prototypeType = args.trajectoryType | 42 prototypeType = args.trajectoryType |
| 32 if args.trajectoryType == 'objectfeatures': | 43 if args.trajectoryType == 'objectfeatures': |
| 33 trajectoryType = 'object' | 44 trajectoryType = 'object' |
| 47 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | 58 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
| 48 nTrajectories = len(trajectories) | 59 nTrajectories = len(trajectories) |
| 49 | 60 |
| 50 similarities = -np.ones((nTrajectories, nTrajectories)) | 61 similarities = -np.ones((nTrajectories, nTrajectories)) |
| 51 | 62 |
| 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 | 63 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize, args.randomInitialization, True, None) # this line can be called again without reinitializing similarities |
| 53 | 64 |
| 54 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 65 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
| 55 print(clusterSizes) | 66 print(clusterSizes) |
| 56 | 67 |
| 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] | 68 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] |
| 58 | 69 |
| 59 if args.saveSimilarities: | 70 if args.saveSimilarities: |
| 60 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') | 71 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') |
| 72 | |
| 73 # if args.saveMatches: | |
| 74 # out = storage.openCheck(utils.removeExtension(args.databaseFilename)+'prototypes-matches.csv', 'w') | |
| 75 # for o in ojbects: | |
| 76 # out.write('') | |
| 61 | 77 |
| 62 if args.display: | 78 if args.display: |
| 63 from matplotlib.pyplot import figure, show, axis | 79 from matplotlib.pyplot import figure, show, axis |
| 64 figure() | 80 figure() |
| 65 for i,o in enumerate(objects): | 81 for i,o in enumerate(objects): |
| 70 o.plot(utils.colors[labels[i]]) | 86 o.plot(utils.colors[labels[i]]) |
| 71 for i in prototypeIndices: | 87 for i in prototypeIndices: |
| 72 objects[i].plot(utils.colors[i]+'o') | 88 objects[i].plot(utils.colors[i]+'o') |
| 73 axis('equal') | 89 axis('equal') |
| 74 show() | 90 show() |
| 75 | |
| 76 # TODO store the prototypes trajectories, add option so store similarities (the most expensive stuff) with limited accuracy |
