opencv Openvino运行时权重更改

mo49yndu  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(140)

我尝试从opencv重新实现dasiamrpn tracker,但是使用openvino推理。在init方法中,我假设一些层参数已经被r1和cls1头产生的Tensor改变了

siamRPN.setInput(blob);
    cv::Mat out1;
    siamRPN.forward(out1, "63");

    siamKernelCL1.setInput(out1);
    siamKernelR1.setInput(out1);

    cv::Mat cls1 = siamKernelCL1.forward();
    cv::Mat r1 = siamKernelR1.forward();
    std::vector<int> r1_shape = { 20, 256, 4, 4 }, cls1_shape = { 10, 256, 4, 4 }; //same shape as conv layers 65 and 68

    siamRPN.setParam(siamRPN.getLayerId("65"), 0, r1.reshape(0, r1_shape));
    siamRPN.setParam(siamRPN.getLayerId("68"), 0, cls1.reshape(0, cls1_shape));

但是我在openvino中找不到一个API或者某种方法来实现这个。有人遇到过这样的问题吗?
what I'm trying to do
我假设权重存储在这两个节点中:

<layer id="31" name="new_layer_2.weight" type="Const" version="opset1">
        <data element_type="f32" shape="10, 256, 4, 4" offset="17349120" size="163840"/>
        <rt_info>
            <attribute name="fused_names" version="0" value="new_layer_2.weight"/>
        </rt_info>
        <output>
            <port id="0" precision="FP32" names="new_layer_2.weight">
                <dim>10</dim>
                <dim>256</dim>
                <dim>4</dim>
                <dim>4</dim>
            </port>
        </output>
    </layer>
    <layer id="38" name="new_layer_1.weight" type="Const" version="opset1">
        <data element_type="f32" shape="20, 256, 4, 4" offset="19873280" size="327680"/>
        <rt_info>
            <attribute name="fused_names" version="0" value="new_layer_1.weight"/>
        </rt_info>
        <output>
            <port id="0" precision="FP32" names="new_layer_1.weight">
                <dim>20</dim>
                <dim>256</dim>
                <dim>4</dim>
                <dim>4</dim>
            </port>
        </output>
    </layer>

我可以在模型操作中查看此节点

auto ops = model->get_ops();

但是我不知道如何改变它的重量数据。有没有办法在运行时改变它?

k3fezbri

k3fezbri1#

您可以使用OpenVINO Runtime参考推理管道来推断模型,它显示了您需要在应用程序代码中执行的步骤。
要在一个应用程序中读取多个网络,可以参考Pedestrian Tracker C++ Demo

dxpyg8gm

dxpyg8gm2#

我找到了解决办法

auto ops = _siamRPN->get_ordered_ops();
    auto cls_weight = std::make_shared<ov::opset1::Constant>(ov::element::f32, ov::Shape{ 10,256,4,4 }, cls1.data());
    cls_weight->set_friendly_name(ops[111]->get_friendly_name());        // operation name
    cls_weight->output(0).set_names(ops[111]->output(0).get_names());
    auto r1_weight = std::make_shared<ov::opset1::Constant>(ov::element::f32, ov::Shape{ 20,256,4,4 }, r1.data());
    r1_weight->set_friendly_name(ops[127]->get_friendly_name());        // operation name
    r1_weight->output(0).set_names(ops[127]->output(0).get_names());

    _siamRPN->replace_node(ops[111], cls_weight);
    _siamRPN->replace_node(ops[127], r1_weight);
    _siamRPN->validate_nodes_and_infer_types();
    
    compiled_siamRPN = std::make_shared < ov::CompiledModel>(core->compile_model(_siamRPN, "CPU"));

但我认为这不是最好的解决办法。有没有更清晰、更快速的方法?

相关问题