コード例 #1
0
        public void trainNestedAlgorithm(ContextualMemoryNestedAlgorithm nestedAlgorithm)
        {
            BlackAndWhiteConverter blackAndWhiteConverter = new BlackAndWhiteConverter(1);
            //BlackAndWhiteConverter blackAndWhiteConverter = new BlackAndWhiteConverter(63);

            List <String> fileList = new List <string>(benchmark.getTrainingFilesPathList());

            List <ContextualMemoryNestedAlgorithmLayer> layers = nestedAlgorithm.getLayers();

            for (int layerIndex = 0; layerIndex < layers.Count; layerIndex++)
            {
                ContextualMemoryNestedAlgorithmLayer layer = layers[layerIndex];
                layer.initialize();
                Console.WriteLine("Layer: " + (layerIndex + 1) + "/" + layers.Count);

                EdgeDetectionAlgorithm algorithm = layer.algorithm;

                DateTime trainingStart      = DateTime.Now;
                float    totalLoss          = 0;
                int      totalNumberOfFiles = numberOfTrainingSetPasses * fileList.Count;
                int      totalIndex         = 0;
                for (int pass = 0; pass < numberOfTrainingSetPasses; pass++)
                {
                    ListUtils.Shuffle(fileList);
                    int      index             = 1;
                    float    totalPassLoss     = 0;
                    DateTime trainingPassStart = DateTime.Now;
                    foreach (string trainingFileName in fileList)
                    {
                        DateTime start = DateTime.Now;
                        Console.WriteLine("Pass: "******"/" + numberOfTrainingSetPasses + ", " + index + "/" + fileList.Count + " Training file: " + Path.GetFileName(trainingFileName));

                        ImageDescription inputImage = ImageFileHandler.loadFromPath(trainingFileName);
                        int layerResizeFactor       = layer.resizeFactor;

                        ImageDescription computedImage = null;
                        if (layerIndex > 0)
                        {
                            List <ImageDescription> computedImages = nestedAlgorithm.computeImageForLayers(inputImage, layerIndex);
                            computedImage = computedImages[layerIndex - 1];
                        }

                        ImageDescription inputImageGroundTruth = ImageFileHandler.loadFromPath(benchmark.getTrainingFileGroundTruth(trainingFileName));
                        inputImageGroundTruth = blackAndWhiteConverter.filter(inputImageGroundTruth);

                        ImageDescription newInputImage            = null;
                        ImageDescription newInputImageGroundTruth = null;

                        ResizeFilter resizeGrayscale = new ResizeFilter(inputImage.sizeX / layerResizeFactor, inputImage.sizeY / layerResizeFactor, ImageDescriptionUtil.grayscaleChannel);
                        ResizeFilter resizeColor     = new ResizeFilter(inputImage.sizeX / layerResizeFactor, inputImage.sizeY / layerResizeFactor, ImageDescriptionUtil.colorChannels);

                        if (layerResizeFactor == 1)
                        {
                            newInputImage            = inputImage;
                            newInputImageGroundTruth = inputImageGroundTruth;
                        }
                        else
                        {
                            newInputImage            = resizeColor.filter(inputImage);
                            newInputImageGroundTruth = resizeGrayscale.filter(inputImageGroundTruth);
                        }
                        if (layerIndex > 0)
                        {
                            ImageDescription resizedComputed = resizeGrayscale.filter(computedImage);
                            newInputImage.setColorChannel(ColorChannelEnum.Layer, resizedComputed.gray);
                        }

                        float loss = algorithm.train(newInputImage, newInputImageGroundTruth);

                        totalLoss     += loss;
                        totalPassLoss += loss;
                        index++;
                        totalIndex++;

                        double timeElapsed      = (DateTime.Now - start).TotalSeconds;
                        double timeElapsedSoFar = (DateTime.Now - trainingStart).TotalSeconds;
                        double estimatedTime    = (timeElapsedSoFar / totalIndex) * (totalNumberOfFiles - totalIndex);
                        Console.WriteLine("Loss: " + loss.ToString("0.00") + " Time: " + timeElapsed.ToString("0.00") + "s Time elapsed: "
                                          + timeElapsedSoFar.ToString("0.00") + "s ETA: " + estimatedTime.ToString("0.00") + "s");
                    }
                    double tariningPassTimeElapsed = (DateTime.Now - trainingPassStart).TotalSeconds;
                    Console.WriteLine("Pass took " + tariningPassTimeElapsed.ToString("0.00") + " sec. Pass loss: " + totalPassLoss.ToString("0.00")
                                      + " Avg loss: " + (totalPassLoss / (fileList.Count)).ToString("0.00"));
                }
                double totalTimeElapsed = (DateTime.Now - trainingStart).TotalSeconds;
                Console.WriteLine("Training took " + totalTimeElapsed.ToString("0.00") + " sec. Total loss: " + totalLoss.ToString("0.00")
                                  + " Avg loss: " + (totalLoss / (totalNumberOfFiles)).ToString("0.00"));
            }

            Console.WriteLine("Training blender");

            DateTime     blenderTrainingStart      = DateTime.Now;
            float        blenderTotalLoss          = 0;
            int          blenderTotalNumberOfFiles = /* numberOfTrainingSetPasses * */ fileList.Count;
            int          blenderTotalIndex         = 0;
            ImageBlender blender = nestedAlgorithm.getImageBlender();
            //for (int pass = 0; pass < numberOfTrainingSetPasses; pass++)
            {
                ListUtils.Shuffle(fileList);
                int      index             = 1;
                float    totalPassLoss     = 0;
                DateTime trainingPassStart = DateTime.Now;
                foreach (string trainingFileName in fileList)
                {
                    DateTime start = DateTime.Now;
                    //Console.Write("Pass: "******"/" + numberOfTrainingSetPasses + ", ");
                    Console.WriteLine(index + "/" + fileList.Count + " Training file: " + Path.GetFileName(trainingFileName));

                    ImageDescription        inputImage     = ImageFileHandler.loadFromPath(trainingFileName);
                    List <ImageDescription> computedImages = nestedAlgorithm.computeImageForLayers(inputImage, layers.Count);

                    ImageDescription inputImageGroundTruth = ImageFileHandler.loadFromPath(benchmark.getTrainingFileGroundTruth(trainingFileName));
                    inputImageGroundTruth = blackAndWhiteConverter.filter(inputImageGroundTruth);

                    float blenderLoss = blender.train(computedImages, inputImageGroundTruth);

                    blenderTotalLoss += blenderLoss;
                    totalPassLoss    += blenderLoss;
                    index++;
                    blenderTotalIndex++;

                    double timeElapsed      = (DateTime.Now - start).TotalSeconds;
                    double timeElapsedSoFar = (DateTime.Now - blenderTrainingStart).TotalSeconds;
                    double estimatedTime    = (timeElapsedSoFar / blenderTotalIndex) * (blenderTotalNumberOfFiles - blenderTotalIndex);
                    Console.WriteLine("Loss: " + blenderLoss.ToString("0.00") + " Time: " + timeElapsed.ToString("0.00") + "s Time elapsed: "
                                      + timeElapsedSoFar.ToString("0.00") + "s ETA: " + estimatedTime.ToString("0.00") + "s");
                }
                //double tariningPassTimeElapsed = (DateTime.Now - trainingPassStart).TotalSeconds;
                //Console.WriteLine("Pass took " + tariningPassTimeElapsed.ToString("0.00") + " sec. Pass loss: " + totalPassLoss.ToString("0.00")
                //+ " Avg loss: " + (totalPassLoss / (fileList.Count)).ToString("0.00"));
            }
            double blenderTotalTimeElapsed = (DateTime.Now - blenderTrainingStart).TotalSeconds;

            Console.WriteLine("Training took " + blenderTotalTimeElapsed.ToString("0.00") + " sec. Total loss: " + blenderTotalLoss.ToString("0.00")
                              + " Avg loss: " + (blenderTotalLoss / (blenderTotalNumberOfFiles)).ToString("0.00"));
        }
