Is it possible to slice a model at an arbitrary layer?

Hi all,

I’m looking to chop an exciting network up in parts so I could later chain different parts together, eg.

model = densenet121()
first_half = densenet121[:6]
second half = densenet121[6:]

However, this has not been so easy, I’ve tried splitting the model using model.parameters, model.modules and model.features, but I’ve not been able retain functionality when tied back together.

What is the proper way to copy or disregard one half of a model?

I would recommend to derive a custom class using the Densenet121 implementation as the parent class. Inside the __init__, you could try to split the self.features sequential module and create your own forward method using these splits.

Your code snippet would work for very simple models, which can be defined in an nn.Sequential module alone.