Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 927:c030f735c594
added assignment of trajectories to prototypes and cleanup of insert queries
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 11 Jul 2017 17:56:23 -0400 |
| parents | acb5379c5fd7 |
| children | 584b9405e494 |
comparison
equal
deleted
inserted
replaced
| 926:dbd81710d515 | 927:c030f735c594 |
|---|---|
| 22 parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true') | 22 parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true') |
| 23 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') | 23 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') |
| 24 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) | 24 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) |
| 25 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') | 25 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') |
| 26 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') | 26 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') |
| 27 #parser.add_argument('--save-matches', dest = 'saveMatches', help = 'save the matched prototype information', action = 'store_true') | 27 parser.add_argument('--save-matches', dest = 'saveMatches', help = 'saves the assignments of the objects (not for features) to the prototypes', action = 'store_true') |
| 28 #parser.add_argument('--assign', dest = 'assign', help = 'saves the assignments of the objects (not for features) to the prototypes', action = 'store_true') # default is train, but one could want only to assign the objects to the loaded prototypes without learning | |
| 28 | 29 |
| 29 args = parser.parse_args() | 30 args = parser.parse_args() |
| 30 | 31 |
| 31 # use cases | 32 # use cases |
| 32 # 1. learn proto from one file, save in same or another (with traj) | 33 # 1. learn proto from one file, save in same or another (with traj) |
| 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 | 69 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 |
| 69 | 70 |
| 70 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 71 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
| 71 print(clusterSizes) | 72 print(clusterSizes) |
| 72 | 73 |
| 73 | |
| 74 prototypes = [moving.Prototype(args.databaseFilename, objects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices] | 74 prototypes = [moving.Prototype(args.databaseFilename, objects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices] |
| 75 if args.outputPrototypeDatabaseFilename is None: | 75 if args.outputPrototypeDatabaseFilename is None: |
| 76 outputPrototypeDatabaseFilename = args.databaseFilename | 76 outputPrototypeDatabaseFilename = args.databaseFilename |
| 77 else: | 77 else: |
| 78 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | 78 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
| 79 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) | 79 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) |
| 80 | 80 |
| 81 if args.saveSimilarities: | 81 if args.saveSimilarities: |
| 82 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') |
| 83 | 83 |
| 84 # if args.saveMatches: | 84 labelsToProtoIndices = {protoId: i for i, protoId in enumerate(prototypeIndices)} |
| 85 # out = storage.openCheck(utils.removeExtension(args.databaseFilename)+'prototypes-matches.csv', 'w') | 85 if args.saveMatches: # or args.assign |
| 86 # for o in ojbects: | 86 # save in the db that contained originally the data |
| 87 # out.write('') | 87 # retirer les assignations anterieures? |
| 88 storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objects, [labelsToProtoIndices[l] for l in labels], prototypes) | |
| 88 | 89 |
| 89 if args.display: | 90 if args.display: |
| 90 from matplotlib.pyplot import figure, show, axis | 91 from matplotlib.pyplot import figure, show, axis |
| 91 figure() | 92 figure() |
| 92 for i,o in enumerate(objects): | 93 for i,o in enumerate(objects): |
