-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpore_utils.py
67 lines (44 loc) · 1.43 KB
/
pore_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
65
66
67
import numpy as np
import torch
from network_tools import scale_tensor
def get_coarsened_list(x, scales):
"""
X : 3D np array
returns a list with the desired number of coarse-grained tensors
"""
# converts to tensor and adds channel and batch dim
x = torch.Tensor(add_dims(x, 1))
ds_x = []
ds_x.append(x)
for i in range( scales-1 ):
ds_x.append( scale_tensor( ds_x[-1], scale_factor=1/2 ) )
return ds_x[::-1] # returns the reversed list (small images first)
"""
Tensor operations
"""
def changepres(x, ttype=None):
if ttype == 'f32':
return x.float()
elif ttype == 'f16':
return x.half()
def torchpres(x, ttype=None):
if isinstance(x,list) == True:
x = [torchpres(xi, ttype) for xi in x]
else:
x = changepres(x, ttype)
return x
def gpu2np(x):
if type(x) == list:
x = [gpu2np(xi) for xi in x]
else:
x = x.detach().cpu().numpy().squeeze()
return x
def add_dims(x, num_dims):
for dims in range(num_dims):
x = x[np.newaxis]
return x
def rnd_array(size, scales, device='cpu'):
return get_coarsened_list( ((torch.rand(1,
size,
size,
size)>0.5)*1.0).to(device),scales)