otx.core.optimizer#

Modules related to an optimizer.

Classes

OptimizerCallableSupportHPO(optimizer_cls, ...)

Optimizer callable supports OTX hyper-parameter optimization (HPO) algorithm.

class otx.core.optimizer.OptimizerCallableSupportHPO(optimizer_cls: type[Optimizer] | str, optimizer_kwargs: dict[str, int | float | bool])[source]#

Bases: object

Optimizer callable supports OTX hyper-parameter optimization (HPO) algorithm.

It makes OptimizerCallable pickelable and accessible to parameters. It is used for HPO and adaptive batch size.

Parameters:
  • optimizer_cls – Optimizer class type or string class import path. See examples for details.

  • optimizer_kwargs – Keyword arguments used for the initialization of the given optimizer_cls.

Examples

This is an example to create MobileNetV3ForMulticlassCls with a SGD optimizer and custom configurations.

```python from torch.optim import SGD from otx.algo.classification.mobilenet_v3_large import MobileNetV3ForMulticlassCls

model = MobileNetV3ForMulticlassCls(

num_classes=3, optimizer=OptimizerCallableSupportHPO(

optimizer_cls=SGD, optimizer_kwargs={

“lr”: 0.1, “momentum”: 0.9, “weight_decay”: 1e-4,

},

),

)#

It can be created from the string class import path such as

```python from otx.algo.classification.mobilenet_v3_large import MobileNetV3ForMulticlassCls

model = MobileNetV3ForMulticlassCls(

num_classes=3, optimizer=OptimizerCallableSupportHPO(

optimizer_cls=”torch.optim.SGD”, optimizer_kwargs={

“lr”: 0.1, “momentum”: 0.9, “weight_decay”: 1e-4,

},

),

)#

__call__(params: params_t) Optimizer[source]#

Create torch.optim.Optimizer instance for the given parameters.

classmethod from_callable(func: OptimizerCallable) OptimizerCallableSupportHPO[source]#

Create this class instance from an existing optimizer callable.

to_lazy_instance() ClassType[source]#

Return lazy instance of this class.

Because OTX is rely on jsonargparse library, the default value of class initialization argument should be the lazy instance. Please refer to https://jsonargparse.readthedocs.io/en/stable/#default-values for more details.

Examples

This is an example to implement a new model with a SGD optimizer and custom configurations as a default.

```python class MyAwesomeMulticlassClsModel(OTXMulticlassClsModel):

def __init__(

self, label_info: LabelInfoTypes, optimizer: OptimizerCallable = OptimizerCallableSupportHPO(

optimizer_cls=SGD, optimizer_kwargs={

“lr”: 0.1, “momentum”: 0.9, “weight_decay”: 1e-4,

},

).to_lazy_instance(), scheduler: LRSchedulerCallable | LRSchedulerListCallable = DefaultSchedulerCallable, metric: MetricCallable = MultiClassClsMetricCallable, torch_compile: bool = False,

) -> None: …

```