question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can we get values of weights from any layer of the model

See original GitHub issue

I want to get the weight & bias values of any layer of choice, how can I access the same? I could get the Kernel Shape, stride, pad, inputs, output for a particular layer. But how do I get the parameter values of a layer from model.onnx file. I am new to Protobuf.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
TMVectorcommented, Jun 2, 2020

Hi @holoLens8, here is an example (I’m using mobilenetv2). You can load the model in Netron to explore visually and find the nodes you want.

image

You will be able to see the names of the node inputs. Here I have selected the first Conv node, and the weight input W is named mobilenetv20_features_conv0_weight. This code snippet will give you the constant (weight matrix, bias, etc.) as a numpy array:

import onnx, onnx.numpy_helper
model = onnx.load("path/to/model.onnx")
[tensor] = [t for t in model.graph.initializer if t.name == "mobilenetv20_features_conv0_weight"]
w = numpy_helper.to_array(tensor)

You can understand how to interpret the numpy array by looking at the operator documentation – for example the Conv operator says that the weight tensor is of shape:

(M x C/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps.

13reactions
houseroadcommented, Sep 19, 2018

For example, in python:

model = onnx.load('model.onnx')
weights = model.graph.initializer
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the weights of Keras model? - AI Pool
Keras has implemented some functions for getting or setting weights for every layer. layer.get_weights(): returns the weights of the layer ...
Read more >
How do I get weights and biases from my model?
In any case, take into account that there are layers which weights aren't saved (as they don't have weights), so if asking for...
Read more >
Get weights and biases of a particular layer of NN in MXNet
We can access weights and bias with the .data() method. We can access the weight or bias of a specific layer by using...
Read more >
Model Summary - Getting Layers With Weights - YouTube
Model Summary | Plotting Model | Getting Layers With Weights | Saving Models | Loading Weight ************************************This video ...
Read more >
Access all weights of a model - PyTorch Forums
Please note that, I know that weights can be accessed layer-wise ( my_mlp.layers[0].weight, my_mlp.layers[2].weight). Thanks with best regards.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found