# How can I print the image of each layer in CNN?

**URL:** <https://discuss.pytorch.org/t/how-can-i-print-the-image-of-each-layer-in-cnn/67597>\
**Category:** Uncategorized\
**Created:** [January 25, 2020, 3:46am UTC](https://discuss.pytorch.org/t/how-can-i-print-the-image-of-each-layer-in-cnn/67597 "2020-01-25T03:46:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yolkandwhite](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yolkandwhite/32/17604_2.png) [@Yolkandwhite](https://discuss.pytorch.org/u/Yolkandwhite)\
**Post date:** [January 25, 2020, 3:46am UTC](https://discuss.pytorch.org/t/how-can-i-print-the-image-of-each-layer-in-cnn/67597/1 "2020-01-25T03:46:16Z")

</div>

![image](https://discuss.pytorch.org/uploads/default/original/3X/7/8/78d73a367ece71d1adcd82258c7c41ed3bba41db.png)  
I want to print the output in image of each layer just like picture above  
how can I do it??

```
class CNN(nn.Module):

    def __init__ (self):
        super(CNN, self). __init__ ()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size = 3, stride = 1, padding = 1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size = 2, stride = 2))
    
        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding =1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size = 2, stride = 2))
    
        self.fc = nn.Linear(7 * 7 * 64, 10, bias = True) #0~9 10개
        torch.nn.init.xavier_uniform(self.fc.weight)
    
    def forward(self, x):
        out = self.layer1(x)
        imgprt = out.numpy()
        imgplot = plt.imshow(imgprt)
        out = torch.from_numpy(out)
        out = self.layer2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out

RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

```

i tried to change the data type in numpy and use plt.imshow() in forward but didn’t work.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [January 25, 2020, 7:26am UTC](https://discuss.pytorch.org/t/how-can-i-print-the-image-of-each-layer-in-cnn/67597/2 "2020-01-25T07:26:37Z")

</div>

Was the suggested workaround also not working? (`Use var.detach().numpy() instead.`)

For a more general approach, I would recommend to store the wanted activation in a `list` or `dict` by using forward hooks as described [here](https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6).

---

<div class="post-metadata">

**Author:** ![111346](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111346/32/25774_2.png) [@111346](https://discuss.pytorch.org/u/111346)\
**Post date:** [June 19, 2020, 6:39am UTC](https://discuss.pytorch.org/t/how-can-i-print-the-image-of-each-layer-in-cnn/67597/4 "2020-06-19T06:39:14Z")

</div>

The image is really awesome, would you please share where you get the original picture?  
thank you very much !

---

<div class="post-metadata">

**Author:** ![Yolkandwhite](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yolkandwhite/32/17604_2.png) [@Yolkandwhite](https://discuss.pytorch.org/u/Yolkandwhite)\
**Post date:** [June 29, 2020, 8:03am UTC](https://discuss.pytorch.org/t/how-can-i-print-the-image-of-each-layer-in-cnn/67597/5 "2020-06-29T08:03:07Z")

</div>

I just copied image from somewhere.

I forgot where I copied it from. It was quite a long time.

but you can try right click on that image and search image in google.  
(If you are using google chrome browser)