コード例 #2
0
        public void testNestedAlgorithm(ContextualMemoryNestedAlgorithm nestedAlgorithm)
        {
            DateTime      testingStart = DateTime.Now;
            List <String> fileList     = benchmark.getTestFilesPathList();
            int           index        = 1;

            string outputDirectory = null;

            foreach (string testFileName in fileList)
            {
                DateTime start = DateTime.Now;
                outputDirectory = Path.GetDirectoryName(benchmark.getTestFileOutputPathWithoutExtension(testFileName));
                if (!Directory.Exists(outputDirectory))
                {
                    Directory.CreateDirectory(outputDirectory);
                }
                Console.WriteLine(index + "/" + fileList.Count + " Testing file: " + Path.GetFileName(testFileName));


                ImageDescription inputImage = ImageFileHandler.loadFromPath(testFileName);
                List <ContextualMemoryNestedAlgorithmLayer> layers = nestedAlgorithm.getLayers();

                List <ImageDescription> computedImages = nestedAlgorithm.computeImageForLayers(inputImage, layers.Count);
                for (int i = 0; i < layers.Count; i++)
                {
                    if (layers[i].outputResults)
                    {
                        ImageFileHandler.saveToPath(computedImages[i], benchmark.getTestFileOutputPathWithoutExtension(testFileName) + "_layer" + i, outputFileExtension);
                    }
                }
                ImageDescription outputImage = nestedAlgorithm.getImageBlender().blendImages(computedImages);
                ImageFileHandler.saveToPath(outputImage, benchmark.getTestFileOutputPathWithoutExtension(testFileName), outputFileExtension);

                double timeElapsed      = (DateTime.Now - start).TotalSeconds;
                double timeElapsedSoFar = (DateTime.Now - testingStart).TotalSeconds;
                double estimatedTime    = (timeElapsedSoFar / index) * (fileList.Count - index);
                Console.WriteLine(timeElapsed.ToString("0.00") + "s Time elapsed: "
                                  + timeElapsedSoFar.ToString("0.00") + "s ETA: " + estimatedTime.ToString("0.00") + "s");
                index++;
            }
            double totalTimeElapsed = (DateTime.Now - testingStart).TotalSeconds;

            Console.WriteLine("Testing took " + totalTimeElapsed.ToString("0.00") + " sec.");

            if (testOnTrainingFiles)
            {
                Console.WriteLine("Testing on training files");
                testingStart = DateTime.Now;
                index        = 0;

                // we have the outputDirectory from test, else, relative to the exe
                outputDirectory = Path.Combine(outputDirectory, trainingFilesTestOutput);
                if (!Directory.Exists(outputDirectory))
                {
                    Directory.CreateDirectory(outputDirectory);
                }

                fileList = new List <string>(benchmark.getTrainingFilesPathList());
                foreach (string trainingFileName in fileList)
                {
                    DateTime start      = DateTime.Now;
                    string   outputPath = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(trainingFileName));
                    Console.WriteLine((index + 1) + "/" + fileList.Count + " Testing file: " + Path.GetFileName(trainingFileName));
                    ImageDescription inputImage = ImageFileHandler.loadFromPath(trainingFileName);

                    List <ContextualMemoryNestedAlgorithmLayer> layers = nestedAlgorithm.getLayers();
                    List <ImageDescription> computedImages             = nestedAlgorithm.computeImageForLayers(inputImage, layers.Count);
                    for (int i = 0; i < layers.Count; i++)
                    {
                        if (layers[i].outputResults)
                        {
                            ImageFileHandler.saveToPath(computedImages[i], outputPath + "_layer" + i, outputFileExtension);
                        }
                    }
                    ImageDescription outputImage = nestedAlgorithm.getImageBlender().blendImages(computedImages);

                    ImageFileHandler.saveToPath(outputImage, outputPath, outputFileExtension);
                    index++;

                    double timeElapsed = (DateTime.Now - start).TotalSeconds;
                    Console.WriteLine(timeElapsed.ToString("0.00") + " seconds");
                }
                totalTimeElapsed = (DateTime.Now - testingStart).TotalSeconds;
                Console.WriteLine("Testing on training files took " + totalTimeElapsed.ToString("0.00") + " sec.");
            }
        }
