pystiche.optim

pystiche.optim.default_image_optimizer(input_image)
Parameters

input_image (Tensor) – Image to be optimized.

Return type

LBFGS

Returns

torch.optim.LBFGS optimizer with a learning rate of 1.0. The pixels of input_image are set as optimization parameters.

pystiche.optim.image_optimization(input_image, criterion, optimizer=None, num_steps=500, preprocessor=None, postprocessor=None, quiet=False)

Perform an image optimization with integrated logging.

Parameters
  • input_image (Tensor) – Image to be optimized.

  • criterion (Module) – Optimization criterion.

  • optimizer (Union[Optimizer, Callable[[Tensor], Optimizer], None]) – Optional optimizer or optimizer getter. If omitted, default_image_optimizer() is used. If a preprocessor is used, has to be a getter.

  • num_steps (Union[int, Iterable[int]]) – Number of optimization steps. Defaults to 500.

  • preprocessor (Optional[Module]) – Optional preprocessor that is called with the input_image before the optimization.

  • postprocessor (Optional[Module]) – Optional preprocessor that is called with the input_image after the optimization.

  • quiet (bool) – If True, no information is printed to STDOUT during the optimization. Defaults to False.

Raises
  • RuntimeError – If preprocessor is used and optimizer is not passed as

  • getter.

Return type

Tensor

pystiche.optim.pyramid_image_optimization(input_image, criterion, pyramid, get_optimizer=None, preprocessor=None, postprocessor=None, quiet=False)

Perform a image optimization for pystiche.pyramid.ImagePyramid s with integrated logging.

Parameters
  • input_image (Tensor) – Image to be optimized.

  • criterion (Module) – Optimization criterion.

  • pyramid (ImagePyramid) – Image pyramid.

  • get_optimizer (Optional[Callable[[Tensor], Optimizer]]) – Optional getter for the optimizer. If None, default_image_optimizer() is used. Defaults to None.

  • preprocessor (Optional[Module]) – Optional preprocessor that is called with the input_image before the optimization.

  • postprocessor (Optional[Module]) – Optional preprocessor that is called with the input_image after the optimization.

  • quiet (bool) – If True, no information is printed to STDOUT during the optimization. Defaults to False.

Return type

Tensor

pystiche.optim.default_model_optimizer(transformer)
Parameters

transformer (Module) – Transformer to be optimized.

Return type

Optimizer

Returns

torch.optim.Adam optimizer with a learning rate of 1e-3. The parameters of transformer are set as optimization parameters.

pystiche.optim.model_optimization(image_loader, transformer, criterion, criterion_update_fn=None, optimizer=None, quiet=False)

Perform a model optimization for a single epoch with integrated logging.

Parameters
  • image_loader (DataLoader) – Images used as input for the transformer. Drawing from this should yield either an batched image or a tuple or list with a batched image as first item.

  • transformer (Module) – Transformer to be optimized.

  • criterion (Module) – Optimization criterion.

  • criterion_update_fn (Optional[Callable[[Tensor, Module], None]]) – Is called before each optimization step with the current images and the optimization criterion. If omitted and criterion is a PerceptualLoss or a GuidedPerceptualLoss this defaults to invoking set_content_image().

  • optimizer (Optional[Optimizer]) – Optional optimizer. If None, default_model_optimizer() is used.

  • quiet (bool) – If True, no information is printed to STDOUT during the optimization. Defaults to False.

Return type

Module

pystiche.optim.multi_epoch_model_optimization(image_loader, transformer, criterion, criterion_update_fn=None, epochs=2, optimizer=None, lr_scheduler=None, quiet=False)

Perform a model optimization for multiple epochs with integrated logging.

Parameters
  • image_loader (DataLoader) – Images used as input for the transformer. Drawing from this should yield either an batched image or a tuple or list with a batched image as first item.

  • transformer (Module) – Transformer to be optimized.

  • criterion (Module) – Optimization criterion.

  • criterion_update_fn (Optional[Callable[[Tensor, Module], None]]) – Is called before each optimization step with the current images and the optimization criterion. If omitted and criterion is a PerceptualLoss or a GuidedPerceptualLoss this defaults to invoking set_content_image().

  • epochs (int) – Number of epochs. Defaults to 2.

  • optimizer (Optional[Optimizer]) – Optional optimizer. If None, it is extracted from lr_scheduler or func:default_model_optimizer is used.

  • lr_scheduler (Optional[_LRScheduler]) – Optional learning rate scheduler. step() is invoked after every epoch.

  • quiet (bool) – If True, no information is printed to STDOUT during the optimization. Defaults to False.

Return type

Module