如何在 Maskcrnn libtorch 中获取元组对象返回的值
Posted
技术标签:
【中文标题】如何在 Maskcrnn libtorch 中获取元组对象返回的值【英文标题】:How to get values return by Tuple Object in Maskcrnn libtorch 【发布时间】:2021-10-18 03:25:22 【问题描述】:我是 C++ 和 libtorch 的新手,我尝试通过 torchscript 加载模型并执行推理,代码如下:
torch::jit::script::Module module;
try
module = torch::jit::load("../../weights/card_extraction/pytorch/2104131340/best_model_27_mAP=0.9981_torchscript.pt");
catch (const c10::Error& e)
std::cerr << "Error to load model\n";
return -1;
std::cout << "Load model successful!\n";
torch::DeviceType device_type;
device_type = torch::kCPU;
torch::Device device(device_type, 0);
module.to(device);
torch::Tensor sample = torch::zeros(3, 800, 800);
std::vector<torch::jit::IValue> inputs;
std::vector<torch::Tensor> images;
images.push_back(sample);
/* images.push_back(torch::ones(3, 224, 224)); */
inputs.push_back(images);
auto t1 = std::chrono::high_resolution_clock::now();
auto output = module.forward(inputs);
auto t2 = std::chrono::high_resolution_clock::now();
int duration = std::chrono::duration_cast<std::chrono::milliseconds> (t2 - t1).count();
std::cout << "Inference time: " << duration << " ms" << std::endl;
std::cout << output << std::endl;
结果如下:
Load model successful!
[W mask_rcnn.py:86] Warning: RCNN always returns a (Losses, Detections) tuple in scripting (function )
Inference time: 2321 ms
(, [boxes: [ CPUFloatType0,4 ], labels: [ CPULongType0 ], scores: [ CPUFloatType0 ], masks: [ CPUFloatType0,1,800,800 ]])
如何使用 c++ 从返回的输出对象中获取值框、标签、分数和掩码? 我尝试了很多方法,但编译总是出错并抛出“c10::IValue”错误。
还有一个问题,为什么当我将模型转换为torchscript时,C++执行的时间推断比python慢? 非常感谢
【问题讨论】:
【参考方案1】:To draw bounding boxes you can try the following code
auto bbox = output.at("pred_boxes").toTensor();
int num_instances = bbox.sizes()[0];
std::cout << mask.size() << std::endl;
//cv::resize(img, img, cv::Size(244, 244));
for(int num = 0; num < bbox.sizes()[0]; ++num)
float x1 = bbox[num][0].item().toFloat();
float y1 = bbox[num][1].item().toFloat();
float x2 = bbox[num][2].item().toFloat();
float y2 = bbox[num][3].item().toFloat();
cv::rectangle(img, cv::Point2f(x1, y1), cv::Point2f(x2, y2), cv::Scalar(255));
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。【参考方案2】:您可以像这里一样访问元素:我可以解析元组参数并像张量格式一样访问它。它可以帮助你。
auto output1_t = output.toTuple()->elements()[0].toTensor();
auto output2_t = output.toTuple()->elements()[1].toTensor();
https://discuss.pytorch.org/t/how-can-i-get-access-to-first-and-second-tensor-from-tuple-returned-from-forward-method-in-libtorch-c-c/139741
【讨论】:
以上是关于如何在 Maskcrnn libtorch 中获取元组对象返回的值的主要内容,如果未能解决你的问题,请参考以下文章