-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
205 lines (166 loc) · 7.14 KB
/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import torch.nn as nn
import torch
def conv3x3(in_channels, out_channels, **kwargs):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, **kwargs),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.MaxPool2d(2)
)
def conv3x3nopool(in_channels, out_channels, **kwargs):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, **kwargs),
nn.BatchNorm2d(out_channels),
nn.ReLU()
)
def conv3x3nobatch(in_channels, out_channels, **kwargs):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, **kwargs),
nn.ReLU(),
nn.MaxPool2d(2)
)
class PrototypicalNetworkHAT(nn.Module):
def __init__(self, in_channels, out_channels, hidden_size=64, num_tasks = 0):
super(PrototypicalNetworkHAT, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.hidden_size = hidden_size
self.taskcla = num_tasks
self.ec1=torch.nn.Embedding(self.taskcla,64)
self.ec2=torch.nn.Embedding(self.taskcla,64)
self.ec3=torch.nn.Embedding(self.taskcla,64)
self.ec4=torch.nn.Embedding(self.taskcla,64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(2)
self.gate=torch.nn.Sigmoid()
self.conv1 = conv3x3(in_channels, hidden_size)
self.conv2 = conv3x3(hidden_size, hidden_size)
self.conv3 = conv3x3(hidden_size, hidden_size)
self.conv4 = conv3x3(hidden_size, out_channels)
self.domain_out = torch.nn.ModuleList()
for _ in range(self.taskcla):
self.task = nn.Sequential(
conv3x3(hidden_size, out_channels)
)
self.domain_out.append(self.task)
def forward(self, inputs, domain_id, s=1):
masks=self.mask(inputs.get_device(), domain_id, s=s)
gc1,gc2,gc3,gc4 = masks
h = self.conv1(inputs.view(-1, *inputs.shape[2:]))
h=h*gc1.view(1,-1,1,1).expand_as(h)
h = self.conv2(h)
h=h*gc2.view(1,-1,1,1).expand_as(h)
h = self.conv3(h)
h=h*gc3.view(1,-1,1,1).expand_as(h)
h = self.conv4(h)
h=h*gc4.view(1,-1,1,1).expand_as(h)
h = self.domain_out[domain_id](h)
return h.view(*inputs.shape[:2], -1), masks
def mask(self, device, t, s=1):
t = torch.tensor(t).to(device)
gc1=self.gate(s*self.ec1(t))
gc2=self.gate(s*self.ec2(t))
gc3=self.gate(s*self.ec3(t))
gc4=self.gate(s*self.ec4(t))
return [gc1,gc2,gc3,gc4]
def get_view_for(self,n,masks):
gc1,gc2,gc3,gc4 = masks
if n=='conv1.0.weight':
return gc1.data.view(-1,1,1,1).expand_as(self.conv1[0].weight)
elif n=='conv1.0.bias':
return gc1.data.view(-1)
elif n=='conv2.0.weight':
post=gc2.data.view(-1,1,1,1).expand_as(self.conv2[0].weight)
pre=gc1.data.view(1,-1,1,1).expand_as(self.conv2[0].weight)
return torch.min(post,pre)
elif n=='conv2.0.bias':
return gc2.data.view(-1)
elif n=='conv3.0.weight':
post=gc3.data.view(-1,1,1,1).expand_as(self.conv3[0].weight)
pre=gc2.data.view(1,-1,1,1).expand_as(self.conv3[0].weight)
return torch.min(post,pre)
elif n=='conv3.0.bias':
return gc3.data.view(-1)
elif n=='conv4.0.weight':
post=gc4.data.view(-1,1,1,1).expand_as(self.conv4[0].weight)
pre=gc3.data.view(1,-1,1,1).expand_as(self.conv4[0].weight)
return torch.min(post,pre)
elif n=='conv4.0.bias':
return gc4.data.view(-1)
return None
class PrototypicalNetwork(nn.Module):
def __init__(self, in_channels, out_channels, hidden_size=64, num_tasks = 0):
super(PrototypicalNetwork, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.hidden_size = hidden_size
self.taskcla = num_tasks
self.conv1 = conv3x3nobatch(in_channels, hidden_size)
self.conv2 = conv3x3nobatch(hidden_size, hidden_size)
self.conv3 = conv3x3nobatch(hidden_size, hidden_size)
self.domain_out = torch.nn.ModuleList()
for _ in range(self.taskcla):
self.task = nn.Sequential(
conv3x3(hidden_size, hidden_size),
conv3x3(hidden_size, out_channels)
)
self.domain_out.append(self.task)
def forward(self, inputs, domain_id, s=1):
h = self.conv1(inputs.view(-1, *inputs.shape[2:]))
h = self.conv2(h)
h = self.conv3(h)
h = self.domain_out[domain_id](h)
return h.view(*inputs.shape[:2], -1)
def set_req_grad(self, domain_id, req_grad):
for i in range(self.taskcla):
if i!= domain_id:
params = list(self.domain_out[i].parameters())
for ind in range(len(params)):
params[ind].requires_grad = req_grad
else:
params = list(self.domain_out[domain_id].parameters())
for ind in range(len(params)):
params[ind].requires_grad = True
return
class PrototypicalNetworkJoint(nn.Module):
def __init__(self, in_channels, out_channels, hidden_size=64, num_tasks = 0):
super(PrototypicalNetworkJoint, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.hidden_size = hidden_size
self.taskcla = num_tasks
self.conv1 = conv3x3(in_channels, hidden_size)
self.conv2 = conv3x3(hidden_size, hidden_size)
self.conv3 = conv3x3(hidden_size, hidden_size)
self.conv4 = conv3x3(hidden_size, hidden_size)
self.conv5 = conv3x3(hidden_size, out_channels)
def forward(self, inputs, domain_id = None):
h = self.conv1(inputs.view(-1, *inputs.shape[2:]))
h = self.conv2(h)
h = self.conv3(h)
h = self.conv4(h)
h = self.conv5(h)
return h.view(*inputs.shape[:2], -1)
class PrototypicalNetworkMultitask(nn.Module):
def __init__(self, in_channels, out_channels, hidden_size=64, num_tasks = 0):
super(PrototypicalNetworkMultitask, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.hidden_size = hidden_size
self.taskcla = num_tasks
self.conv1 = conv3x3(in_channels, hidden_size)
self.conv2 = conv3x3(hidden_size, hidden_size)
self.conv3 = conv3x3(hidden_size, hidden_size)
self.domain_out = torch.nn.ModuleList()
for _ in range(self.taskcla):
self.task = nn.Sequential(
conv3x3(hidden_size, hidden_size),
conv3x3(hidden_size, out_channels)
)
self.domain_out.append(self.task)
def forward(self, inputs, domain_id, s=1):
h = self.conv1(inputs.view(-1, *inputs.shape[2:]))
h = self.conv2(h)
h = self.conv3(h)
h = self.domain_out[domain_id](h)
return h.view(*inputs.shape[:2], -1)