# Intermediate Layers of AlexNet/VGG

**URL:** <https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510>\
**Category:** vision\
**Created:** [March 8, 2020, 9:12pm UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510 "2020-03-08T21:12:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arjun\_Gupta](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arjun_gupta/32/12756_2.png) [@Arjun\_Gupta](https://discuss.pytorch.org/u/Arjun_Gupta)\
**Post date:** [March 8, 2020, 9:12pm UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510/1 "2020-03-08T21:12:31Z")

</div>

I am trying to use PyTorch to get the outputs from intermediate layers of AlexNet/VGG:

```auto
alexnet_model = models.alexnet(pretrained=True)
modules = list((alexnet_model).children())[:-1*int(depth)]
alexnet_model = nn.Sequential(*modules)

```

What is odd is that I get the same output values (i.e. the same exact model) when `depth=1` and `depth=2`, and then the same output values for `depth=3` all the way to `depth=10`. I observe this same phenomenon for VGG too. However, I don’t observe this for ResNet, which gives me different output values (i.e. different models) for all depths [1, 10].

Any ideas about what might be going on?

---

<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:** [March 9, 2020, 12:52am UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510/2 "2020-03-09T00:52:48Z")

</div>

`list((alexnet_model).children())` will return a list of length 3 containing the first `nn.Sequential` block for the feature extraction, the `nn.AdaptiveAvgPool2d` layer, and the last `nn.Sequential` block used as the classifier.  
If you use `depth>=3`, `modules` will be empty and you will just get back your input tensor.

---

<div class="post-metadata">

**Author:** ![Arjun\_Gupta](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arjun_gupta/32/12756_2.png) [@Arjun\_Gupta](https://discuss.pytorch.org/u/Arjun_Gupta)\
**Post date:** [March 9, 2020, 12:55am UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510/3 "2020-03-09T00:55:30Z")

</div>

Thanks for your response! How do I get outputs of the layers within the sequential blocks? And how is this working for the ResNet architecture?

---

<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:** [March 9, 2020, 1:07am UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510/4 "2020-03-09T01:07:13Z")

</div>

You could use forward hook as described in [this example](https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6).

---

<div class="post-metadata">

**Author:** ![Arjun\_Gupta](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arjun_gupta/32/12756_2.png) [@Arjun\_Gupta](https://discuss.pytorch.org/u/Arjun_Gupta)\
**Post date:** [March 9, 2020, 3:55pm UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510/5 "2020-03-09T15:55:37Z")

</div>

Thanks for the reference! Apparently, the layers within the sequential blocks (of AlexNet, VGG, etc.) don’t have names associated with them (e.g. ‘self.fc2’); how could I extract outputs from certain layers within the last sequential block using your function?

---

<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:** [March 9, 2020, 11:36pm UTC](https://discuss.pytorch.org/t/intermediate-layers-of-alexnet-vgg/72510/6 "2020-03-09T23:36:42Z")

</div>

You can access a module inside an `nn.Sequential` block by indexing it:

```python
model = nn.Sequential(
    nn.Conv2d(3, 6, 3, 1, 1),
    nn.ReLU(),
    nn.Conv2d(6, 1, 3, 1, 1)
)
# get second conv layer
c = model[2]

```
