Mercurial Hosting > traffic-intelligence
changeset 1043:b735895c8815
work in progress on process (learn motion patterns)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 04 Jul 2018 17:39:39 -0400 |
parents | b1ba6d44fcb9 |
children | 75a6ad604cc5 |
files | scripts/learn-motion-patterns.py scripts/process.py |
diffstat | 2 files changed, 52 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/learn-motion-patterns.py Wed Jul 04 16:21:09 2018 -0400 +++ b/scripts/learn-motion-patterns.py Wed Jul 04 17:39:39 2018 -0400 @@ -12,7 +12,7 @@ parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) parser.add_argument('-o', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes') parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') -parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeature', 'feature', 'object'], default = 'objectfeatures') +parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeature', 'feature', 'object'], default = 'objectfeature') parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1) parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) @@ -24,6 +24,7 @@ parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') +parser.add_argument('--similarities-filename', dest = 'similaritiesFilename', help = 'filename of the similarities') parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') parser.add_argument('--save-assignments', dest = 'saveAssignments', help = 'saves the assignments of the objects to the prototypes', action = 'store_true') parser.add_argument('--assign', dest = 'assign', help = 'assigns the objects to the prototypes and saves the assignments', action = 'store_true') @@ -40,17 +41,7 @@ # TODO add possibility to load all trajectories and use minclustersize # load trajectories to cluster or assign -if args.trajectoryType == 'objectfeature': - trajectoryType = 'feature' - objectFeatureNumbers = storage.loadObjectFeatureFrameNumbers(args.databaseFilename, objectNumbers = args.nTrajectories) - featureNumbers = [] - for numbers in objectFeatureNumbers.values(): - featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] - objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) -else: - trajectoryType = args.trajectoryType - objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) - +objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType, args.nTrajectories, timeStep = args.positionSubsamplingRate) trajectories = [o.getPositions().asArray().T for o in objects] # load initial prototypes, if any @@ -66,9 +57,10 @@ initialPrototypeIndices = None lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) -nTrajectories = len(trajectories) - -similarities = -np.ones((nTrajectories, nTrajectories)) +if args.similaritiesFilename is not None: + similarities = np.loadtxt(args.similaritiesFilename) +if args.similaritiesFilename is None or similarities.shape[0] != len(trajectories) or similarities.shape[1] != len(trajectories): + similarities = -np.ones((len(trajectories), len(trajectories))) similarityFunc = lambda x,y : lcss.computeNormalized(x, y) # the next line can be called again without reinitializing similarities if args.learn: @@ -90,6 +82,10 @@ if args.learn and not args.assign: prototypes = [] + if args.trajectoryType == 'objectfeature': + trajectoryType = 'feature' + else: + trajectoryType = args.trajectoryType for i in prototypeIndices: if i<len(initialPrototypes): prototypes.append(initialPrototypes[i]) @@ -136,7 +132,7 @@ objectNumbers.append(objNum) storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objectNumbers, 'object', objectLabels, initialPrototypes) else: - storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, [obj.getNum() for obj in objects], trajectoryType, labels[len(initialPrototypes):], initialPrototypes) + storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, [obj.getNum() for obj in objects], args.trajectoryType, labels[len(initialPrototypes):], initialPrototypes) if args.display: plt.figure() for i,o in enumerate(objects): @@ -150,4 +146,7 @@ plt.show() if (args.learn or args.assign) and args.saveSimilarities: - np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') + if args.similaritiesFilename is not None: + np.savetxt(args.similaritiesFilename, similarities, '%.4f') + else: + np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f')
--- a/scripts/process.py Wed Jul 04 16:21:09 2018 -0400 +++ b/scripts/process.py Wed Jul 04 17:39:39 2018 -0400 @@ -28,8 +28,34 @@ # common options parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int) +parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories', choices = ['objectfeature', 'feature', 'object'], default = 'objectfeature') parser.add_argument('--dry', dest = 'dryRun', help = 'dry run of processing', action = 'store_true') parser.add_argument('--nthreads', dest = 'nProcesses', help = 'number of processes to run in parallel', type = int, default = 1) +parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) +parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') + +### process options +# motion pattern learning and assignment +parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes') +#parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') +parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1) +parser.add_argument('--maxdist', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) +parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance +parser.add_argument('-minsimil', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True) +parser.add_argument('-min-cluster-size', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0) +parser.add_argument('--learn', dest = 'learn', help = 'learn', action = 'store_true') +parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true') +parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') +#parser.add_argument('--similarities-filename', dest = 'similaritiesFilename', help = 'filename of the similarities') +parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') +parser.add_argument('--save-assignments', dest = 'saveAssignments', help = 'saves the assignments of the objects to the prototypes', action = 'store_true') +parser.add_argument('--assign', dest = 'assign', help = 'assigns the objects to the prototypes and saves the assignments', action = 'store_true') + +# safety analysis +parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (cvd: vector computation (approximate); cve: equation solving; cv: discrete time (approximate)), normal adaptation, point set prediction)', choices = ['cvd', 'cve', 'cv', 'na', 'ps', 'mp']) +parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true') +# override other tracking config, erase sqlite? + # analysis options parser.add_argument('--output', dest = 'output', help = 'kind of output to produce (interval means)', choices = ['figure', 'interval', 'event']) @@ -40,11 +66,6 @@ dpi = 150 # unit of analysis: site or video sequence? -# safety analysis -parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (cvd: vector computation (approximate); cve: equation solving; cv: discrete time (approximate)), normal adaptation, point set prediction)', choices = ['cvd', 'cve', 'cv', 'na', 'ps', 'mp']) -parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true') -# override other tracking config, erase sqlite? - # need way of selecting sites as similar as possible to sql alchemy syntax # override tracking.cfg from db # manage cfg files, overwrite them (or a subset of parameters) @@ -61,8 +82,10 @@ videoSequences = [] if args.videoIds is not None: videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds] + siteIds = set([vs.cameraView.siteIdx for vs in videoSequences]) elif args.siteIds is not None: - for siteId in args.siteIds: + siteIds = set(args.siteIds) + for siteId in siteIds: for site in getSite(session, siteId): for cv in site.cameraViews: videoSequences += cv.videoSequences @@ -121,7 +144,13 @@ pool.join() elif args.process == 'prototype': # motion pattern learning - pass + # learn by site by default -> group videos by site (or by camera view? TODO add cameraviews) + # by default, load all objects, learn and then assign + objects = {siteId: [] for siteId in siteIds} + for vs in videoSequences: + print('Loading '+vs.getDatabaseFilename()) + objects[vs.cameraView.siteIdx] += storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), args.trajectoryType, args.nTrajectories, timeStep = args.positionSubsamplingRate) + elif args.process == 'interaction': # safety analysis TODO make function in safety analysis script @@ -183,10 +212,6 @@ row.append(aggSpeeds) data.append(row) data = DataFrame(data, columns = headers) - if args.siteIds is None: - siteIds = set([vs.cameraView.siteIdx for vs in videoSequences]) - else: - siteIds = set(args.siteIds) if args.output == 'figure': for name in headers[4:]: plt.ioff()