def test_model_script_detection(self): model = detr_resnet50(pretrained=False).eval() scripted_model = torch.jit.script(model) x = nested_tensor_from_tensor_list( [torch.rand(3, 200, 200), torch.rand(3, 200, 250)] ) out = model(x) out_script = scripted_model(x) self.assertTrue(out["pred_logits"].equal(out_script["pred_logits"])) self.assertTrue(out["pred_boxes"].equal(out_script["pred_boxes"]))
def test_model_onnx_detection(self): model = detr_resnet50(pretrained=False).eval() dummy_image = torch.ones(1, 3, 800, 800) * 0.3 model(dummy_image) # Test exported model on images of different size, or dummy input self.run_model( model, [(torch.rand(1, 3, 750, 800),)], input_names=["inputs"], output_names=["pred_logits", "pred_boxes"], tolerate_small_mismatch=True, )
def test_model_detection_different_inputs(self): model = detr_resnet50(pretrained=False).eval() # support NestedTensor x = nested_tensor_from_tensor_list( [torch.rand(3, 200, 200), torch.rand(3, 200, 250)] ) out = model(x) self.assertIn("pred_logits", out) # and 4d Tensor x = torch.rand(1, 3, 200, 200) out = model(x) self.assertIn("pred_logits", out) # and List[Tensor[C, H, W]] x = torch.rand(3, 200, 200) out = model([x]) self.assertIn("pred_logits", out)
def test_warpped_model_script_detection(self): class WrappedDETR(nn.Module): def __init__(self, model): super().__init__() self.model = model def forward(self, inputs: List[Tensor]): sample = nested_tensor_from_tensor_list(inputs) return self.model(sample) model = detr_resnet50(pretrained=False) wrapped_model = WrappedDETR(model) wrapped_model.eval() scripted_model = torch.jit.script(wrapped_model) x = [torch.rand(3, 200, 200), torch.rand(3, 200, 250)] out = wrapped_model(x) out_script = scripted_model(x) self.assertTrue(out["pred_logits"].equal(out_script["pred_logits"])) self.assertTrue(out["pred_boxes"].equal(out_script["pred_boxes"]))