what.models.detection.utils.array_utils

tools to convert specified type

 1"""
 2tools to convert specified type
 3"""
 4import torch as t
 5import numpy as np
 6
 7
 8def to_numpy(data):
 9    if isinstance(data, np.ndarray):
10        return data
11    if isinstance(data, t.Tensor):
12        return data.detach().cpu().numpy()
13
14
15def to_tensor(data, device):
16    if isinstance(data, np.ndarray):
17        tensor = t.from_numpy(data)
18    if isinstance(data, t.Tensor):
19        tensor = data.detach()
20    return tensor.to(device)
21
22
23def to_scalar(data):
24    if isinstance(data, np.ndarray):
25        return data.reshape(1)[0]
26    if isinstance(data, t.Tensor):
27        return data.item()
def to_numpy(data):
 9def to_numpy(data):
10    if isinstance(data, np.ndarray):
11        return data
12    if isinstance(data, t.Tensor):
13        return data.detach().cpu().numpy()
def to_tensor(data, device):
16def to_tensor(data, device):
17    if isinstance(data, np.ndarray):
18        tensor = t.from_numpy(data)
19    if isinstance(data, t.Tensor):
20        tensor = data.detach()
21    return tensor.to(device)
def to_scalar(data):
24def to_scalar(data):
25    if isinstance(data, np.ndarray):
26        return data.reshape(1)[0]
27    if isinstance(data, t.Tensor):
28        return data.item()