コード例 #3
0
        private static void trainTestAndSaveNestedAlgorithm()
        {
            string relativeImageOutputFolder = "cm m20 collisions 4layers thr1 1 2 4 8 canny kirsh nms newderivative (gauss) square l16 squaremix";
            string outputModelFilename       = relativeImageOutputFolder + ".alg";
            int    numberOfPasses            = 1;
            int?   trainingSetSizeLimit      = null;
            bool   useRotations        = false;
            bool   testOnTrainingFiles = false;

            ISet <ColorChannelEnum> colorChannels = new HashSet <ColorChannelEnum> {
                ColorChannelEnum.Red, ColorChannelEnum.Green, ColorChannelEnum.Blue, ColorChannelEnum.Canny, ColorChannelEnum.Kirsch
            };
            ISet <ColorChannelEnum> colorAndComputedChannels = new HashSet <ColorChannelEnum> {
                ColorChannelEnum.Red, ColorChannelEnum.Green, ColorChannelEnum.Blue, ColorChannelEnum.Canny, ColorChannelEnum.Kirsch, ColorChannelEnum.Layer
            };

            ContextualMemoryNestedAlgorithm nestedAlgorithm = new ContextualMemoryNestedAlgorithm();

            ContextualMemoryNestedAlgorithmLayer layer0 = new ContextualMemoryNestedAlgorithmLayer();

            layer0.colorChannels        = colorChannels;
            layer0.longestContextLength = 16;
            layer0.numberOfRays         = 16;
            layer0.tableSizeBits        = 20;
            layer0.resizeFactor         = 1;
            layer0.outputResults        = true;
            nestedAlgorithm.addLayer(layer0);

            ContextualMemoryNestedAlgorithmLayer layer1 = new ContextualMemoryNestedAlgorithmLayer();

            layer1.colorChannels        = colorAndComputedChannels;
            layer1.longestContextLength = 16;
            layer1.numberOfRays         = 16;
            layer1.tableSizeBits        = 20;
            layer1.resizeFactor         = 2;
            layer1.outputResults        = true;
            nestedAlgorithm.addLayer(layer1);

            ContextualMemoryNestedAlgorithmLayer layer2 = new ContextualMemoryNestedAlgorithmLayer();

            layer2.colorChannels        = colorAndComputedChannels;
            layer2.longestContextLength = 16;
            layer2.numberOfRays         = 16;
            layer2.tableSizeBits        = 20;
            layer2.resizeFactor         = 4;
            layer2.outputResults        = true;
            nestedAlgorithm.addLayer(layer2);

            ContextualMemoryNestedAlgorithmLayer layer3 = new ContextualMemoryNestedAlgorithmLayer();

            layer3.colorChannels        = colorAndComputedChannels;
            layer3.longestContextLength = 16;
            layer3.numberOfRays         = 16;
            layer3.tableSizeBits        = 19;
            layer3.resizeFactor         = 8;
            layer3.outputResults        = true;
            nestedAlgorithm.addLayer(layer3);

            //nestedAlgorithm.setImageBlender(new SimpleGrayscaleImageBlender());
            nestedAlgorithm.setImageBlender(new LogisticMixGrayscaleImageBlender(nestedAlgorithm.getLayers().Count));

            BerkeleyEdgeDetectionBenchmark benchmark = new BerkeleyEdgeDetectionBenchmark(localBenchmarkPath, Path.Combine(localBenchmarkPath, relativeImageOutputFolder), useRotations, false, trainingSetSizeLimit);
            EdgeDetectionProcessor         processor = new EdgeDetectionProcessor(benchmark, numberOfPasses, testOnTrainingFiles);

            Console.WriteLine("Training started.");
            processor.trainNestedAlgorithm(nestedAlgorithm);
            Console.WriteLine("Training finished.");

            EdgeDetectionAlgorithmUtil.saveToCompressedFile(nestedAlgorithm, outputModelFilename);

            nestedAlgorithm = null;

            EdgeDetectionAlgorithm algorithm = EdgeDetectionAlgorithmUtil.loadAlgorithmFromCompressedFile(outputModelFilename);

            Console.WriteLine("Testing started.");
            processor.testNestedAlgorithm((ContextualMemoryNestedAlgorithm)algorithm);
            Console.WriteLine("Testing finished.");
        }