示例#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++;
                    }
                }
        }
示例#2
0
        public DataSet LoadUCSD()
        {
            string datasetPath = @"D:\! Egyetem\! RWTH\Semester 2\Seminar CV\Datasets\UCSD\";

            List <string>          allFrames = new List <string>(Directory.GetFiles(Path.Combine(datasetPath, "frames")).Where((s) => s.EndsWith("png")));
            List <IList <PointF> > peoplePos = new List <IList <PointF> >(PeoplePositions.ReadFromFile(Path.Combine(datasetPath, "people_positions.txt")));

            int nTrain      = 640;
            int nValidation = 800 - 640;
            int nTest       = 2000 - 800;

            DataSet ds = new DataSet();

            ds.DefaultWidth  = 238;
            ds.DefaultHeight = 158;
            ds.DefaultGridN  = 4;
            ds.DefaultGridM  = 6;

            ds.Path            = datasetPath;
            ds.TrainInput      = allFrames.GetRange(0, nTrain);
            ds.ValidationInput = allFrames.GetRange(nTrain, nValidation);
            ds.TestInput       = allFrames.GetRange(nTrain + nValidation, nTest);

            ds.TrainOutput      = peoplePos.GetRange(0, nTrain);
            ds.ValidationOutput = peoplePos.GetRange(nTrain, nValidation);
            ds.TestOutput       = peoplePos.GetRange(nTrain + nValidation, nTest);
            return(ds);
        }
示例#3
0
        public DataSet LoadMall()
        {
            string datasetPath = @"D:\! Egyetem\! RWTH\Semester 2\Seminar CV\Datasets\Mall\";

            List <string>          allFrames = new List <string>(Directory.GetFiles(Path.Combine(datasetPath, "frames")).Where((s) => s.EndsWith("jpg")));
            List <IList <PointF> > peoplePos = new List <IList <PointF> >(PeoplePositions.ReadFromFile(Path.Combine(datasetPath, "people_positions.txt")));

            int nTrain      = 640;
            int nValidation = 800 - 640;
            int nTest       = 2000 - 800;

            DataSet ds = new DataSet();

            ds.DefaultWidth  = 640;
            ds.DefaultHeight = 480;

            ds.DefaultGridN    = 8;
            ds.DefaultGridM    = 8;
            ds.LinearCorrector = new LinearPerspectiveCorrector(75 / 480.0, 53.0, 408 / 480.0, 143.0);
            ds.LinearCorrector = new LinearPerspectiveCorrector(75 / 480.0, 53.0, 408 / 480.0, 143.0);

            ds.Path            = datasetPath;
            ds.TrainInput      = allFrames.GetRange(0, nTrain);
            ds.ValidationInput = allFrames.GetRange(nTrain, nValidation);
            ds.TestInput       = allFrames.GetRange(nTrain + nValidation, nTest);

            ds.TrainPlusValidationInput = new List <string>(ds.TrainInput.Concat(ds.ValidationInput));

            ds.TrainOutput      = peoplePos.GetRange(0, nTrain);
            ds.ValidationOutput = peoplePos.GetRange(nTrain, nValidation);
            ds.TestOutput       = peoplePos.GetRange(nTrain + nValidation, nTest);

            ds.TrainPlusValidationOutput = new List <IList <PointF> >(ds.TrainOutput.Concat(ds.ValidationOutput));
            return(ds);
        }
示例#4
0
        public void Train(IList <string> frameImageFilePaths, IList <IList <PointF> > peoplePositions)
        {
            Math::Matrix <double> X = LoadOrCalculateInputMatrix(frameImageFilePaths);

            dataNormalizer.TrainNormalization(X);
            dataNormalizer.Normalize(X);

            Math::Matrix <double> Y = PeoplePositions.GridQuantize(peoplePositions, featureExtractor.N, featureExtractor.M, 640, 480);

            innerRegression.Train(X, Y);
        }
        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);
        }