-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
64 lines (42 loc) · 1.73 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from cv2 import cvtColor, COLOR_RGB2BGR
from numpy import around, array, ndarray, uint8
from os import makedirs
from os.path import exists
from pickle import dump, load
from torch import device, from_numpy, Tensor
from torch.nn import Module
def create_dir(path: str):
if not exists(path=path):
makedirs(name=path)
def get_device(module: Module) -> device:
return next(module.parameters()).device
def convert_list_to_tensor(list: list) -> Tensor:
return convert_numpy_to_tensor(ndarray=array(object=list))
def convert_tensor_to_list(tensor: Tensor) -> Tensor:
return convert_tensor_to_numpy(tensor=tensor).tolist()
def convert_tensor_to_numpy(tensor: Tensor) -> ndarray:
return tensor.detach().cpu().numpy()
def convert_numpy_to_tensor(ndarray: ndarray) -> Tensor:
return from_numpy(ndarray)
def convert_tensor_image_to_opencv_image(image: Tensor) -> ndarray:
image = unormalize_image(image=convert_tensor_to_numpy(tensor=image))
if len(image.shape) == 3:
image = image.transpose(1, 2, 0)
image = cvtColor(src=image, code=COLOR_RGB2BGR)
return image
def unormalize_image(image: ndarray) -> ndarray:
return uint8(around(a=image * 255))
def save_to_pickle(object: object, path: str) -> None:
pkl = open(file=path, mode='wb')
dump(obj=object, file=pkl)
pkl.close()
def load_from_pickle(path: str) -> object:
pkl = open(file=path, mode='rb')
object = load(file=pkl)
pkl.close()
return object
def max_min_normalize(input: ndarray) -> tuple:
input_max = input.max()
input_min = input.min()
output = (input - input_min) / (input_max - input_min)
return input_max, input_min, output