How can l load my best model as a feature extractor/evaluator?

I’ve created a small code snippet using a forward hook to store one activation from fc2:

class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.cl1 = nn.Linear(25, 60)
        self.cl2 = nn.Linear(60, 16)
        self.fc1 = nn.Linear(16, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        
    def forward(self, x):
        x = F.relu(self.cl1(x))
        x = F.relu(self.cl2(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.log_softmax(self.fc3(x), dim=1)
        return x


activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook


model = MyModel()
model.fc2.register_forward_hook(get_activation('fc2'))
x = torch.randn(1, 25)
output = model(x)
print(activation['fc2'])
88 Likes
Get intermediate value from a block in nn.Sequential in a model
Undestanding register_*_hook() functions
Getting Intermediate Output of Self Created Sequential
Extract activation maps
Pre trained VGG 19 maxpool layer output
Extracting features from specific layers on a trained network
Get layer's output from nn.Sequential
Using feature extraction layers from pre-trained FRCNN
How to get output of layers?
How to check or view the intermediate results or output of a network?
Why is the input of hook function a tuple?
How to use parameters from autoencoder to CNN for classification
ResNet18 - access to the output of each BasicBlock
How to add activation histogram in tensorboard?
cycleGAN inference pictures differ from images generated in training
Retrieve intermediate node values without explicitly return them
How to access intermediate layer in a pretrained model and sequence layers?
Extract specific outputs through pre trained classifier
How to get image featrue using my pre-trained model?
How to get the output at the 2nd block of a resnet-50?
Extraction and modification of intermediate layer outputs from pre-trained models and layer by layer inference
Lasagne.layers.get_output equivalent method in pytorch
How to select pixels of ROI from feature map
How to get feature map from pre-trained Resnet50
Intermediate tensors in NeMo
Network produces different output for the same image from different Dataloaders
Testing model's performance after saving and without saving the model
To get a middle activation map
Activation values in ResNet
Heatmap localization
Make values consistent across versions
How to index into the layers within the EfficentNet backbone block?
Logging and changing activation values outside the forward method
Loss nan when resuming from a pretrained model
FP16 gives NaN loss when using pre-trained model
Extract feature vector/latent factors from Embedding layer in Pytorch
Get 2048 Length Feature Vectors of Inception Model TorchVision 0.7.0
How is feature extraction done from by using pre-activations last CNN layer in VGG-19
How do I print output of each layer in sequential?
Converting keras model to pytorch
Visualize feature map
How to save inner nn.Sequential layer's output
Online Knowledge Distillation
Extracting features from boxes in the pretrained Faster Rcnn
About Normalization using pre-trained vgg16 networks
Nan Loss coming after some time
Basic MNIST Keras model to Pytorch implementation
Get output of intermediate layer of pre-trained model
Extracting features using my QNN Hybrid model
How can I get the Hidden Embeddings from the 2nd last fully connected layer for t-SNE visualization?
My transformer NMT model is giving "nan" loss value
How can I gets output of one layer?
Torchvision Mask-rcnn with Resnext101 backbone occur Nan loss during the training
How to modify and rewrite the activation output of a layer before applying the output to next layer?
Return item in forward hook
How to get the intermediate features from Alexnet
Visualise sequential model feature maps
Nan Loss with torch.cuda.amp and CrossEntropyLoss
How can I get the mean and variance from a saved CNN model?
How to freeze the vector at the second last layer of shallow model?
Intermediate output from sequential layers and to use the output for further processing while training
How to read an intermediate layer of a pretrained network
Training with Half Precision
Extract intermediate representation of MiDaS neural network?
Selecting a particular layer from the pytorch pre-trained model
Correct way to upload saved model weights faster r-cnn
Inconsistencies in getting intermediate layer outputs
How can i ignore thoes layers?
Type mismatch with hooks input when using JIT
How to obtain the tensor from the middle part of a CNN model (EfficientNet_b0), instead of the last layer?
Best way to store feature vectors from pretrained model to hard disk?
Want to output intermediate layers from pretrained Resnet 18
Extract the 2048 vector of a fine-tuned Inception V3 on test set
Visualize feature maps of the 1st layer
Extracting speccific layers' output from a module list
Unique module identifier
Obtain intermediate outputs from pre-trained models
Model (resnet34) performs better without model.eval()
Compare two models layer by layer
Memory/Performance of Training Wav2vec2 Model
Access to the weight of an intermediate layer in a network based on a specific input
TraceError: symbolically traced variables cannot be used as inputs to control flow
Representation projection to 2D plot
Tabular Data (DAE + MLP model): nan values while training
Analyzing the inference of a of a model
How to take the features of net1 as the input of net2
How can I extract intermediate layer output from loaded CNN model?
Pipe Pretrained Model to Custom Layers
Feature Vector Extraction from Densenet121
Equivalent of register forward hook for parameters?
Create feature map of pretrained resnet50 layers
Need feature maps of ResNet50
Memory usage increases by at least 30 when applying model
Is there the train model store the output in pytorch?
Same activation at different layers
How to access input/output activations of a layer given its parameters names?
Retrieve neural network name inside registered hook
Is it necessary to overwrite the forward method for a class that inherits from nn.Module?
Saving extracted features
Extract c3d features, given video
Pathological loss values when model reloaded
Feature pyramid
How can I print the image of each layer in CNN?
How to use hook to visualise feature maps
Intermediate Layers of AlexNet/VGG
How to delete layer in pretrained model?