Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use MIVisionX on AMD 890M GPU #1477

Open
zydjohnHotmail opened this issue Jan 8, 2025 · 5 comments
Open

How to use MIVisionX on AMD 890M GPU #1477

zydjohnHotmail opened this issue Jan 8, 2025 · 5 comments
Assignees
Labels
help wanted Extra attention is needed Under Investigation

Comments

@zydjohnHotmail
Copy link

Hi,
I have a new AMD AI HX370 CPU with 890M GPU.
I have some old code using OpenCv to do something like find contour; but the performance on CPU is not good.
I have installed HIP SDK 6.2; so I want to know if I can write some C++ code using HIP SDK to do the same job like find contour in OpenCv but using basically GPU to improve the performance.
If it is possible, please show me some code.
Thanks,

@lucbruni-amd
Copy link

Hi @zydjohnHotmail, thanks for opening this issue!

Would you mind confirming whether you're on Windows, since you have mentioned installing the HIP SDK? I will start with the fact that the AMD 890M is not supported by ROCm (see here), so you may not be able to use the GPU backend for your workload in this case.

As for using MIVisionX, follow the installation guide here depending on your environment. You can give it a try by verifying with our samples, where the canny sample might be of particular interest to you. If a GPU is supported, you would be able to select it to run OpenVX graphs through runvx's -affinity option.

If you would like to use HIP to write a kernel to find contours similarly to your OpenCV program, I would first check whether a simple HIP kernel may be executed on your 890M. There are C++ code examples here you could try running against your GPU first to verify whether this is possible.

Please let me know if you have additional questions or concerns. Thank you!

@zydjohnHotmail
Copy link
Author

Hi,
Yes, I am using Windows 11. And I have tested quite a number of the code in the example code, but they are too special. I can show you one of my OpenCvSharp code for detect contours:

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;

public class ContourDetection
{
public static void Main()
{
// Load the image
Mat src = Cv2.ImRead("path_to_your_image.jpg");
Mat gray = new Mat();
Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);

    // Initial threshold parameters
    double thresholdValue = 100;
    double maxValue = 255;

    List<Point[]> contours = new List<Point[]>();

    // Dynamic threshold adjustment
    for (int i = 0; i < 3; i++)
    {
        Mat binary = new Mat();
        Cv2.Threshold(gray, binary, thresholdValue, maxValue, ThresholdTypes.Binary);

        // Find contours
        Cv2.FindContours(binary, out contours, out _, RetrievalModes.List, ContourApproximationModes.ApproxSimple);

        // Filter contours by length and dimensions
        var railContours = contours.Where(c => Cv2.ArcLength(c, true) > src.Width / 2).ToList();
        var dogContours = contours.Where(c => {
            Rect rect = Cv2.BoundingRect(c);
            return rect.Width > rect.Height && rect.Height > src.Height / 20 && rect.Width > src.Width / 20;
        }).ToList();

        // Check if we have the desired number of contours
        if (railContours.Count >= 1 && dogContours.Count <= 6)
        {
            break;
        }

        // Adjust threshold value for the next iteration
        thresholdValue -= 30;
    }

    // Draw contours on the image
    Mat result = src.Clone();
    Cv2.DrawContours(result, contours, -1, Scalar.Red, 2);
    Cv2.ImShow("Contours", result);
    Cv2.WaitKey(0);
}

}
=> If it is easy for you to understand this simple code, then simply show me the HIP SDK/ROCm code which can do the same job. Those OpenCvSharp is rather easy to understand, and it can do the job, but even with Powerful AMD HX 370 CPU, the performance is not good, so I want to see if I can port this code to MIVisionX.
By the way, I can see there are a lot of CUDA code for similar job, as NVIDIA has its computer vision library, but I think AMD says AMD GPU can do the same job as NVIDIA GPU, supporting CUDA instructions. Then give me some idea if 890M GPU can run CUDA instructions or not. By the way, if 890M GPU is not able to do so, then the new GPU, like RX9070 series card can do this or not? Run CUDA instructions.
Thanks for your help.

@lucbruni-amd
Copy link

lucbruni-amd commented Jan 9, 2025

Hi @zydjohnHotmail, thank you for your response.

For MIVisionX porting, since you already have OpenCV code (albeit in C#) I would first recommend you take a look at amd_opencv, an extension of amd_openvx. You will have to make changes to your code to accommodate the OpenVX API, while maintaining your OpenCV image processing and function calls (through OpenVX kernels listed in the first link). Then, when running the application you could specify the device via environment variables. As I mentioned in my previous comment, your GPU is not supported by ROCm, so there would likely be a CPU-backend fallback, which is not what you desire given that the changes you will need to make to the code are non-trivial and you are looking for GPU performance.

However, it is good news that you are able to find CUDA code for a similar job. Though CUDA programs are not directly supported on AMD GPUs, we maintain a tool known as Hipify that can translate CUDA code to HIP C++ code. This is necessary because CUDA programs are not supported on AMD GPUs. It is not supported on Windows currently but you can use WSL for the translations. My suggestion is to first try this out and see if you are able to execute a simple HIP kernel on your GPU. If not, then the card is certainly unsupported unfortunately and you will need a supported GPU. You do not need the new RX9070 card (although it is likely to be supported by ROCm in the future) to do this, take a look at the supported GPUs for Windows for other options.

Please don't hesitate to let me know if you have any further questions. Thanks!

@zydjohnHotmail
Copy link
Author

Hi,
Thanks for your advices, since I don't have experience with amd_opencv, it could take some time to pick up. However, if you have experience with amd_opencv, then is it possible that you translate my OpenCvSharp code into amd_opencv code and do the exactly job as described by the code.
This way, I could pick up amd_opencv very quickly.
Thanks,

@lucbruni-amd
Copy link

lucbruni-amd commented Jan 10, 2025

Hi @zydjohnHotmail,

Thank you for following up. To guide you, here are the sources of OpenVX kernels that access OpenCV functionality, implemented from the list I sent previously. I do not see a contouring example that matches your desired code, but the implementation of canny could help you start with a C++ version of your code for use as an OpenVX kernel (I have checked the OpenCV documentation, and there are 1:1 API mappings between C#/C++ for things like Threshold and FindContours). If you feel the documentation on amd_opencv is insufficient, I encourage you to open another ticket on this and I'd be happy to help further.

We do not provide 1:1 code translations at the moment, but if you complete the code based on the guides above, I'd be happy to help debug any problems you have. Sorry for the inconvenience.

Feel free to let me know if you have any further questions or thoughts. Thank you!

@kiritigowda kiritigowda added the help wanted Extra attention is needed label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed Under Investigation
Projects
None yet
Development

No branches or pull requests

4 participants