vis4d.engine.util¶
Run utilities.
Functions
|
Recursively applies a function to all elements of a certain dtype. |
Check if obj is dataclass instance. |
|
|
Transfers a collection of data to the given device. |
Classes
A custom type for data that can be moved to a torch device. |
- class TransferableDataType[source]¶
A custom type for data that can be moved to a torch device.
Example
>>> isinstance(dict, TransferableDataType) False >>> isinstance(torch.rand(2, 3), TransferableDataType) True >>> class CustomObject: ... def __init__(self): ... self.x = torch.rand(2, 2) ... def to(self, device): ... self.x = self.x.to(device) ... return self >>> isinstance(CustomObject(), TransferableDataType) True
- apply_to_collection(data, dtype, function, *args, wrong_dtype=None, include_none=True, **kwargs)[source]¶
Recursively applies a function to all elements of a certain dtype.
- Parameters:
data (
Any
) – the collection to apply the function todtype (
Union
[type
,Any
,tuple
[Union
[type
,Any
]]]) – the given function will be applied to all elements of this dtypefunction (
Callable
[[Any
],Any
]) – the function to apply*args (
Any
) – positional arguments (will be forwarded to calls offunction
)wrong_dtype (
Union
[None
,type
,tuple
[type
,...
]]) – the given function won’t be applied if this type is specified and the given collections is of thewrong_dtype
even if it is of typedtype
include_none (
bool
) – Whether to include an element if the output offunction
isNone
.**kwargs (
Any
) – keyword arguments (will be forwarded to calls offunction
)
- Raises:
ValueError – If frozen dataclass is passed to apply_to_collection.
- Return type:
Any
- Returns:
The resulting collection
- is_dataclass_instance(obj)[source]¶
Check if obj is dataclass instance.
https://docs.python.org/3/library/dataclasses.html#module-level-decorators-classes-and-functions
- Return type:
bool
- move_data_to_device(batch, device, convert_to_numpy=False)[source]¶
Transfers a collection of data to the given device.
Any object that defines a method
to(device)
will be moved and all other objects in the collection will be left untouched.This implementation is modified from https://github.com/Lightning-AI/lightning
- Parameters:
batch (
Any
) – A tensor or collection of tensors or anything that has a method.to(...)
. Seeapply_to_collection()
for a list of supported collection types.device (
device
|str
|int
) – The device to which the data should be moved.convert_to_numpy (
bool
) – Whether to convert from tensor to numpy array.
- Return type:
Any
- Returns:
- The same collection but with all contained tensors residing on the new
device.