pytorch凍結網絡參數

part  one: 一篇博客的介紹:

Pytorch的load方法和load_state_dict方法只能較爲固定的讀入參數文件,他們要求讀入的state_dict的key和Model.state_dict()的key對應相等。

而我們在進行遷移學習的過程中也許只需要使用某個預訓練網絡的一部分,把多個網絡拼和成一個網絡,或者爲了得到中間層的輸出而分離預訓練模型中的Sequential 等等,這些情況下。傳統的load方法就不是很有效了。

例如,我們想利用Mobilenet的前7個卷積並把這幾層凍結,後面的部分接別的結構,或者改寫成FCN結構,傳統的方法就不奏效了。

最普適的方法是:構建一個字典,使得字典的keys和我們自己創建的網絡相同,我們再從各種預訓練網絡把想要的參數對着新的keys填進去就可以有一個新的state_dict了,這樣我們就可以load這個新的state_dict,目前只能想到這個方法應對較爲複雜的網絡變換。

網上查“載入部分模型”,“凍結部分模型”一般都是隻改個FC,根本沒有用,初學的時候自己寫state_dict也踩了一些坑,發出來記錄一下。


一.載入部分預訓練參數

我們先看看Mobilenet的結構

( 來源github,附帶預訓練模型mobilenet_sgd_rmsprop_69.526.tar)

  1. class Net(nn.Module):
  2. def __init__(self):
  3. super(Net, self).__init__()
  4. def conv_bn(inp, oup, stride):
  5. return nn.Sequential(
  6. nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
  7. nn.BatchNorm2d(oup),
  8. nn.ReLU(inplace=True)
  9. )
  10. def conv_dw(inp, oup, stride):
  11. return nn.Sequential(
  12. nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
  13. nn.BatchNorm2d(inp),
  14. nn.ReLU(inplace=True),
  15. nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
  16. nn.BatchNorm2d(oup),
  17. nn.ReLU(inplace=True),
  18. )
  19. self.model = nn.Sequential(
  20. conv_bn( 3, 32, 2),
  21. conv_dw( 32, 64, 1),
  22. conv_dw( 64, 128, 2),
  23. conv_dw(128, 128, 1),
  24. conv_dw(128, 256, 2),
  25. conv_dw(256, 256, 1),
  26. conv_dw(256, 512, 2),
  27. conv_dw(512, 512, 1),
  28. conv_dw(512, 512, 1),
  29. conv_dw(512, 512, 1),
  30. conv_dw(512, 512, 1),
  31. conv_dw(512, 512, 1),
  32. conv_dw(512, 1024, 2),
  33. conv_dw(1024, 1024, 1),
  34. nn.AvgPool2d(7),
  35. )
  36. self.fc = nn.Linear(1024, 1000)
  37. def forward(self, x):
  38. x = self.model(x)
  39. x = x.view(-1, 1024)
  40. x = self.fc(x)
  41. return x

我們只需要前7層卷積,並且爲了方便日後concate操作,我們把Sequential拆開,成爲下面的樣子

  1. class Net(nn.Module):
  2. def __init__(self):
  3. super(Net, self).__init__()
  4. def conv_bn(inp, oup, stride):
  5. return nn.Sequential(
  6. nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
  7. nn.BatchNorm2d(oup),
  8. nn.ReLU(inplace=True)
  9. )
  10. def conv_dw(inp, oup, stride):
  11. return nn.Sequential(
  12. nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
  13. nn.BatchNorm2d(inp),
  14. nn.ReLU(inplace=True),
  15. nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
  16. nn.BatchNorm2d(oup),
  17. nn.ReLU(inplace=True),
  18. )
  19. self.conv1 = conv_bn( 3, 32, 2)
  20. self.conv2 = conv_dw( 32, 64, 1)
  21. self.conv3 = conv_dw( 64, 128, 2)
  22. self.conv4 = conv_dw(128, 128, 1)
  23. self.conv5 = conv_dw(128, 256, 2)
  24. self.conv6 = conv_dw(256, 256, 1)
  25. self.conv7 = conv_dw(256, 512, 2)
  26. # 原來這些不要了
  27. # 可以自己接後面的結構
  28. '''
  29. self.features = nn.Sequential(
  30. conv_dw(512, 512, 1),
  31. conv_dw(512, 512, 1),
  32. conv_dw(512, 512, 1),
  33. conv_dw(512, 512, 1),
  34. conv_dw(512, 512, 1),
  35. conv_dw(512, 1024, 2),
  36. conv_dw(1024, 1024, 1),
  37. nn.AvgPool2d(7),)
  38. self.fc = nn.Linear(1024, 1000)
  39. '''
  40. def forward(self, x):
  41. x1 = self.conv1(x)
  42. x2 = self.conv2(x1)
  43. x3 = self.conv3(x2)
  44. x4 = self.conv4(x3)
  45. x5 = self.conv5(x4)
  46. x6 = self.conv6(x5)
  47. x7 = self.conv7(x6)
  48. #x8 = self.features(x7)
  49. #out = self.fc
  50. return (x1,x2,x3,x4,x4,x6,x7)

