annotate scripts/learn-poi.py @ 1240:bb14f919d1cb

cleaned use of centile (np only) and added info in classify-objects
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 05 Feb 2024 14:14:14 -0500
parents cc5cb04b04b0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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: 927
diff changeset
1 #! /usr/bin/env python3
786
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
1028
cc5cb04b04b0 major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
9 from trafficintelligence import storage, ml
786
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.)
916
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
20 parser.add_argument('--display', dest = 'display', help = 'displays points of interests', action = 'store_true') # default is manhattan distance
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
21 parser.add_argument('--assign', dest = 'assign', help = 'assigns the trajectories to the POIs and saves the assignments', action = 'store_true')
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
22 parser.add_argument('--display-paths', dest = 'displayPaths', help = 'displays all possible origin destination if assignment is done', action = 'store_true')
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
23
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
24 # TODO test Variational Bayesian Gaussian Mixture BayesianGaussianMixture
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
25
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26 args = parser.parse_args()
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27
914
f228fd649644 corrected bugs in learn-pois.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 913
diff changeset
28 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
29
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
30 beginnings = []
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
31 ends = []
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
32 for o in objects:
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
33 beginnings.append(o.getPositionAt(0).aslist())
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
34 ends.append(o.getPositionAt(int(o.length())-1).aslist())
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
35 if args.assign:
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
36 o.od = [-1, -1]
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
37
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
38 beginnings = np.array(beginnings)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
39 ends = np.array(ends)
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
40
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
41 nDestinationClusters = args.nDestinationClusters
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
42 if args.nDestinationClusters is None:
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
43 nDestinationClusters = args.nOriginClusters
786
1f2b2d1f4fbf added script and code to learn POIs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
44
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
45 gmmId=0
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
46 models = {}
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
47 for nClusters, points, gmmType in zip([args.nOriginClusters, nDestinationClusters],
913
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
48 [beginnings, ends],
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
49 ['beginning', 'end']):
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
50 # estimation
871
6db83beb5350 work in progress to update gaussian mixtures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 844
diff changeset
51 gmm = mixture.GaussianMixture(n_components=nClusters, covariance_type = args.covarianceType)
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
52 models[gmmType]=gmm.fit(points)
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
53 if not models[gmmType].converged_:
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
54 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
55 if args.display or args.assign:
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
56 labels = models[gmmType].predict(points)
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
57 # plot
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
58 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
59 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
60 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
61 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
62 plt.imshow(img)
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
63 ml.plotGMMClusters(models[gmmType], 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
64 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
65 plt.title(gmmType)
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
66 print(gmmType+' Clusters:\n{}'.format(ml.computeClusterSizes(labels, range(models[gmmType].n_components))))
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
67 # save
927
c030f735c594 added assignment of trajectories to prototypes and cleanup of insert queries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 916
diff changeset
68 storage.savePOIsToSqlite(args.databaseFilename, models[gmmType], gmmType, gmmId)
913
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
69 # save assignments
1cd878812529 work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 871
diff changeset
70 if args.assign:
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
71 for o, l in zip(objects, labels):
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
72 if gmmType == 'beginning':
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
73 o.od[0] = l
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
74 elif gmmType == 'end':
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
75 o.od[1] = l
805
180b6b0231c0 added saving/loading points of interests
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 787
diff changeset
76 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
77
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
78 if args.assign:
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
79 storage.savePOIAssignments(args.databaseFilename, objects)
916
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
80 if args.displayPaths:
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: 927
diff changeset
81 for i in range(args.nOriginClusters):
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: 927
diff changeset
82 for j in range(args.nDestinationClusters):
916
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
83 odObjects = [o for o in objects if o.od[0] == i and o.od[1] == j]
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
84 if len(odObjects) > 0:
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
85 fig = plt.figure()
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
86 ax = fig.add_subplot(111)
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
87 ml.plotGMM(models['beginning'].means_[i], models['beginning'].covariances_[i], i, fig, 'b')
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
88 ml.plotGMM(models['end'].means_[j], models['end'].covariances_[j], j, fig, 'r')
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
89 for o in odObjects:
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
90 o.plot(withOrigin = True)
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
91 plt.title('OD {} to {}'.format(i,j))
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
92 plt.axis('equal')
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
93 plt.show()
7345f0d51faa added display of paths
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 915
diff changeset
94
915
13434f5017dd work to save trajectory assignment to origin and destinations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 914
diff changeset
95
818
181bcb6dad3a added option to learn motion patterns and show to display results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 805
diff changeset
96 if args.display:
844
5a68779d7777 added capability to save prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 818
diff changeset
97 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
98 plt.show()