首页 > 解决方案 > 如何在火炬张量中检索 x 和 y 坐标?

问题描述

我正在使用 mobilenetv2 模型。所以我正在使用检测并且我能够获得分数(一个对象与训练集列表上的某物的相似程度)和对象名称。我只需要弄清楚如何获取 x 和 y 坐标以及检测的宽度和高度。我提供了一个实时示例(作为注释),说明变量在整个运行时过程中的样子。我正在处理 [3, 224, 224] 的张量形状

    const ImageFrame& frame =
            cc->Inputs().Index(0).Value().Get<ImageFrame>();
    auto tensor = ImageFrameToNormalizedTensor(frame, 0, 255.);
    std::vector<torch::jit::IValue> inputs;
    inputs.push_back(tensor->unsqueeze(0));
       // tensor->unsqueeze(0).sizes() = [1, 3, 224, 224]
    auto result = model.forward(inputs).toTensor();
    result = result.squeeze(0);
       // result.argmax(0) = 733
       // result.index({result.argmax(0)}) = 7.50187


    auto output_detections = absl::make_unique<Detections>();
    const auto class_id = result.argmax(0).item<int>();
    const auto score = result.index({result.argmax(0)}).item<float>();
    Detection detection;
    detection.add_label_id(class_id);
    detection.add_score(score);

我能够获得 class_id 和分数。谁能指导我如何获得 x、y、w(宽度)和 h(高度)。

标签: machine-learningpytorchtensorlibtorch

解决方案


推荐阅读