How can I extract features?

I’m using a model for object detection for images . After training data set I got files with .pth extention for 15 epochs … what should I do for extracting the features for the images

So I assume that you have saved the model in pth file after training for 15 epochs.
I am not sure from which layer you want to extract features.

model(input) will provide the features or output from the last layer.
For getting features from intermediate layer you can explicitly return in forward function of the model.

or you can get features using the layer variable names.
model = new_model(output_layer = ‘layer4’)


new_classifier = nn.Sequential(*list(model.children())[:-1])

thanks a lot for replying . I found the method called forward … do you mean this method ?

def forward(self, x):
x = self.forward_features(x)
return x

Yes, in this function you can save output from any layer and return it.

x1 = self.cnn1(x)
x2 = self.cnn2(x1)

return x1,x2

excuse me so this function save the features as it return x ?

this function take an input and generate a output ragardign that input.
Suppose in image classification you will pass and input and in the end you will get a output from last (this is usual case in all the code forums.)

But what I am trying to say you can return any variable in forward function. Forward function is just how each layer is being called and passed to next. so you can return output from any layer.

does the result will be like this

[{391895: [array([[3.43202576e+02, 2.43713760e+01, 4.96343414e+02, 3.32774170e+02,


{‘391895’: array([[1.15911709e-03, 3.19864594e-05, 1.06177040e-05, 6.39094651e-07,
1.44435398e-06, 1.01086480e-05,

i mean the structure of format of the features will start with symbol { or will be a list that start with symbol [ ?

Can you show me the model

which part do you need ? is there any specific function ?

I need model code and forword function mainly and which features you need