1

I have been looking for a way to get the tensor of weights/parameters and biases for each layer of the network using the C++ API on the OpenVINO framework. I can't find anything on the documentation nor any example on the samples. How could I extract these tensors?

Thanks, César.

EDIT: Code for getting weights and biases separately:

for (auto&& layer : this->pImplementation->network) {
        weightsbuf << "Layer name: " << layer->name << std::endl;
        weightsbuf << "Parameters:" << std::endl;

        for (auto&& param : layer->params) {

            weightsbuf << '\t' << param.first << ": " << param.second << std::endl;
        }

        std::vector<int> kernelvect;
        auto kernelsize = layer->params.at("kernel");

        std::stringstream ss(kernelsize);

        // split by comma kernel size
        for (int i; ss >> i;) {
            kernelvect.push_back(i);
            if (ss.peek() == ',')
                ss.ignore();
        }
        int noutputs = std::stoi(layer->params.at("output"));
        int nweights = kernelvect[0] * kernelvect[1] * noutputs;
        int nbias = noutputs;

        for (auto&& blob : layer->blobs) {
            weightsbuf << '\t' << blob.first << ": ";
            for (size_t w = 0; w < nweights; ++w) {
                weightsbuf << blob.second->buffer().as<float*>()[w] << " ";
            }
            weightsbuf << std::endl;
            weightsbuf << '\t' << "biases:";
            for (size_t b = 0; b < nbias; ++b) {
                weightsbuf << blob.second->buffer().as<float*>()[nweights + b] << " ";
            }
        }
        weightsbuf << std::endl;
    }

1 Answer 1

1

Looks like there is no official example to show that functionality. I haven't found anything like that as well.

I implemented a basic sample which prints information about each layer of a network. Please take a look: https://github.com/ArtemSkrebkov/dldt/blob/askrebko/iterate-through-network/inference-engine/samples/cnn_network_parser/main.cpp

I believe the idea how to use API is clear.

The sample is based on the current state of the dldt repo (branch '2019', it corresponds to the release 2019 R3.1)

Another link, which might be useful, is the documentation on CNNLayer class: https://docs.openvinotoolkit.org/latest/classInferenceEngine_1_1CNNLayer.html

3
  • Thank you very much for your help! I modified your code a little bit to print the weights and biases, is this correct (you can see the changes on the original post above)? or they save the biases in another location? OpenVINO should have documentation regarding the way their parameters file (.bin) is organized... Feb 1, 2020 at 18:27
  • You're very welcome! Please take a look at my example more careful. The sample already print weights and biases. Member 'blobs' contains 'weights' as the first element and 'biases' as the second. I print only first three bytes of each array, you can extend the sample to print the whole array. The issue of your code is that you are considering that 'blob' contains weights and biases together. But weights and biases are separate elements of 'layer->blobs'. if something is still unclear, feel free to ask :) Feb 2, 2020 at 13:55
  • More information about format of IR you can find here docs.openvinotoolkit.org/latest/… Feb 2, 2020 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.