Example #1
0
def test_inception_extractor_wrong_inputs():

    with pytest.raises(ValueError,
                       match=r"Inputs should be a tensor of dim 4"):
        InceptionModel(return_features=True)(torch.rand(2))

    with pytest.raises(ValueError,
                       match=r"Inputs should be a tensor with 3 channels"):
        InceptionModel(return_features=True)(torch.rand(2, 2, 2, 0))
Example #2
0
    def __init__(
            self,
            num_features: Optional[int] = None,
            feature_extractor: Optional[torch.nn.Module] = None,
            output_transform: Callable = lambda x: x,
            device: Union[str, torch.device] = torch.device("cpu"),
    ) -> None:

        try:
            import numpy as np  # noqa: F401
        except ImportError:
            raise RuntimeError("This module requires numpy to be installed.")

        try:
            import scipy  # noqa: F401
        except ImportError:
            raise RuntimeError("This module requires scipy to be installed.")

        if num_features is None and feature_extractor is None:
            num_features = 1000
            feature_extractor = InceptionModel(return_features=False,
                                               device=device)

        self._eps = 1e-6

        super(FID, self).__init__(
            num_features=num_features,
            feature_extractor=feature_extractor,
            output_transform=output_transform,
            device=device,
        )
Example #3
0
def test_device_mismatch_cuda():
    images = torch.rand(10, 3, 299, 299)
    result = InceptionModel(return_features=False, device="cuda")(images)
    assert result.is_cuda
    assert result.shape == torch.Size([10, 1000])
    result = InceptionModel(return_features=False)(images.cuda())
    assert not result.is_cuda
    assert result.shape == torch.Size([10, 1000])

    images = torch.rand(10, 5)
    result = DummyInceptionMetric(num_features=5,
                                  device="cuda")._extract_features(images)
    assert result.is_cuda
    assert result.shape == torch.Size([10, 5])
    result = DummyInceptionMetric(num_features=5)._extract_features(
        images.cuda())
    assert not result.is_cuda
    assert result.shape == torch.Size([10, 5])
Example #4
0
    def __init__(
        self,
        num_features: Optional[int] = None,
        feature_extractor: Optional[torch.nn.Module] = None,
        output_transform: Callable = lambda x: x,
        device: Union[str, torch.device] = torch.device("cpu"),
    ) -> None:

        if num_features is None and feature_extractor is None:
            num_features = 1000
            feature_extractor = InceptionModel(return_features=False, device=device)

        self._eps = 1e-16

        super(InceptionScore, self).__init__(
            num_features=num_features,
            feature_extractor=feature_extractor,
            output_transform=output_transform,
            device=device,
        )
Example #5
0
def test_inception_model_probability():
    x = torch.rand(2, 3, 299, 299)
    y = InceptionModel(return_features=False)(x)
    assert pytest.approx(torch.sum(y[0]).item()) == 1.0
    assert pytest.approx(torch.sum(y[1]).item()) == 1.0
    assert torch.all(0 <= y)
Example #6
0
def test_no_torchvision(mock_no_torchvision):
    with pytest.raises(
            RuntimeError,
            match=r"This module requires torchvision to be installed."):
        InceptionModel(return_features=True)