Coder Social home page Coder Social logo

Comments (6)

utkuozbulak avatar utkuozbulak commented on July 21, 2024 2

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.

from pytorch-cnn-visualizations.

Raunak13 avatar Raunak13 commented on July 21, 2024

Hi @visonpon , @utkuozbulak , I am getting the same errors. Can you tell me what did you do?
While using model.getnamedchildren() the function 'get_output_from_specific_layer' returns values for first few layers, but then it gives the error: "RuntimeError: Given groups=1, weight[48, 192, 1, 1], so expected input[1, 64, 105, 105] to have 192 channels, but got 64 channels instead"
I am using Inception Resnet V3

from pytorch-cnn-visualizations.

utkuozbulak avatar utkuozbulak commented on July 21, 2024

I'm also getting some emails about how hard the whole procedure becomes when the model has nested elements (like Resnets). I might just make a ResNet example soon.

from pytorch-cnn-visualizations.

himat avatar himat commented on July 21, 2024

Has a ResNet example been made?

from pytorch-cnn-visualizations.

Saharkakavand avatar Saharkakavand commented on July 21, 2024

@utkuozbulak did you make that example about resnet?

from pytorch-cnn-visualizations.

utkuozbulak avatar utkuozbulak commented on July 21, 2024

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.

from pytorch-cnn-visualizations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.