-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_SE_resnet18.py
45 lines (40 loc) · 1.35 KB
/
delete_SE_resnet18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import print_function
import torch
from torchvision import datasets, transforms
from torch import nn
from SELayer import SELayer
import numpy as np
checkpoint = torch.load("model_save/resnet18_SE_train_best.pth.tar")
model = torch.load("models/resnet18_SE.pth")
model.load_state_dict(checkpoint['state_dict'])
model.to('cuda')
av = torch.load("attentions/resnet18/av_avg.pth")
model.se1 = nn.Sequential()
model.layer1[0].se1 = nn.Sequential()
model.layer1[0].se2 = nn.Sequential()
model.layer1[1].se1 = nn.Sequential()
model.layer1[1].se2 = nn.Sequential()
model.layer2[0].se1 = nn.Sequential()
model.layer2[0].se2 = nn.Sequential()
model.layer2[1].se1 = nn.Sequential()
model.layer2[1].se2 = nn.Sequential()
model.layer3[0].se1 = nn.Sequential()
model.layer3[0].se2 = nn.Sequential()
model.layer3[1].se1 = nn.Sequential()
model.layer3[1].se2 = nn.Sequential()
model.layer4[0].se1 = nn.Sequential()
model.layer4[0].se2 = nn.Sequential()
model.layer4[1].se1 = nn.Sequential()
model.layer4[1].se2 = nn.Sequential()
k1 = 0
k2 = 0
for n, m in model.named_modules():
if isinstance(m, nn.BatchNorm2d):
if k not in [7,12,17]:
m.weight.data = torch.mul(m.weight.data,av[k2])
m.bias.data = torch.mul(m.bias.data,av[k2])
k1+=1
k2+=1
else:
k+=1
torch.save(model,"models/resnet18_delete_SE.pth")