Args: input, other (Instances):
(input, other, rtol=1e-5, msg="")
| 61 | |
| 62 | |
| 63 | def assert_instances_allclose(input, other, rtol=1e-5, msg=""): |
| 64 | """ |
| 65 | Args: |
| 66 | input, other (Instances): |
| 67 | """ |
| 68 | if not msg: |
| 69 | msg = "Two Instances are different! " |
| 70 | else: |
| 71 | msg = msg.rstrip() + " " |
| 72 | assert input.image_size == other.image_size, ( |
| 73 | msg + f"image_size is {input.image_size} vs. {other.image_size}!" |
| 74 | ) |
| 75 | fields = sorted(input.get_fields().keys()) |
| 76 | fields_other = sorted(other.get_fields().keys()) |
| 77 | assert fields == fields_other, msg + f"Fields are {fields} vs {fields_other}!" |
| 78 | |
| 79 | for f in fields: |
| 80 | val1, val2 = input.get(f), other.get(f) |
| 81 | if isinstance(val1, Boxes): |
| 82 | # boxes in the range of O(100) and can have a larger tolerance |
| 83 | assert torch.allclose(val1.tensor, val2.tensor, atol=100 * rtol), ( |
| 84 | msg + f"Field {f} differs too much!" |
| 85 | ) |
| 86 | elif isinstance(val1, torch.Tensor): |
| 87 | if val1.dtype.is_floating_point: |
| 88 | mag = torch.abs(val1).max().cpu().item() |
| 89 | assert torch.allclose(val1, val2, atol=mag * rtol), ( |
| 90 | msg + f"Field {f} differs too much!" |
| 91 | ) |
| 92 | else: |
| 93 | assert torch.equal(val1, val2), msg + f"Field {f} is different!" |
| 94 | else: |
| 95 | raise ValueError(f"Don't know how to compare type {type(val1)}") |
nothing calls this directly
no test coverage detected