shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.37k stars 1.15k forks source link

Memory usage has doubled in using a Darknet DNN model #1594

Open RaimondB opened 1 year ago

RaimondB commented 1 year ago

Summary of your issue

Since version 4.7.0.20230115 (compared to 4.6.0.20220608 and earlier) using Dnn Darknet model has doubled native memory usage. Using a memory profiler I have been able to trace it down to the use of the Net.Forward method.

Also, I have seen that according to the profiler Net objects get created multiple times. Which is strange since only 1 object exists that is holding the Net object as a resource.

I have always found it strange that also the earlier version had double the size of the neural net in memory usage. I would expected that as soon as it is loaded, the filebuffers could be freed and you would only have the size of the net once in memory. Or is it that the memory size of a Darknet model is twice the size on disk?

Environment

Windows 11, Ubuntu 20

What did you do when you faced the problem?

I am using the Yolo Darknet model (that is around 300 MB) to do detections. This program has always used around 690 MB, but since the upgrade to the opencvsharp 4.8 version, its usage had doubled to 1.2GB for unknown reasons.

Example code:

//loading the net: memory jump with around 300MB, expected because of the model size
var createdNet = OpenCvSharp.Dnn.CvDnn.ReadNetFromDarknet(cfg, weight);

//executing the model: in the past another 300MB was added, but now 900MB
// Note: The "outs" Mat array is being reused for each execution
using var blob = CvDnn.BlobFromImages(images, scaleFactor, scaleSize, crop: false);
nnet.SetInput(blob);
nnet.Forward(outs, _outNames);

Output:

Memory usage of the app has doubled.

What did you intend to be?

Memory usage should be stable