hi all, at present, there is only C + + classification code about TVM 
deployment, but there is no target detection code. If anyone sees this problem, 
please help me solve the problem and share your TVM version code about C + + 
Darknet. Thank you very much!!!

Here is my code, but I can only write so much, I can’t realize the following 
processing
```
#include <dlpack/dlpack.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h>
#include <tvm/runtime/packed_func.h>
#include <fstream>
#include <opencv2/opencv.hpp>

#include <cuda_runtime.h>
using namespace std;

void Mat_to_CHW(float *data, cv::Mat &frame)
{
    assert(data && !frame.empty());
    unsigned int volChl = 416 * 416;

    for(int c = 0; c < 3; ++c)
    {
        for (unsigned j = 0; j < volChl; ++j)
            data[c*volChl + j] = static_cast<float>(float(frame.data[j * 3 + 
c]) / 255.0);
    }

}

int main() {
    tvm::runtime::Module mod_dylib = 
tvm::runtime::Module::LoadFromFile("/data_2/git/onnx_to_tvm/darknet.so");
    std::ifstream json_in("/data_2/git/onnx_to_tvm/darknet.json", std::ios::in);
    std::string json_data((std::istreambuf_iterator<char>(json_in)), 
std::istreambuf_iterator<char>());
    json_in.close();
    // parameters in binary
    std::ifstream params_in("/data_2/git/onnx_to_tvm/darknet.params", 
std::ios::binary);
    std::string params_data((std::istreambuf_iterator<char>(params_in)), 
std::istreambuf_iterator<char>());
    params_in.close();

    TVMByteArray params_arr;
    params_arr.data = params_data.c_str();
    params_arr.size = params_data.length();
    int dtype_code = kDLFloat;
    int dtype_bits = 32;
    int dtype_lanes = 1;
    int device_type = kDLGPU;
    int device_id = 0;
    tvm::runtime::Module mod = 
(*tvm::runtime::Registry::Get("tvm.graph_runtime.create"))
            (json_data, mod_dylib, device_type, device_id);
    DLTensor *x;
    int in_ndim = 4;
    int64_t in_shape[4] = {1, 3, 416, 416};
    TVMArrayAlloc(in_shape, in_ndim, dtype_code, dtype_bits, dtype_lanes, 
device_type, device_id, &x);
    //其中DLTensor是个灵活的结构,可以包容各种类型的张量,而在创建了这个张量后,我们需要将OpenCV中读取的图像信息传入到这个张量结构中:
    // 这里依然读取了papar.png这张图
    cv::Mat image = cv::imread("/home/em/.tvm_test_data/data/dog.jpg");
    cv::Mat frame, input;
    cv::cvtColor(image, frame, cv::COLOR_BGR2RGB);
    cv::resize(frame, input,  cv::Size(416,416), cv::INTER_CUBIC);

    float data[416 * 416 * 3];
    // 在这个函数中 将OpenCV中的图像数据转化为CHW的形式
    Mat_to_CHW(data, input);

    cudaMemcpy(x->data, &data, 3 * 416 * 416 * sizeof(float), 
cudaMemcpyHostToDevice);
    //memcpy(x->data, &data, 3 * 416 * 416 * sizeof(float));

    // get the function from the module(set input data)
    tvm::runtime::PackedFunc set_input = mod.GetFunction("set_input");
    set_input("data", x);

    // get the function from the module(load patameters)
    tvm::runtime::PackedFunc load_params = mod.GetFunction("load_params");
    load_params(params_arr);

    DLTensor* y;
    int out_ndim = 1;
    int64_t out_shape[4] = {1, 255 ,52, 52};
    TVMArrayAlloc(out_shape, out_ndim, dtype_code, dtype_bits, dtype_lanes, 
device_type, device_id, &y);

    // get the function from the module(run it)
    tvm::runtime::PackedFunc run = mod.GetFunction("run");
    // get the function from the module(get output data)
    tvm::runtime::PackedFunc get_output = mod.GetFunction("get_output");
    run();
    tvm::runtime::NDArray res;
    for(int k = 0; k< 3 ; ++k){
        std::cout << "yes" << std::endl;
        res = get_output(k*4);
        float p_res[255*52*52];
        cudaMemcpy(p_res, res->data, 255 * 52 * 52 * sizeof(float), 
cudaMemcpyDeviceToHost);
        //std::cout << res->ndim << std::endl;
        std::cout << res.Shape()[0] << std::endl;
        std::cout << res.Shape()[1] << std::endl;
        std::cout << res.Shape()[2] << std::endl;
        std::cout << res.Shape()[3] << std::endl;
//        cv::Mat out(int(res.Shape()[2]),int(res.Shape()[3]),CV_32FC(255));
//        cudaMemcpy(out.data, res->data, 
int(res.Shape()[2])*int(res.Shape()[3])*255* sizeof(float), 
cudaMemcpyDeviceToHost);
//        cout << out.cols << out.rows << out.channels() << endl;
//        std::cout << out << std::endl;
    }
    return 0;
}
```





---
[Visit 
Topic](https://discuss.tvm.ai/t/how-to-implement-tvm-c-darknet-code/6480/1) to 
respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.tvm.ai/email/unsubscribe/51d4b2ad3f18dedf0b1448e843992e3fcb19d74084457880c241176b08d5e614).

Reply via email to