我运行Tensorflow使用它的C API。
我有以下调用,它在finalOutput中返回四个张量:
std::string str1 = "detection_boxes";
std::string str2 = "detection_scores";
std::string str3 = "detection_classes";
std::string str4 = "num_detections";
std::vector<Tensor> finalOutput;
status = session->Run({ {InputName, inputTensor} }, { str1, str2, str3, str4 }, {}, &finalOutput);
std::cout << finalOutput[0].DebugString() << std::endl;
print语句输出以下内容:
”张量
现在我有了一个包含100个元素的张量,每个元素有4个值,我如何迭代元素和值?
非常感谢你的帮助!
我能做到这一点
// tensor<float, 3>: 3 here because it's a 3-dimension tensor
auto output_detection_boxes = outputs[0].tensor<float, 3>();
std::cout << "detection boxes" << std::endl;
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 4; ++j)
// using (index_1, index_2, index_3) to access the element in a tensor
std::cout << output_detection_boxes(0, i, j)
<< "\t";
std::cout << std::endl;
}
这是输出
detection boxes
0.0370 0.0465 0.8914 0.3171
0.0924 0.3944 0.9344 0.9769
0.0155 0.2988 0.8812 0.5263
0.1518 0.3817 0.9316 1.0000
0.0000 0.3285 0.8447 0.6669
0.0389 0.2030 0.8504 0.4749
...
(100 in total)