Ejemplo n.º 1
0
        public static void CreateVideo(CrowdCountingRegression regression, int N, int M, int width, int height, IEnumerable <string> frames, List <IList <PointF> > outputs)
        {
            MathN::Matrix <double> shouldBe = PeoplePositions.GridQuantize(outputs, N, M, width, height);

            using (VideoWriter numberVideoWriter = new VideoWriter("D:\\video_predictions.avi", fps: 1, width: width, height: height, isColor: true))
                using (VideoWriter differenceVideoWriter = new VideoWriter("D:\\video_differences.avi", fps: 1, width: width, height: height, isColor: true))
                {
                    int cellHeight = height / N;
                    int cellWidth  = width / M;

                    MathN::Matrix <double> prediction = regression.Predict(new List <string>(frames));
                    int frameID = 0;
                    foreach (string framePath in frames)
                    {
                        using (Image <Bgr, Byte> countFrame = new Image <Bgr, Byte>(framePath))
                            using (Image <Bgr, Byte> differenceFrame = new Image <Bgr, Byte>(framePath))
                            {
                                for (int i = 1; i < N; ++i)
                                {
                                    LineSegment2D line = new LineSegment2D(
                                        new Point(0, i * cellHeight),
                                        new Point(width, i * cellHeight));
                                    countFrame.Draw(line, new Bgr(Color.Yellow), 2);
                                    differenceFrame.Draw(line, new Bgr(Color.Red), 2);
                                }

                                for (int j = 1; j < M; ++j)
                                {
                                    LineSegment2D line = new LineSegment2D(
                                        new Point(j * cellWidth, 0),
                                        new Point(j * cellWidth, height));
                                    countFrame.Draw(line, new Bgr(Color.Yellow), 2);
                                    differenceFrame.Draw(line, new Bgr(Color.Red), 2);
                                }


                                for (int i = 0; i < N; ++i)
                                {
                                    for (int j = 0; j < M; ++j)
                                    {
                                        double    cellPrediction = prediction[frameID, i *M + j];
                                        int       cellShoudlBe   = (int)Math.Round(shouldBe[frameID, i * M + j]);
                                        Rectangle rect           = new Rectangle(j * cellWidth, i * cellHeight, cellWidth, cellHeight);

                                        drawText(countFrame, rect, Brushes.Yellow, String.Format("{0:0.0}", cellPrediction));

                                        double difference       = (cellPrediction - cellShoudlBe);
                                        string differenceString = difference > 0 ? "+" + String.Format("{0:0.0}", difference) :  String.Format("{0:0.0}", difference);
                                        drawText(differenceFrame, rect, Brushes.Red, differenceString);
                                    }
                                }

                                numberVideoWriter.WriteFrame(countFrame);
                                differenceVideoWriter.WriteFrame(differenceFrame);
                            }

                        frameID++;
                    }
                }
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            DataSets sets    = new DataSets();
            DataSet  dataset = sets.LoadMall();

            string            datasetPath = dataset.Path;
            Image <Bgr, byte> background  = new Image <Bgr, byte>(Path.Combine(datasetPath, "derived images", "background.png"));

            int width  = 320; // dataset.DefaultWidth;
            int height = 240; // dataset.DefaultHeight;

            //IPerspectiveCorrector linearPerspectiveCorrector = new LinearPerspectiveCorrector(75 / 480.0, 53.0, 408 / 480.0, 143.0);

            int N = 8; //dataset.DefaultGridN;
            int M = 8; // dataset.DefaultGridM;

            FeatureExtractor extractor      = new FeatureExtractor(new IdlePerspectiveCorrector(), N, M, width, height, background);
            IDataNormalizer  dataNormalizer = new MeanAndVarianceDataNormalizer();

            IMultiRegression        regression         = new MultiRidgeRegression(1e-3);//100, 1e-5);
            CrowdCountingRegression countingRegression = new CrowdCountingRegression(extractor, dataNormalizer, regression);
            //countingRegression.CachePath = Path.Combine(dataset.Path, "cache");

            //string experimentFile = Path.Combine(datasetPath,"analysis","experiments_nullcorrection.txt");

            Stopwatch sw = new Stopwatch();

            sw.Start();
            countingRegression.Train(dataset.TrainPlusValidationInput, dataset.TrainPlusValidationOutput);
            Console.WriteLine("Training time (800 frames): {0} ms", sw.ElapsedMilliseconds);

            //PeopleCountingVideoCreator.CreateVideo(countingRegression, N, M, width, height, dataset.TestInput, dataset.TestOutput);
            MathN::Matrix <double> shouldBe = PeoplePositions.GridQuantize(dataset.TestOutput, N, M, 640, 480);

            sw.Restart();
            MathN::Matrix <double> predictions2 = countingRegression.Predict(dataset.TestInput);

            sw.Stop();
            Console.WriteLine("Parallelized prediction time (1200 frames): {0} ms", sw.ElapsedMilliseconds);

            File.WriteAllText("D:\\groundtruth.txt", shouldBe.ToMatrixString(5000, 5000), Encoding.UTF8);


            sw.Restart();
            double[] results = Evaluator.Evaluate(predictions2, shouldBe, new SquaredErrorCalculator(), new AbsoluteErrorCalculator(), new RelativeDeviationErrorCalculator());
            sw.Stop();
            Console.WriteLine("Eval time: {0} ms", sw.ElapsedMilliseconds);

            string resultString = String.Format("{0} ; {1}", countingRegression.ToString(), String.Join(" ; ", results));


            Console.WriteLine(resultString);

            MathN::Matrix <double> shouldBeTrain = PeoplePositions.GridQuantize(dataset.TrainOutput, N, M, 640, 480);

            Console.Write(shouldBeTrain.Row(0));

            //MathN::Matrix<double> predictions = countingRegression.Predict(dataset.ValidationInput);

            //countingRegression.Train(new List<string>(dataset.TrainInput.Concat(dataset.ValidationInput)),
            //    new List<IList<PointF>>(dataset.TrainOutput.Concat(dataset.ValidationOutput)));

            //MathN::Matrix<double> predictions = countingRegression.Predict(dataset.TestInput);
            //MathN::Matrix<double> shouldBe = PeoplePositions.GridQuantize(dataset.ValidationOutput, N, M, 640, 480);

            //double[] results = Evaluator.Evaluate(predictions, shouldBe, new SquaredErrorCalculator(), new AbsoluteErrorCalculator(), new RelativeDeviationErrorCalculator());
            //string resultString = String.Format("{0} ; {1}", countingRegression.ToString(), String.Join(" ; ", results));



            //object locker = new object();

            //IMultiRegression regression = new SVMRegression(1, 1e-3);
            //CrowdCountingRegression countingRegression = new CrowdCountingRegression(extractor, dataNormalizer, regression);
            //countingRegression.CachePath = Path.Combine(dataset.Path, "cache");

            ////lock (locker)
            ////{
            ////    if (File.ReadLines(experimentFile).FirstOrDefault((string s) => s.StartsWith(countingRegression.ToString() + " ;")) != null)
            ////    {
            ////        ; //already did this experiment
            ////    }
            ////}

            //Console.WriteLine(countingRegression.ToString());
            //countingRegression.Train(dataset.TrainInput, dataset.TrainOutput);

            //MathN::Matrix<double> predictions = countingRegression.Predict(dataset.ValidationInput);

            ////countingRegression.Train(new List<string>(dataset.TrainInput.Concat(dataset.ValidationInput)),
            ////    new List<IList<PointF>>(dataset.TrainOutput.Concat(dataset.ValidationOutput)));

            ////MathN::Matrix<double> predictions = countingRegression.Predict(dataset.TestInput);
            //MathN::Matrix<double> shouldBe = PeoplePositions.GridQuantize(dataset.ValidationOutput, N, M, 640, 480);

            //double[] results = Evaluator.Evaluate(predictions, shouldBe, new SquaredErrorCalculator(), new AbsoluteErrorCalculator(), new RelativeDeviationErrorCalculator());
            //string resultString = String.Format("{0} ; {1}", countingRegression.ToString(), String.Join(" ; ", results));

            //lock (locker)
            //{
            //    File.AppendAllLines(experimentFile, new string[] { resultString });
            //}
            ////    }
            ////});


            return(0);
        }