Hello,
I’m testing a inference based on mxnet and resnet using c++ application.
As for this, I wrote the c++ code[1] and built it correctly.
The original example code I referred to used “cat.bin” file as input data.
So I converted cat.png I have to cat.bin.raw after resizing 256x256 to 224x224 using InfanView app[2].
However, I faced with a error[3] while running it.
Is there something I missed and could you guide me how I could test it using c++ app?
Thanks,
Inki Dae
- [1] c++ application
#include <dlpack/dlpack.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h>
#include <tvm/runtime/packed_func.h>
#include
#include
#include
int main(void)
{
// tvm module for compiled functions
tvm::runtime::Module mod_syslib = tvm::runtime::Module::LoadFromFile("./mxnet_resnet18_v1.so");
// json graph
std::ifstream json_in("./mxnet_resnet18_v1.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("./mxnet_resnet18_v1.params", std::ios::binary);
std::string params_data((std::istreambuf_iterator<char>(params_in)), std::istreambuf_iterator<char>());
params_in.close();
// parameters need to be TVMByteArray type to indicate the binary data
TVMByteArray params_arr;
params_arr.data = params_data.c_str();
params_arr.size = params_data.length();
std::cout << "param size : " << params_arr.size << std::endl;
int dtype_code = kDLFloat;
int dtype_bits = 32;
int dtype_lanes = 1;
int device_type = kDLCPU;
int device_id = 0;
// get global function module for graph runtime
tvm::runtime::Module mod = (*tvm::runtime::Registry::Get("tvm.graph_runtime.create"))(json_data, mod_syslib, device_type, device_id);
DLTensor* x;
int in_ndim = 4;
int64_t in_shape[4] = {1, 3, 224, 224};
TVMArrayAlloc(in_shape, in_ndim, dtype_code, dtype_bits, dtype_lanes, device_type, device_id, &x);
// load image data saved in binary
std::ifstream data_fin("./cat.bin.raw", std::ios::binary);
data_fin.read(static_cast<char*>(x->data), 3 * 224 * 224 * 4);
// 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);
// get the function from the module(run it)
tvm::runtime::PackedFunc run = mod.GetFunction("run");
run();
DLTensor* y;
int out_ndim = 1;
int64_t out_shape[1] = {1000, };
TVMArrayAlloc(out_shape, out_ndim, dtype_code, dtype_bits, dtype_lanes, device_type, device_id, &y);
// get the function from the module(get output data)
tvm::runtime::PackedFunc get_output = mod.GetFunction("get_output");
get_output(0, y);
// get the maximum position in output vector
auto y_iter = static_cast<float*>(y->data);
auto max_iter = std::max_element(y_iter, y_iter + 1000);
auto max_index = std::distance(y_iter, max_iter);
std::cout << "The maximum position in output vector is: " << max_index << std::endl;
TVMArrayFree(x);
TVMArrayFree(y);
return 0;
}
-
[3] error log
daeinki@daeinki-linux:~/project/working/tvm_pc/tvm/build/test/app_test_pc$ ./mxnet_resnet
param size : 46765317
terminate called after throwing an instance of ‘dmlc::Error’
what(): [12:40:27] /home/daeinki/project/working/test/tvm/src/runtime/graph/graph_runtime.cc:151: Check failed: data->ndim == data_out->ndim (2 vs. 1)
Stack trace returned 9 entries:
[bt] (0) ./mxnet_resnet(dmlc::StackTraceabi:cxx11+0x54) [0x409424]
[bt] (1) ./mxnet_resnet(dmlc::LogMessageFatal::~LogMessageFatal()+0x2a) [0x4096f0]
[bt] (2) /home/daeinki/project/s5pc210/public/tizen_5.0/working/tvm_pc/tvm/build/test/app_test_pc/…/…/…/build/libtvm.so(tvm::runtime::GraphRuntime::CopyOutputTo(int, DLTensor*)+0x1ac) [0x7f20f5b99bfc]
[bt] (3) /home/daeinki/project/s5pc210/public/tizen_5.0/working/tvm_pc/tvm/build/test/app_test_pc/…/…/…/build/libtvm.so(+0x71def5) [0x7f20f5b94ef5]
[bt] (4) ./mxnet_resnet(std::function<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)>::operator()(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) const+0x5a) [0x40a892]
[bt] (5) ./mxnet_resnet(tvm::runtime::TVMRetValue tvm::runtime::PackedFunc::operator()<int, DLTensor*&>(int&&, DLTensor*&) const+0xd0) [0x40b09a]
[bt] (6) ./mxnet_resnet(main+0x6cb) [0x4088c9]
[bt] (7) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f20f4714830]
[bt] (8) ./mxnet_resnet(_start+0x29) [0x408019]