def test_clone(self, feature_type):
        input = make_feature(feature_type)
        output = input.clone()

        assert type(output) is feature_type
        assert_close(output, input)
        assert output._meta_data == input._meta_data
Beispiel #2
0
def assert_packed_grad_close(actual: PackedSequence,
                             expected: PackedSequence,
                             inputs: Union[Tensor, List[Tensor], Tuple[Tensor,
                                                                       ...]],
                             check_device: bool = True,
                             check_dtype: bool = True,
                             check_stride: bool = True) -> None:
    kwargs = dict(check_device=check_device,
                  check_dtype=check_dtype,
                  check_stride=check_stride)

    grad = torch.rand_like(actual.data)

    actual_grads = torch.autograd.grad(
        actual.data,
        inputs,
        grad,
        create_graph=False,
    )

    expected_grads = torch.autograd.grad(
        expected.data,
        inputs,
        grad,
        create_graph=False,
    )

    for actual_grad, expected_grad in zip(actual_grads, expected_grads):
        assert_close(actual_grad, expected_grad, **kwargs)
    def test_torch_function(self, feature_type):
        input = make_feature(feature_type)
        # This can be any Tensor operation besides clone
        output = input + 1

        assert type(output) is torch.Tensor
        assert_close(output, input + 1)
    def test_bounding_box(self):
        def resize(input: features.BoundingBox,
                   size: torch.Tensor) -> features.BoundingBox:
            old_height, old_width = input.image_size
            new_height, new_width = size

            height_scale = new_height / old_height
            width_scale = new_width / old_width

            old_x1, old_y1, old_x2, old_y2 = input.convert("xyxy").to_parts()

            new_x1 = old_x1 * width_scale
            new_y1 = old_y1 * height_scale

            new_x2 = old_x2 * width_scale
            new_y2 = old_y2 * height_scale

            return features.BoundingBox.from_parts(new_x1,
                                                   new_y1,
                                                   new_x2,
                                                   new_y2,
                                                   like=input,
                                                   format="xyxy",
                                                   image_size=tuple(
                                                       size.tolist()))

        def horizontal_flip(
                input: features.BoundingBox) -> features.BoundingBox:
            x, y, w, h = input.convert("xywh").to_parts()
            x = input.image_size[1] - (x + w)
            return features.BoundingBox.from_parts(x,
                                                   y,
                                                   w,
                                                   h,
                                                   like=input,
                                                   format="xywh")

        def compose(input: features.BoundingBox,
                    size: torch.Tensor) -> features.BoundingBox:
            return horizontal_flip(resize(input, size)).convert("xyxy")

        image_size = (8, 6)
        input = features.BoundingBox([2, 4, 2, 4],
                                     format="cxcywh",
                                     image_size=image_size)
        size = torch.tensor((4, 12))
        expected = features.BoundingBox([6, 1, 10, 3],
                                        format="xyxy",
                                        image_size=image_size)

        actual_eager = compose(input, size)
        assert_close(actual_eager, expected)

        sample_inputs = (features.BoundingBox(torch.zeros((4, )),
                                              image_size=(10, 10)),
                         torch.tensor((20, 5)))
        actual_jit = torch.jit.trace(compose, sample_inputs,
                                     check_trace=False)(input, size)
        assert_close(actual_jit, expected)
    def test_serialization(self, tmpdir, feature):
        file = tmpdir / "test_serialization.pt"

        torch.save(feature, str(file))
        loaded_feature = torch.load(str(file))

        assert isinstance(loaded_feature, type(feature))
        assert_close(loaded_feature, feature)
        assert loaded_feature._meta_data == feature._meta_data
Beispiel #6
0
    def test_lukasz_marcin_approach_backward_pass(self):
        img = torch.randn(size=(2, 3, 10, 10))
        r1 = cond_resnet18(norm_layer=None)
        r2 = cond_resnet18(norm_layer=None)
        r2.load_state_dict(r1.state_dict())

        assert_equal(r1.fc.weight, r2.fc.weight)

        r1.eval()
        r2.eval()

        ks = list(range(1, 64, 6))
        loss_fn = CrossEntropyLoss()
        y_true = torch.tensor([1, 2])

        # marcin approach
        loss_1 = 0

        for k in ks:
            y_pred = r1(img, full_k=k)
            l = loss_fn(y_pred, y_true)
            loss_1 += l

        # lukasz approach
        loss_2 = 0
        _, intermediate_full = r2(img, return_intermediate=True)
        emb_full = intermediate_full["embedding"]

        for k in ks:
            mask = torch.ones_like(emb_full)
            k_out = k * 8
            mask[torch.arange(len(mask)), k_out:] = 0
            emb_mask = emb_full * mask
            y_pred = r2.fc(emb_mask)
            l = loss_fn(y_pred, y_true)
            loss_2 += l

        assert_equal(loss_1, loss_2)
        loss_1.backward()
        loss_2.backward()

        for ((n1, p1), (n2, p2)) in zip(r1.named_parameters(),
                                        r2.named_parameters()):
            self.assertEqual(n1, n2)
            assert_equal(p1, p2, msg=n1)

            if p1.grad is None:
                self.assertIsNone(p2.grad)
            else:
                assert_close(p1.grad, p2.grad, msg=n1)
Beispiel #7
0
def assert_packed_sequence_close(actual: PackedSequence,
                                 expected: PackedSequence,
                                 check_device: bool = True,
                                 check_dtype: bool = True,
                                 check_stride: bool = True) -> None:
    kwargs = dict(check_device=check_device,
                  check_dtype=check_dtype,
                  check_stride=check_stride)

    assert_close(actual.data, expected.data, **kwargs)
    assert_close(actual.batch_sizes, expected.batch_sizes, **kwargs)

    if actual.sorted_indices is None:
        assert expected.sorted_indices is None
    else:
        assert_close(actual.sorted_indices, expected.sorted_indices, **kwargs)

    if actual.unsorted_indices is None:
        assert expected.unsorted_indices is None
    else:
        assert_close(actual.unsorted_indices, expected.unsorted_indices,
                     **kwargs)
 def test_cycle_consistency(self, format, intermediate_format):
     input = make_bounding_box(format=format)
     output = input.convert(intermediate_format).convert(format)
     assert_close(input, output)