Mercurial Hosting > traffic-intelligence
annotate 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 |
rev | line source |
---|---|
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
980
diff
changeset
|
1 #! /usr/bin/env python3 |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 import sys, argparse |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
4 |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
5 import numpy as np |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
6 |
1028
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
7 from trafficintelligence import ml, utils, storage, moving |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
8 |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
9 parser = argparse.ArgumentParser(description='The program learns prototypes for the motion patterns') #, epilog = '' |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
10 #parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
11 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) |
921
630934595871
work in progress with prototype class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
920
diff
changeset
|
12 parser.add_argument('-o', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes') |
630934595871
work in progress with prototype class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
920
diff
changeset
|
13 parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') |
878
8e8ec4ece66e
minor + bug corrected in motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
870
diff
changeset
|
14 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') |
920
499154254f37
improved prototype loading
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
919
diff
changeset
|
15 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1) |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
17 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
18 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
19 parser.add_argument('-s', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True) |
953
989917b1ed85
assign and learn work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
952
diff
changeset
|
20 parser.add_argument('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0) |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
21 parser.add_argument('--learn', dest = 'learn', help = 'learn', action = 'store_true') |
908
b297525b2cbf
added options to the prototype cluster algorithm, work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
907
diff
changeset
|
22 parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true') |
843
5dc7a507353e
updated to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
835
diff
changeset
|
23 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') |
907
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
24 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) |
843
5dc7a507353e
updated to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
835
diff
changeset
|
25 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') |
5dc7a507353e
updated to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
835
diff
changeset
|
26 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') |
927
c030f735c594
added assignment of trajectories to prototypes and cleanup of insert queries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
922
diff
changeset
|
27 parser.add_argument('--save-matches', dest = 'saveMatches', help = 'saves the assignments of the objects (not for features) to the prototypes', action = 'store_true') |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
28 parser.add_argument('--assign', dest = 'assign', help = 'assigns the objects to the prototypes and saves the assignments', action = 'store_true') |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
29 |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
30 args = parser.parse_args() |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
31 |
907
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
32 # use cases |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
33 # 1. learn proto from one file, save in same or another |
907
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
34 # 2. load proto, load objects, update proto, save proto |
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
35 # 3. assign objects from one db to proto |
920
499154254f37
improved prototype loading
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
919
diff
changeset
|
36 # 4. load objects from several files, save in another -> see metadata: site with view and times |
917
89cc05867c4c
reorg and work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
910
diff
changeset
|
37 # 5. keep prototypes, with positions/velocities, in separate db (keep link to original data through filename, type and index) |
907
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
38 |
948
584b9405e494
added safety analysis parameters for motion patterns
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
927
diff
changeset
|
39 # TODO add possibility to cluster with velocities |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
40 # TODO add possibilite to load all trajectories and use minclustersize |
907
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
41 # save the objects that match the prototypes |
9fd7b18f75b4
re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
878
diff
changeset
|
42 # write an assignment function for objects |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
43 |
979
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
44 # load trajectories to cluster or assign |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
45 if args.trajectoryType == 'objectfeatures': |
979
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
46 trajectoryType = 'feature' |
910
b58a1061a717
loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
908
diff
changeset
|
47 objectFeatureNumbers = storage.loadObjectFeatureFrameNumbers(args.databaseFilename, objectNumbers = args.nTrajectories) |
b58a1061a717
loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
908
diff
changeset
|
48 featureNumbers = [] |
b58a1061a717
loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
908
diff
changeset
|
49 for numbers in objectFeatureNumbers.values(): |
b58a1061a717
loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
908
diff
changeset
|
50 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] |
b58a1061a717
loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
908
diff
changeset
|
51 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) |
b58a1061a717
loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
908
diff
changeset
|
52 else: |
979
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
53 trajectoryType = args.trajectoryType |
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
54 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
55 |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
56 trajectories = [o.getPositions().asArray().T for o in objects] |
979
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
57 |
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
58 # load initial prototypes, if any |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
59 if args.inputPrototypeDatabaseFilename is not None: |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
60 initialPrototypes = storage.loadPrototypesFromSqlite(args.inputPrototypeDatabaseFilename, True) |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
61 trajectories = [p.getMovingObject().getPositions().asArray().T for p in initialPrototypes]+trajectories |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
62 if len(initialPrototypes) > 0: |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
980
diff
changeset
|
63 initialPrototypeIndices = list(range(len(initialPrototypes))) |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
64 else: |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
65 initialPrototypeIndices = None |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
66 else: |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
67 initialPrototypes = [] |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
68 initialPrototypeIndices = None |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
69 |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
70 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
71 nTrajectories = len(trajectories) |
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
72 |
735
0e875a7f5759
modified prototypeCluster algorithm to enforce similarity when re-assigning and to compute only the necessary similarities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
734
diff
changeset
|
73 similarities = -np.ones((nTrajectories, nTrajectories)) |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
74 similarityFunc = lambda x,y : lcss.computeNormalized(x, y) |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
75 # the next line can be called again without reinitializing similarities |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
76 if args.learn: |
953
989917b1ed85
assign and learn work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
952
diff
changeset
|
77 prototypeIndices = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, similarityFunc, args.optimizeCentroid, args.randomInitialization, initialPrototypeIndices) |
989917b1ed85
assign and learn work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
952
diff
changeset
|
78 else: |
989917b1ed85
assign and learn work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
952
diff
changeset
|
79 prototypeIndices = initialPrototypeIndices |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
80 |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
81 if args.assign: # TODO don't touch initial prototypes if not from same db as trajectories |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
82 #if not args.learn and args.minClusterSize >= 1: # allow only |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
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)) |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
84 # if args.minClusterSize >= 1: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
85 # if initialPrototypeIndices is None: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
86 # prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc, args.minClusterSize) |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
87 # else: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
88 # print('Not assigning with non-zero minimum cluster size and initial prototypes (would remove initial prototypes based on other trajectories') |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
89 # else: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
90 # prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc) |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
91 prototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, args.minSimilarity, similarityFunc) |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
92 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
93 print(clusterSizes) |
734
1d4dcb5c8708
first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
94 |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
95 if args.learn and not args.assign: |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
96 prototypes = [] |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
97 for i in prototypeIndices: |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
98 if i<len(initialPrototypes): |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
99 prototypes.append(initialPrototypes[i]) |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
100 else: |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
101 prototypes.append(moving.Prototype(args.databaseFilename, objects[i-len(initialPrototypes)].getNum(), trajectoryType)) |
844
5a68779d7777
added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
843
diff
changeset
|
102 |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
103 if args.outputPrototypeDatabaseFilename is None: |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
104 outputPrototypeDatabaseFilename = args.databaseFilename |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
948
diff
changeset
|
105 else: |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
106 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
107 if args.inputPrototypeDatabaseFilename == args.outputPrototypeDatabaseFilename: |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
108 storage.deleteFromSqlite(args.outputPrototypeDatabaseFilename, 'prototype') |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
109 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, prototypes) |
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
110 |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
111 if not args.learn and args.assign: # no new prototypes # not save assignments of past prototypes if removes with minClusterSize |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
112 prototypes = [] |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
113 for i in prototypeIndices: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
114 nMatchings = clusterSizes[i]-1 |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
115 if initialPrototypes[i].nMatchings is None: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
116 initialPrototypes[i].nMatchings = nMatchings |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
117 else: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
118 initialPrototypes[i].nMatchings += nMatchings |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
119 prototypes.append(initialPrototypes[i]) |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
120 if args.outputPrototypeDatabaseFilename is None: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
121 outputPrototypeDatabaseFilename = args.databaseFilename |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
122 else: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
123 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
124 storage.setPrototypeMatchingsInSqlite(outputPrototypeDatabaseFilename, prototypes) |
951 | 125 |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
126 labelsToProtoIndices = {protoId: i for i, protoId in enumerate(prototypeIndices)} |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
127 if args.saveMatches: |
979
cc89267b5ff9
work on learning and assigning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
953
diff
changeset
|
128 storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objects, trajectoryType, [labelsToProtoIndices[l] for l in labels], prototypes) |
952
a9b2beef0db4
loading and assigning motion patterns works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
951
diff
changeset
|
129 |
1033
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
130 if (args.learn or args.assign) and args.saveSimilarities: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
131 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
132 |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
133 if args.display and args.assign: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
134 from matplotlib.pyplot import figure, show, axis |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
135 figure() |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
136 for i,o in enumerate(objects): |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
137 if i not in prototypeIndices: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
138 if labels[i] < 0: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
139 o.plot('kx') |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
140 else: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
141 o.plot(utils.colors[labels[i]]) |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
142 for i in prototypeIndices: |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
143 objects[i].plot(utils.colors[i]+'o') |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
144 axis('equal') |
8ffb3ae9f3d2
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1032
diff
changeset
|
145 show() |