我們更具改過的結構創建一個net,看看他的state_dict和我們預訓練文件的state_dict有啥區別

  1. net = Net()
  2. #我的電腦沒有GPU,他的參數是GPU訓練的cudatensor,於是要下面這樣轉換一下
  3. dict_trained = torch.load("mobilenet_sgd_rmsprop_69.526.tar",map_location=lambda storage, loc: storage)["state_dict"]
  4. dict_new = net.state_dict().copy()
  5. new_list = list (net.state_dict().keys() )
  6. trained_list = list (dict_trained.keys() )
  7. print("new_state_dict size: {} trained state_dict size: {}".format(len(new_list),len(trained_list)) )
  8. print("New state_dict first 10th parameters names")
  9. print(new_list[:10])
  10. print("trained state_dict first 10th parameters names")
  11. print(trained_list[:10])
  12. print(type(dict_new))
  13. print(type(dict_trained))

得到輸出如下:

我們截斷一半之後,參數由137變成65了,前十個參數看出,名字變了但是順序其實沒變。state_dict的數據類型是Odict,可以按照dict的操作方法操作。

new_state_dict size: 65 trained state_dict size: 137

New state_dict first 10th parameters names
['conv1.0.weight', 'conv1.1.weight', 'conv1.1.bias', 'conv1.1.running_mean', 'conv1.1.running_var', 'conv2.0.weight', 'conv2.1.weight', 'conv2.1.bias', 'conv2.1.running_mean', 'conv2.1.running_var']

trained state_dict first 10th parameters names
['module.model.0.0.weight', 'module.model.0.1.weight', 'module.model.0.1.bias', 'module.model.0.1.running_mean', 'module.model.0.1.running_var', 'module.model.1.0.weight', 'module.model.1.1.weight', 'module.model.1.1.bias', 'module.model.1.1.running_mean', 'module.model.1.1.running_var']

<class 'collections.OrderedDict'>
<class 'collections.OrderedDict'>

我們看出只要構建一個字典,使得字典的keys和我們自己創建的網絡相同,我們在從各種預訓練網絡把想要的參數對着新的keys填進去就可以有一個新的state_dict了,這樣我們就可以load這個新的state_dict,這是最普適的方法適用於所有的網絡變化。

  1. for i in range(65):
  2. dict_new[ new_list[i] ] = dict_trained[ trained_list[i] ]
  3. net.load_state_dict(dict_new)

還有別的情況,比如我們只是在後面加了一些層,沒有改變原來網絡層的名字和結構,可以用下面的簡便方法:

loaded_dict = {k: loaded_dict[k] for k, _ in model.state_dict()}

 


二.凍結這幾層參數

方法很多,這裏用和上面方法對應的凍結方法

  1. 發現之前的凍結有問題,還是建議看一下
  2. https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088
  3. 或者
  4. https://discuss.pytorch.org/t/correct-way-to-freeze-layers/26714
  5. 或者

對應的,在訓練時候,optimizer裏面只能更新requires_grad = True的參數,於是

optimizer = torch.optim.Adam( filter(lambda p: p.requires_grad, net.parameters(),lr) )

 

part two 我的用法:

首先介紹一下我的需求,我的是:

先訓練一個網絡,然後再構建一個網絡:new_model + older_model,即後面的older_model借用前面訓練好的網絡參數,並在後續訓練中進行凍結,不進行梯度更新。梯度更新僅存在於前面的new_model網絡。

參考瞭如下資料:

  1. I have some confusion regarding the correct way to freeze layers.
  2. Suppose I have the following NN: layer1, layer2, layer3
  3. I want to freeze the weights of layer2, and only update layer1 and layer3.
  4. Based on other threads, I am aware of the following ways of achieving this goal.
  5. Method 1:
  6. optim = {layer1, layer3}
  7. compute loss
  8. loss.backward()
  9. optim.step()
  10. Method 2:
  11. layer2_requires_grad=False
  12. optim = {all layers with requires_grad = True}
  13. compute loss
  14. loss.backward()
  15. optim.step()
  16. Method 3:
  17. optim = {layer1, layer2, layer3}
  18. layer2_old_weights = layer2.weight (this saves layer2 weights to a variable)
  19. compute loss
  20. loss.backward()
  21. optim.step()
  22. layer2.weight = layer2_old_weights (this sets layer2 weights to old weights)
  23. Method 4:
  24. optim = {layer1, layer2, layer3}
  25. compute loss
  26. loss.backward()
  27. set layer2 gradients to 0
  28. optim.step()
  29. My questions:
  30. Should we get different results for each method?
  31. Is any of these methods wrong?
  32. Is there a preferred method?

最後直接將先前訓練的網絡保存下來,在後續定義網絡時,加載之前保存的模型,並直接作如下設置就可以了:

 param.requires_grad = False

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章