-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearner.py
156 lines (119 loc) · 5.17 KB
/
learner.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
from __future__ import division, print_function, absolute_import
import pdb
import copy
from collections import OrderedDict
from torch.nn import functional as F
import torch
import torch.nn as nn
import numpy as np
import math
class Value(torch.nn.Module):
def __init__(self, dim_input, dim_val):
super(Value, self).__init__()
self.dim_val = dim_val
self.fc1 = nn.Linear(dim_input, dim_val, bias = True)
def forward(self, x):
x = self.fc1(x)
return x
class Key(torch.nn.Module):
def __init__(self, dim_input, dim_attn):
super(Key, self).__init__()
self.dim_attn = dim_attn
self.fc1 = nn.Linear(dim_input, dim_attn, bias = True)
def forward(self, x):
x = self.fc1(x)
return x
class Query(torch.nn.Module):
def __init__(self, dim_input, dim_attn):
super(Query, self).__init__()
self.dim_attn = dim_attn
self.fc1 = nn.Linear(dim_input, dim_attn, bias = True)
def forward(self, x):
x = self.fc1(x)
return x
class Learner(nn.Module):
def __init__(self, image_size, bn_eps, bn_momentum, n_classes):
super(Learner, self).__init__()
self.model = nn.ModuleDict({'features1': nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(3, 32, 3, padding=1)),
('norm1', nn.BatchNorm2d(32, bn_eps, bn_momentum)),
('relu1', nn.ReLU(inplace=False)),
('pool1', nn.MaxPool2d(2)),
('conv2', nn.Conv2d(32, 32, 3, padding=1)),
('norm2', nn.BatchNorm2d(32, bn_eps, bn_momentum)),
('relu2', nn.ReLU(inplace=False)),
('pool2', nn.MaxPool2d(2)),
('conv3', nn.Conv2d(32, 32, 3, padding=1)),
('norm3', nn.BatchNorm2d(32, bn_eps, bn_momentum)),
('relu3', nn.ReLU(inplace=False)),
('pool3', nn.MaxPool2d(2)),
]))
,'features2': nn.Sequential(OrderedDict([
('conv4', nn.Conv2d(32, 32, 3, padding=1)),
('norm4', nn.BatchNorm2d(32, bn_eps, bn_momentum)),
('relu4', nn.ReLU(inplace=False)),
]))
,'features3': nn.Sequential(OrderedDict([
('pool3', nn.MaxPool2d(2)),
]))
})
clr_in = image_size // 2**4
self.model.update({'cls': nn.Linear(32*clr_in*clr_in, n_classes)})
self.criterion = nn.CrossEntropyLoss()
self.model.update({'value': nn.Linear(252, 252)})
self.model.update({'key': nn.Linear(252, 252)})
self.model.update({'query': nn.Linear(252, 252)})
self.model.update({'dense': nn.Linear(252, 252)})
self.model.update({'layernorm': nn.LayerNorm(252, bn_eps)})
def forward(self, x, x_t):
batch_size, c, h, w=x.size()
batch_size_t, _, _, _=x_t.size()
x = x.reshape([batch_size,h,w*c])
if len(x_t)==3:
x_t = x_t.reshape([h,w*c])
else:
x_t = x_t.reshape([batch_size_t,h,w*c])
v_h = self.model.value(x)
k_h = self.model.key(x)
q_h = self.model.query(x_t)
dots = torch.matmul(q_h, k_h.transpose(-1, -2))
dots = dots / math.sqrt(w)
dots = nn.Softmax(dim=-1)(dots)
out = torch.matmul(dots, v_h)
hidden_states = self.model.dense(out)
hidden_states = self.model.layernorm(hidden_states+x)
x = x_t + hidden_states
x = x.reshape([batch_size, c, h, w])
x_f = self.model.features1(x)
x_f_2 = self.model.features2(x_f)
x_f_3 = self.model.features3(x_f_2)
x = torch.reshape(x_f_3, [x_f_3.size(0), -1])
outputs = self.model.cls(x)
return outputs , x_f_2
def get_flat_params(self):
return torch.cat([p.view(-1) for p in self.model.parameters()], 0)
def copy_flat_params(self, cI):
idx = 0
for p in self.model.parameters():
plen = p.view(-1).size(0)
p.data.copy_(cI[idx: idx+plen].view_as(p))
idx += plen
def transfer_params(self, learner_w_grad, cI):
# Use load_state_dict only to copy the running mean/var in batchnorm, the values of the parameters
# are going to be replaced by cI
self.load_state_dict(learner_w_grad.state_dict())
# replace nn.Parameters with tensors from cI (NOT nn.Parameters anymore).
idx = 0
for m in self.model.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.Linear):
wlen = m._parameters['weight'].view(-1).size(0)
m._parameters['weight'] = cI[idx: idx+wlen].view_as(m._parameters['weight']).clone()
idx += wlen
if m._parameters['bias'] is not None:
blen = m._parameters['bias'].view(-1).size(0)
m._parameters['bias'] = cI[idx: idx+blen].view_as(m._parameters['bias']).clone()
idx += blen
def reset_batch_stats(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.reset_running_stats()