-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathsample.py
286 lines (229 loc) · 11.4 KB
/
sample.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import time
import pickle
import argparse
from tqdm import tqdm
import torch
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torch.autograd import Variable
from utils.models_debugger import *
from utils.dataset import *
from utils.loss import *
from utils.build_tag import *
class CaptionSampler(object):
def __init__(self, args):
self.args = args
self.vocab = self.__init_vocab()
self.tagger = self.__init_tagger()
self.transform = self.__init_transform()
self.data_loader = self.__init_data_loader(self.args.file_lits)
self.model_state_dict = self.__load_mode_state_dict()
self.extractor = self.__init_visual_extractor()
self.mlc = self.__init_mlc()
self.co_attention = self.__init_co_attention()
self.sentence_model = self.__init_sentence_model()
self.word_model = self.__init_word_word()
def test(self):
self.extractor.train()
self.mlc.train()
self.co_attention.train()
self.sentence_model.train()
self.word_model.train()
progress_bar = tqdm(self.data_loader, desc='Testing')
for images, _, label, captions, prob in progress_bar:
images = self.__to_var(images, requires_grad=False)
visual_features = self.extractor.forward(images)
tags, semantic_features = self.mlc.forward(visual_features)
sentence_states = None
prev_hidden_states = self.__to_var(torch.zeros(images.shape[0], 1, self.args.hidden_size))
context = self.__to_var(torch.Tensor(captions).long(), requires_grad=False)
prob_real = self.__to_var(torch.Tensor(prob).long(), requires_grad=False)
for sentence_index in range(captions.shape[1]):
ctx = self.co_attention.forward(visual_features, semantic_features, prev_hidden_states)
topic, p_stop, hidden_states, sentence_states = self.sentence_model.forward(ctx,
prev_hidden_states,
sentence_states)
for word_index in range(1, captions.shape[2]):
words = self.word_model.forward(topic, context[:, sentence_index, :word_index])
# Debugging...
# print("Context:{}".format(context[:, 0, :word_index]))
# print("word index: {}".format(word_index))
# print("Pred: {}".format(torch.max(words.squeeze(1), 1)[1]))
# print("Real: {}".format(context[:, sentence_index, word_index]))
# print()
def sample(self, image_file):
self.extractor.train()
self.mlc.train()
self.co_attention.train()
self.sentence_model.train()
self.word_model.train()
# image_data = Image.open(image_file).convert('RGB')
image = torch.randn((1, 3, 224, 224))
image = self.transform(image)
image = self.__to_var(image, requires_grad=False)
visual_features = self.extractor.forward(image)
tags, semantic_features = self.mlc.forward(visual_features)
sentence_states = None
prev_hidden_states = self.__to_var(torch.zeros(1, 1, self.args.hidden_size))
pred_sentences = {}
for i in range(self.args.s_max):
ctx = self.co_attention.forward(visual_features, semantic_features, prev_hidden_states)
topic, p_stop, hidden_state, sentence_states = self.sentence_model.forward(ctx,
prev_hidden_states,
sentence_states)
p_stop = p_stop.squeeze(1)
p_stop = torch.max(p_stop, 1)[1].unsqueeze(1)
start_tokens = np.zeros((1, 1))
start_tokens[:, 0] = self.vocab('<start>')
start_tokens = self.__to_var(torch.Tensor(start_tokens).long(), requires_grad=False)
sampled_ids = self.word_model.sample(topic, start_tokens)
prev_hidden_states = hidden_state
sampled_ids = sampled_ids * p_stop
pred_sentences[i] = self.__vec2sent(sampled_ids.cpu().detach().numpy())
self.tagger.array2tags(torch.topk(tags, self.args.k)[1].cpu().detach().numpy())
def __load_mode_state_dict(self):
try:
model_state_dict = torch.load(self.args.load_model_path)
print("[Load Model-{} Succeed!]".format(self.args.load_model_path))
print("Load From Epoch {}".format(model_state_dict['epoch']))
return model_state_dict
except Exception as err:
print("[Load Model Failed] {}".format(err))
raise err
def __init_tagger(self):
return Tag()
def __vec2sent(self, array):
sampled_caption = []
for word_id in array:
word = self.vocab.get_word_by_id(word_id)
if word == '<start>':
continue
if word == '<end>' or word == '':
break
sampled_caption.append(word)
return ' '.join(sampled_caption)
def __init_vocab(self):
with open(self.args.vocab_path, 'rb') as f:
vocab = pickle.load(f)
return vocab
def __init_data_loader(self, file_list):
data_loader = get_loader(image_dir=self.args.image_dir,
caption_json=self.args.caption_json,
file_list=file_list,
vocabulary=self.vocab,
transform=self.transform,
batch_size=self.args.batch_size,
shuffle=False)
return data_loader
def __init_transform(self):
transform = transforms.Compose([
transforms.Resize((self.args.resize, self.args.resize)),
# transforms.RandomCrop(self.args.crop_size),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))])
return transform
def __to_var(self, x, requires_grad=True):
if self.args.cuda:
x = x.cuda()
return Variable(x, requires_grad=requires_grad)
def __init_visual_extractor(self):
model = VisualFeatureExtractor(pretrained=False)
if self.model_state_dict is not None:
print("Visual Extractor Loaded!")
model.load_state_dict(self.model_state_dict['extractor'])
if self.args.cuda:
model = model.cuda()
return model
def __init_mlc(self):
model = MLC(classes=self.args.classes,
sementic_features_dim=self.args.sementic_features_dim,
fc_in_features=self.extractor.out_features,
k=self.args.k)
if self.model_state_dict is not None:
print("MLC Loaded!")
model.load_state_dict(self.model_state_dict['mlc'])
if self.args.cuda:
model = model.cuda()
return model
def __init_co_attention(self):
model = CoAttention(embed_size=self.args.embed_size,
hidden_size=self.args.hidden_size,
visual_size=self.extractor.out_features)
if self.model_state_dict is not None:
print("Co-Attention Loaded!")
model.load_state_dict(self.model_state_dict['co_attention'])
if self.args.cuda:
model = model.cuda()
return model
def __init_sentence_model(self):
model = SentenceLSTM(embed_size=self.args.embed_size,
hidden_size=self.args.hidden_size,
num_layers=self.args.sentence_num_layers)
if self.model_state_dict is not None:
print("Sentence Model Loaded!")
model.load_state_dict(self.model_state_dict['sentence_model'])
if self.args.cuda:
model = model.cuda()
return model
def __init_word_word(self):
model = WordLSTM(vocab_size=len(self.vocab),
embed_size=self.args.embed_size,
hidden_size=self.args.hidden_size,
num_layers=self.args.word_num_layers)
if self.model_state_dict is not None:
print("Word Model Loaded!")
model.load_state_dict(self.model_state_dict['word_model'])
if self.args.cuda:
model = model.cuda()
return model
if __name__ == '__main__':
model_dir = './report_models/only_training/20180528-02:44:52'
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('--resize', type=int, default=224,
help='size for resizing images')
parser.add_argument('--pretrained', action='store_true', default=False,
help='not using pretrained model when training')
parser.add_argument('--vocab_path', type=str, default='./data/vocab.pkl',
help='the path for vocabulary object')
parser.add_argument('--image_dir', type=str, default='./data/images',
help='the path for images')
parser.add_argument('--caption_json', type=str, default='./data/captions.json',
help='path for captions')
parser.add_argument('--file_lits', type=str, default='./data/val_data.txt',
help='the path for test file list')
parser.add_argument('--load_model_path', type=str, default=os.path.join(model_dir, 'best_stop.pth.tar'),
help='The path of loaded model')
parser.add_argument('--result_path', type=str, default=os.path.join(model_dir, 'results'),
help='the path for storing results')
parser.add_argument('--result_name', type=str, default='train',
help='the name of results')
parser.add_argument('--classes', type=int, default=156)
parser.add_argument('--sementic_features_dim', type=int, default=512)
parser.add_argument('--kernel_size', type=int, default=7)
parser.add_argument('--fc_in_features', type=int, default=2048)
parser.add_argument('--k', type=int, default=10)
parser.add_argument('--embed_size', type=int, default=512)
parser.add_argument('--hidden_size', type=int, default=512)
parser.add_argument('--visual_size', type=int, default=49)
parser.add_argument('--sentence_num_layers', type=int, default=2)
parser.add_argument('--word_num_layers', type=int, default=1)
parser.add_argument('--s_max', type=int, default=6)
parser.add_argument('--n_max', type=int, default=30)
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--lambda_tag', type=float, default=10000)
parser.add_argument('--lambda_stop', type=float, default=10)
parser.add_argument('--lambda_word', type=float, default=1)
args = parser.parse_args()
args.cuda = torch.cuda.is_available()
sampler = CaptionSampler(args)
# tag_loss, stop_loss, word_loss, loss = sampler.test()
#
# print("tag loss:{}".format(tag_loss))
# print("stop loss:{}".format(stop_loss))
# print("word loss:{}".format(word_loss))
# print("loss:{}".format(loss))
sampler.sample()