Esempio n. 1
0
        public void Operator()
        {
            var image = this.GetDataFile("2008_001322.jpg");
            var path  = Path.Combine(this.ModelDirectory, "semantic_segmentation_voc2012net.dnn");

            using (var loss1 = LossMulticlassLogPerPixel.Deserialize(path))
                using (var loss2 = LossMulticlassLogPerPixel.Deserialize(File.ReadAllBytes(path)))
                    using (var matrix = Dlib.LoadImageAsMatrix <RgbPixel>(image.FullName))
                        using (var ret1 = loss1.Operator(matrix))
                            using (var ret2 = loss2.Operator(matrix))
                            {
                                Assert.Equal(1, ret1.Count);
                                Assert.Equal(1, ret2.Count);

                                var r1 = ret1[0];
                                var r2 = ret2[0];

                                Assert.Equal(r1.Rows, r2.Rows);
                                Assert.Equal(r1.Columns, r2.Columns);

                                for (var c = 0; c < r1.Columns; c++)
                                {
                                    for (var r = 0; r < r1.Rows; r++)
                                    {
                                        Assert.Equal(r1[r, c], r2[r, c]);
                                    }
                                }
                            }
        }
Esempio n. 2
0
        public void Deserialize2()
        {
            var path = Path.Combine(this.ModelDirectory, "semantic_segmentation_voc2012net.dnn");

            using (var loss = LossMulticlassLogPerPixel.Deserialize(File.ReadAllBytes(path)))
                Assert.Equal(220, loss.NumLayers);
        }
Esempio n. 3
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("You call this program like this: ");
                Console.WriteLine("./dnn_semantic_segmentation_train_ex /path/to/images");
                Console.WriteLine();
                Console.WriteLine("You will also need a trained 'semantic_segmentation_voc2012net.dnn' file.");
                Console.WriteLine("You can either train it yourself (see example program");
                Console.WriteLine("dnn_semantic_segmentation_train_ex), or download a");
                Console.WriteLine("copy from here: http://dlib.net/files/semantic_segmentation_voc2012net.dnn");
                return;
            }

            try
            {
                // Read the file containing the trained network from the working directory.
                using (var net = LossMulticlassLogPerPixel.Deserialize("semantic_segmentation_voc2012net.dnn"))
                {
                    // Show inference results in a window.
                    using (var win = new ImageWindow())
                    {
                        // Find supported image files.
                        var files = Directory.GetFiles(args[0])
                                    .Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg") || s.EndsWith(".png")).ToArray();
                        Console.WriteLine($"Found {files.Length} images, processing...");
                        foreach (var file in files)
                        {
                            // Load the input image.
                            using (var inputImage = Dlib.LoadImageAsMatrix <RgbPixel>(file))
                            {
                                // Create predictions for each pixel. At this point, the type of each prediction
                                // is an index (a value between 0 and 20). Note that the net may return an image
                                // that is not exactly the same size as the input.
                                using (var output = net.Operator(inputImage))
                                    using (var temp = output.First())
                                    {
                                        // Crop the returned image to be exactly the same size as the input.
                                        var rect = Rectangle.CenteredRect((int)(temp.Columns / 2d), (int)(temp.Rows / 2d), (uint)inputImage.Columns, (uint)inputImage.Rows);
                                        using (var dims = new ChipDims((uint)inputImage.Rows, (uint)inputImage.Columns))
                                            using (var chipDetails = new ChipDetails(rect, dims))
                                                using (var indexLabelImage = Dlib.ExtractImageChip <ushort>(temp, chipDetails, InterpolationTypes.NearestNeighbor))
                                                {
                                                    // Convert the indexes to RGB values.
                                                    using (var rgbLabelImage = IndexLabelImageToRgbLabelImage(indexLabelImage))
                                                    {
                                                        // Show the input image on the left, and the predicted RGB labels on the right.
                                                        using (var joinedRow = Dlib.JoinRows(inputImage, rgbLabelImage))
                                                        {
                                                            win.SetImage(joinedRow);

                                                            // Find the most prominent class label from amongst the per-pixel predictions.
                                                            var classLabel = GetMostProminentNonBackgroundClassLabel(indexLabelImage);

                                                            Console.WriteLine($"{file} : {classLabel} - hit enter to process the next image");
                                                            Console.ReadKey();
                                                        }
                                                    }
                                                }
                                    }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }