示例#1
0
        public void trainWithBaseAlgorithm(EdgeDetectionAlgorithm algorithm, EdgeDetectionAlgorithm baseAlgorithm, int resizeFactor)
        {
            DateTime      trainingStart = DateTime.Now;
            float         totalLoss     = 0;
            List <String> fileList      = new List <string>(benchmark.getTrainingFilesPathList());

            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);
                    ImageDescription computedImage = baseAlgorithm.test(inputImage);

                    ResizeFilter     resizeColor   = new ResizeFilter(inputImage.sizeX / resizeFactor, inputImage.sizeY / resizeFactor, ImageDescriptionUtil.colorChannels);
                    ImageDescription newInputImage = resizeColor.filter(inputImage);

                    ImageDescription inputImageGroundTruth = ImageFileHandler.loadFromPath(benchmark.getTrainingFileGroundTruth(trainingFileName));
                    inputImageGroundTruth.computeGrayscale();
                    ResizeFilter     resizeGrayscale          = new ResizeFilter(inputImage.sizeX / resizeFactor, inputImage.sizeY / resizeFactor, ImageDescriptionUtil.grayscaleChannel);
                    ImageDescription newInputImageGroundTruth = resizeGrayscale.filter(inputImageGroundTruth);

                    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"));
        }
        public static void makeAllImagesSameSize(List <ImageDescription> inputImages, out int newSizeX, out int newSizeY, out List <ImageDescription> outputImages)
        {
            newSizeX = 0;
            newSizeY = 0;
            foreach (ImageDescription image in inputImages)
            {
                if (image.sizeX > newSizeX)
                {
                    newSizeX = image.sizeX;
                }
                if (image.sizeY > newSizeY)
                {
                    newSizeY = image.sizeY;
                }
            }

            outputImages = new List <ImageDescription>(inputImages.Count);
            ResizeFilter rf = new ResizeFilter(newSizeX, newSizeY, new HashSet <ColorChannelEnum> {
                ColorChannelEnum.Gray
            });

            foreach (ImageDescription image in inputImages)
            {
                if (image.sizeX != newSizeX || image.sizeY != newSizeY)
                {
                    outputImages.Add(rf.filter(image));
                }
                else
                {
                    outputImages.Add(image);
                }
            }
        }
示例#3
0
        public List <ImageDescription> computeImageForLayers(ImageDescription inputImage, int numberOfLayersToCompute)
        {
            List <ImageDescription> computedImages = new List <ImageDescription>(numberOfLayersToCompute);

            for (int i = 0; i < numberOfLayersToCompute; i++)
            {
                EdgeDetectionAlgorithm algorithm = layers[i].algorithm;
                int layerResizeFactor            = layers[i].resizeFactor;

                ImageDescription newInputImage = 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;
                }
                else
                {
                    newInputImage = resizeColor.filter(inputImage);
                }
                if (i > 0)
                {
                    ImageDescription resizedComputed = resizeGrayscale.filter(computedImages[i - 1]);
                    newInputImage.setColorChannel(ColorChannelEnum.Layer, resizedComputed.gray);
                }

                ImageDescription layerOutputImage = algorithm.test(newInputImage);
                computedImages.Add(layerOutputImage);
            }

            return(computedImages);
        }
示例#4
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"));
        }