annotate scripts/learn-poi.py @ 914:f228fd649644

corrected bugs in learn-pois.py
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 28 Jun 2017 23:43:52 -0400
parents 1cd878812529
children 13434f5017dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 import argparse
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5 import numpy as np
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 from sklearn import mixture
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7 import matplotlib.pyplot as plt
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9 import storage, ml
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
11 parser = argparse.ArgumentParser(description='The program learns and displays Gaussians fit to beginnings and ends of object trajectories (based on Mohamed Gomaa Mohamed 2015 PhD).')
786
1f2b2d1f4fbf added script and code to learn POIs
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)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object')
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
14 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to display', type = int)
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
15 parser.add_argument('-norigins', dest = 'nOriginClusters', help = 'number of clusters for trajectory origins', required = True, type = int)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
16 parser.add_argument('-ndestinations', dest = 'nDestinationClusters', help = 'number of clusters for trajectory destinations (=norigins if not provided)', type = int)
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
17 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full")
787
0a428b449b80 improved script to display over world image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 786
diff changeset
18 parser.add_argument('-w', dest = 'worldImageFilename', help = 'filename of the world image')
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
19 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units of distance per pixel', type = float, default = 1.)
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
20 parser.add_argument('--display', dest = 'display', help = 'display points of interests', action = 'store_true') # default is manhattan distance
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
21 parser.add_argument('--assign', dest = 'assign', help = 'display points of interests', action = 'store_true')
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
22
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
23 args = parser.parse_args()
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
24
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
25 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType, args.nObjects)
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 beginnings = []
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28 ends = []
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 for o in objects:
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
30 beginnings.append(o.getPositionAt(0).aslist())
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
31 ends.append(o.getPositionAt(int(o.length())-1).aslist())
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
32
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
33 beginnings = np.array(beginnings)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
34 ends = np.array(ends)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
35
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
36 nDestinationClusters = args.nDestinationClusters
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
37 if args.nDestinationClusters is None:
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
38 nDestinationClusters = args.nOriginClusters
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
39
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
40 gmmId=0
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
41 for nClusters, points, gmmType in zip([args.nOriginClusters, nDestinationClusters],
913
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
42 [beginnings, ends],
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
43 ['beginning', 'end']):
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
44 # estimation
871
6db83beb5350 work in progress to update gaussian mixtures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 844
diff changeset
45 gmm = mixture.GaussianMixture(n_components=nClusters, covariance_type = args.covarianceType)
913
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
46 model=gmm.fit(points)
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
47 if not model.converged_:
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
48 print('Warning: model for '+gmmType+' points did not converge')
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
49 if args.display or args.assign:
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
50 labels = model.predict(points)
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
51 # plot
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
52 if args.display:
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
53 fig = plt.figure()
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
54 if args.worldImageFilename is not None and args.unitsPerPixel is not None:
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
55 img = plt.imread(args.worldImageFilename)
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
56 plt.imshow(img)
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
57 ml.plotGMMClusters(model, labels, points, fig, nUnitsPerPixel = args.unitsPerPixel)
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
58 plt.axis('image')
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
59 plt.title(gmmType)
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
60 print(gmmType+' Clusters:\n{}'.format(ml.computeClusterSizes(labels, range(model.n_components))))
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
61 # save
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
62 storage.savePOIs(args.databaseFilename, model, gmmType, gmmId)
913
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
63 # save assignments
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
64 if args.assign:
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
65 pass # savePOIAssignments(
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
66 gmmId += 1
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
67
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
68 if args.display:
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 818
diff changeset
69 plt.axis('equal')
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
70 plt.show()
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
71
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
72 # fig = plt.figure()
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
73 # if args.worldImageFilename is not None and args.pixelsPerUnit is not None:
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
74 # img = plt.imread(args.worldImageFilename)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
75 # plt.imshow(img)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
76 # ml.plotGMMClusters(, , fig, nPixelsPerUnit = args.pixelsPerUnit)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
77 # plt.axis('equal')
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
78 # plt.title()
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
79 # print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))