Mercurial Hosting > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 1033:8ffb3ae9f3d2
work in progress
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 20 Jun 2018 00:07:03 -0400 |
parents | d0e339359d8a |
children | 933588568bec |
comparison
equal
deleted
inserted
replaced
1032:d0e339359d8a | 1033:8ffb3ae9f3d2 |
---|---|
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 = 'saves the assignments of the objects (not for features) to the prototypes', 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 = 'assigns the objects to the prototypes and saves them (do not use min cluster size as it will discard prototypes at the beginning if the initial cluster is too small)', action = 'store_true') | 28 parser.add_argument('--assign', dest = 'assign', help = 'assigns the objects to the prototypes and saves the assignments', action = 'store_true') |
29 | 29 |
30 args = parser.parse_args() | 30 args = parser.parse_args() |
31 | 31 |
32 # use cases | 32 # use cases |
33 # 1. learn proto from one file, save in same or another | 33 # 1. learn proto from one file, save in same or another |
76 if args.learn: | 76 if args.learn: |
77 prototypeIndices = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, similarityFunc, args.optimizeCentroid, args.randomInitialization, initialPrototypeIndices) | 77 prototypeIndices = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, similarityFunc, args.optimizeCentroid, args.randomInitialization, initialPrototypeIndices) |
78 else: | 78 else: |
79 prototypeIndices = initialPrototypeIndices | 79 prototypeIndices = initialPrototypeIndices |
80 | 80 |
81 if args.assign: | 81 if args.assign: # TODO don't touch initial prototypes if not from same db as trajectories |
82 if not args.learn and args.minClusterSize >= 1: | 82 #if not args.learn and args.minClusterSize >= 1: # allow only |
83 print('Warning: you did not learn the prototypes and you are using minimum cluster size of {}, which may lead to removing prototypes and assigning them to others'.format(args.minClusterSize)) | 83 # print('Warning: you did not learn the prototypes and you are using minimum cluster size of {}, which may lead to removing prototypes and assigning them to others'.format(args.minClusterSize)) |
84 prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc, args.minClusterSize) | 84 # if args.minClusterSize >= 1: |
85 # if initialPrototypeIndices is None: | |
86 # prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc, args.minClusterSize) | |
87 # else: | |
88 # print('Not assigning with non-zero minimum cluster size and initial prototypes (would remove initial prototypes based on other trajectories') | |
89 # else: | |
90 # prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc) | |
91 prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc) | |
85 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 92 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
86 print(clusterSizes) | 93 print(clusterSizes) |
87 | 94 |
88 if args.learn or args.assign: | 95 if args.learn and not args.assign: |
89 prototypes = [] | 96 prototypes = [] |
90 for i in prototypeIndices: | 97 for i in prototypeIndices: |
91 if args.assign: | |
92 nMatchings = clusterSizes[i]-1 | |
93 #else: | |
94 # nMatchings = 0 | |
95 if i<len(initialPrototypes): | 98 if i<len(initialPrototypes): |
96 if args.assign: | |
97 initialPrototypes[i].nMatchings += nMatchings | |
98 prototypes.append(initialPrototypes[i]) | 99 prototypes.append(initialPrototypes[i]) |
99 else: | 100 else: |
100 prototypes.append(moving.Prototype(args.databaseFilename, objects[i-len(initialPrototypes)].getNum(), trajectoryType, nMatchings)) | 101 prototypes.append(moving.Prototype(args.databaseFilename, objects[i-len(initialPrototypes)].getNum(), trajectoryType)) |
101 | 102 |
102 if args.outputPrototypeDatabaseFilename is None: | 103 if args.outputPrototypeDatabaseFilename is None: |
103 outputPrototypeDatabaseFilename = args.databaseFilename | 104 outputPrototypeDatabaseFilename = args.databaseFilename |
104 else: | 105 else: |
105 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | 106 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
106 if args.inputPrototypeDatabaseFilename == args.outputPrototypeDatabaseFilename: | 107 if args.inputPrototypeDatabaseFilename == args.outputPrototypeDatabaseFilename: |
107 storage.deleteFromSqlite(args.outputPrototypeDatabaseFilename, 'prototype') | 108 storage.deleteFromSqlite(args.outputPrototypeDatabaseFilename, 'prototype') |
108 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) | 109 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) |
109 | 110 |
110 if args.saveSimilarities: | 111 if not args.learn and args.assign: # no new prototypes # not save assignments of past prototypes if removes with minClusterSize |
111 # todo save trajectories and prototypes | 112 prototypes = [] |
112 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') | 113 for i in prototypeIndices: |
114 nMatchings = clusterSizes[i]-1 | |
115 if initialPrototypes[i].nMatchings is None: | |
116 initialPrototypes[i].nMatchings = nMatchings | |
117 else: | |
118 initialPrototypes[i].nMatchings += nMatchings | |
119 prototypes.append(initialPrototypes[i]) | |
120 if args.outputPrototypeDatabaseFilename is None: | |
121 outputPrototypeDatabaseFilename = args.databaseFilename | |
122 else: | |
123 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | |
124 storage.setPrototypeMatchingsInSqlite(outputPrototypeDatabaseFilename, prototypes) | |
113 | 125 |
114 labelsToProtoIndices = {protoId: i for i, protoId in enumerate(prototypeIndices)} | 126 labelsToProtoIndices = {protoId: i for i, protoId in enumerate(prototypeIndices)} |
115 if args.assign and args.saveMatches: | 127 if args.saveMatches: |
116 storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objects, trajectoryType, [labelsToProtoIndices[l] for l in labels], prototypes) | 128 storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objects, trajectoryType, [labelsToProtoIndices[l] for l in labels], prototypes) |
117 | 129 |
118 if args.display and args.assign: | 130 if (args.learn or args.assign) and args.saveSimilarities: |
119 from matplotlib.pyplot import figure, show, axis | 131 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') |
120 figure() | 132 |
121 for i,o in enumerate(objects): | 133 if args.display and args.assign: |
122 if i not in prototypeIndices: | 134 from matplotlib.pyplot import figure, show, axis |
123 if labels[i] < 0: | 135 figure() |
124 o.plot('kx') | 136 for i,o in enumerate(objects): |
125 else: | 137 if i not in prototypeIndices: |
126 o.plot(utils.colors[labels[i]]) | 138 if labels[i] < 0: |
127 for i in prototypeIndices: | 139 o.plot('kx') |
128 objects[i].plot(utils.colors[i]+'o') | 140 else: |
129 axis('equal') | 141 o.plot(utils.colors[labels[i]]) |
130 show() | 142 for i in prototypeIndices: |
131 else: | 143 objects[i].plot(utils.colors[i]+'o') |
132 print('Not learning nor assigning: doing nothing') | 144 axis('equal') |
145 show() |