2020-05-29 14:45:21 +02:00
|
|
|
import torch
|
|
|
|
import numpy as np
|
2020-09-12 15:39:01 +08:00
|
|
|
from copy import deepcopy
|
2020-07-07 12:40:55 +02:00
|
|
|
from numbers import Number
|
2020-05-29 14:45:21 +02:00
|
|
|
from typing import Union, Optional
|
|
|
|
|
2020-07-21 10:47:56 +02:00
|
|
|
from tianshou.data.batch import _parse_value, Batch
|
2020-05-29 14:45:21 +02:00
|
|
|
|
|
|
|
|
2020-09-12 15:39:01 +08:00
|
|
|
def to_numpy(
|
|
|
|
x: Optional[Union[Batch, dict, list, tuple, np.number, np.bool_, Number,
|
|
|
|
np.ndarray, torch.Tensor]]
|
|
|
|
) -> Union[Batch, dict, list, tuple, np.ndarray]:
|
2020-05-29 14:45:21 +02:00
|
|
|
"""Return an object without torch.Tensor."""
|
2020-08-27 12:15:18 +08:00
|
|
|
if isinstance(x, torch.Tensor): # most often case
|
2020-09-12 15:39:01 +08:00
|
|
|
return x.detach().cpu().numpy()
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, np.ndarray): # second often case
|
2020-09-12 15:39:01 +08:00
|
|
|
return x
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, (np.number, np.bool_, Number)):
|
2020-09-12 15:39:01 +08:00
|
|
|
return np.asanyarray(x)
|
2020-08-27 12:15:18 +08:00
|
|
|
elif x is None:
|
2020-09-12 15:39:01 +08:00
|
|
|
return np.array(None, dtype=np.object)
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, Batch):
|
2020-09-12 15:39:01 +08:00
|
|
|
x = deepcopy(x)
|
2020-08-27 12:15:18 +08:00
|
|
|
x.to_numpy()
|
2020-09-12 15:39:01 +08:00
|
|
|
return x
|
2020-05-29 14:45:21 +02:00
|
|
|
elif isinstance(x, dict):
|
2020-09-12 15:39:01 +08:00
|
|
|
return {k: to_numpy(v) for k, v in x.items()}
|
2020-07-21 10:47:56 +02:00
|
|
|
elif isinstance(x, (list, tuple)):
|
|
|
|
try:
|
2020-09-12 15:39:01 +08:00
|
|
|
return to_numpy(_parse_value(x))
|
2020-07-21 10:47:56 +02:00
|
|
|
except TypeError:
|
2020-09-12 15:39:01 +08:00
|
|
|
return [to_numpy(e) for e in x]
|
2020-07-21 10:47:56 +02:00
|
|
|
else: # fallback
|
2020-09-12 15:39:01 +08:00
|
|
|
return np.asanyarray(x)
|
2020-05-29 14:45:21 +02:00
|
|
|
|
|
|
|
|
2020-09-12 15:39:01 +08:00
|
|
|
def to_torch(
|
|
|
|
x: Union[Batch, dict, list, tuple, np.number, np.bool_, Number, np.ndarray,
|
|
|
|
torch.Tensor],
|
|
|
|
dtype: Optional[torch.dtype] = None,
|
|
|
|
device: Union[str, int, torch.device] = "cpu",
|
|
|
|
) -> Union[Batch, dict, list, tuple, torch.Tensor]:
|
2020-05-29 14:45:21 +02:00
|
|
|
"""Return an object without np.ndarray."""
|
2020-09-12 15:39:01 +08:00
|
|
|
if isinstance(x, np.ndarray) and issubclass(
|
|
|
|
x.dtype.type, (np.bool_, np.number)
|
|
|
|
): # most often case
|
2020-09-13 19:31:50 +08:00
|
|
|
x = torch.from_numpy(x).to(device) # type: ignore
|
2020-08-27 12:15:18 +08:00
|
|
|
if dtype is not None:
|
|
|
|
x = x.type(dtype)
|
2020-09-12 15:39:01 +08:00
|
|
|
return x
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, torch.Tensor): # second often case
|
2020-05-30 15:40:31 +02:00
|
|
|
if dtype is not None:
|
|
|
|
x = x.type(dtype)
|
2020-09-13 19:31:50 +08:00
|
|
|
return x.to(device) # type: ignore
|
2020-08-27 12:15:18 +08:00
|
|
|
elif isinstance(x, (np.number, np.bool_, Number)):
|
2020-09-12 15:39:01 +08:00
|
|
|
return to_torch(np.asanyarray(x), dtype, device)
|
2020-05-29 14:45:21 +02:00
|
|
|
elif isinstance(x, dict):
|
2020-09-12 15:39:01 +08:00
|
|
|
return {k: to_torch(v, dtype, device) for k, v in x.items()}
|
2020-05-29 14:45:21 +02:00
|
|
|
elif isinstance(x, Batch):
|
2020-09-12 15:39:01 +08:00
|
|
|
x = deepcopy(x)
|
2020-05-30 15:40:31 +02:00
|
|
|
x.to_torch(dtype, device)
|
2020-09-12 15:39:01 +08:00
|
|
|
return x
|
2020-07-21 10:47:56 +02:00
|
|
|
elif isinstance(x, (list, tuple)):
|
|
|
|
try:
|
2020-09-12 15:39:01 +08:00
|
|
|
return to_torch(_parse_value(x), dtype, device)
|
2020-07-21 10:47:56 +02:00
|
|
|
except TypeError:
|
2020-09-12 15:39:01 +08:00
|
|
|
return [to_torch(e, dtype, device) for e in x]
|
2020-07-21 10:47:56 +02:00
|
|
|
else: # fallback
|
2020-08-27 12:15:18 +08:00
|
|
|
raise TypeError(f"object {x} cannot be converted to torch.")
|
2020-06-03 13:59:47 +08:00
|
|
|
|
|
|
|
|
2020-09-12 15:39:01 +08:00
|
|
|
def to_torch_as(
|
|
|
|
x: Union[Batch, dict, list, tuple, np.ndarray, torch.Tensor],
|
|
|
|
y: torch.Tensor,
|
|
|
|
) -> Union[Batch, dict, list, tuple, torch.Tensor]:
|
2020-09-11 07:55:37 +08:00
|
|
|
"""Return an object without np.ndarray.
|
|
|
|
|
|
|
|
Same as ``to_torch(x, dtype=y.dtype, device=y.device)``.
|
2020-06-03 13:59:47 +08:00
|
|
|
"""
|
|
|
|
assert isinstance(y, torch.Tensor)
|
|
|
|
return to_torch(x, dtype=y.dtype, device=y.device)
|