Ejemplo n.º 1
0
        private static void Validation(string baseName,
                                       LossMulticlassLog net,
                                       IList <Matrix <RgbPixel> > trainingImages,
                                       IList <uint> trainingLabels,
                                       IList <Matrix <RgbPixel> > testingImages,
                                       IList <uint> testingLabels,
                                       bool useConsole,
                                       bool saveToXml,
                                       out double trainAccuracy,
                                       out double testAccuracy)
        {
            trainAccuracy = 0;
            testAccuracy  = 0;

            using (var predictedLabels = net.Operator(trainingImages))
            {
                var numRight = 0;
                var numWrong = 0;

                // And then let's see if it classified them correctly.
                for (var i = 0; i < trainingImages.Count; ++i)
                {
                    if (predictedLabels[i] == trainingLabels[i])
                    {
                        ++numRight;
                    }
                    else
                    {
                        ++numWrong;
                    }
                }

                if (useConsole)
                {
                    Console.WriteLine($"training num_right: {numRight}");
                    Console.WriteLine($"training num_wrong: {numWrong}");
                    Console.WriteLine($"training accuracy:  {numRight / (double)(numRight + numWrong)}");
                }

                trainAccuracy = numRight / (double)(numRight + numWrong);

                using (var predictedLabels2 = net.Operator(testingImages))
                {
                    numRight = 0;
                    numWrong = 0;
                    for (var i = 0; i < testingImages.Count; ++i)
                    {
                        if (predictedLabels2[i] == testingLabels[i])
                        {
                            ++numRight;
                        }
                        else
                        {
                            ++numWrong;
                        }
                    }

                    if (useConsole)
                    {
                        Console.WriteLine($"testing num_right: {numRight}");
                        Console.WriteLine($"testing num_wrong: {numWrong}");
                        Console.WriteLine($"testing accuracy:  {numRight / (double)(numRight + numWrong)}");
                    }

                    testAccuracy = numRight / (double)(numRight + numWrong);

                    // Finally, you can also save network parameters to XML files if you want to do
                    // something with the network in another tool.  For example, you could use dlib's
                    // tools/convert_dlib_nets_to_caffe to convert the network to a caffe model.
                    if (saveToXml)
                    {
                        Dlib.NetToXml(net, $"{baseName}.xml");
                    }
                }
            }
        }