Visualize feature maps of the 1st layer

jyothi_sri: <p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(1, 6, 5)<br>
self.pool = nn.MaxPool2d(2, 2)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 4 * 4, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 10)</p>
<pre><code>def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(-1, 16 * 4 * 4)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x
</code></pre>
<p>I want to visualize the 6 output feature maps of the self.conv1 1st layer in network. could anybody please help .</p>

ptrblck: <p>You could directly return the desired activation from the <code>forward</code> method or you could use forward hooks as described <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">here</a>.</p>

jyothi_sri: <p>ok will try n thank you</p>

jyothi_sri: <p>model = MyModel()<br>
model.fc2.register_forward_hook(get_activation(‘fc2’))<br>
x = torch.randn(1, 25)<br>
output = model(x)<br>
print(activation[‘fc2’])</p>
<p>what is this x = torch.randn(1, 25)?</p>

ptrblck: <p><code>x</code> is just a random input which is used as an example to show how hooks are used.</p>

jyothi_sri: <p>class Net(nn.Module):</p>
<pre><code>def __init__(self):
    super(Net, self).__init__()
    # 1 input image channel, 6 output channels, 5x5 square convolution
    # kernel
    self.conv1 = nn.Conv2d(1, 6, 5)
    self.conv2 = nn.Conv2d(6, 16, 5)
    # an affine operation: y = Wx + b
    #self.fc1 = nn.Linear(16 * 5 * 5, 120)  # 5*5 from image dimension
    self.fc1 = nn.Linear(13456, 120)
    self.fc2 = nn.Linear(120, 84)
    self.fc3 = nn.Linear(84, 1)

def forward(self, x):
    # Max pooling over a (2, 2) window
    print('x', str(x.size()))
    x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
    print('x', str(x.size()))
    #ts.show(x,ncols=4)
    # If the size is a square, you can specify with a single number
    x = F.max_pool2d(F.relu(self.conv2(x)), 2)
    x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x
</code></pre>
<p>activation = {}<br>
def get_activation(name):<br>
def hook(model, input, output):<br>
activation[name] = output.detach()<br>
return hook</p>
<p>activation = {}<br>
def get_activation(name):<br>
def hook(model, input, output):<br>
activation[name] = output.detach()<br>
return hook</p>
<p><span class="hashtag">#model</span> = Net()<br>
model = Net()<br>
model.fc2.register_forward_hook(get_activation(‘conv1’))<br>
x = torch.randn(1,6)<br>
output = model(x)<br>
print(activation[‘conv1’])</p>
<p>for  the above code  ,since i want conv1 features gave x = torch.randn(1,6)<br>
but got this error RuntimeError: Expected 4-dimensional input for 4-dimensional weight [6, 1, 5, 5], but got 2-dimensional input of size [1, 6] instead</p>

jyothi_sri: <p>does this x depends on our model</p>

ptrblck: <p>Yes, the model input (in my example <code>x</code>) depends on the model architecture. The error is raised since you are passing a wrong input to your model and you should use your real samples instead of my example input.</p>

jyothi_sri: <p>i gave x as x = torch.randn(1,6,5,5) y because  in self.conv1 = nn.Conv2d(1, 6, 5) is like this and got the error as<br>
RuntimeError: Given groups=1, weight of size [6, 1, 5, 5], expected input[1, 6, 5, 5] to have 1 channels, but got 6 channels instead</p>

jyothi_sri: <p>how to directly return the desired activation function from the forward method can you please give an example</p>

jyothi_sri: <p>how to directly return the desired activation function from the forward method can you please give an example</p>

ptrblck: <p>This should work:</p>
<pre><code class="lang-python">def forward(self, x):
    x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
    x = F.max_pool2d(F.relu(self.conv2(x)), 2)
    x = torch.flatten(x, 1)
    x = F.relu(self.fc1(x))
    act = self.fc2(x)
    x = F.relu(act)
    x = self.fc3(x)
    return x, act
</code></pre>

jyothi_sri: <p>will try with ur sample code ,thank u</p>