Skip to content

Commit

Permalink
minor change to adapt to pytorch 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jianwei Yang committed Sep 13, 2018
1 parent e374942 commit 759afc2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/model/rpn/anchor_target_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def forward(self, input):

if cfg.TRAIN.RPN_POSITIVE_WEIGHT < 0:
num_examples = torch.sum(labels[i] >= 0)
positive_weights = 1.0 / num_examples
negative_weights = 1.0 / num_examples
positive_weights = 1.0 / num_examples.item()
negative_weights = 1.0 / num_examples.item()
else:
assert ((cfg.TRAIN.RPN_POSITIVE_WEIGHT > 0) &
(cfg.TRAIN.RPN_POSITIVE_WEIGHT < 1))
Expand Down
13 changes: 6 additions & 7 deletions lib/model/rpn/proposal_target_layer_cascade.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ def _sample_rois_pytorch(self, all_rois, gt_boxes, fg_rois_per_image, rois_per_i
offset = torch.arange(0, batch_size)*gt_boxes.size(1)
offset = offset.view(-1, 1).type_as(gt_assignment) + gt_assignment

labels = gt_boxes[:,:,4].contiguous().view(-1).index(offset.view(-1))\
.view(batch_size, -1)
labels = gt_boxes[:,:,4].contiguous().view(-1).index((offset.view(-1), )).view(batch_size, -1)

labels_batch = labels.new(batch_size, rois_per_image).zero_()
rois_batch = all_rois.new(batch_size, rois_per_image, 5).zero_()
Expand All @@ -151,8 +150,8 @@ def _sample_rois_pytorch(self, all_rois, gt_boxes, fg_rois_per_image, rois_per_i
if fg_num_rois > 0 and bg_num_rois > 0:
# sampling fg
fg_rois_per_this_image = min(fg_rois_per_image, fg_num_rois)
# torch.randperm seems has a bug on multi-gpu setting that cause the segfault.

# torch.randperm seems has a bug on multi-gpu setting that cause the segfault.
# See https://github.com/pytorch/pytorch/issues/1868 for more details.
# use numpy instead.
#rand_num = torch.randperm(fg_num_rois).long().cuda()
Expand All @@ -162,8 +161,8 @@ def _sample_rois_pytorch(self, all_rois, gt_boxes, fg_rois_per_image, rois_per_i
# sampling bg
bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image

# Seems torch.rand has a bug, it will generate very large number and make an error.
# We use numpy rand instead.
# Seems torch.rand has a bug, it will generate very large number and make an error.
# We use numpy rand instead.
#rand_num = (torch.rand(bg_rois_per_this_image) * bg_num_rois).long().cuda()
rand_num = np.floor(np.random.rand(bg_rois_per_this_image) * bg_num_rois)
rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()
Expand All @@ -188,7 +187,7 @@ def _sample_rois_pytorch(self, all_rois, gt_boxes, fg_rois_per_image, rois_per_i
fg_rois_per_this_image = 0
else:
raise ValueError("bg_num_rois = 0 and fg_num_rois = 0, this should not happen!")

# The indices that we're selecting (both fg and bg)
keep_inds = torch.cat([fg_inds, bg_inds], 0)

Expand Down

0 comments on commit 759afc2

Please sign in to comment.