The web content provides a method for efficiently extracting features from an intermediate layer of a pre-trained ResNet model in PyTorch without duplicating the model in memory.
Abstract
The article discusses an improved technique for feature extraction from a pre-trained ResNet model in PyTorch. It addresses the memory inefficiency of the previous method, which involved creating a sequential model using the pre-trained model's modules, by introducing a new approach that utilizes hooks to access intermediate layer outputs. This method is particularly useful when the primary requirement is to obtain output predictions, and the intermediate layer outputs are only needed for visualization, debugging, or computations. The article guides the reader through additional library imports, building a new model by subclassing ResNet and overriding the _forward_impl method, and creating a model instance with pre-trained weights. It also includes code snippets and a model summary to illustrate the process and concludes with instructions on how to modify the code for flexibility in choosing models.
Opinions
The author suggests that using hooks is a superior method for feature extraction compared to the previous approach, as it avoids memory duplication.
The article implies that the previous method of feature extraction, which resulted in almost identical models, was less efficient due to increased memory usage.
It is conveyed that the new method is easier and does not have the disadvantages of the previous method.
The author emphasizes the importance of understanding the PyTorch source code for creating functions to load pre-trained weights specific to the architecture being used.
The article encourages reader interaction by asking for claps if the content is appreciated and inviting feedback for further improvement.
Extracting Features from an Intermediate Layer of a Pretrained ResNet Model in PyTorch (Easy way)
2048 feature maps of dimension 7X7 obtained from ‘layer4’ of ResNet50
In the previous article, we looked at a method to extract features from an intermediate layer of a pre-trained model in PyTorch by building a sequential model using the modules in the pre-trained model. But this method will result in 2 almost identical models and will take up double the memory until the pre-trained model was declared None.
So, in this article, we will look at a different method which is easier and also does not have the disadvantages of the previous method.
If obtaining the output predictions is the primary requirement and the intermediate layer outputs are needed only for visualization or debugging purposes, or even for other computations, then it is better to use hooks.
The base model we will be using here is the ResNet model. But one can use whatever model one likes and the next line will change according to that.
from torchvision.models.resnet import *
from torchvision.models.resnet import BasicBlock, Bottleneck
The reason for doing the above is that even though BasicBlock and Bottleneck are defined in ‘torchvision/models/resnet.py’, it is not exported by the module as it is not defined in __all__. Importing using the first line imports only ResNet, resnet18, resnet34 and other such functions. So, we have to import the classes BasicBlock and Bottleneck separately.
Get the model URLs
from torchvision.models.resnet import model_urls
Building a new Model
We are creating this class as a subclass of the class ResNet. Since we need an output from an intermediate layer, we just need to override the _forward_impl method, such that it returns the output from the layer we want the output from.
In lines 6–10, we create a list of the layers containing the names of the layers up to the layer we want the output from (let, this layer be called ‘output_layer’). So, in this example the list self._layers is
In the next line, we create an OrderedDict using the layer names as keys and the attributes with the same names as the corresponding values. For example,
Next, we override the _forward_impl method. We pass the input through the layers in the OrderedDict we just created and thus we get the output from the ‘output_layer’, the layer we wanted the output from.
Still not over…
We have to create a function which will create the model and also load the pre-trained weights. Again, this method will depend on the architecture we are using and this function will change accordingly. For this, we may need to look at the PyTorch source code.
Create a Model instance
Printing the model summary
from torchsummary importsummarysummary(model,input_size=(3, 224, 224))