def test_multi_scale_loss_gmsd_modes(x, y, device: str) -> None: for reduction in ['mean', 'sum', 'none']: MultiScaleGMSDLoss(reduction=reduction)(x.to(device), y.to(device)) for reduction in ['DEADBEEF', 'random']: with pytest.raises(ValueError): MultiScaleGMSDLoss(reduction=reduction)(x.to(device), y.to(device))
def test_multi_scale_loss_gmsd_modes(prediction: torch.Tensor, target: torch.Tensor, device: str) -> None: for reduction in ['mean', 'sum', 'none']: MultiScaleGMSDLoss(reduction=reduction)(prediction.to(device), target.to(device)) for reduction in ['DEADBEEF', 'random']: with pytest.raises(KeyError): MultiScaleGMSDLoss(reduction=reduction)(prediction.to(device), target.to(device))
def test_multi_scale_gmsd_loss_forward_backward(prediction: torch.Tensor, target: torch.Tensor, device: str) -> None: prediction.requires_grad_() loss_value = MultiScaleGMSDLoss(chromatic=True)(prediction.to(device), target.to(device)) loss_value.backward() assert torch.isfinite(prediction.grad).all(), LEAF_VARIABLE_ERROR_MESSAGE
def test_multi_scale_gmsd_loss_supports_different_data_ranges(x, y, device: str) -> None: x_255 = x * 255 y_255 = y * 255 loss = MultiScaleGMSDLoss() measure = loss(x.to(device), y.to(device)) loss_255 = MultiScaleGMSDLoss(data_range=255) measure_255 = loss_255(x_255.to(device), y_255.to(device)) diff = torch.abs(measure_255 - measure) assert diff <= 1e-4, f'Result for same tensor with different data_range should be the same, got {diff}'
def test_multi_scale_gmsd_loss_supports_different_data_ranges(prediction: torch.Tensor, target: torch.Tensor, device: str) -> None: prediction_255 = prediction * 255 target_255 = target * 255 loss = MultiScaleGMSDLoss() measure = loss(prediction.to(device), target.to(device)) loss_255 = MultiScaleGMSDLoss(data_range=255) measure_255 = loss_255(prediction_255.to(device), target_255.to(device)) diff = torch.abs(measure_255 - measure) assert diff <= 1e-4, f'Result for same tensor with different data_range should be the same, got {diff}'
def test_multi_scale_gmsd_loss_raise_exception_for_small_images( device: str) -> None: target = torch.ones(3, 1, 32, 32) prediction = torch.zeros(3, 1, 32, 32) loss = MultiScaleGMSDLoss(scale_weights=[3., 4., 2., 1., 1.]) with pytest.raises(ValueError): loss(prediction.to(device), target.to(device))
def test_multi_scale_gmsd_loss_fails_for_greyscale_tensors_chromatic_flag( device: str) -> None: loss = MultiScaleGMSDLoss(chromatic=True) target = torch.ones(2, 1, 96, 96) prediction = torch.zeros(2, 1, 96, 96) with pytest.raises(AssertionError): loss(prediction.to(device), target.to(device))
def test_multi_scale_gmsd_loss_zero_for_equal_tensors(prediction: torch.Tensor, device: str) -> None: loss = MultiScaleGMSDLoss() target = prediction.clone() measure = loss(prediction.to(device), target.to(device)) assert measure.abs( ) <= 1e-6, f'MultiScaleGMSD for equal tensors must be 0, got {measure}'
def test_multi_scale_gmsd_loss_raise_exception_for_small_images( device: str) -> None: y = torch.ones(3, 1, 32, 32) x = torch.zeros(3, 1, 32, 32) loss = MultiScaleGMSDLoss(scale_weights=torch.tensor([3., 4., 2., 1., 2.])) with pytest.raises(ValueError): loss(x.to(device), y.to(device))
def test_multi_scale_gmsd_loss_supports_custom_weights(prediction: torch.Tensor, target: torch.Tensor, device: str) -> None: loss = MultiScaleGMSDLoss(scale_weights=[3., 4., 2., 1., 2.]) loss(prediction.to(device), target.to(device)) loss = MultiScaleGMSDLoss(scale_weights=torch.tensor([3., 4., 2., 1., 2.])) loss(prediction.to(device), target.to(device))
def test_multi_scale_gmsd_loss_supports_greyscale_tensors(device: str) -> None: loss = MultiScaleGMSDLoss() target = torch.ones(2, 1, 96, 96) prediction = torch.zeros(2, 1, 96, 96) loss(prediction.to(device), target.to(device))
def test_multi_scale_gmsd_loss_supports_custom_weights(x, y, device: str) -> None: loss = MultiScaleGMSDLoss(scale_weights=torch.tensor([3., 4., 2., 1., 2.])) loss(x.to(device), y.to(device))
def test_multi_scale_gmsd_loss_supports_greyscale_tensors(device: str) -> None: loss = MultiScaleGMSDLoss() y = torch.ones(2, 1, 96, 96) x = torch.zeros(2, 1, 96, 96) loss(x.to(device), y.to(device))
def test_multi_scale_gmsd_loss_zero_for_equal_tensors(x, device: str) -> None: loss = MultiScaleGMSDLoss() y = x.clone() measure = loss(x.to(device), y.to(device)) assert measure.abs() <= 1e-6, f'MultiScaleGMSD for equal tensors must be 0, got {measure}'
def test_multi_scale_gmsd_loss_forward_backward(x, y, device: str) -> None: x.requires_grad_() loss_value = MultiScaleGMSDLoss(chromatic=True)(x.to(device), y.to(device)) loss_value.backward() assert torch.isfinite(x.grad).all(), LEAF_VARIABLE_ERROR_MESSAGE