test_all.py 9.19 KB
Newer Older
facebook-github-bot's avatar
facebook-github-bot committed
1
2
3
4
5
6
7
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import io
import unittest
from typing import List

Yanghan Wang's avatar
Yanghan Wang committed
8
9
import torch
from detr.hub import detr_resnet50, detr_resnet50_panoptic
facebook-github-bot's avatar
facebook-github-bot committed
10
from detr.models.backbone import Backbone
Yanghan Wang's avatar
Yanghan Wang committed
11
12
13
14
15
from detr.models.matcher import HungarianMatcher
from detr.models.position_encoding import (
    PositionEmbeddingSine,
    PositionEmbeddingLearned,
)
facebook-github-bot's avatar
facebook-github-bot committed
16
17
from detr.util import box_ops
from detr.util.misc import nested_tensor_from_tensor_list
Yanghan Wang's avatar
Yanghan Wang committed
18
from torch import nn, Tensor
facebook-github-bot's avatar
facebook-github-bot committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# onnxruntime requires python 3.5 or above
try:
    import onnxruntime
except ImportError:
    onnxruntime = None


class Tester(unittest.TestCase):
    def test_box_cxcywh_to_xyxy(self):
        t = torch.rand(10, 4)
        r = box_ops.box_xyxy_to_cxcywh(box_ops.box_cxcywh_to_xyxy(t))
        self.assertLess((t - r).abs().max(), 1e-5)

    @staticmethod
    def indices_torch2python(indices):
        return [(i.tolist(), j.tolist()) for i, j in indices]

    def test_hungarian(self):
        n_queries, n_targets, n_classes = 100, 15, 91
        logits = torch.rand(1, n_queries, n_classes + 1)
        boxes = torch.rand(1, n_queries, 4)
        tgt_labels = torch.randint(high=n_classes, size=(n_targets,))
        tgt_boxes = torch.rand(n_targets, 4)
        matcher = HungarianMatcher()
Yanghan Wang's avatar
Yanghan Wang committed
44
45
46
47
48
49
50
51
52
        targets = [{"labels": tgt_labels, "boxes": tgt_boxes}]
        indices_single = matcher({"pred_logits": logits, "pred_boxes": boxes}, targets)
        indices_batched = matcher(
            {
                "pred_logits": logits.repeat(2, 1, 1),
                "pred_boxes": boxes.repeat(2, 1, 1),
            },
            targets * 2,
        )
facebook-github-bot's avatar
facebook-github-bot committed
53
54
        self.assertEqual(len(indices_single[0][0]), n_targets)
        self.assertEqual(len(indices_single[0][1]), n_targets)
Yanghan Wang's avatar
Yanghan Wang committed
55
56
57
58
59
60
61
62
        self.assertEqual(
            self.indices_torch2python(indices_single),
            self.indices_torch2python([indices_batched[0]]),
        )
        self.assertEqual(
            self.indices_torch2python(indices_single),
            self.indices_torch2python([indices_batched[1]]),
        )
facebook-github-bot's avatar
facebook-github-bot committed
63
64
65
66

        # test with empty targets
        tgt_labels_empty = torch.randint(high=n_classes, size=(0,))
        tgt_boxes_empty = torch.rand(0, 4)
Yanghan Wang's avatar
Yanghan Wang committed
67
68
69
70
71
72
73
74
        targets_empty = [{"labels": tgt_labels_empty, "boxes": tgt_boxes_empty}]
        indices = matcher(
            {
                "pred_logits": logits.repeat(2, 1, 1),
                "pred_boxes": boxes.repeat(2, 1, 1),
            },
            targets + targets_empty,
        )
facebook-github-bot's avatar
facebook-github-bot committed
75
        self.assertEqual(len(indices[1][0]), 0)
Yanghan Wang's avatar
Yanghan Wang committed
76
77
78
79
80
81
82
        indices = matcher(
            {
                "pred_logits": logits.repeat(2, 1, 1),
                "pred_boxes": boxes.repeat(2, 1, 1),
            },
            targets_empty * 2,
        )
facebook-github-bot's avatar
facebook-github-bot committed
83
84
85
86
87
88
89
        self.assertEqual(len(indices[0][0]), 0)

    def test_position_encoding_script(self):
        m1, m2 = PositionEmbeddingSine(), PositionEmbeddingLearned()
        mm1, mm2 = torch.jit.script(m1), torch.jit.script(m2)  # noqa

    def test_backbone_script(self):
Yanghan Wang's avatar
Yanghan Wang committed
90
        backbone = Backbone("resnet50", True, False, False)
facebook-github-bot's avatar
facebook-github-bot committed
91
92
93
94
95
        torch.jit.script(backbone)  # noqa

    def test_model_script_detection(self):
        model = detr_resnet50(pretrained=False).eval()
        scripted_model = torch.jit.script(model)
Yanghan Wang's avatar
Yanghan Wang committed
96
97
98
        x = nested_tensor_from_tensor_list(
            [torch.rand(3, 200, 200), torch.rand(3, 200, 250)]
        )
facebook-github-bot's avatar
facebook-github-bot committed
99
100
101
102
103
104
105
106
        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_script_panoptic(self):
        model = detr_resnet50_panoptic(pretrained=False).eval()
        scripted_model = torch.jit.script(model)
Yanghan Wang's avatar
Yanghan Wang committed
107
108
109
        x = nested_tensor_from_tensor_list(
            [torch.rand(3, 200, 200), torch.rand(3, 200, 250)]
        )
