def test_if_returns_expected_value_for_multiclass(self, images, target, classes, result): images = images.unsqueeze(dim=CHANNEL_DIM).repeat(1, classes, 1) target = target.unsqueeze(dim=CHANNEL_DIM).repeat(1, classes, 1) result = [result for _ in range(classes)] assert np.all( np.isclose(F.precision(images, target), result, atol=1.e-3))
def __call__(self, prediction: torch.Tensor, target: torch.Tensor) -> torch.Tensor: """ Args: prediction: Network output. Dimensions - (Batch, Class, Depth, Height, Width) target: Target values. The classes should be ont-hot encoded. Dimensions - (Batch, Class, Depth, Height, Width) Returns: torch.Tensor: Precision score for each class averaged across the whole batch """ return F.precision(prediction, target, self.epsilon)
def test_if_returns_correct_number_of_classes(self, input_shape, result): images = torch.ones(*input_shape) target = torch.ones(*input_shape) assert len(F.precision(images, target)) == result
def test_if_returns_expected_values(self, images, target, result): assert np.isclose(F.precision(images.unsqueeze(dim=CHANNEL_DIM), target.unsqueeze(dim=CHANNEL_DIM)), result, atol=1.e-3)
def test_if_returns_0_for_worst_fit(self): images = torch.zeros(*BATCH_DIMS) target = torch.ones(*BATCH_DIMS) assert np.isclose(F.precision(images, target), 0, atol=1.e-4)
def test_if_returns_1_for_perfect_fit(self): images = torch.ones(*BATCH_DIMS) target = torch.ones(*BATCH_DIMS) assert np.isclose(F.precision(images, target), 1, atol=1.e-4)