How to index into the layers within the EfficentNet backbone block?

Dear community,

I am trying to examine the outputs a specific layer within the EfficenNet backbone, however I am not able to index properly into the network. This is what I am doing:

mod = nn.Sequential(
              *list(models.efficientnet_v2_s(pretrained=True).children())[:-2][0][:5]
              )
mod

which returns the nn.sequential definition of the backbone. I am interested in the Backbone defined from the start to the (4): MBConv( ) Block , however when trying to index into it with [:4] or by trying to remove the last block using [:-1] I get the (3): MBConv( ) block. How can I examine the output of the (4): MBConv( ) without it being attached to the (5): MBConv( ) block?

Forward hooks would be a good way to inspect the output of a specific layer as given in e.g. this post.

Based on your code snippet it seem you want to recreate the model somehow, which might be tricky since you are re-wrapping everything into an nn.Sequential block, which might miss important functional API calls from the original forward method.

Thank you Patrik. I will look in to the forward hook.

nn.Sequential block, which might miss important functional API calls from the original forward method.

I was not aware of this. How would you use the backbone to ensure that the forward push is unaffected and not wrapped into an nn.sequential ? The liner layer and avg pooling layer would still have to be removed though.

A clean way would be to inspect the model including its source (in particular the forward definition) and to override it with your own forward method in a custom model.
Wrapping submodules into nn.Sequential might work, but could also easily break as seen in other topic in this discussion board as e.g. often flattening operations are missing since these are often not defined as a module but used inside the forward via e.g. x = x.view(x.size(0), -1).