Skip to content

Commit

Permalink
Add compatibility to resnet features.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruotianluo committed Apr 28, 2018
1 parent 5f8f7e7 commit 9ebae0a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ def get_batch(self, split, batch_size=None, seq_per_img=None):
data['att_masks'] = np.zeros(data['att_feats'].shape[:2], dtype='float32')
for i in range(len(att_batch)):
data['att_masks'][i*seq_per_img:(i+1)*seq_per_img, :att_batch[i].shape[0]] = 1
# set att_masks to None if attention features have same length
if data['att_masks'].sum() == data['att_masks'].size:
data['att_masks'] = None

data['labels'] = np.vstack(label_batch)
# generate mask
Expand All @@ -187,6 +190,8 @@ def __getitem__(self, index):
ix = index #self.split_ix[index]
if self.use_att:
att_feat = np.load(os.path.join(self.input_att_dir, str(self.info['images'][ix]['id']) + '.npz'))['feat']
# Reshape to K x C
att_feat = att_feat.reshape(-1, att_feat.shape[-1])
if self.norm_att_feat:
att_feat = att_feat / np.linalg.norm(att_feat, 2, 1, keepdims=True)
if self.use_box:
Expand Down
2 changes: 1 addition & 1 deletion train.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def train(opt):
start = time.time()

tmp = [data['fc_feats'], data['att_feats'], data['labels'], data['masks'], data['att_masks']]
tmp = [torch.from_numpy(_).cuda() for _ in tmp]
tmp = [_ if _ is None else torch.from_numpy(_).cuda() for _ in tmp]
fc_feats, att_feats, labels, masks, att_masks = tmp

optimizer.zero_grad()
Expand Down

0 comments on commit 9ebae0a

Please sign in to comment.