当前位置:网站首页>Pytorch model tuning - only some layers of the pre training model are loaded
Pytorch model tuning - only some layers of the pre training model are loaded
2022-06-13 08:51:00 【Human high quality Algorithm Engineer】
Redefine a model structure , In fact, fine-tuning is to remove the original model fc layer , New plus one fc linear layer , That is, only some layers of the pre training model are loaded , Do not load as : Parameters of classification layer
if opt.continue_model != '':
print(f'loading pretrained model from {
opt.continue_model}')
pretrained_dict = torch.load(opt.continue_model)
model_dict = model.state_dict()
pretrained_dict = {
k: v for k, v in pretrained_dict.items() if (k in model_dict and 'Prediction' not in k)}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
The key is pretrained_dict = {k: v for k, v in pretrained_dict.items() if (k in model_dict and ‘Prediction’ not in k)}, Here's one if Filter parameters , Write filtered according to your own needs if Conditions , My changes are as follows :
backbone_pth = r"J:\code\gate-decorator-pruning\pre_train\face\orbbec_ir.pth".replace('\\', '/')
pretrained_dict = {
'module.'+k: v for k, v in torch.load(backbone_pth).items() if ('fc' not in k and 'features' not in k)}
# print(pretrained_dict.keys())
model_dict = pack.net.state_dict()
model_dict.update(pretrained_dict)
pack.net.load_state_dict(model_dict)
common model.state_dict() It's a OrderedDict type ,
OrderedDict([('module.conv1.weight', tensor([[[[-1.1209e-13, -1.1880e-13, -4.3911e-14],
[ 1.0236e-13, 2.1390e-13, 2.1669e-13],
[ 1.1124e-13, 2.7313e-13, 2.8194e-13]],
[[-1.1167e-13, -1.1761e-13, -4.2205e-14],
[ 1.0327e-13, 2.1355e-13, 2.2042e-13],
[ 1.0807e-13, 2.7697e-13, 2.8198e-13]],
[[-1.1175e-13, -1.1731e-13, -4.3114e-14],
[ 1.0148e-13, 2.1327e-13, 2.1844e-13],
[ 1.1156e-13, 2.7669e-13, 2.8263e-13]]],
[[[ 8.3619e-24, -2.7362e-23, -5.2695e-23],
[ 2.8068e-23, 5.4774e-24, -6.4889e-23],
[-3.7576e-24, 3.1777e-24, -4.6432e-23]],
[[ 2.2920e-24, -2.1073e-23, -5.5368e-23],
[ 3.4743e-23, 6.5649e-24, -5.4008e-23],
[-8.6869e-24, -5.3493e-24, -4.7047e-23]],
[[ 1.0901e-23, -2.7621e-23, -6.0086e-23],
[ 3.6049e-23, 1.0260e-23, -6.2163e-23],
[-5.4703e-24, -4.8527e-25, -4.3399e-23]]],
[[[-9.7617e-03, 5.3041e-03, -1.0028e-02],
[ 2.2801e-03, 3.3919e-02, 1.9303e-02],
[-7.5236e-03, 2.8626e-02, 5.3559e-03]],
The weight name and the corresponding value are returned as tuples ,
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
print(dic.items())
# Output :odict_items([('k1', 'v1'), ('k2', 'v2')])
边栏推荐
- Installing pytorch under Anaconda
- Emotion handling skills
- Implement authentication code login and remember password (cookie)
- Screenshot of cesium implementation scenario
- Centering problem - the width and height of child elements are known
- Animation through svg
- 13.inline,const,mutable,this,static
- 12. constructor explanation, explicit, initialization list
- CentOS installing MySQL and setting up remote access
- Mobile terminal development I: basic concepts
猜你喜欢

A solution to create a new EXCEL workbook on win10 computer and change the suffix to xlsm (normally it should be xlsx)

「解读」华为云桌面说“流畅”的时候,究竟在说什么?

VS安装VAssistX插件导致WPF-XAML文件输入中文出现乱码问题解决方案

0. Quelques doutes au sujet de SolidWorks

DIY UAV (anonymous controller p2+f330 rack)

关于RSA加密解密原理

Form exercise 2

turf. JS usage

JS - set countdown for Date object case

Cesium displays a pop-up box at the specified position and moves with the map
随机推荐
[pychart pit stepping record] prompt after configuring remote operation, [errno 2] no such file or directory
DIY UAV (anonymous controller p2+f330 rack)
Filebeat collects logs to elk
useRoutes() may be used only in the context of a <Router> component.
Browser render passes
Screenshot of cesium implementation scenario
redis.exceptions.ConnectionError: Error 111 connecting to 172.16.8.128:6379. Connection refused.
Three methods to make the scroll bar of div automatically scroll to the bottom
0.一些自己初学Solidworks的疑惑
0. Quelques doutes au sujet de SolidWorks
About RSA encryption and decryption principle
2、 Three ways to write JS code
Is signed or unsigned selected to create an integer field in MySQL? The answer is as follows:
JS - print 99 multiplication table of the for cycle case
JS string method
What is the difference between getfullyear() and getyear()
JS array using the reduce() method
Uni app essay
Can I open an account for the reverse repurchase of treasury bonds? Can I directly open the security of securities companies on the app for the reverse repurchase of treasury bonds? How can I open an
Sky background map, navigation page lovefanfan top