How to extract deep features from a pretrained model in pytorch?

Hello,
I want to extract deep features from this pretrained model in tensorflow https://www.kaggle.com/code/amyjang/tensorflow-transfer-learning-melanoma/output. I tried this but I am not sure if it is right.

import h5py
file = h5py.File('model_weights.hdf5', 'r')
weights_layer_1 = file['weights/layer_1'][:]
import torch

# create the PyTorch model
model = MyModel()

Here I need to create the model in pytorch, right?

There is no another way to use to pretrained model without the need to create it first?

Thanks in advance.

Hi,

I guess you want to access the features for a particular image / input. If so:

  • you need to know the model of that is associated with the weights (your MyModel)
  • load the weights into your model.
  • Pass the input / image trough the backbone
  • Extract the elements of the backbone at the different stages (for example, the result of self.layer1(x), self.layer2(x), … self.layer4(x)

The pytorch tutorials are really nice and should introduce the main concepts for the steps above. If the model in indeed a tensorflow model, you need to work with tensorflow not pytorch.

My code is in pytorch and I want to compute perceptual loss by extracting deep features from this model and add it to my loss function. I think it is not adequate to use pytorch and tensorflow together in the same code.