pystiche.optim
¶
- pystiche.optim.default_image_optimizer(input_image)¶
- Parameters
input_image (
Tensor
) – Image to be optimized.- Return type
- Returns
torch.optim.LBFGS
optimizer with a learning rate of1.0
. The pixels ofinput_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 apreprocessor
is used, has to be a getter.num_steps (
Union
[int
,Iterable
[int
]]) – Number of optimization steps. Defaults to500
.preprocessor (
Optional
[Module
]) – Optional preprocessor that is called with theinput_image
before the optimization.postprocessor (
Optional
[Module
]) – Optional preprocessor that is called with theinput_image
after the optimization.quiet (
bool
) – IfTrue
, no information is printed to STDOUT during the optimization. Defaults toFalse
.
- Raises
RuntimeError – If
preprocessor
is used andoptimizer
is not passed asgetter. –
- Return type
- 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. IfNone
,default_image_optimizer()
is used. Defaults toNone
.preprocessor (
Optional
[Module
]) – Optional preprocessor that is called with theinput_image
before the optimization.postprocessor (
Optional
[Module
]) – Optional preprocessor that is called with theinput_image
after the optimization.quiet (
bool
) – IfTrue
, no information is printed to STDOUT during the optimization. Defaults toFalse
.
- Return type
- pystiche.optim.default_model_optimizer(transformer)¶
- Parameters
transformer (
Module
) – Transformer to be optimized.- Return type
- Returns
torch.optim.Adam
optimizer with a learning rate of1e-3
. The parameters oftransformer
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 thetransformer
. 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 optimizationcriterion
. If omitted andcriterion
is aPerceptualLoss
or aGuidedPerceptualLoss
this defaults to invokingset_content_image()
.optimizer (
Optional
[Optimizer
]) – Optional optimizer. IfNone
,default_model_optimizer()
is used.quiet (
bool
) – IfTrue
, no information is printed to STDOUT during the optimization. Defaults toFalse
.
- Return type
- 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 thetransformer
. 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 optimizationcriterion
. If omitted andcriterion
is aPerceptualLoss
or aGuidedPerceptualLoss
this defaults to invokingset_content_image()
.epochs (
int
) – Number of epochs. Defaults to2
.optimizer (
Optional
[Optimizer
]) – Optional optimizer. IfNone
, it is extracted fromlr_scheduler
or func:default_model_optimizer is used.lr_scheduler (
Optional
[_LRScheduler
]) – Optional learning rate scheduler.step()
is invoked after every epoch.quiet (
bool
) – IfTrue
, no information is printed to STDOUT during the optimization. Defaults toFalse
.
- Return type