takuya-takeuchi / FaceRecognitionDotNet

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

Question with FaceRecognitionDotNet.FaceLandmark(). #170

Closed Hakentha closed 3 years ago

Hakentha commented 3 years ago

Hi, I use DlibDotNet Cuda 11.2, FaceRecognitionDotNet and Dasync.Collections for parallel work.

This is my class to save results. class ImagesResults { public int id; public string filePath; public Image toCompareData; public IEnumerable<IDictionary<FacePart, IEnumerable<FacePoint>>> landmarks; } And this is the code: ` _FaceRecognition = FaceRecognition.Create("path to models route");

  List<string> filesToProcess = Directory.GetFiles("path to images").ToList();

  Dictionary<int, ImagesResults> _imagesResults = new Dictionary<int, ImagesResults>();

  await filesToProcess.ParallelForEachAsync(async currentFile =>
  {
    //All the images has numbers as filenames.
    string _file = Path.GetFileName(currentFile).Substring(0, Path.GetFileName(currentFile).Length - 4);
    _imagesResults.Add((int)Int64.Parse(_file), new ImagesResults { id = (int)Int64.Parse(_file), filePath = currentFile });
  });

  await _imagesResults.ParallelForEachAsync(async _result =>
  {
    Image toCompareData = FaceRecognition.LoadImageFile(_result.Value.filePath);
    _result.Value.toCompareData = toCompareData;
  });

  await _imagesResults.ParallelForEachAsync(async _result =>
  {
    _result.Value.landmarks = _FaceRecognition.FaceLandmark(_result.Value.toCompareData, null, PredictorModel.Large, Model.Cnn);
  });`

When i use this, i dont have problem. But when i use multiple instances of this code, i get "cuda memory" error. Whe i use the same to get FaceLocation, or Distances, i dont have problem with multiple instances. But when i call FaceLandmark() trow me that.

I have rtx2070 in my laptop.

Thank you!!!

takuya-takeuchi commented 3 years ago

@Hakentha

FaceLandmark call FaceLocation if you do not pass location argument. And you specify Model.Cnn, it means that FRDN use DNN and consume GPU memory. I guess your program does not throw exception when calling FaceLocation with Model.Hog. And distance method never use GPU memory.

And I have some advices,

Hakentha commented 3 years ago

Thank you for ur response. You help me a lot!!!