ClassConfig#

Note

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

pydantic model ClassConfig[source]#

Configure class information for a machine learning task.

Show JSON schema
{
   "title": "ClassConfig",
   "description": "Configure class information for a machine learning task.",
   "type": "object",
   "properties": {
      "names": {
         "description": "Names of classes. The i-th class in this list will have class ID = i.",
         "items": {
            "type": "string"
         },
         "title": "Names",
         "type": "array"
      },
      "colors": {
         "anyOf": [
            {
               "items": {
                  "anyOf": [
                     {
                        "type": "string"
                     },
                     {
                        "items": {},
                        "type": "array"
                     }
                  ]
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "description": "Colors used to visualize classes. Can be color strings accepted by matplotlib or RGB tuples. If None, a random color will be auto-generated for each class.",
         "title": "Colors"
      },
      "null_class": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "description": "Optional name of class in `names` to use as the null class. This is used in semantic segmentation to represent the label for imagery pixels that are NODATA or that are missing a label. If None and the class names include \"null\", it will automatically be used as the null class. If None, and this Config is part of a SemanticSegmentationConfig, a null class will be added automatically.",
         "title": "Null Class"
      },
      "type_hint": {
         "const": "class_config",
         "default": "class_config",
         "title": "Type Hint",
         "type": "string"
      }
   },
   "additionalProperties": false,
   "required": [
      "names"
   ]
}

Config:
  • extra: str = forbid

  • validate_assignment: bool = True

Fields:
Validators:
field colors: list[str | tuple] | None = None#

Colors used to visualize classes. Can be color strings accepted by matplotlib or RGB tuples. If None, a random color will be auto-generated for each class.

Validated by:
field names: list[str] [Required]#

Names of classes. The i-th class in this list will have class ID = i.

Validated by:
field null_class: str | None = None#

Optional name of class in names to use as the null class. This is used in semantic segmentation to represent the label for imagery pixels that are NODATA or that are missing a label. If None and the class names include “null”, it will automatically be used as the null class. If None, and this Config is part of a SemanticSegmentationConfig, a null class will be added automatically.

Validated by:
field type_hint: Literal['class_config'] = 'class_config'#
Validated by:
build()#

Build an instance of the corresponding type of object using this config.

For example, BackendConfig will build a Backend object. The arguments to this method will vary depending on the type of Config.

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

ensure_null_class() None[source]#

Add a null class if one isn’t set. This method is idempotent.

Return type:

None

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_class_id(name: str) int[source]#
Parameters:

name (str) –

Return type:

int

get_color_to_class_id() dict[str | tuple[int, int, int], int][source]#
Return type:

dict[str | tuple[int, int, int], int]

get_name(id: int) str[source]#
Parameters:

id (int) –

Return type:

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.

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.

validator validate_colors  »  all fields[source]#

Compare length w/ names. Also auto-generate if not specified.

Return type:

Self

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

validator validate_null_class  »  all fields[source]#

Check if in names. If ‘null’ in names, use it as null class.

Return type:

Self

property color_triples: list[tuple[float, float, float]]#

Class colors in a normalized form.

property null_class_id: int#