shimat / opencvsharp

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

Caffe Performance Question #619

Closed turowicz closed 5 years ago

turowicz commented 5 years ago

I'm on OpenCvSharp 3.x and OpenCV 3.x compiled with GPU.

I've rewritten the following article in C# and am using the models the original paper author has published:

https://itywik.org/2018/03/26/age-and-gender-detection-with-opencv-on-the-raspberry-pi/

My question is about performance. Running these predictions takes around 100ms each, which is way slower in comparison to running FaceRecognition by @takuya-takeuchi.

Question Should I be investigating the performance issues or is it a normal processing time of raster data?

turowicz commented 5 years ago

my code:

        private (int Id, double Probability) Predict(Mat blob, string[] labels, Net net)
        {
            net.SetInput(blob, "data");

            using (var predictions = net.Forward("prob"))
            {
                GetMaxClass(predictions, out int classId, out double classProb);

                return (classId, classProb);
            }
        }

        private void GetMaxClass(Mat probBlob, out int classId, out double classProb)
        {
            using (var probMat = probBlob.Reshape(1, 1))
            {
                Cv2.MinMaxLoc(probMat, out _, out classProb, out _, out var classNumber);
                classId = classNumber.X;
            }
        }
turowicz commented 5 years ago

cc @shimat

shimat commented 5 years ago

Did you try setPreferableTarget and/or setPreferableBackend ? I'm sorry but I'm not familiar with dnn. Their implementation in OpenCvSharp is halfway. The enum flags are not defined.

net.SetPreferableTarget(1); // DNN_TARGET_OPENCL
turowicz commented 5 years ago

@shimat thank you I will try that

turowicz commented 5 years ago

Using setPreferableTarget(1) sorted out the problem. Using setPreferableBackend together with setPreferableTarget resulted in an error.

Problem solved @shimat, thanks!

joreg commented 5 years ago

@turowicz have you since tried this with latest opencvsharp4? @shimat or would it no longer work in opencvsharp4 because you dropped gpu support there?

i try to understand if the OpenCL support for DNN is probably independent of the general opencv CUDA support and therefore should work also with opencvsharp4?

turowicz commented 5 years ago

@joreg I'm still on 3.4.x because I'm using a lot of frameworks that depend on it. Dlib, Tensorflow and others.