changeset 854:33d296984dd8

rework and more info on speed probabilities for classification
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 22 Sep 2016 17:50:35 -0400
parents 95e7622b11be
children 2277ab1a8141
files classifier.cfg python/storage.py python/utils.py scripts/classify-objects.py
diffstat 4 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/classifier.cfg	Thu Sep 22 16:49:43 2016 -0400
+++ b/classifier.cfg	Thu Sep 22 17:50:35 2016 -0400
@@ -30,6 +30,7 @@
 mean-ped-speed = 4.91
 std-ped-speed = 0.88
 # mean cyclist speed and standard deviation (in a log-normal distribution) 11.+-4.83 km/h
+# to multiply the parameters by a, loc = loc+ln(a)
 cyc-speed-loc = 2.31
 cyc-speed-scale = 0.42
 # mean vehicle speed and standard deviation (in a normal distribution) 18.45+-7.6 km/h
--- a/python/storage.py	Thu Sep 22 16:49:43 2016 -0400
+++ b/python/storage.py	Thu Sep 22 17:50:35 2016 -0400
@@ -1244,7 +1244,7 @@
         self.stdVehicleSpeed = self.stdVehicleSpeed/denominator
         # special case for the lognormal distribution
         self.locationCyclistSpeed = self.locationCyclistSpeed-log(denominator)
-        #self.scaleCyclistSpeed = self.scaleCyclistSpeed
+        #self.scaleCyclistSpeed = self.scaleCyclistSpeed # no modification of scale
 
         
 class ProcessParameters(VideoFilenameAddable):
--- a/python/utils.py	Thu Sep 22 16:49:43 2016 -0400
+++ b/python/utils.py	Thu Sep 22 17:50:35 2016 -0400
@@ -39,9 +39,11 @@
 
 def logNormalMeanVar(loc, scale):
     '''location and scale are respectively the mean and standard deviation of the normal in the log-normal distribution
-    https://en.wikipedia.org/wiki/Log-normal_distribution'''
+    https://en.wikipedia.org/wiki/Log-normal_distribution
+
+    same as lognorm.stats(scale, 0, exp(loc))'''
     mean = exp(loc+(scale**2)/2)
-    var = (exp(loc**2)-1)*exp(2*loc+scale**2)
+    var = (exp(scale**2)-1)*exp(2*loc+scale**2)
     return mean, var
 
 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False):
--- a/scripts/classify-objects.py	Thu Sep 22 16:49:43 2016 -0400
+++ b/scripts/classify-objects.py	Thu Sep 22 17:50:35 2016 -0400
@@ -16,11 +16,12 @@
 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)')
 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None)
 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true')
-parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display', type = float, default = 50.)
+parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display (km/h)', type = float, default = 50.)
 
 args = parser.parse_args()
 params = storage.ProcessParameters(args.configFilename)
 classifierParams = storage.ClassifierParameters(params.classifierFilename)
+classifierParams.convertToFrames(params.videoFrameRate, 3.6) # conversion from km/h to m/frame
 
 if args.videoFilename is not None:
     videoFilename = args.videoFilename
@@ -31,7 +32,6 @@
 else:
     databaseFilename = params.databaseFilename
 
-classifierParams.convertToFrames(params.videoFrameRate, 3.6) # conversion from km/h to m/s
 if params.homography is not None:
     invHomography = np.linalg.inv(params.homography)
 else:
@@ -53,15 +53,24 @@
 bikeCarSVM.load(classifierParams.bikeCarSVMFilename)
 
 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.)
-speedProbabilities = {'car': lambda s: norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed).pdf(s),
-                      'pedestrian': lambda s: norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed).pdf(s), 
-                      'bicycle': lambda s: lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed)).pdf(s)} # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is location (mean of the normal)
+carNorm = norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed)
+pedNorm = norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed)
+# numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is exp(location) (loc=mean of the normal)
+bicLogNorm = lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed))
+speedProbabilities = {'car': lambda s: carNorm.pdf(s),
+                      'pedestrian': lambda s: pedNorm.pdf(s), 
+                      'bicycle': lambda s: bicLogNorm.pdf(s)}
 
 if args.plotSpeedDistribution:
     import matplotlib.pyplot as plt
     plt.figure()
     for k in speedProbabilities:
-        plt.plot(np.arange(0.1, args.maxSpeedDistributionPlot, 0.1), [speedProbabilities[k](s/3.6/25) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)], label = k)
+        plt.plot(np.arange(0.1, args.maxSpeedDistributionPlot, 0.1), [speedProbabilities[k](s/(3.6*params.videoFrameRate)) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)], label = k)
+    maxProb = -1.
+    for k in speedProbabilities:
+        maxProb = max(maxProb, np.max([speedProbabilities[k](s/(3.6*params.videoFrameRate)) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)]))
+    plt.plot([classifierParams.minSpeedEquiprobable*3.6*params.videoFrameRate]*2, [0., maxProb], 'k-')
+    plt.text(classifierParams.minSpeedEquiprobable*3.6*params.videoFrameRate, maxProb, 'threshold for equiprobable class')
     plt.xlabel('Speed (km/h)')
     plt.ylabel('Probability')
     plt.legend()