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

Strict definition:

The

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 task

^{1}。

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 sets

^{1}。

pytorch`torchvision`

The model in`models`

is as follows:

These models can be called directly.

The following content is transferred from`https://blog.csdn.net/weixin_41278720/article/details/80759933`

**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 as`squeezenet`

. It consists of two parts`features`

`classifier`

. If the number of output categories can be rebuilt`classifier`

.

```
# 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
model_dict.update(pretrained_dict)
# Load the state_dict we really need
model.load_state_dict(model_dict)
```

- Load your own model

Method 1 (Recommended):

The first method is also the official recommendation method, only**Save and restore the parameters in the model**。

Use this method, we**Need to import the structure information of the model**。

(1) Save

```
torch.save(model.state_dict(), PATH)
#example
torch.save(resnet50.state_dict(),'ckp/model.pth')
```

(2) recovery

```
model = ModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
#example
resnet=resnet50(pretrained=True)
resnet.load_state_dict(torch.load('ckp/model.pth'))
```

Method 2:

Use this method, will**Save parameters and structural information of the model**。

(1) Save

```
torch.save (model, PATH)
```

(2) recovery

```
model = torch.load(PATH)
```