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.

visualize model that not in the model zoo

See original GitHub issue

HI! as you said:

If you want to port this code to use it on your model that does not have such separation, you just need to do some editing on parts where it calls model.features and model.classifier.

I try to modify the model.features or .classifier part , but i get confused how to do it. Below is part of my script , hope that you can give some details about how to visualize own trained model.

model =torch.load(model_save_path)
for index,(layer,_) in enumerate(model.items()):
      # model.items() return the weight of this layer, eg, model.items()[0]=model.0.1.weight
      x = layer(x)
        ......

but i get the TypeError that the object is not callable. i wonder how to read the layer from the trained model that didn’t have features attribute. thanks~

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
utkuozbulakcommented, Mar 22, 2018

Hey, it doesn’t have to contain features, what is important is for the model to contain nn.Sequential so that you can iterate.

Let me elaborate.

You can have an __ init __() function like this:

...
def __init__(self):
  self.my_layers = nn.Sequential(
    nn.Conv2d(1, 6, (5, 5), padding=2),
    nn.Conv2d(1, 6, (5, 5), padding=2),
    nn.Conv2d(1, 6, (5, 5), padding=2),
    .
    .
    .
 )

def forward(self, input):
  output = self.my_layers(input)

or this

...
def __init__(self):
    self.layer1 = nn.Conv2d(1, 6, (5, 5), padding=2)
    self.layer2 = nn.Conv2d(1, 6, (5, 5), padding=2)
    self.layer3 = nn.Conv2d(1, 6, (5, 5), padding=2)
    .
    .
    .
 
def forward(self, x):
  x = self.layer1(x)
  x = self.layer2(x)
  x = self.layer3(x)
  ...

If you have a model that wraps up layers with nn.Sequential then iterating through layers one by one is super easy.

x = input
for single_layer in model.my_layers():
   x = single_layer(x)

If there is no nn.Sequential wrapping it up, you can’t use this form (becase there is no pre-defined iterative process) but you can use .modules() or .named_children().

Have a look at these: https://discuss.pytorch.org/t/module-children-vs-module-modules/4551 https://discuss.pytorch.org/t/how-to-loop-over-all-the-variable-in-a-nn-module/912

Hope it helps.

0reactions
utkuozbulakcommented, Nov 21, 2019

I had one done for one of my projects but did not put it in this repository.

Adapting the code to resnet, or for any other architecture that has nested blocks, is not hard. You only need to change two or three lines of code where it hooks the layers. Because resnet has residual blocks, what you want is to hook the layers inside these blocks and not the blocks themselves.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Model Zoo - Deep learning code and pretrained models for ...
ModelZoo curates and provides a platform for deep learning researchers to easily find code and pre-trained models for a variety of platforms and...
Read more >
FiftyOne Model Zoo - Voxel51
FiftyOne provides a Model Zoo that contains a collection of pre-trained models that you can download and run inference on your FiftyOne Datasets...
Read more >
python - How to use Tensorflows object detection model zoo ...
Unfortunately, almost all of the tutorials use label files (.pbtxt) that I do not have. On the appropriate download page of Tensorflow ...
Read more >
Why model in tensor flow model zoo have low mAP?
As far as I know, none of the current SOTA detection models have been able to achieve 0.70 mAP on coco. See:.
Read more >
Keras Applications
Model Size (MB) Top‑1 Accuracy Top‑5 Accuracy Parameters Depth Time (ms) per infer... Xception 88 79.0% 94.5% 22.9M 81 109.4 VGG16 528 71.3% 90.1% 138.4M...
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