コード例 #1
0
ファイル: Program.cs プロジェクト: zhangbo27/DlibDotNet
        // Calculate the per-pixel accuracy on a dataset whose file names are supplied as a parameter.
        private static double CalculateAccuracy(LossMulticlassLogPerPixel anet, IEnumerable <ImageInfo> dataset)
        {
            var numRight = 0;
            var numWrong = 0;

            foreach (var imageInfo in dataset)
            {
                // Load the input image.
                using (var inputImage = Dlib.LoadImageAsMatrix <RgbPixel>(imageInfo.ImageFilename))
                {
                    // Load the ground-truth (RGB) labels.;
                    using (var rgbLabelImage = Dlib.LoadImageAsMatrix <RgbPixel>(imageInfo.ClassLabelFilename))
                    {
                        // 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 = anet.Operator(inputImage))
                            using (var temp = output.First())
                            {
                                // Convert the indexes to RGB values.
                                using (var indexLabelImage = new Matrix <ushort>())
                                {
                                    PascalVOC2012.RgbLabelImageToIndexLabelImage(rgbLabelImage, indexLabelImage);

                                    // Crop the net output to be exactly the same size as the input.
                                    using (var chipDims = new ChipDims((uint)inputImage.Rows, (uint)inputImage.Columns))
                                        using (var chipDetails = new ChipDetails(Dlib.CenteredRect(temp.Columns / 2, temp.Rows / 2,
                                                                                                   (uint)inputImage.Columns,
                                                                                                   (uint)inputImage.Rows),
                                                                                 chipDims))
                                        {
                                            using (var netOutput = Dlib.ExtractImageChip <ushort>(temp, chipDetails, InterpolationTypes.NearestNeighbor))
                                            {
                                                var nr = indexLabelImage.Rows;
                                                var nc = indexLabelImage.Columns;

                                                // Compare the predicted values to the ground-truth values.
                                                for (var r = 0; r < nr; ++r)
                                                {
                                                    for (var c = 0; c < nc; ++c)
                                                    {
                                                        var truth = indexLabelImage[r, c];
                                                        if (truth != LossMulticlassLogPerPixel.LabelToIgnore)
                                                        {
                                                            var prediction = netOutput[r, c];
                                                            if (prediction == truth)
                                                            {
                                                                ++numRight;
                                                            }
                                                            else
                                                            {
                                                                ++numWrong;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                }
                            }
                    }
                }
            }

            // Return the accuracy estimate.
            return(numRight / (double)(numRight + numWrong));
        }