Пример #1
0
    def test_not_available(self, mock_execution_with):
        # hopefully no one ever has this available when running this test
        device = "mkldnn"
        mock_execution_with(device=device)

        with exits(should_succeed=False, check_err=device):
            main()
Пример #2
0
def test_help_smoke(option, mock_execution_with):
    mock_execution_with(option)

    def check_out(out):
        assert out

    with exits(check_out=check_out):
        main()
Пример #3
0
def test_version(option, mock_execution_with):
    mock_execution_with(option)

    def check_out(out):
        assert out == pystiche.__version__

    with exits(check_out=check_out):
        main()
Пример #4
0
def test_num_steps(mock_execution_with, option):
    num_steps = 42
    mock = mock_execution_with(f"{option}={num_steps}")

    with exits():
        cli.main()

    _, call_kwargs = mock.call_args

    assert call_kwargs["num_steps"] == num_steps
Пример #5
0
    def test_perceptual_loss(self, mock_execution_with):
        content_loss = "FeatureReconstruction"
        style_loss = "Gram"

        mock_execution_with(
            "--verbose", f"--content-loss={content_loss}", f"--style-loss={style_loss}",
        )

        with exits(check_out=("PerceptualLoss", content_loss, style_loss)):
            main()
Пример #6
0
    def test_smoke(self, mock_execution_with, option):
        # If the output file is not specified,
        # we would get output to STDOUT regardless of -v / --verbose
        mock_execution_with(option, "--output-image=foo.jpg")

        def check_out(out):
            assert out

        with exits(check_out=check_out):
            main()
Пример #7
0
def test_smoke(mock_image_optimization, mock_write_image, set_argv):
    optim_mock = mock_image_optimization()
    io_mock = mock_write_image()
    set_argv()

    with exits():
        main()

    optim_mock.assert_called_once()
    io_mock.assert_called_once()
Пример #8
0
    def test_main(self, mock_execution_with, option, loss_name):
        # We also set the layer here to avoid creating a loss.MultiLayerEncoderLoss
        mock = mock_execution_with(
            f"--{option}-loss={loss_name}", f"--{option}-layer=conv1_1"
        )

        with exits():
            main()

        (_, perceptual_loss), _ = mock.call_args

        assert isinstance(
            getattr(perceptual_loss, f"{option}_loss"),
            getattr(loss, f"{loss_name}Loss"),
        )
Пример #9
0
def test_output_image(mock_image_optimization, mock_write_image, set_argv, option):
    expected_file = "/path/to/output/image"
    expected_image = object()

    mock_image_optimization(return_value=expected_image)
    mock = mock_write_image()
    set_argv(f"{option}={expected_file}")

    with exits():
        cli.main()

    (actual_image, actual_file), _ = mock.call_args

    assert actual_image is expected_image
    assert actual_file == expected_file
Пример #10
0
    def test_file_starting_point(self, mock_execution_with):
        image = demo.images()["bird2"]
        expected = image.read()
        file = pathlib.Path(pystiche.home()) / image.file
        mock = self._mock_execution(
            mock_execution_with, option="starting_point", value=str(file)
        )

        with exits(), pytest.warns(UserWarning):
            main()

        (input_image, perceptual_loss), _ = mock.call_args

        ptu.assert_allclose(
            input_image,
            resize(expected, list(extract_image_size(perceptual_loss.content_image))),
        )
Пример #11
0
    def test_main(self, mock_execution_with, option, layer):
        layers = layer.split(",")
        mock = mock_execution_with(f"--{option}-layer={layer}")

        with exits():
            main()

        (_, perceptual_loss), _ = mock.call_args

        partial_loss = getattr(perceptual_loss, f"{option}_loss")

        if len(layers) == 1:
            assert isinstance(partial_loss.encoder, enc.SingleLayerEncoder)
            assert partial_loss.encoder.layer == layer
        else:
            assert isinstance(partial_loss, loss.MultiLayerEncodingLoss)
            assert {name for name, _ in partial_loss.named_children()} == set(layers)
Пример #12
0
    def test_main(self, mock_execution_with):
        mock = mock_execution_with("--mle=vgg19")

        with exits():
            main()

        (_, perceptual_loss), _ = mock.call_args

        mles = {
            mle
            for mle in perceptual_loss.modules()
            if isinstance(mle, enc.MultiLayerEncoder)
        }

        assert len(mles) == 1
        mle = mles.pop()
        assert isinstance(mle, enc.VGGMultiLayerEncoder)
        assert mle.arch == "vgg19"
Пример #13
0
    def test_file(self, mock_execution_with, option):
        image = demo.images()["bird2"]
        # TODO: make this independent of the default size value
        expected = image.read(size=500)
        file = pathlib.Path(pystiche.home()) / image.file
        mock = self._mock_execution(mock_execution_with, option=option, value=str(file))

        with exits():
            cli.main()

        (input_image, perceptual_loss), _ = mock.call_args

        if option == "content":
            actual = perceptual_loss.content_image
        else:  # option == "style":
            actual = perceptual_loss.style_image

        ptu.assert_allclose(actual, expected)
Пример #14
0
    def test_demo_image(self, mock_execution_with, option):
        name = "bird2"
        mock = self._mock_execution(mock_execution_with, option=option, value=name)

        with exits():
            cli.main()

        (input_image, perceptual_loss), _ = mock.call_args

        if option == "content":
            image = perceptual_loss.content_image
        elif option == "style":
            image = perceptual_loss.style_image
        else:  # option == "starting_point"
            image = input_image

        ptu.assert_allclose(
            image, demo.images()[name].read(size=extract_image_size(image)),
        )
Пример #15
0
    def test_unknown(self, mock_execution_with, layer):
        mock_execution_with("--mle=vgg19, " f"--content-layer={layer}")

        with exits(should_succeed=False, check_err=layer):
            main()
Пример #16
0
    def test_device(self, mock_execution_with):
        device = "cpu"
        mock_execution_with("--verbose", f"--device={device}")

        with exits(check_out=device):
            main()
Пример #17
0
    def test_unknown(self, mock_execution_with, loss):
        mock_execution_with(f"--content-loss={loss}")

        with exits(should_succeed=False, check_err=loss):
            main()
Пример #18
0
    def test_mle(self, mock_execution_with):
        mle = "vgg19"
        mock_execution_with("--verbose", f"--multi-layer-encoder={mle}")

        with exits(check_out=("VGGMultiLayerEncoder", mle)):
            main()
Пример #19
0
    def test_non_existing_file(self, mock_execution_with, option):
        file = "/unknown/file.jpg"
        self._mock_execution(mock_execution_with, option=option, value=file)

        with exits(should_succeed=False, check_err=file):
            main()
Пример #20
0
    def test_smoke(self, mock_execution_with):
        mock_execution_with("--device=cpu")

        with exits():
            main()
Пример #21
0
    def test_unknown_demo_images(self, mock_execution_with, option, name):
        self._mock_execution(mock_execution_with, option=option, value=name)

        with exits(should_succeed=False, check_err=name):
            main()
Пример #22
0
    def test_unknown(self, mock_execution_with, mle):
        mock_execution_with(f"--mle={mle}")

        with exits(should_succeed=False, check_err=mle):
            main()
Пример #23
0
    def test_unknown(self, mock_execution_with):
        device = "unknown_device_type"
        mock_execution_with(device=device)

        with exits(should_succeed=False, check_err=device):
            main()