Skip to content

Commit

Permalink
Fix for AnchorGenerator when device switch happen (#1745)
Browse files Browse the repository at this point in the history
* Fix AnchorGenerator if moving from one device to another

* Fixes for the test
  • Loading branch information
fmassa authored Jan 13, 2020
1 parent 7bfbc81 commit 66f2922
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,28 @@ def test_fasterrcnn_double(self):
self.assertTrue("scores" in out[0])
self.assertTrue("labels" in out[0])

@unittest.skipIf(not torch.cuda.is_available(), 'needs GPU')
def test_fasterrcnn_switch_devices(self):
model = models.detection.fasterrcnn_resnet50_fpn(num_classes=50, pretrained_backbone=False)
model.cuda()
model.eval()
input_shape = (3, 300, 300)
x = torch.rand(input_shape, device='cuda')
model_input = [x]
out = model(model_input)
self.assertIs(model_input[0], x)
self.assertEqual(len(out), 1)
self.assertTrue("boxes" in out[0])
self.assertTrue("scores" in out[0])
self.assertTrue("labels" in out[0])
# now switch to cpu and make sure it works
model.cpu()
x = x.cpu()
out_cpu = model([x])
self.assertTrue("boxes" in out_cpu[0])
self.assertTrue("scores" in out_cpu[0])
self.assertTrue("labels" in out_cpu[0])


for model_name in get_available_classification_models():
# for-loop bodies don't define scopes, so we have to save the variables
Expand Down
7 changes: 6 additions & 1 deletion torchvision/models/detection/rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ def generate_anchors(self, scales, aspect_ratios, dtype=torch.float32, device="c
def set_cell_anchors(self, dtype, device):
# type: (int, Device) -> None # noqa: F821
if self.cell_anchors is not None:
return
cell_anchors = self.cell_anchors
assert cell_anchors is not None
# suppose that all anchors have the same device
# which is a valid assumption in the current state of the codebase
if cell_anchors[0].device == device:
return

cell_anchors = [
self.generate_anchors(
Expand Down

0 comments on commit 66f2922

Please sign in to comment.