This module is a collection of factories for creating objects of datasets, models, optimizers and other useful components. For example, a ResNet-50 visual backbone can be created as:

>>> # Explicitly by name, args and kwargs:
>>> backbone = VisualBackboneFactory.create(
...     "torchvision::resnet50", pretrained=False
... )
>>> # Directly from a config object:
>>> _C = Config(override_list=["MODEL.VISUAL.NAME", "torchvision::resnet50"])
>>> backbone = VisualBackboneFactory.from_config(_C)

Creating directly from Config is fast and simple, and ensures minimal changes throughout the codebase upon any change in the call signature of underlying class; or config hierarchy. Refer description of specific factories for more details.

class virtex.factories.Factory[source]

Bases: object

Base class for all factories. All factories must inherit this base class and follow these guidelines for a consistent behavior:

  • Factory objects cannot be instantiated, doing factory = SomeFactory() is illegal. Child classes should not implement __init__ methods.

  • All factories must have an attribute named PRODUCTS of type Dict[str, Callable], which associates each class with a unique string name which can be used to create it.

  • All factories must implement one classmethod, from_config() which contains logic for creating an object directly by taking name and other arguments directly from Config. They can use create() already implemented in this base class.

  • from_config() should not use too many extra arguments than the config itself, unless necessary (such as model parameters for optimizer).

classmethod create(name: str, *args, **kwargs) Any[source]

Create an object by its name, args and kwargs.

classmethod from_config(config: virtex.config.Config) Any[source]

Create an object directly from config.