forked from gmayday1997/pytorch_CAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg16_model.py
147 lines (134 loc) · 4.77 KB
/
vgg16_model.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import torch
import torch.nn as nn
import torch.nn.init as init
###### orginal vgg-cam ######
class vgg_cam(nn.Module):
def __init__(self):
super(vgg_cam,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3,64,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(64,64,3,1,padding=1),
nn.ReLU(),
nn.MaxPool2d(2,2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(64,128,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(128,128,3,1,padding=1),
nn.ReLU(),
nn.MaxPool2d(2,2),
)
self.conv3 = nn.Sequential(
nn.Conv2d(128,256,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(256,256,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(256,256,3,1,padding=1),
nn.ReLU(),
nn.MaxPool2d(2,2),
)
self.conv4 = nn.Sequential(
nn.Conv2d(256,512,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(512,512,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(512,512,3,1,padding=1),
nn.ReLU(),
nn.MaxPool2d(2,2),
)
self.conv5 = nn.Sequential(
nn.Conv2d(512,512,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(512,512,3,1,padding=1),
nn.ReLU(),
nn.Conv2d(512,512,3,1,padding=1),
nn.ReLU(),
#nn.MaxPool2d(2,2),
)
#self.cam = nn.AvgPool2d(7,7)
#self.fc = nn.Linear(512,40,bias=False)
self.classifier = nn.Sequential(
nn.Conv2d(512,1024,3,1,padding=1),
nn.ReLU(),
)
'''''''''
self.classifier = nn.Sequential(
nn.Conv2d(512, 4096, 7,padding=3),
nn.ReLU(inplace=True),
nn.Dropout2d(),
nn.Conv2d(4096, 4096, 1),
nn.ReLU(inplace=True)
)
'''''''''
self.cam = nn.AvgPool2d(14,14)
self.fc = nn.Sequential(
nn.Dropout2d(),
nn.Linear(1024,40,bias=False)
)
def forward(self,x):
x = self.conv1(x)
#print x.data.cuda().numpy()
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
#x1 = x.data.cpu().numpy()
#print(x.size)
x = self.classifier(x)
cam = self.cam(x)
#x2 = cam.data.cpu().numpy()
#print(cam.size(0))
cam_x = cam.view(cam.size(0),-1)
#s = cam_x.data.cpu().numpy()
out = self.fc(cam_x)
return out,cam_x
def copy_params_from_pretrain_vgg(self,pretrain_vgg16, init_fc8 = True):
conv_blocks = [self.conv1,
self.conv2,
self.conv3,
self.conv4,
self.conv5]
ranges = [[0, 4], [5, 9], [10, 16], [17, 23], [24, 29]]
features = list(pretrain_vgg16.features.children())
for idx, conv_block in enumerate(conv_blocks):
for l1, l2 in zip(features[ranges[idx][0]:ranges[idx][1]], conv_block):
if isinstance(l1, nn.Conv2d) and isinstance(l2, nn.Conv2d):
# print idx, l1, l2
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data
'''''''''
for i1, i2 in zip([0, 3], [0, 3]):
l1 = pretrain_vgg16.classifier[i1]
l2 = self.classifier[i2]
# print type(l1), dir(l1),
l2.weight.data = l1.weight.data.view(l2.weight.size())
l2.bias.data = l1.bias.data.view(l2.bias.size())
'''''''''
'''''''''
n_class = self.fc.weight.size()[0]
if init_fc8:
l1 = self.fc
init.kaiming_normal(l1.weight)
else:
l1 = pretrain_vgg16.classifier[7]
l2 = self.fc
l2.weight.data = l1.weight.data[:n_class, :].view(l2.weight.size())
'''''''''
###########source code from
########### https://github.com/wkentaro/pytorch-fcn/blob/master/torchfcn/models/fcn32s.py
def copy_params_from_vgg16(self, vgg16):
for l1, l2 in zip(vgg16.features, self.features):
if (isinstance(l1, nn.Conv2d) and
isinstance(l2, nn.Conv2d)):
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data
for i in [0, 3]:
l1 = vgg16.classifier[i]
l2 = self.classifier[i]
l2.weight.data = l1.weight.data.view(l2.weight.size())
l2.bias.data = l1.bias.data.view(l2.bias.size())