Skip to content

Commit 0ff90bd

Browse files
dmitriy-serdyukruotianluo
authored andcommitted
Bump pytorch to 4.1 (ruotianluo#70)
* Update to pytorch4.1 * Refactor
1 parent 622b6a5 commit 0ff90bd

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

dataloader.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ def __len__(self):
204204
return len(self.info['images'])
205205

206206

207+
class ArraySampler(data.sampler.SubsetRandomSampler):
208+
def __iter__(self):
209+
return iter(self.indices)
210+
211+
207212
class BlobFetcher():
208213
"""Experimental class for prefetching blobs in a separate process."""
209214
def __init__(self, split, dataloader, if_shuffle=False):
@@ -225,7 +230,8 @@ def reset(self):
225230
the get_minibatch_inds already.
226231
"""
227232
# batch_size is 0, the merge is done in DataLoader class
228-
sampler = self.dataloader.split_ix[self.split][self.dataloader.iterators[self.split]:]
233+
sampler = ArraySampler(
234+
self.dataloader.split_ix[self.split][self.dataloader.iterators[self.split]:])
229235
self.split_loader = iter(
230236
data.DataLoader(dataset=self.dataloader,
231237
batch_size=1,

eval_utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,24 @@ def eval_split(model, crit, loader, eval_kwargs={}):
8585
if data.get('labels', None) is not None:
8686
# forward the model to get loss
8787
tmp = [data['fc_feats'], data['att_feats'], data['labels'], data['masks']]
88-
tmp = [Variable(torch.from_numpy(_), volatile=True).cuda() for _ in tmp]
89-
fc_feats, att_feats, labels, masks = tmp
88+
with torch.no_grad():
89+
tmp = [Variable(torch.from_numpy(_)).cuda() for _ in tmp]
90+
fc_feats, att_feats, labels, masks = tmp
9091

91-
loss = crit(model(fc_feats, att_feats, labels), labels[:,1:], masks[:,1:]).data[0]
92+
loss = crit(model(fc_feats, att_feats, labels), labels[:,1:], masks[:,1:]).item()
9293
loss_sum = loss_sum + loss
9394
loss_evals = loss_evals + 1
9495

9596
# forward the model to also get generated samples for each image
9697
# Only leave one feature for each image, in case duplicate sample
9798
tmp = [data['fc_feats'][np.arange(loader.batch_size) * loader.seq_per_img],
9899
data['att_feats'][np.arange(loader.batch_size) * loader.seq_per_img]]
99-
tmp = [Variable(torch.from_numpy(_), volatile=True).cuda() for _ in tmp]
100-
fc_feats, att_feats = tmp
101-
# forward the model to also get generated samples for each image
102-
seq, _ = model.sample(fc_feats, att_feats, eval_kwargs)
100+
with torch.no_grad():
101+
tmp = [Variable(torch.from_numpy(_)).cuda() for _ in tmp]
102+
fc_feats, att_feats = tmp
103+
# forward the model to also get generated samples for each image
104+
seq, _ = model.sample(fc_feats, att_feats, eval_kwargs)
105+
seq = seq.cpu().numpy()
103106

104107
#set_trace()
105108
sents = utils.decode_sequence(loader.get_vocab(), seq)

misc/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def if_use_att(caption_model):
1515

1616
# Input: seq, N*D numpy array, with element 0 .. vocab_size. 0 is END token.
1717
def decode_sequence(ix_to_word, seq):
18-
N, D = seq.size()
18+
N, D = seq.shape
1919
out = []
2020
for i in range(N):
2121
txt = ''
@@ -42,9 +42,9 @@ def __init__(self):
4242

4343
def forward(self, input, target, mask):
4444
# truncate to the same size
45-
target = target[:, :input.size(1)]
46-
mask = mask[:, :input.size(1)]
47-
input = to_contiguous(input).view(-1, input.size(2))
45+
target = target[:, :input.shape[1]]
46+
mask = mask[:, :input.shape[1]]
47+
input = to_contiguous(input).view(-1, input.shape[2])
4848
target = to_contiguous(target).view(-1, 1)
4949
mask = to_contiguous(mask).view(-1, 1)
5050
output = - input.gather(1, target) * mask
@@ -59,4 +59,4 @@ def set_lr(optimizer, lr):
5959
def clip_gradient(optimizer, grad_clip):
6060
for group in optimizer.param_groups:
6161
for param in group['params']:
62-
param.grad.data.clamp_(-grad_clip, grad_clip)
62+
param.grad.data.clamp_(-grad_clip, grad_clip)

scripts/prepro_feats.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,20 @@ def main(params):
8080

8181
I = I.astype('float32')/255.0
8282
I = torch.from_numpy(I.transpose([2,0,1])).cuda()
83-
I = Variable(preprocess(I), volatile=True)
84-
tmp_fc, tmp_att = my_resnet(I, params['att_size'])
85-
# write to hdf5
86-
87-
d_set_fc = file_fc.create_dataset(str(img['cocoid']),
88-
(2048,), dtype="float")
89-
d_set_att = file_att.create_dataset(str(img['cocoid']),
90-
(params['att_size'], params['att_size'], 2048), dtype="float")
83+
with torch.no_grad():
84+
I = Variable(preprocess(I))
85+
tmp_fc, tmp_att = my_resnet(I, params['att_size'])
9186

92-
d_set_fc[...] = tmp_fc.data.cpu().float().numpy()
93-
d_set_att[...] = tmp_att.data.cpu().float().numpy()
87+
# write to hdf5
88+
d_set_fc = file_fc.create_dataset(
89+
str(img['cocoid']),
90+
(2048,), dtype="float")
91+
d_set_att = file_att.create_dataset(
92+
str(img['cocoid']),
93+
(params['att_size'], params['att_size'], 2048), dtype="float")
94+
95+
d_set_fc[...] = tmp_fc.cpu().float().numpy()
96+
d_set_att[...] = tmp_att.cpu().float().numpy()
9497
if i % 1000 == 0:
9598
print('processing %d/%d (%.2f%% done)' % (i, N, i*100.0 / N))
9699
file_fc.close()

0 commit comments

Comments
 (0)