SoFunction
Updated on 2024-11-20

Building AlexNet operations with pytorch (fine-tuning pre-trained models and building them manually)

This article describes how to build AlexNet under pytorch, using two methods, one is to load the pre-trained model directly and fine-tune it according to your needs (change the last fully connected layer output from 1000 to 10), and the other is to build it manually.

When building a model class, you need to inherit from the class and rewrite the __ \_\___init__ \_\___ method and the forward method for forward passes. My own understanding here is that building the network is written in the __ \_\___init__ \_\___, and the part of the computation needed for each forward pass is written in the forward, such as flattening the matrix or something like that. The

After loading the pre-training alexnet, you can print out to see the structure and information of the model:

model = (pretrained=True)
print(model)

Divided into two parts, features and classifier, the subsequent construction of the model can also be written as these two parts, and from the printout of the model information can also be seen in each layer of the reference mode, easy to modify, for example, [1] refers to the Linear(in_features=9216, out_features=4096, bias=True) layer. bias=True) layer.

The full build code is released below:

import  as nn
from torchvision import models

class BuildAlexNet():
  def __init__(self, model_type, n_output):
    super(BuildAlexNet, self).__init__()
    self.model_type = model_type
    if model_type == 'pre':
      model = (pretrained=True)
       = 
      fc1 = (9216, 4096)
       = [1].bias
       = [1].weight
      
      fc2 = (4096, 4096)
       = [4].bias
       = [4].weight
      
       = (
          (),
          fc1,
          (inplace=True),
          (),
          fc2,
          (inplace=True),
          (4096, n_output)) 
      # or just change it to
#      [6]==(4096,n_output)
#       = 
    if model_type == 'new':
       = (
          nn.Conv2d(3, 64, 11, 4, 2),
          (inplace = True),
          nn.MaxPool2d(3, 2, 0),
          nn.Conv2d(64, 192, 5, 1, 2),
          (inplace=True),
          nn.MaxPool2d(3, 2, 0),
          nn.Conv2d(192, 384, 3, 1, 1),
          (inplace = True),
          nn.Conv2d(384, 256, 3, 1, 1),
          (inplace=True),
          nn.MaxPool2d(3, 2, 0))
       = (
          (),
          (9216, 4096),
          (inplace=True),
          (),
          (4096, 4096),
          (inplace=True),
          (4096, n_output))
      
  def forward(self, x):
    x = (x)
    x = ((0), -1)
    out = (x)
    return out

The idea of fine-tuning the pre-training model is to directly retain the FEATURES part of the original model and rewrite the CLASSIFIER part. In the classifier part, we actually need to modify only the last fully connected layer, before the two fully connected layers do not need to be modified, so when rewriting the two layers need to be retained in the pre-training weights and offsets, but also like the two lines of code commented out as a direct reference to the last layer of the fully connected layer for modification.

After the network is set up you can do a little testing to check that the dimensions are correct.

import numpy as np
from  import Variable
import torch

if __name__ == '__main__':
  model_type = 'pre'
  n_output = 10
  alexnet = BuildAlexNet(model_type, n_output)
  print(alexnet)
  
  x = (1,3,224,224)
  x = (np.float32)
  x_ts = torch.from_numpy(x)
  x_in = Variable(x_ts)
  y = alexnet(x_in)

It's a bit strange that if you don't add "x = (np.float32)" you'll get a type error.

Output () gives a 10-dimensional output, indicating that the network was built correctly.

The above article using pytorch to build AlexNet operations (fine-tuning pre-training models and manual construction) is all I have to share with you, I hope to be able to give you a reference, and I hope you will support me more.