annotate scripts/learn-poi.py @ 844:5a68779d7777

added capability to save prototypes
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 14 Jul 2016 00:34:59 -0400
parents 181bcb6dad3a
children 6db83beb5350
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
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
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). TODO: save the data')
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')
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
14 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
15 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
16 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
17 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
18 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
19 parser.add_argument('--display', dest = 'display', help = 'display points of interests', action = 'store_true') # default is manhattan distance
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
20
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
21 args = parser.parse_args()
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 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
24
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
25 beginnings = []
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26 ends = []
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 for o in objects:
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28 beginnings.append(o.getPositionAt(0).aslist())
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 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
30
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
31 beginnings = np.array(beginnings)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
32 ends = np.array(ends)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
33
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
34 nDestinationClusters = args.nDestinationClusters
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
35 if args.nDestinationClusters is None:
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
36 nDestinationClusters = args.nOriginClusters
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
37
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
38 gmmId=0
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
39 for nClusters, points, gmmType in zip([args.nOriginClusters, nDestinationClusters],
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
40 [beginnings, ends],
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
41 ['beginning', 'end']):
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
42 # estimation
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
43 gmm = mixture.GMM(n_components=nClusters, covariance_type = args.covarianceType)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
44 model=gmm.fit(beginnings)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
45 if not model.converged_:
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
46 print('Warning: model for '+gmmType+' points did not converge')
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
47 # plot
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
48 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
49 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
50 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
51 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
52 plt.imshow(img)
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
53 labels = ml.plotGMMClusters(model, points, fig, nUnitsPerPixel = args.unitsPerPixel)
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
54 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
55 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
56 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
57 # save
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
58 storage.savePOIs(args.databaseFilename, model, gmmType, gmmId)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
59 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
60
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
61 if args.display:
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 818
diff changeset
62 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
63 plt.show()
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
64
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
65 # fig = plt.figure()
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
66 # 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
67 # img = plt.imread(args.worldImageFilename)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
68 # plt.imshow(img)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
69 # ml.plotGMMClusters(, , fig, nPixelsPerUnit = args.pixelsPerUnit)
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
70 # plt.axis('equal')
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
71 # plt.title()
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
72 # print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))