Comments (6)
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.
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.
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.
Has a ResNet example been made?
from pytorch-cnn-visualizations.
@utkuozbulak did you make that example about resnet?
from pytorch-cnn-visualizations.
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)
- Why this cam-zoo don't have grad-cam++?May you add grad-cam++ in this project? HOT 1
- Support for LayerCAM HOT 4
- a question on "cam = np.ones(target.shape[1:], dtype=np.float32)" in gradcam.py HOT 2
- How to get the sampling points with deformable conv? HOT 1
- A question about the method to get output from specific layer HOT 4
- Support for non-VGG models. HOT 1
- question on image generation
- Image Reconstruction size is same as conv1 layer HOT 1
- Visualizations for CNN trained on timeseries classification HOT 1
- Could you please provide the feature importance included dataset that has been generated? HOT 1
- AttributeError: 'MyCNN' object has no attribute 'features' HOT 1
- Extract gradient without model_output HOT 3
- GradCAM
- GradCAM HOT 1
- Why take np.maximum(cam, 0) in GradCam? HOT 1
- attention HOT 1
- Help with understanding layer backpropagation. HOT 1
- Can this tool be used on non-classification tasks HOT 1
- Code application related issues HOT 1
- RUN
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytorch-cnn-visualizations.