def test_deg2rad(self): # generate input data x_deg = 180. * torch.rand(2, 3, 4) # convert radians/degrees x_rad = tgm.deg2rad(x_deg) x_rad_to_deg = tgm.rad2deg(x_rad) # compute error error = utils.compute_mse(x_deg, x_rad_to_deg) self.assertAlmostEqual(error.item(), 0.0, places=4) # functional self.assertTrue(torch.allclose(x_rad, tgm.DegToRad()(x_deg)))
def batch_polygonToRotRectangle(bbox): """ :param bbox: The polygon stored in format [x1, y1, x2, y2, x3, y3, x4, y4] :return: Rotated Rectangle in format [cx, cy, w, h, theta] """ bbox = torch.stack([bbox[:, 0::2], bbox[:, 1::2]], dim=1) angle = torch.atan2(-(bbox[:, 0, 1] - bbox[:, 0, 0]), bbox[:, 1, 1] - bbox[:, 1, 0]) center = torch.zeros(bbox.size(0), 2, 1, dtype=bbox.dtype, device=bbox.device) for i in range(4): center[:, 0, 0] += bbox[:, 0, i] center[:, 1, 0] += bbox[:, 1, i] center = center / 4.0 R = torch.stack([ torch.cos(angle), -torch.sin(angle), torch.sin(angle), torch.cos(angle) ], dim=1) R = R.reshape(-1, 2, 2) normalized = torch.matmul(R.transpose(2, 1), bbox - center) if bbox.size(0) == 0: return torch.empty((0, 5), dtype=bbox.dtype, device=bbox.device) xmin = torch.min(normalized[:, 0, :], dim=1)[0] xmax = torch.max(normalized[:, 0, :], dim=1)[0] ymin = torch.min(normalized[:, 1, :], dim=1)[0] ymax = torch.max(normalized[:, 1, :], dim=1)[0] w = xmax - xmin h = ymax - ymin center = center.squeeze(-1) center_x = center[:, 0] center_y = center[:, 1] new_box = torch.stack([center_x, center_y, w, h, -tgm.rad2deg(angle)], dim=1) return new_box
def test_deg2rad(batch_shape, device_type): # generate input data x_deg = 180. * torch.rand(batch_shape) x_deg = x_deg.to(torch.device(device_type)) # convert radians/degrees x_rad = tgm.deg2rad(x_deg) x_rad_to_deg = tgm.rad2deg(x_rad) # compute error error = utils.compute_mse(x_deg, x_rad_to_deg) assert pytest.approx(error.item(), 0.0) # functional assert torch.allclose(x_rad, tgm.DegToRad()(x_deg)) assert gradcheck(tgm.deg2rad, (utils.tensor_to_gradcheck_var(x_deg), ), raise_exception=True)
def test_rad2deg(batch_shape, device_type): # generate input data x_rad = tgm.pi * torch.rand(batch_shape) x_rad = x_rad.to(torch.device(device_type)) # convert radians/degrees x_deg = tgm.rad2deg(x_rad) x_deg_to_rad = tgm.deg2rad(x_deg) # compute error error = utils.compute_mse(x_rad, x_deg_to_rad) assert pytest.approx(error.item(), 0.0) # functional assert torch.allclose(x_deg, tgm.RadToDeg()(x_rad)) # evaluate function gradient assert gradcheck(tgm.rad2deg, (utils.tensor_to_gradcheck_var(x_rad), ), raise_exception=True)