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.
- 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:
- 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.
- build_external_model(save_dir: str, hubconf_dir: str | None = None, ddp_rank: int | None = None) Module #
Build and return an external model.
- classmethod deserialize(inp: str | dict | Config) Self #
Deserialize Config from a JSON file or dict, upgrading if possible.
If
inp
is already aConfig
, it is returned as is.
- 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()#
- 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.
- 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.