1

I'd like to get the tensor of weights and biases for each layer of the network using the C++/Python API on the OpenVINO framework. I found this solution but unfortunately it uses an older API (release 2019 R3.1) that is no longer supported. For example, the class CNNNetReader does not exist in the latest OpenVINO. Does anyone know how to achieve this in new releases? (2020+)

Thanks!

shahar
  • 11
  • 1

2 Answers2

1

Can you try something like this? First you need to read-in a model, the iterate over the nodes in the graph and filter the Constants (I'm making a copy here because the nodes are held as shared pointers). Then each Constant node can be transformed into a vector of values if you're looking for that. To do that you can use the templated method cast_vector<T> and you can figure out which type to use by checking what the Constant holds (using get_element_type).

const auto model = core.read_model("path");

std::vector<std::shared_ptr<ov::op::v0::Constant>> weights;
const auto ops = model->get_ops();
std::copy_if(std::begin(ops), std::end(ops), std::back_inserter(weights), [](const std::shared_ptr<ov::Node>& op) {
    return std::dynamic_pointer_cast<ov::op::v0::Constant>(op) != nullptr;
});

for (const auto& w : weights) {
    if (w->get_element_type() == ov::element::Type_t::f32) {
        const std::vector<float> floats = w->cast_vector<float>();
    } else if (w->get_element_type() == ov::element::Type_t::i32) {
        const std::vector<int32_t> ints = w->cast_vector<int32_t>();
    } else if (...) { 
        ... 
    }
}
tomdol
  • 381
  • 3
  • 14
  • For anyone who want to do this in Python, you can pass the Node object to Numpy array constructor like `np.array(node)`. – witoong623 Dec 06 '22 at 03:02
0

Use the class Core and the method read_model to read the model. Next, use the methods in the class Model to get the detail for each layer.

import openvino.runtime as ov

ie = ov.Core()

network = ie.read_model('face-detection-adas-0001.xml')

# print(network.get_ops())

# print(network.get_ordered_ops())

# print(network.get_output_op(0))

# print(network.get_result())

Refer to OpenVINO Python API and OpenVINO Runtime C++ API for more information.

Wan_Intel
  • 76
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 05:53