takuya-takeuchi / FaceRecognitionDotNet

The world's simplest facial recognition api for .NET on Windows, MacOS and Linux
MIT License
1.27k stars 309 forks source link

Performance On Example Images #55

Closed theladyjaye closed 4 years ago

theladyjaye commented 5 years ago

It takes about 20-24 seconds: (Using the Cnn model Hog is near instant)

Using images from:

https://github.com/takuya-takeuchi/FaceRecognitionDotNet/tree/master/test/FaceRecognitionDotNet.Tests/TestImages

And the following code:

_FaceRecognition = FaceRecognition.Create(@"C:\Path\To\Models\models");
var model = FaceRecognitionDotNet.Model.Cnn;
var imageToCheck = @"C:\Path\To\Image\obama2.jpg";

for(var i = 0; i < 10; i++)
{
    using (var unknownImage = FaceRecognition.LoadImageFile(imageToCheck))
    {
        var stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        var faceLocations = _FaceRecognition.FaceLocations(unknownImage, 0, model).ToArray();
        stopwatch.Stop();
        var elapsed = (int)stopwatch.Elapsed.TotalSeconds;

        foreach (var faceLocation in faceLocations)
            PrintResult(imageToCheck, faceLocation);

    }
}
takuya-takeuchi commented 5 years ago

What package did you try? I tried the FRDN.CUDA100 and original python face_recognition for same obama2.jpg.

Your performance is a bit slow.

python

import timeit

# Note: This example is only tested with Python 3 (not Python 2)

# This is a very simple benchmark to give you an idea of how fast each step of face recognition will run on your system.
# Notice that face detection gets very slow at large image sizes. So you might consider running face detection on a
# scaled down version of your image and then running face encodings on the the full size image.

TEST_IMAGES = [
    "obama2.jpg"
]

def run_test(setup, test, iterations_per_test=5, tests_to_run=10):
    fastest_execution = min(timeit.Timer(test, setup=setup).repeat(tests_to_run, iterations_per_test))
    execution_time = fastest_execution / iterations_per_test
    fps = 1.0 / execution_time
    return execution_time, fps

setup_locate_faces = """
import face_recognition

image = face_recognition.load_image_file("{}")
"""

test_locate_faces = """
face_locations = face_recognition.face_locations(image, 0, "cnn")
"""

setup_end_to_end = """
import face_recognition

image = face_recognition.load_image_file("{}")
"""

test_end_to_end = """
encoding = face_recognition.face_encodings(image)[0]
"""

print("Benchmarks (Note: All benchmarks are only using a single CPU core)")
print()

for image in TEST_IMAGES:
    print(" - Face locations: {:.4f}s ({:.2f} fps)".format(*run_test(setup_locate_faces.format(image), test_locate_faces)))
    print()

dotnet

/*
 * This sample program is ported by C# from https://github.com/ageitgey/face_recognition/blob/master/examples/benchmark.py.
*/

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using FaceRecognitionDotNet;
using Microsoft.Extensions.CommandLineUtils;

namespace Benchmark
{

    internal class Program
    {

        #region Fields

        private static FaceRecognition FaceRecognition;

        #endregion

        #region Methods

        private static void Main(string[] args)
        {
            var app = new CommandLineApplication(false);
            app.Name = nameof(Benchmark);
            app.Description = "The program for measure face encoding performance";
            app.HelpOption("-h|--help");

            var modelsOption = app.Option("-m|--model", "model files directory path", CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                if (!modelsOption.HasValue())
                {
                    app.ShowHelp();
                    return -1;
                }

                var directory = modelsOption.Value();
                if (!Directory.Exists(directory))
                {
                    app.ShowHelp();
                    return -1;
                }

                FaceRecognition = FaceRecognition.Create(directory);

                var testImages = new[]
                {
                    "obama2.jpg",
                };

                Console.WriteLine("Benchmarks");
                Console.WriteLine();

                foreach (var image in testImages)
                {
                    var faceLocations = RunTest(image, SetupLocateFaces, TestLocateFaces);
                    Console.WriteLine($" - Face locations: {faceLocations.Item1:F4}s ({faceLocations.Item2:F2} fps)");
                }

                return 0;
            });

            app.Execute(args);
        }

        #region Helpers

        private static Tuple<double, double> RunTest<T>(string path, Func<string, T> setup, Action<T> test, int iterationsPerTest = 5, int testsToRun = 10)
        {
            var image = setup(path);

            var iteration = new Func<double>(() =>
            {
                var sw = new Stopwatch();
                sw.Start();
                for (var count = 0; count < iterationsPerTest; count++)
                    test(image);
                sw.Stop();

                return sw.ElapsedMilliseconds;
            });

            var fastestExecution = Enumerable.Repeat(0, testsToRun).Select(i => iteration()).Min();
            var executionTime = fastestExecution / 1000 / iterationsPerTest;
            var fps = 1.0 / executionTime;

            (image as IDisposable)?.Dispose();

            return new Tuple<double, double>(executionTime, fps);
        }

        private static Image SetupEndToEnd(string path)
        {
            return FaceRecognition.LoadImageFile(path);
        }

        private static Image SetupLocateFaces(string path)
        {
            return FaceRecognition.LoadImageFile(path);
        }

        private static void TestEndToEnd(Image image)
        {
            var encoding = FaceRecognition.FaceEncodings(image).First();
        }

        private static void TestLocateFaces(Image image)
        {
            var faceLocations = FaceRecognition.FaceLocations(image, 0, FaceRecognitionDotNet.Model.Cnn).ToArray();
        }

        #endregion

        #endregion

    }

}

The above benchmark

python

Benchmarks (Note: All benchmarks are only using a single CPU core)

 - Face locations: 0.0467s (21.43 fps)

Benchmarks (Note: All benchmarks are only using a single CPU core)

 - Face locations: 0.0468s (21.38 fps)

Benchmarks (Note: All benchmarks are only using a single CPU core)

 - Face locations: 0.0467s (21.43 fps)

dotnet

Benchmarks

 - Face locations: 0.0490s (20.41 fps)
Benchmarks

 - Face locations: 0.0492s (20.33 fps)
Benchmarks

 - Face locations: 0.0492s (20.33 fps)
theladyjaye commented 5 years ago

Ah.. I was just using FaceRecognitionDotNet not any of the CUDA libs. That might account for it.

takuya-takeuchi commented 4 years ago

It seems that the reporter resolve issue.