-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttendModel.py
181 lines (143 loc) · 7.66 KB
/
AttendModel.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import *
import misc.utils as utils
from .CaptionModel import CaptionModel
class AttentionModel(CaptionModel):
def __init__(self, opt):
super(OldModel, self).__init__()
self.vocab_size = opt.vocab_size
self.seq_length = opt.seq_length
self.linear = nn.Linear(2048, 512)
self.word_embed = nn.Embedding(self.vocab_size + 1, 512)
self.logit = nn.Linear(512, self.vocab_size + 1)
self.dropout = nn.Dropout(0.5)
self.init_weights()
def init_weights(self):
initrange = 0.1
self.word_embed.weight.data.uniform_(-initrange, initrange)
self.logit.bias.data.fill_(0)
self.logit.weight.data.uniform_(-initrange, initrange)
def init_hidden(self, fc_feats):
image_map = self.linear(fc_feats).view(-1, 1, 512).transpose(0, 1)
return (image_map, image_map)
def forward(self, fc_feats, att_feats, seq):
batch_size = fc_feats.size(0)
state = self.init_hidden(fc_feats)
outputs = []
for i in range(seq.size(1) - 1):
it = seq[:, i].clone()
# break if all the sequences end
if i >= 1 and seq[:, i].data.sum() == 0:
break
xt = self.word_embed(it)
output, state = self.core(xt, fc_feats, att_feats, state)
output = F.log_softmax(self.logit(self.dropout(output)))
outputs.append(output)
return torch.cat([_.unsqueeze(1) for _ in outputs], 1)
def get_logprobs_state(self, it, tmp_fc_feats, tmp_att_feats, state):
xt = self.word_embed(it)
output, state = self.core(xt, tmp_fc_feats, tmp_att_feats, state)
logprobs = F.log_softmax(self.logit(self.dropout(output)))
return logprobs, state
def sample_beam(self, fc_feats, att_feats, opt={}):
beam_size = opt.get('beam_size', 10)
batch_size = fc_feats.size(0)
assert beam_size <= self.vocab_size + 1, 'lets assume this for now, otherwise this corner case causes a few headaches down the road. can be dealt with in future if needed'
seq = torch.LongTensor(self.seq_length, batch_size).zero_()
seqLogprobs = torch.FloatTensor(self.seq_length, batch_size)
# lets process every image independently for now, for simplicity
self.done_beams = [[] for _ in range(batch_size)]
for k in range(batch_size):
tmp_fc_feats = fc_feats[k:k+1].expand(beam_size, 2048)
tmp_att_feats = att_feats[k:k+1].expand(*((beam_size,)+att_feats.size()[1:])).contiguous()
state = self.init_hidden(tmp_fc_feats)
beam_seq = torch.LongTensor(self.seq_length, beam_size).zero_()
beam_seq_logprobs = torch.FloatTensor(self.seq_length, beam_size).zero_()
beam_logprobs_sum = torch.zeros(beam_size) # running sum of logprobs for each beam
done_beams = []
for t in range(1):
if t == 0: # input <bos>
it = fc_feats.data.new(beam_size).long().zero_()
xt = self.word_embed(Variable(it, requires_grad=False))
output, state = self.core(xt, tmp_fc_feats, tmp_att_feats, state)
logprobs = F.log_softmax(self.logit(self.dropout(output)))
self.done_beams[k] = self.beam_search(state, logprobs, tmp_fc_feats, tmp_att_feats, opt=opt)
seq[:, k] = self.done_beams[k][0]['seq'] # the first beam has highest cumulative score
seqLogprobs[:, k] = self.done_beams[k][0]['logps']
# return the samples and their log likelihoods
return seq.transpose(0, 1), seqLogprobs.transpose(0, 1)
def sample(self, fc_feats, att_feats, opt={}):
sample_max = opt.get('sample_max', 1)
beam_size = opt.get('beam_size', 1)
temperature = opt.get('temperature', 1.0)
if beam_size > 1:
return self.sample_beam(fc_feats, att_feats, opt)
batch_size = fc_feats.size(0)
state = self.init_hidden(fc_feats)
seq = []
seqLogprobs = []
for t in range(self.seq_length + 1):
if t == 0: # input <bos>
it = fc_feats.data.new(batch_size).long().zero_()
elif sample_max:
sampleLogprobs, it = torch.max(logprobs.data, 1)
it = it.view(-1).long()
else:
if temperature == 1.0:
prob_prev = torch.exp(logprobs.data).cpu() # fetch prev distribution: shape Nx(M+1)
else:
# scale logprobs by temperature
prob_prev = torch.exp(torch.div(logprobs.data, temperature)).cpu()
it = torch.multinomial(prob_prev, 1).cuda()
sampleLogprobs = logprobs.gather(1, Variable(it, requires_grad=False)) # gather the logprobs at sampled positions
it = it.view(-1).long() # and flatten indices for downstream processing
xt = self.word_embed(Variable(it, requires_grad=False))
if t >= 1:
# stop when all finished
if t == 1:
unfinished = it > 0
else:
unfinished = unfinished * (it > 0)
if unfinished.sum() == 0:
break
it = it * unfinished.type_as(it)
seq.append(it) #seq[t] the input of t+2 time step
seqLogprobs.append(sampleLogprobs.view(-1))
output, state = self.core(xt, fc_feats, att_feats, state)
logprobs = F.log_softmax(self.logit(self.dropout(output)))
return torch.cat([_.unsqueeze(1) for _ in seq], 1), torch.cat([_.unsqueeze(1) for _ in seqLogprobs], 1)
class ShowAttendTellCore(nn.Module):
def __init__(self, opt):
super(ShowAttendTellCore, self).__init__()
self.feature_size = 512
self.attention_hidden_size = 512
self.rnn = nn.LSTM(512 + self.feature_size, 512, 1, bias=False, dropout=0.5) #attention feature size: 512
self.context_to_att = nn.Linear(512, self.attention_hidden_size)
self.hidden_to_att = nn.Linear(512, self.attention_hidden_size)
self.percep_score = nn.Linear(self.attention_hidden_size, 1)
def forward(self, xt, fc_feats, att_feats, state):
att_size = att_feats.numel() // att_feats.size(0) // self.feature_size
img_att = att_feats.view(-1, self.feature_size)
img_att = self.context_to_att(img_att)
img_att = img_att.view(-1, att_size, self.attention_hidden_size)
hid_att = self.hidden_to_att(state[0][-1])
hid_att = hid_att.unsqueeze(1).expand_as(img_att)
alignment = img_att + hid_att
alignment = F.tanh(alignment)
alignment = alignment.view(-1, self.attention_hidden_size)
alignment = self.percep_score(alignment)
alignment = alignment.view(-1, att_size)
score = F.softmax(alignment)
att_feats_ = att_feats.view(-1, att_size, self.feature_size)
att_res = torch.bmm(score.unsqueeze(1), att_feats_).squeeze(1)
output, state = self.rnn(torch.cat([xt, att_res], 1).unsqueeze(0), state)
return output.squeeze(0), state
class ShowAttendTellModel(AttentionModel):
def __init__(self, opt):
super(ShowAttendTellModel, self).__init__(opt)
self.core = ShowAttendTellCore(opt)