shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

OpenCvSharp GetLayer #1560

Open Visioneery opened 1 year ago

Visioneery commented 1 year ago

Summary of your issue

Hi! Currently I am working on implementing SRCNN modules to upscale images using OpecCvSharp library. https://github.com/opencv/opencv_contrib/tree/4.x/modules/dnn_superres I am facing few issues , for example first one is related to ESPCN_x2 OpenCvSharp.OpenCVException: 'ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0' as far as I understood I need to modify the input channels of the neural network model to be divisible by the number of groups But unfortunately I don't see anything that allows to access specific layers of models , like model.GetLayer().. etc.

Another issue is related to that I am not able to set proper input blob to the model FSRCNN_x2, OpenCvSharp.OpenCVException: 'Requested blob "conv1" not found' , I am able to get layer names, but probably in this case I am also not able to access them.

Environment

Local, Windows forms application, .NET Core 3.1 , OpenCvSharp4, OpenCvSharp4.Extensions, OpenCvSharp4.runtime.win

What did you do when you faced the problem?

Upscaling image using 2 different pre trained models which are provided by opencv

Example code:

        public void UpscaleUsingDNN2(string originalImagePath)
        {

            // Load the image
            Mat image = Cv2.ImRead(originalImagePath, ImreadModes.Color);

            // Load the model
            var net = CvDnn.ReadNetFromTensorflow("C:\\Users\\User\\Desktop\\Upscaling\\models\\ESPCN_x2.pb");

            // Prepare the input blob
            Mat blob = CvDnn.BlobFromImage(image, 1.0 / 255.0, new OpenCvSharp.Size(0, 0), new Scalar(0, 0, 0), true, false);
            net.SetInput(blob);

            // Forward pass
            Mat result = net.Forward();

            // Reshape the result to image size
            int height = image.Rows;
            int width = image.Cols;
            int channels = result.Cols;
            result = result.Reshape(1, height, width, channels);

            // Convert result to 8-bit image
            result.ConvertTo(result, MatType.CV_8U, 255.0);

            // Display the input and output images
            Cv2.ImShow("Input Image", image);
            Cv2.ImShow("Output Image", result);
            Cv2.WaitKey(0);
        }

        public void UpscaleUsingDNN3(string originalImagePath)
        {
            // Load the input image
            var inputImage = Cv2.ImRead(originalImagePath, ImreadModes.Color);

            // Load the FSRCNN_x2 model
            var model = CvDnn.ReadNetFromTensorflow("C:\\Users\\User\\Desktop\\Upscaling\\models\\FSRCNN_x2.pb");

            // Resize the input image to the desired upscale factor (2x in this example)
            var upscaledSize = new OpenCvSharp.Size(inputImage.Cols * 2, inputImage.Rows * 2);
            var inputImageResized = new Mat();
            Cv2.Resize(inputImage, inputImageResized, upscaledSize);

            // Convert the input image to a blob
            var blob = CvDnn.BlobFromImage(inputImageResized, 1.0 / 255.0, new OpenCvSharp.Size(0, 0), new Scalar(0, 0, 0), false, false);

            // Set the input blob to the model
            model.SetInput(blob, "conv2");

            // Forward pass to get the output blob
            var outputBlob = model.Forward("NHWC_output");

            // Split the channels of the output blob
            Mat[] channels = new Mat[outputBlob.Channels()];
            Cv2.Split(outputBlob, out channels);

            // Convert the output channels to Mats
            var outputMat1 = channels[0];
            var outputMat2 = channels[1];
            var outputMat3 = channels[2];

            // Merge the output Mats to get the final output Mat
            var outputMat = new Mat();
            Cv2.Merge(new Mat[] { outputMat1, outputMat2, outputMat3 }, outputMat);

            // Save the output image
            var outputImagePath = @"path/to/output/image.png";
            Cv2.ImWrite(outputImagePath, outputMat);

            // Cleanup
            model.Dispose();
            outputBlob.Dispose();
            inputImage.Dispose();
            inputImageResized.Dispose();
            outputMat1.Dispose();
            outputMat2.Dispose();
            outputMat3.Dispose();
            outputMat.Dispose();
        }

Output:

OpenCvSharp.OpenCVException: 'ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0'
OpenCvSharp.OpenCVException: 'Requested blob "conv1" not found'

What did you intend to be?

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.