Prediction Type#

Scalar: TypeAliasType = StandardScalar | np.generic | np.ndarray | torch.Tensor#

Type alias for a scalar prediction. For more information, see SCALAR.

Tensor: TypeAliasType = Iterable[Scalar | Tensor] | PIL.Image.Image#

Type alias for a tensor prediction. For more information, see TENSOR.

Default: TypeAliasType = Scalar | Tensor#

Type alias for a default prediction. For more information, see DEFAULT.

StandardScalar: TypeAliasType = bool | int | float | str#

Type alias for a standard scalar prediction. For more information, see STANDARD_SCALAR.

bool | int | float | str

StandardTensor: TypeAliasType = list[StandardScalar | StandardTensor]#

Type alias for a standard tensor prediction. For more information, see STANDARD_TENSOR.

StandardDefault: TypeAliasType = StandardScalar | StandardTensor#

Type alias for a standard default prediction. For more information, see STANDARD_DEFAULT.

bool | int | float | str | list

NonDefault[T]: TypeAliasType = T#

Type alias for a non-default prediction. For more information, see NON_DEFAULT.

Either: TypeAliasType = Default | NonDefault#

Type alias for either default or non-default prediction, i.e., any prediction.

class glasses_detector.components.pred_type.PredType(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

Enum class for expected prediction types.

This class specifies the expected prediction types mainly for classification, detection and segmentation models that work with image data. The expected types are called Default and there are two categories of them:

  1. Standard: these are the basic Python types, i.e., bool, int, float, str, and list. Standard types are easy to work with, e.g., they can be parsed by JSON and YAML formats.

  2. Non-standard: these additionally contain types like numpy.ndarray, torch.Tensor, and PIL.Image.Image. They are convenient due to more flexibility for model prediction outputs. In most cases, they can be converted to standard types via standardize().

Note

The constants defined in this class are only enums, not actual classes or type objects. For type hints, corresponding type aliases are defined in the same file as this class.

Warning

The enum types are not exclusive, for example, SCALAR is also a DEFAULT.

Examples

Type aliases (TypeAliasType objects defined using type keyword) corresponding to the enums of this class are defined in the same file as PredType. They can be used to specify the expected types when defining the methods:

>>> from glasses_detector.components.pred_type import StandardScalar
>>> def predict_class(
...     self,
...     image: Image.Image,
...     output_format: str = "score",
... ) -> StandardScalar:
...     ...

PredType static and class methods can be used to check the type of the prediction:

>>> PredType.is_standard_scalar(1)
True
>>> PredType.is_standard_scalar(np.array([1])[0])
False
>>> PredType.is_default(Image.fromarray(np.zeros((1, 1))))
True

Finally, standardize() can be used to convert the prediction to a standard type:

>>> PredType.standardize(np.array([1, 2, 3]))
[1, 2, 3]
>>> PredType.standardize(Image.fromarray(np.zeros((1, 1))))
[[0.0]]
SCALAR#

Scalar type. A prediction is considered to be a scalar if it is one of the following types:

Type:

PredType

TENSOR#

Tensor type. A prediction is considered to be a tensor if it is one of the following types:

Type:

PredType

DEFAULT#

Default type. A prediction is considered to be a default type if it is one of the following types:

  • Any of the types defined in SCALAR.

  • Any of the types defined in TENSOR.

Type:

PredType

STANDARD_SCALAR#

Standard scalar type. A prediction is considered to be a standard scalar if it is one of the following types:

Type:

PredType

STANDARD_TENSOR#

Standard tensor type. A prediction is considered to be a standard tensor if it is one of the following types:

  • list of standard scalars or standard tensors. No other iterables than lists are allowed.

Type:

PredType

STANDARD_DEFAULT#

Standard default type. A prediction is considered to be a standard default type if it is one of the following types:

Type:

PredType

NON_DEFAULT#

Non-default type. A prediction is considered to be a non-default type if it is not a default type, i.e., it is not any of the types defined in DEFAULT.

Type:

PredType

static is_scalar(pred: Any) TypeGuard[Scalar][source]#

Checks if the prediction is a scalar.

See also

SCALAR

Parameters:

pred (Any) – The value to check.

Returns:

True if the value is a scalar, False otherwise.

Return type:

TypeGuard[Scalar]

static is_standard_scalar(pred: Any) TypeGuard[StandardScalar][source]#

Checks if the prediction is a standard scalar.

See also

STANDARD_SCALAR

Parameters:

pred (Any) – The value to check.

Returns:

True if the value is a standard scalar, False otherwise.

Return type:

TypeGuard[StandardScalar]

classmethod is_tensor(pred: Any) TypeGuard[Tensor][source]#

Checks if the prediction is a tensor.

See also

TENSOR

Parameters:

pred (Any) – The value to check.

Returns:

True if the value is a tensor, False otherwise.

Return type:

TypeGuard[Tensor]

classmethod is_standard_tensor(pred: Any) TypeGuard[StandardTensor][source]#

Checks if the prediction is a standard tensor.

See also

STANDARD_TENSOR

Parameters:

pred (Any) – The value to check.

Returns:

True if the value is a standard tensor, False otherwise.

Return type:

TypeGuard[StandardTensor]

classmethod is_default(pred: Any) TypeGuard[Default][source]#

Checks if the prediction is a default type.

See also

DEFAULT

Parameters:

pred (Any) – The value to check.

Returns:

True if the type of the value is default, False otherwise.

Return type:

TypeGuard[Default]

classmethod is_standard_default(pred: Any) TypeGuard[StandardDefault][source]#

Checks if the prediction is a standard default type.

See also

STANDARD_DEFAULT

Parameters:

pred (Any) – The value to check.

Returns:

True if the type of the value is standard default, False otherwise.

Return type:

TypeGuard[StandardDefault]

classmethod check(pred: Any) Self[source]#

Checks the type of the prediction and returns its enum.

Checks the type of the prediction and returns the corresponding enum of the lowest type category. First, it checks if the prediction is a standard scalar or a regular scalar (in that order). If not, it checks if the prediction is a standard tensor or a regular tensor (in that order). Finally, if none of the previous checks are successful, it returns NON_DEFAULT.

Note

All four types, i.e., STANDARD_SCALAR, SCALAR, STANDARD_TENSOR, and TENSOR are subclasses of DEFAULT.

Parameters:

pred (Any) – The value to check.

Returns:

The corresponding enum of the lowest type category or NON_DEFAULT if no default category is applicable.

Return type:

Self

classmethod standardize(pred: Default) StandardDefault[source]#

Standardize the prediction.

Standardize the prediction to a standard default type. If the prediction is already a standard default type, it is returned as-is. Otherwise, it is converted to a standard default type using the following rules:

Parameters:

pred (Default) – The default prediction to standardize.

Raises:

ValueError – If the prediction cannot be standardized. This can happen if a prediction is not default or if a class, such as torch.Tensor or numpy.ndarray returns a scalar that is not of type defined in SCALAR.

Returns:

The standardized prediction.

Return type:

StandardDefault