-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
36 lines (25 loc) · 804 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
import numpy as np
def o2t(obj):
if isinstance(obj, torch.Tensor):
return obj
elif isinstance(obj, np.ndarray):
return torch.from_numpy(obj)
elif isinstance(obj, list):
return torch.FloatTensor(obj)
else:
raise AttributeError('Not supported type: {}'.format(type(obj)))
def i2t(image):
image = np.expand_dims(image, axis=0)
if len(image.shape) == 3:
image = np.expand_dims(image, axis=0)
else:
image = np.transpose(image, axes=(0, 3, 1, 2))
return image
def t2i(tensor):
if isinstance(tensor, torch.Tensor):
tensor = tensor.detach().numpy()
tensor = np.squeeze(tensor)
if len(tensor.shape) == 3:
tensor = np.transpose(tensor, axes=(1, 2, 0))
return tensor