Пример #1
0
    def test_rectangular(self):
        t = torch.Tensor([[[
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.1],
            [0.0, 0.0, 0.0, 0.0, 0.1, 0.8],
        ]]])
        coords = torch.Tensor([[[1, 1]]])

        actual = average_loss(kl_reg_losses(t, coords, 2.0))

        self.assertEqual(1.2646753877545842, actual.item())
Пример #2
0
def test_kl_rectangular():
    t = torch.Tensor([[[
        [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, 0.0, 0.0, 0.1],
        [0.0, 0.0, 0.0, 0.0, 0.1, 0.8],
    ]]])
    coords = torch.Tensor([[[1, 1]]])

    actual = average_loss(kl_reg_losses(t, coords, 2.0))

    assert actual.item() == 1.2646753877545842
Пример #3
0
    def test_rectangular(self):
        t = torch.Tensor([[[
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.1],
            [0.0, 0.0, 0.0, 0.0, 0.1, 0.8],
        ]]])
        coords = torch.Tensor([[[1, 1]]])

        actual = average_loss(kl_reg_losses(Variable(t), Variable(coords),
                                            2.0))

        self.assertEqual(1.2646753877545842, actual.data[0])
def test_kl_rectangular():
    t = torch.tensor([[
        [
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.1],
            [0.0, 0.0, 0.0, 0.0, 0.1, 0.8],
        ]
    ]])
    coords = torch.tensor([[[1.0, 1.0]]])

    actual = average_loss(kl_reg_losses(t, coords, 2.0))

    assert actual.item() == pytest.approx(1.26467538775)
Пример #5
0
    def _calculate_reg_loss(self, target_var, reg, hm_var, hm_sigma):
        # Convert sigma (aka standard deviation) from pixels to normalized units
        sigma = (2.0 * hm_sigma / hm_var.size(-1))

        # Apply a regularisation term relating to the shape of the heatmap.
        if reg == 'var':
            reg_loss = dsntnn.variance_reg_losses(hm_var, sigma, )
        elif reg == 'kl':
            reg_loss = dsntnn.kl_reg_losses(hm_var, target_var, sigma)
        elif reg == 'js':
            reg_loss = dsntnn.js_reg_losses(hm_var, target_var, sigma)
        # elif reg == 'mse':

            # reg_loss = dsntnn.mse_reg_losses(hm_var, target_var, sigma, mask_var)
        else:
            reg_loss = 0

        return reg_loss
Пример #6
0
    def test_mask(self):
        t = torch.Tensor([[[
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.1],
            [0.0, 0.0, 0.1, 0.8],
        ],
                           [
                               [0.8, 0.1, 0.0, 0.0],
                               [0.1, 0.0, 0.0, 0.0],
                               [0.0, 0.0, 0.0, 0.0],
                               [0.0, 0.0, 0.0, 0.0],
                           ]]])
        coords = torch.Tensor([[[1, 1], [0, 0]]])
        mask = torch.Tensor([[1, 0]])

        actual = average_loss(kl_reg_losses(t, coords, 2.0), mask)

        self.assertEqual(1.2228811717796824, actual.item())
Пример #7
0
def test_kl_mask():
    t = torch.Tensor([[[
        [0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, 0.1],
        [0.0, 0.0, 0.1, 0.8],
    ],
                       [
                           [0.8, 0.1, 0.0, 0.0],
                           [0.1, 0.0, 0.0, 0.0],
                           [0.0, 0.0, 0.0, 0.0],
                           [0.0, 0.0, 0.0, 0.0],
                       ]]])
    coords = torch.Tensor([[[1, 1], [0, 0]]])
    mask = torch.Tensor([[1, 0]])

    actual = average_loss(kl_reg_losses(t, coords, 2.0), mask)

    assert actual.item() == approx(1.2228811717796824)
Пример #8
0
    def test_mask(self):
        t = torch.Tensor([[[
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.1],
            [0.0, 0.0, 0.1, 0.8],
        ],
                           [
                               [0.8, 0.1, 0.0, 0.0],
                               [0.1, 0.0, 0.0, 0.0],
                               [0.0, 0.0, 0.0, 0.0],
                               [0.0, 0.0, 0.0, 0.0],
                           ]]])
        coords = torch.Tensor([[[1, 1], [0, 0]]])
        mask = torch.Tensor([[1, 0]])

        actual = average_loss(
            kl_reg_losses(Variable(t), Variable(coords), 2.0), Variable(mask))

        self.assertEqual(1.2228811717796824, actual.data[0])