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
.
- 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
.
- 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:
Standard: these are the basic Python types, i.e.,
bool
,int
,float
,str
, andlist
. Standard types are easy to work with, e.g., they can be parsed by JSON and YAML formats.Non-standard: these additionally contain types like
numpy.ndarray
,torch.Tensor
, andPIL.Image.Image
. They are convenient due to more flexibility for model prediction outputs. In most cases, they can be converted to standard types viastandardize()
.
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.Examples
Type aliases (
TypeAliasType
objects defined usingtype
keyword) corresponding to the enums of this class are defined in the same file asPredType
. 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:
numpy.ndarray
withndim == 0
torch.Tensor
withndim == 0
- Type:
- TENSOR#
Tensor type. A prediction is considered to be a tensor if it is one of the following types:
Iterable
of scalars or tensors of any iterable type, includinglist
,tuple
,Collection
,numpy.ndarray
andtorch.Tensor
objects, and any other iterables.
- Type:
- DEFAULT#
Default type. A prediction is considered to be a default type if it is one of the following types:
- Type:
- STANDARD_SCALAR#
Standard scalar type. A prediction is considered to be a standard scalar if it is one of the following types:
- Type:
- 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:
- STANDARD_DEFAULT#
Standard default type. A prediction is considered to be a standard default type if it is one of the following types:
Any of the types defined in
STANDARD_SCALAR
.Any of the types defined in
STANDARD_TENSOR
.
- Type:
- 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:
- static is_scalar(pred: Any) TypeGuard[Scalar] [source]#
Checks if the prediction is a scalar.
See also
- static is_standard_scalar(pred: Any) TypeGuard[StandardScalar] [source]#
Checks if the prediction is a standard scalar.
See also
- Parameters:
pred (Any) – The value to check.
- Returns:
- Return type:
- classmethod is_tensor(pred: Any) TypeGuard[Tensor] [source]#
Checks if the prediction is a tensor.
See also
- classmethod is_standard_tensor(pred: Any) TypeGuard[StandardTensor] [source]#
Checks if the prediction is a standard tensor.
See also
- Parameters:
pred (Any) – The value to check.
- Returns:
- Return type:
- classmethod is_default(pred: Any) TypeGuard[Default] [source]#
Checks if the prediction is a default type.
See also
- classmethod is_standard_default(pred: Any) TypeGuard[StandardDefault] [source]#
Checks if the prediction is a standard default type.
See also
- 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
, andTENSOR
are subclasses ofDEFAULT
.- 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:
- 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:
numpy.generic
andnumpy.ndarray
withndim
=0
are converted to standard scalars.torch.Tensor
withndim
=0
is converted to standard scalars.numpy.ndarray
andtorch.Tensor
withndim
>0
are converted to standard tensors.PIL.Image.Image
is converted to standard tensors by converting it to anumpy.ndarray
and then applying the previous rule.All other iterables are converted to standard tensors by applying the previous rule to each element.
- 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
ornumpy.ndarray
returns a scalar that is not of type defined inSCALAR
.- Returns:
The standardized prediction.
- Return type: