annotate scripts/learn-motion-patterns.py @ 917:89cc05867c4c

reorg and work in progress
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 04 Jul 2017 18:00:01 -0400
parents b58a1061a717
children 7b3f2e0a2652
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
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 matplotlib.pyplot as plt
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 import numpy as np
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8 import ml, utils, storage
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 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
11 #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
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True)
878
8e8ec4ece66e minor + bug corrected in motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 870
diff changeset
13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures')
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 735
diff changeset
14 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 3)
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
15 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
16 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
17 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
18 parser.add_argument('-s', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True)
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
19 parser.add_argument('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = None)
908
b297525b2cbf added options to the prototype cluster algorithm, work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 907
diff changeset
20 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
21 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
22 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
23 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
24 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true')
907
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
25 #parser.add_argument('--save-matches', dest = 'saveMatches', help = 'save the matched prototype information', action = 'store_true')
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 args = parser.parse_args()
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28
907
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
29 # use cases
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
30 # 1. learn proto from one file, save in same or another (with traj)
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
31 # 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
32 # 3. assign objects from one db to proto
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
33 # 4. load objects from several files, save in another
917
89cc05867c4c reorg and work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 910
diff changeset
34 # 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
35
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
36 # TODO add possibility to cluter with velocities
907
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
37 # TODO add possibility to start with saved prototypes so that one can incrementally learn from several databases
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
38 # save prototypes with database name, add option to keep trajectory along: if saved in same db, no need
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
39 # load proto must load the movingobject
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
40 # save the objects that match the prototypes
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
41 # write an assignment function for objects
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
42
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
43 trajectoryType = args.trajectoryType
878
8e8ec4ece66e minor + bug corrected in motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 870
diff changeset
44 prototypeType = args.trajectoryType
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
45 if args.trajectoryType == 'objectfeatures':
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
46 trajectoryType = 'object'
878
8e8ec4ece66e minor + bug corrected in motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 870
diff changeset
47 prototypeType = 'feature'
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
48
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
49 if args.trajectoryType == 'objectfeatures':
910
b58a1061a717 loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 908
diff changeset
50 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
51 featureNumbers = []
b58a1061a717 loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 908
diff changeset
52 for numbers in objectFeatureNumbers.values():
b58a1061a717 loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 908
diff changeset
53 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
54 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
55 else:
b58a1061a717 loading is faster for longest object features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 908
diff changeset
56 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate)
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
58 trajectories = [o.getPositions().asArray().T for o in objects]
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
59
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
60 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
61 nTrajectories = len(trajectories)
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
62
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
63 similarities = -np.ones((nTrajectories, nTrajectories))
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
64
908
b297525b2cbf added options to the prototype cluster algorithm, work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 907
diff changeset
65 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
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 843
diff changeset
67 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1)
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 843
diff changeset
68 print(clusterSizes)
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 843
diff changeset
69
878
8e8ec4ece66e minor + bug corrected in motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 870
diff changeset
70 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]
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 735
diff changeset
71
843
5dc7a507353e updated to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 835
diff changeset
72 if args.saveSimilarities:
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 843
diff changeset
73 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f')
843
5dc7a507353e updated to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 835
diff changeset
74
907
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
75 # if args.saveMatches:
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
76 # out = storage.openCheck(utils.removeExtension(args.databaseFilename)+'prototypes-matches.csv', 'w')
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
77 # for o in ojbects:
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
78 # out.write('')
9fd7b18f75b4 re arranged motion pattern learning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 878
diff changeset
79
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
80 if args.display:
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 843
diff changeset
81 from matplotlib.pyplot import figure, show, axis
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
82 figure()
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
83 for i,o in enumerate(objects):
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
84 if i not in prototypeIndices:
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
85 if labels[i] < 0:
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
86 o.plot('kx')
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
87 else:
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
88 o.plot(utils.colors[labels[i]])
734
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
89 for i in prototypeIndices:
1d4dcb5c8708 first example script to learn prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
90 objects[i].plot(utils.colors[i]+'o')
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 843
diff changeset
91 axis('equal')
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 735
diff changeset
92 show()