Get intermediate CNN layer output

I have my own CNN model, I want to get intermediate CNN layer features from my trained model, so that I can supply them as input to autoencoders.

You could do something similar to what is done when loading the VGG19 network in this tutorial.

Create your CNN model with two attributes, train it and then just use the part you’re interested in. Something like:

def MyCNNNetwork(nn.Module):
    ...

def MyDenseNetwork(nn.Module):
     ...

def MyModule(nn.Module):
    def __init__(self, ...):
        super(self, MyModule).__init__()
        self.features = MyCNNNetwork(...)
        self.output = MyDenseNetwork(...)

    def forward(self, x):
        batch_size = x.size(0)
        return self.output(self.features(x).view(batch_size, -1))


model = MyModel(...)
my_training_routine(model) # Train your network

x = ... # A sample case
features_of_x = model.features(x)

I think that should do it.

thanks, i would try this out.

1 Like