# 關於pytorch置換backbone ###### tags: `beginner` ## 前言 常在進行機器學習時,我們會需要用到網路上開源的pretrained weights,但因為每個架構的名稱不同,常會導致pretrained weights無法順利讀入使用的狀況,故在這篇對pytorch的pretrained model如何置換backbone、新增或刪除layer進行一些筆記和紀錄 *筆者實際都是使用ResNet50進行* ## load pretrained model ``` language='python' pretrained_model = torch.hub.load( 'pytorch/vision:v0.5.0', 'resnet50', pretrained=True ) #在pretrained使用True就可以下載hub中的pretrained weights # #如果只是想要模型架構,可以設為False,並透過下方程式碼 #對權重進行讀取 checkpoint = torch.load( "checkpoint.pth.tar", map_location="cuda" ) ``` ## add layer * 方法一:將原先的model需要的架構直接包入,可以忽略掉 ``` language ='python' pretrained = model #your pretrained model class MyResNet(torch.nn.Module): def __init__(self, my_pretrained_model): super(MyResNet, self).__init__() self.pretrained = my_pretrained_model self.my_new_layers = layer #New layer you want to add def forward(self, x): x = self.pretrained(x) # Sometimes,we take sub-model, and the code below might be helpful # x = self.pretrained(x)['3'] x = self.my_new_layers(x) return x ``` * 方法二:對pretrained model的名稱進行置換,換成符合自己模型的名稱 ``` language='python' from collections import OrderedDict pretrain_weight = {'state_dict':OrderedDict()} for key in checkpoint['state_dict']: pretrain_weight['state_dict'][key[7:]]=checkpoint['state_dict'][key] new_model.load_state_dict(pretrain_weight['state_dict'],strict=False) ``` ## delete layer and output feature from middle layer # *Still working...*
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up