ObjectDetectionModelConfig#

Note

All Configs are derived from rastervision.pipeline.config.Config, which itself is a pydantic Model.

pydantic model ObjectDetectionModelConfig[source]#

Configure an object detection model.

Show JSON schema
{
   "title": "ObjectDetectionModelConfig",
   "description": "Configure an object detection model.",
   "type": "object",
   "properties": {
      "backbone": {
         "allOf": [
            {
               "$ref": "#/$defs/Backbone"
            }
         ],
         "default": "resnet50",
         "description": "The torchvision.models backbone to use, which must be in the resnet* family."
      },
      "pretrained": {
         "default": true,
         "description": "If True, use ImageNet weights. If False, use random initialization.",
         "title": "Pretrained",
         "type": "boolean"
      },
      "init_weights": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "description": "URI of PyTorch model weights used to initialize model. If set, this supersedes the pretrained option.",
         "title": "Init Weights"
      },
      "load_strict": {
         "default": true,
         "description": "If True, the keys in the state dict referenced by init_weights must match exactly. Setting this to False can be useful if you just want to load the backbone of a model.",
         "title": "Load Strict",
         "type": "boolean"
      },
      "external_def": {
         "anyOf": [
            {
               "$ref": "#/$defs/ExternalModuleConfig"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "description": "If specified, the model will be built from the definition from this external source, using Torch Hub."
      },
      "extra_args": {
         "default": {},
         "description": "Other implementation-specific args that might be useful for constructing the default model. This is ignored if using an external model.",
         "title": "Extra Args",
         "type": "object"
      },
      "type_hint": {
         "const": "object_detection_model",
         "default": "object_detection_model",
         "enum": [
            "object_detection_model"
         ],
         "title": "Type Hint",
         "type": "string"
      }
   },
   "$defs": {
      "Backbone": {
         "enum": [
            "alexnet",
            "densenet121",
            "densenet169",
            "densenet201",
            "densenet161",
            "googlenet",
            "inception_v3",
            "mnasnet0_5",
            "mnasnet0_75",
            "mnasnet1_0",
            "mnasnet1_3",
            "mobilenet_v2",
            "resnet18",
            "resnet34",
            "resnet50",
            "resnet101",
            "resnet152",
            "resnext50_32x4d",
            "resnext101_32x8d",
            "wide_resnet50_2",
            "wide_resnet101_2",
            "shufflenet_v2_x0_5",
            "shufflenet_v2_x1_0",
            "shufflenet_v2_x1_5",
            "shufflenet_v2_x2_0",
            "squeezenet1_0",
            "squeezenet1_1",
            "vgg11",
            "vgg11_bn",
            "vgg13",
            "vgg13_bn",
            "vgg16",
            "vgg16_bn",
            "vgg19_bn",
            "vgg19"
         ],
         "title": "Backbone",
         "type": "string"
      },
      "ExternalModuleConfig": {
         "additionalProperties": false,
         "description": "Config describing an object to be loaded via Torch Hub.",
         "properties": {
            "uri": {
               "anyOf": [
                  {
                     "minLength": 1,
                     "type": "string"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "description": "Local uri of a zip file, or local uri of a directory,or remote uri of zip file.",
               "title": "Uri"
            },
            "github_repo": {
               "anyOf": [
                  {
                     "pattern": ".+/.+",
                     "type": "string"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "description": "<repo-owner>/<repo-name>[:tag]",
               "title": "Github Repo"
            },
            "name": {
               "anyOf": [
                  {
                     "minLength": 1,
                     "type": "string"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "description": "Name of the folder in which to extract/copy the definition files.",
               "title": "Name"
            },
            "entrypoint": {
               "description": "Name of a Callable present in ``hubconf.py``. See docs for ``torch.hub`` for details.",
               "minLength": 1,
               "title": "Entrypoint",
               "type": "string"
            },
            "entrypoint_args": {
               "default": [],
               "description": "Args to pass to the entrypoint. Must be serializable.",
               "items": {},
               "title": "Entrypoint Args",
               "type": "array"
            },
            "entrypoint_kwargs": {
               "default": {},
               "description": "Keyword args to pass to the entrypoint. Must be serializable.",
               "title": "Entrypoint Kwargs",
               "type": "object"
            },
            "force_reload": {
               "default": false,
               "description": "Force reload of module definition.",
               "title": "Force Reload",
               "type": "boolean"
            },
            "type_hint": {
               "const": "external-module",
               "default": "external-module",
               "enum": [
                  "external-module"
               ],
               "title": "Type Hint",
               "type": "string"
            }
         },
         "required": [
            "entrypoint"
         ],
         "title": "ExternalModuleConfig",
         "type": "object"
      }
   },
   "additionalProperties": false
}

Config:
  • extra: str = forbid

  • validate_assignment: bool = True

Fields:
Validators:
field backbone: Backbone = Backbone.resnet50#

The torchvision.models backbone to use, which must be in the resnet* family.

Validated by:
field external_def: ExternalModuleConfig | None = None#

If specified, the model will be built from the definition from this external source, using Torch Hub.

field extra_args: dict = {}#

Other implementation-specific args that might be useful for constructing the default model. This is ignored if using an external model.

field init_weights: str | None = None#

URI of PyTorch model weights used to initialize model. If set, this supersedes the pretrained option.

field load_strict: bool = True#

If True, the keys in the state dict referenced by init_weights must match exactly. Setting this to False can be useful if you just want to load the backbone of a model.

field pretrained: bool = True#

If True, use ImageNet weights. If False, use random initialization.

field type_hint: Literal['object_detection_model'] = 'object_detection_model'#
build(num_classes: int, in_channels: int, save_dir: str | None = None, hubconf_dir: str | None = None, ddp_rank: int | None = None, **kwargs) Module#

Build and return a model based on the config.

Parameters:
  • num_classes (int) – Number of classes.

  • in_channels (int) – Number of channels in the images that will be fed into the model. Defaults to 3.

  • save_dir (str|None) – Used for building external_def if specified. Defaults to None.

  • hubconf_dir (str|None) – Used for building external_def if specified. Defaults to None.

  • **kwargs – Extra args for build_default_model().

  • ddp_rank (int | None) –

Returns:

A PyTorch nn.Module.

Return type:

Module

build_default_model(num_classes: int, in_channels: int, img_sz: int) FasterRCNN[source]#

Returns a FasterRCNN model.

Note that the model returned will have (num_classes + 2) output classes. +1 for the null class (zeroth index), and another +1 (last index) for backward compatibility with earlier Raster Vision versions.

Returns:

a FasterRCNN model.

Return type:

FasterRCNN

Parameters:
  • num_classes (int) –

  • in_channels (int) –

  • img_sz (int) –

build_external_model(save_dir: str, hubconf_dir: str | None = None, ddp_rank: int | None = None) Module#

Build and return an external model.

Parameters:
  • save_dir (str) – The module def will be saved here.

  • hubconf_dir (str|None) – Path to existing definition. Defaults to None.

  • ddp_rank (int | None) –

Returns:

A PyTorch nn.Module.

Return type:

Module

classmethod deserialize(inp: str | dict | Config) Self#

Deserialize Config from a JSON file or dict, upgrading if possible.

If inp is already a Config, it is returned as is.

Parameters:

inp (str | dict | Config) – a URI to a JSON file or a dict.

Return type:

Self

classmethod from_dict(cfg_dict: dict) Self#

Deserialize Config from a dict.

Parameters:

cfg_dict (dict) – Dict to deserialize.

Return type:

Self

classmethod from_file(uri: str) Self#

Deserialize Config from a JSON file, upgrading if possible.

Parameters:

uri (str) – URI to load from.

Return type:

Self

get_backbone_str()#
validator only_valid_backbones  »  backbone[source]#
recursive_validate_config()#

Recursively validate hierarchies of Configs.

This uses reflection to call validate_config on a hierarchy of Configs using a depth-first pre-order traversal.

revalidate()#

Re-validate an instantiated Config.

Runs all Pydantic validators plus self.validate_config().

to_file(uri: str, with_rv_metadata: bool = True) None#

Save a Config to a JSON file, optionally with RV metadata.

Parameters:
  • uri (str) – URI to save to.

  • with_rv_metadata (bool) – If True, inject Raster Vision metadata such as plugin_versions, so that the config can be upgraded when loaded.

Return type:

None

update(*args, **kwargs)#

Update any fields before validation.

Subclasses should override this to provide complex default behavior, for example, setting default values as a function of the values of other fields. The arguments to this method will vary depending on the type of Config.

validate_config()#

Validate fields that should be checked after update is called.

This is to complement the builtin validation that Pydantic performs at the time of object construction.

validate_list(field: str, valid_options: list[str])#

Validate a list field.

Parameters:
  • field (str) – name of field to validate

  • valid_options (list[str]) – values that field is allowed to take

Raises:

ConfigError – if field is invalid