facebook-github-bot's avatar
facebook-github-bot committed
110
111
112
113
114
115
116
117
118
        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"]))
        self.assertTrue(out["pred_masks"].equal(out_script["pred_masks"]))

    def test_model_detection_different_inputs(self):
        model = detr_resnet50(pretrained=False).eval()
        # support NestedTensor
Yanghan Wang's avatar
Yanghan Wang committed
119
120
121
        x = nested_tensor_from_tensor_list(
            [torch.rand(3, 200, 200), torch.rand(3, 200, 250)]
        )
facebook-github-bot's avatar
facebook-github-bot committed
122
        out = model(x)
Yanghan Wang's avatar
Yanghan Wang committed
123
        self.assertIn("pred_logits", out)
facebook-github-bot's avatar
facebook-github-bot committed
124
125
126
        # and 4d Tensor
        x = torch.rand(1, 3, 200, 200)
        out = model(x)
Yanghan Wang's avatar
Yanghan Wang committed
127
        self.assertIn("pred_logits", out)
facebook-github-bot's avatar
facebook-github-bot committed
128
129
130
        # and List[Tensor[C, H, W]]
        x = torch.rand(3, 200, 200)
        out = model([x])
Yanghan Wang's avatar
Yanghan Wang committed
131
        self.assertIn("pred_logits", out)
facebook-github-bot's avatar
facebook-github-bot committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

    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"]))


Yanghan Wang's avatar
Yanghan Wang committed
154
@unittest.skipIf(onnxruntime is None, "ONNX Runtime unavailable")
facebook-github-bot's avatar
facebook-github-bot committed
155
156
157
158
159
class ONNXExporterTester(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        torch.manual_seed(123)

Yanghan Wang's avatar
Yanghan Wang committed
160
161
162
163
164
165
166
167
168
169
    def run_model(
        self,
        model,
        inputs_list,
        tolerate_small_mismatch=False,
        do_constant_folding=True,
        dynamic_axes=None,
        output_names=None,
        input_names=None,
    ):
facebook-github-bot's avatar
facebook-github-bot committed
170
171
172
173
        model.eval()

        onnx_io = io.BytesIO()
        # export to onnx with the first input
Yanghan Wang's avatar
Yanghan Wang committed
174
175
176
177
178
179
180
181
182
183
        torch.onnx.export(
            model,
            inputs_list[0],
            onnx_io,
            do_constant_folding=do_constant_folding,
            opset_version=12,
            dynamic_axes=dynamic_axes,
            input_names=input_names,
            output_names=output_names,
        )
facebook-github-bot's avatar
facebook-github-bot committed
184
185
186
        # validate the exported model with onnx runtime
        for test_inputs in inputs_list:
            with torch.no_grad():
Yanghan Wang's avatar
Yanghan Wang committed
187
188
189
                if isinstance(test_inputs, torch.Tensor) or isinstance(
                    test_inputs, list
                ):
facebook-github-bot's avatar
facebook-github-bot committed
190
191
192
193
                    test_inputs = (nested_tensor_from_tensor_list(test_inputs),)
                test_ouputs = model(*test_inputs)
                if isinstance(test_ouputs, torch.Tensor):
                    test_ouputs = (test_ouputs,)
Yanghan Wang's avatar
Yanghan Wang committed
194
195
196
            self.ort_validate(
                onnx_io, test_inputs, test_ouputs, tolerate_small_mismatch
            )
facebook-github-bot's avatar
facebook-github-bot committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

    def ort_validate(self, onnx_io, inputs, outputs, tolerate_small_mismatch=False):

        inputs, _ = torch.jit._flatten(inputs)
        outputs, _ = torch.jit._flatten(outputs)

        def to_numpy(tensor):
            if tensor.requires_grad:
                return tensor.detach().cpu().numpy()
            else:
                return tensor.cpu().numpy()

        inputs = list(map(to_numpy, inputs))
        outputs = list(map(to_numpy, outputs))

        ort_session = onnxruntime.InferenceSession(onnx_io.getvalue())
        # compute onnxruntime output prediction
Yanghan Wang's avatar
Yanghan Wang committed
214
215
216
        ort_inputs = dict(
            (ort_session.get_inputs()[i].name, inpt) for i, inpt in enumerate(inputs)
        )  # noqa: C402
facebook-github-bot's avatar
facebook-github-bot committed
217
218
219
        ort_outs = ort_session.run(None, ort_inputs)
        for i in range(0, len(outputs)):
            try:
Yanghan Wang's avatar
Yanghan Wang committed
220
221
222
                torch.testing.assert_allclose(
                    outputs[i], ort_outs[i], rtol=1e-03, atol=1e-05
                )
facebook-github-bot's avatar
facebook-github-bot committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
            except AssertionError as error:
                if tolerate_small_mismatch:
                    self.assertIn("(0.00%)", str(error), str(error))
                else:
                    raise

    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,
        )

    @unittest.skip("CI doesn't have enough memory")
    def test_model_onnx_detection_panoptic(self):
        model = detr_resnet50_panoptic(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", "pred_masks"],
            tolerate_small_mismatch=True,
        )


Yanghan Wang's avatar
Yanghan Wang committed
259
if __name__ == "__main__":
facebook-github-bot's avatar
facebook-github-bot committed
260
    unittest.main()