def test_default_image_optim_loop_logging_smoke(caplog, optim_asset_loader): asset = optim_asset_loader("default_image_optim_loop") num_steps = 1 optim_logger = optim.OptimLogger() log_fn = optim.default_image_optim_log_fn(optim_logger, log_freq=1) with asserts.assert_logs(caplog, logger=optim_logger): optim.default_image_optim_loop( asset.input.image, asset.input.criterion, num_steps=num_steps, log_fn=log_fn, )
def test_default_image_optim_loop_logging_smoke(self): asset = self.load_asset(path.join("optim", "default_image_optim_loop")) num_steps = 1 optim_logger = optim.OptimLogger() log_fn = optim.default_image_optim_log_fn(optim_logger, log_freq=1) with self.assertLogs(optim_logger.logger, "INFO"): optim.default_image_optim_loop( asset.input.image, asset.input.criterion, num_steps=num_steps, log_fn=log_fn, )
def test_default_image_optim_loop(self): asset = self.load_asset(path.join("optim", "default_image_optim_loop")) actual = optim.default_image_optim_loop( asset.input.image, asset.input.criterion, get_optimizer=asset.params.get_optimizer, num_steps=asset.params.num_steps, quiet=True, ) desired = asset.output.image self.assertTensorAlmostEqual(actual, desired, rtol=1e-4)
def test_default_image_optim_loop(optim_asset_loader): asset = optim_asset_loader("default_image_optim_loop") actual = optim.default_image_optim_loop( asset.input.image, asset.input.criterion, get_optimizer=asset.params.get_optimizer, num_steps=asset.params.num_steps, quiet=True, ) desired = asset.output.image ptu.assert_allclose(actual, desired, rtol=1e-4)
def gatys_ecker_bethge_2015_nst( content_image: torch.Tensor, style_image: torch.Tensor, num_steps: int = 500, impl_params: bool = True, criterion: Optional[PerceptualLoss] = None, quiet: bool = False, logger: Optional[logging.Logger] = None, log_fn: Optional[Callable[[int, Union[torch.Tensor, pystiche.LossDict]], None]] = None, ) -> torch.Tensor: if criterion is None: criterion = gatys_ecker_bethge_2015_perceptual_loss( impl_params=impl_params) device = content_image.device criterion = criterion.to(device) starting_point = "content" if impl_params else "random" input_image = get_input_image(starting_point=starting_point, content_image=content_image) preprocessor = gatys_ecker_bethge_2015_preprocessor().to(device) postprocessor = gatys_ecker_bethge_2015_postprocessor().to(device) criterion.set_content_image(preprocessor(content_image)) criterion.set_style_image(preprocessor(style_image)) return default_image_optim_loop( input_image, criterion, get_optimizer=gatys_ecker_bethge_2015_optimizer, num_steps=num_steps, preprocessor=preprocessor, postprocessor=postprocessor, quiet=quiet, logger=logger, log_fn=log_fn, )
print(criterion) ######################################################################################## # We set the target images for the optimization ``criterion``. criterion.set_content_image(content_image) criterion.set_style_image(style_image) ######################################################################################## # We perform the unguided NST and show the result. starting_point = "content" input_image = get_input_image(starting_point, content_image=content_image) output_image = default_image_optim_loop(input_image, criterion, num_steps=500, logger=demo_logger()) ######################################################################################## show_image(output_image) ######################################################################################## # While the result is not completely unreasonable, the building has a strong blueish # cast that looks unnatural. Since the optimization was unconstrained the color of the # sky was used for building. In the remainder of this example we will solve this by # dividing the images in multiple separate regions. ######################################################################################## # Guided image optimization # -------------------------
# ---------------------------------- # # As a baseline we use a standard image optimization without pyramid. starting_point = "content" input_image = get_input_image(starting_point, content_image=content_image) show_image(input_image, title="Input image") ######################################################################################## # We time the NST performed by :func:`~pystiche.optim.default_image_optim_loop` and # show the result. start_without_pyramid = time.time() output_image = optim.default_image_optim_loop( input_image, criterion, num_steps=400, logger=demo.logger() ) stop_without_pyramid = time.time() show_image(output_image, title="Output image without pyramid") ######################################################################################## elapsed_time_without_pyramid = stop_without_pyramid - start_without_pyramid print( f"Without pyramid the optimization took {elapsed_time_without_pyramid:.0f} seconds." ) ######################################################################################## # As you can see the small blurry branches on the left side of the image were picked up # by the style transfer. They distort the mosaic pattern, which minders the quality of