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()
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()
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()
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
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()
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()
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()
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"), )
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
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))), )
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)
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"
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)
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)), )
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()
def test_device(self, mock_execution_with): device = "cpu" mock_execution_with("--verbose", f"--device={device}") with exits(check_out=device): main()
def test_unknown(self, mock_execution_with, loss): mock_execution_with(f"--content-loss={loss}") with exits(should_succeed=False, check_err=loss): main()
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()
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()
def test_smoke(self, mock_execution_with): mock_execution_with("--device=cpu") with exits(): main()
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()
def test_unknown(self, mock_execution_with, mle): mock_execution_with(f"--mle={mle}") with exits(should_succeed=False, check_err=mle): main()
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()