Using Python to build alexnet operation (fine tuning pre training model and manual building)


This paper introduces how to build alexnet under python, using two methods: one is to load the pre training model directly and fine tune it according to your own needs (change the output of the last full connection layer from 1000 to 10), and the other is to build it manually.

When building model classes, you need to inherit from torch.nn.Module Class, you have to rewrite it yourself__ * \___ init__ * \___ Method and forward method in forward transmission. My own understanding here is to build a network and write it in__ * \___ init__ * \___ In forward transfer, the part to be calculated is written in forward, such as flattening the matrix.

After loading the pre training alexnet, you can print it to view the structure and information of the model

model = models.alexnet(pretrained=True)

It is divided into two parts, features and classifier. These two parts can also be written in subsequent model building, and the reference mode of each layer can be seen from the printed model information, which is easy to modify model.classifier [1] Linear (in_ features=9216, out_ Features = 4096, bias = true).

The complete build code is released below:

import torch.nn as nn
from torchvision import models

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

The idea of fine-tuning the pre training model is to keep the features part of the original model directly and rewrite the classifier part. In the classifier part, what we actually need to modify is the last fully connected layer. The previous two fully connected layers do not need to be modified. Therefore, we need to keep the pre training weight and offset of these two layers when rewriting. We can also directly refer to the last layer of full connection layer for modification as in the annotated two lines of code.

After the network is set up, you can test it to see if the dimensions are correct.

import numpy as np
from torch.autograd import Variable
import torch

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

If you don’t add “x = x.astype”( np.float32 )”A type error will be reported, which is a little strange.

Output y data.numpy () 10 dimensional output can be obtained, which indicates that the network construction is correct.

The above article using Python to build alexnet operation (fine tuning pre training model and manual building) is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.