Pagling label implementation

2023-01-02   ES  


Migration learning is a model trained on the A issue, which is applied to the B problem.
Strict definition:


migration learning is as follows, given the source domain and source task, target domain, and target task. Such an algorithm of the target task. The source domain is different from the target domain or the source task is different from the target task1

and different domains here can be decomposed into two aspects. One is different feature space. For example, the picture space of the human face and the picture of the birds are different; Sample space, but one is a bird taken in the city, and the other is taken in nature. The difference between TASK is also reflected in two aspects, either refers to different label space or different probability of conditions, such as uneven data distribution of the two data sets1

pytorchtorchvisionThe model inmodelsis as follows:
These models can be called directly.

The following content is transferred from
1. Directly loads the pre -training model

from torchvision import models
net = models.alexnet(pretrained=True)

2. Modify a certain layer

from torchvision import models
from torch import nn as nn

net = models.resnet18(pretrained=True)
num_ftrs = net.fc.in_features#FC layer input feature number
num_class = 12# category
net.fc = nn.Linear(num_ftrs,num_class)

Only update the full connection

from torch import optim as optim
optimizer = optim.SGD(model.fc.parameters(),lr = 0.001,momentum = 0.9)

Update all parameters

from torch import optim as optim
optimizer = optim.SGD(model.parameters(),lr = 0.001,momentum = 0.9)

Some networks have no FC layer, such assqueezenet. It consists of two partsfeaturesclassifier. If the number of output categories can be rebuiltclassifier.

# Unexpectedly, the modification requires the theoretical basis, and calculate the FeatureMap dimension to match it.
resnet.conv1 = nn.Conv2d(3, 64,kernel_size=5, stride=2, padding=3, bias=False)

3. Load some pre -training models

# Load the Model, Model is the model that you define
resnet50 = models.resnet50(pretrained=True) 
model =Net(...) 
# Read parameter
pretrained_dict =resnet50.state_dict() 
model_dict = model.state_dict() 
# 2 2 2 2 2 2 2 2 2 2 2 2 2 2
pretrained_dict =  {
    k: v for k, v in pretrained_dict.items() if k in model_dict} 
# Update the existing Model_dict
# Load the state_dict we really need

  1. Load your own model

Method 1 (Recommended):
The first method is also the official recommendation method, onlySave and restore the parameters in the model
Use this method, weNeed to import the structure information of the model

(1) Save, PATH)

(2) recovery

model = ModelClass(*args, **kwargs)

Method 2:

Use this method, willSave parameters and structural information of the model

(1) Save (model, PATH)

(2) recovery

model = torch.load(PATH)



Related Posts

RecyclerView list Load the picture to refresh the flashing problem Bigsea

Protective measures for vulnerabilities of Android application

React’s eCharts realizes gradient and zoomed in Welkin

Android adds bitmap to the overlay of View

Pagling label implementation

Random Posts

Renren Houxun Router TTL flashing

SpringBoot @Onetoone solves the two -way dead cycle of JPA/Return to JSON data dead loop

hexo+github build a personal blog (super detailed tutorial)

OPENCV cross compilation contains ffmpeg

linux commonly used instruction collection