Extracting features using my QNN Hybrid model

I’ve created a Hybrid Quantum CNN model. And now after calling the instance: I want to use the model as feature extractor and receive the features from training images. I’m a bit confused, how to achieve this in Pytorch. [I can do this on Tensorflow but not on Pytorch yet]


weights = torchvision.models.ResNet18_Weights.IMAGENET1K_V1
model_hybrid = torchvision.models.resnet18(weights=weights)

for param in model_hybrid.parameters():
    param.requires_grad = False

# Notice that model_hybrid.fc is the last layer of ResNet18
model_hybrid.fc = DressedQuantumNet()

# Use CUDA or CPU according to the "device" object.
model_hybrid = model_hybrid.to(device)

Can someone help on how can I extract features from training images? using model_hybrid?

You could use forward hooks as described here or could alternatively replace the classifier with nn.Identity layers if you won’t need the actual classifier output anymore.