-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
249 lines (207 loc) · 10.4 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import torch
import torch.nn as nn
import modules
class RecurrentAttention_OracleClassifier(nn.Module):
def __init__(
self, g, k, s, c, h_g, h_l, std, hidden_size, num_classes, n_classifiers,
):
super().__init__()
self.std = std
self.sensor = modules.GlimpseNetwork(h_g, h_l, g, k, s, c)
self.rnn = modules.CoreNetwork(hidden_size, hidden_size)
self.locator = modules.LocationNetwork(hidden_size, 2, std)
self.baseliner = modules.BaselineNetwork(hidden_size, 1)
self.classifier = modules.ClassificationNetwork(hidden_size, num_classes)
self.ensemble_classifier = nn.ModuleList([
modules.ClassificationNetwork(hidden_size, num_classes)
for _ in range(n_classifiers-1)])
# oracle
self.sensor_oracle = modules.GlimpseNetwork(h_g, h_l, g, k, s, c)
self.rnn_oracle = modules.CoreNetwork(hidden_size, hidden_size)
self.classifier_oracle = modules.ClassificationNetwork(hidden_size, num_classes)
def forward(self, x, l_t_prev, h_t_prev, h_t_oracle_prev, last=False):
g_t = self.sensor(x, l_t_prev)
h_t = self.rnn(g_t, h_t_prev)
g_t_oracle = self.sensor_oracle(x, l_t_prev)
h_t_oracle = self.rnn_oracle(g_t_oracle, h_t_oracle_prev)
log_pi, l_t = self.locator(h_t)
b_t = self.baseliner(h_t).squeeze()
if last:
c_log_pi_ensemble_list = []
for classifier in self.ensemble_classifier:
c_log_pi_i = classifier(h_t.detach())
c_log_pi_ensemble_list.append(c_log_pi_i)
if c_log_pi_ensemble_list == []:
c_log_pi_ensemble = None
else:
c_log_pi_ensemble = torch.stack(c_log_pi_ensemble_list)
c_log_pi = self.classifier(h_t)
c_log_pi_oracle = self.classifier_oracle(h_t_oracle.detach())
return h_t, l_t, b_t, c_log_pi, c_log_pi_ensemble, log_pi, c_log_pi_oracle
return h_t, l_t, b_t, log_pi, h_t_oracle,
class RecurrentAttention(nn.Module):
"""A Recurrent Model of Visual Attention (RAM) [1].
RAM is a recurrent neural network that processes
inputs sequentially, attending to different locations
within the image one at a time, and incrementally
combining information from these fixations to build
up a dynamic internal representation of the image.
References:
[1]: Minh et. al., https://arxiv.org/abs/1406.6247
"""
def __init__(
self, g, k, s, c, h_g, h_l, std, hidden_size, num_classes, n_classifiers,
):
"""Constructor.
Args:
g: size of the square patches in the glimpses extracted by the retina.
k: number of patches to extract per glimpse.
s: scaling factor that controls the size of successive patches.
c: number of channels in each image.
h_g: hidden layer size of the fc layer for `phi`.
h_l: hidden layer size of the fc layer for `l`.
std: standard deviation of the Gaussian policy.
hidden_size: hidden size of the rnn.
num_classes: number of classes in the dataset.
num_glimpses: number of glimpses to take per image,
i.e. number of BPTT steps.
"""
super().__init__()
self.std = std
self.sensor = modules.GlimpseNetwork(h_g, h_l, g, k, s, c)
self.rnn = modules.CoreNetwork(hidden_size, hidden_size)
self.locator = modules.LocationNetwork(hidden_size, 2, std)
self.baseliner = modules.BaselineNetwork(hidden_size, 1)
self.classifier = modules.ClassificationNetwork(hidden_size, num_classes)
self.ensemble_classifier = nn.ModuleList([
modules.ClassificationNetwork(hidden_size, num_classes)
for _ in range(n_classifiers-1)])
def forward(self, x, l_t_prev, h_t_prev, last=False):
"""Run RAM for one timestep on a minibatch of images.
Args:
x: a 4D Tensor of shape (B, H, W, C). The minibatch
of images.
l_t_prev: a 2D tensor of shape (B, 2). The location vector
containing the glimpse coordinates [x, y] for the previous
timestep `t-1`.
h_t_prev: a 2D tensor of shape (B, hidden_size). The hidden
state vector for the previous timestep `t-1`.
last: a bool indicating whether this is the last timestep.
If True, the action network returns an output probability
vector over the classes and the baseline `b_t` for the
current timestep `t`. Else, the core network returns the
hidden state vector for the next timestep `t+1` and the
location vector for the next timestep `t+1`.
Returns:
h_t: a 2D tensor of shape (B, hidden_size). The hidden
state vector for the current timestep `t`.
mu: a 2D tensor of shape (B, 2). The mean that parametrizes
the Gaussian policy.
l_t: a 2D tensor of shape (B, 2). The location vector
containing the glimpse coordinates [x, y] for the
current timestep `t`.
b_t: a vector of length (B,). The baseline for the
current time step `t`.
log_probas: a 2D tensor of shape (B, num_classes). The
output log probability vector over the classes.
log_pi: a vector of length (B,).
"""
g_t = self.sensor(x, l_t_prev)
h_t = self.rnn(g_t, h_t_prev)
log_pi, l_t = self.locator(h_t)
b_t = self.baseliner(h_t).squeeze()
if last:
c_log_pi_ensemble_list = []
for classifier in self.ensemble_classifier:
c_log_pi_i = classifier(h_t.detach())
c_log_pi_ensemble_list.append(c_log_pi_i)
if c_log_pi_ensemble_list == []:
c_log_pi_ensemble = None
else:
c_log_pi_ensemble = torch.stack(c_log_pi_ensemble_list)
c_log_pi = self.classifier(h_t)
# c_log_pi = self.classifier(h_t)
return h_t, l_t, b_t, c_log_pi, c_log_pi_ensemble, log_pi
return h_t, l_t, b_t, log_pi
class EarlyStoppingRecurrentAttention(nn.Module):
"""A Recurrent Model of Visual Attention (RAM) [1].
RAM is a recurrent neural network that processes
inputs sequentially, attending to different locations
within the image one at a time, and incrementally
combining information from these fixations to build
up a dynamic internal representation of the image.
References:
[1]: Minh et. al., https://arxiv.org/abs/1406.6247
"""
def __init__(
self, g, k, s, c, h_g, h_l, std, hidden_size, num_classes, n_classifiers,
):
"""Constructor.
Args:
g: size of the square patches in the glimpses extracted by the retina.
k: number of patches to extract per glimpse.
s: scaling factor that controls the size of successive patches.
c: number of channels in each image.
h_g: hidden layer size of the fc layer for `phi`.
h_l: hidden layer size of the fc layer for `l`.
std: standard deviation of the Gaussian policy.
hidden_size: hidden size of the rnn.
num_classes: number of classes in the dataset.
num_glimpses: number of glimpses to take per image,
i.e. number of BPTT steps.
"""
super().__init__()
self.std = std
self.sensor = modules.GlimpseNetwork(h_g, h_l, g, k, s, c)
self.rnn = modules.CoreNetwork(hidden_size, hidden_size)
# self.planner = modules.Plannernetwork(hidden_size, 2)
self.locator = modules.LocationNetworkEarlyStopping(hidden_size, 3, std)
self.baseliner = modules.BaselineNetwork(hidden_size, 1)
self.classifier = modules.ClassificationNetwork(hidden_size, num_classes)
# self.ensemble_classifier = nn.ModuleList([
# modules.ClassificationNetwork(hidden_size, num_classes)
# for _ in range(n_classifiers-1)])
# modules.ClassificationNetwork(hidden_size, num_classes)
def forward(self, x, l_t_prev, h_t_prev):
"""Run RAM for one timestep on a minibatch of images.
Args:
x: a 4D Tensor of shape (B, H, W, C). The minibatch
of images.
l_t_prev: a 2D tensor of shape (B, 3). The location vector
containing the glimpse coordinates [x, y, p_stop] for the previous
timestep `t-1`.
h_t_prev: a 2D tensor of shape (B, hidden_size). The hidden
state vector for the previous timestep `t-1`.
last: a bool indicating whether this is the last timestep.
If True, the action network returns an output probability
vector over the classes and the baseline `b_t` for the
current timestep `t`. Else, the core network returns the
hidden state vector for the next timestep `t+1` and the
location vector for the next timestep `t+1`.
Returns:
h_t: a 2D tensor of shape (B, hidden_size). The hidden
state vector for the current timestep `t`.
mu: a 2D tensor of shape (B, 2). The mean that parametrizes
the Gaussian policy.
l_t: a 2D tensor of shape (B, 3). The location vector
containing the glimpse coordinates [x, y, p_stop] for the
current timestep `t`.
b_t: a vector of length (B,). The baseline for the
current time step `t`.
log_probas: a 2D tensor of shape (B, num_classes). The
output log probability vector over the classes.
log_pi: a vector of length (B,).
"""
g_t = self.sensor(x, l_t_prev)
h_t = self.rnn(g_t, h_t_prev)
# p_log_pi, action_or_stop = self.planner(h_t)
l_t, l_log_pi, s_pi = self.locator(h_t)
b_t = self.baseliner(h_t).squeeze()
# c_log_pi_list = []
# for classifier in self.ensemble_classifier:
# c_log_pi = classifier(h_t)
# c_log_pi_list.append(c_log_pi)
# c_log_pi = torch.stack(c_log_pi_list)
c_log_pi = self.classifier(h_t)
# return h_t, p_log_pi, action_or_stop, l_t, l_log_pi, b_t, c_log_ps
return h_t, l_t, l_log_pi, s_pi, b_t, c_log_pi