-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.py
77 lines (63 loc) · 2.12 KB
/
util.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
68
69
70
71
72
73
74
75
76
77
import pickle as pk
import sys
import torchvision
from torchvision import transforms
############################################################
### IO
############################################################
class NormalizeInverse(torchvision.transforms.Normalize):
"""
Undoes the normalization and returns the reconstructed images in the input domain.
"""
def __init__(self, mean, std):
mean = torch.as_tensor(mean)
std = torch.as_tensor(std)
std_inv = 1 / (std + 1e-7)
mean_inv = -mean * std_inv
super().__init__(mean=mean_inv, std=std_inv)
def __call__(self, tensor):
return super().__call__(tensor.clone())
def disp_to_term(msg):
sys.stdout.write(msg + '\r')
sys.stdout.flush()
def load_pickle(filename):
try:
p = open(filename, 'r')
except IOError:
print ("Pickle file cannot be opened.")
return None
try:
picklelicious = pk.load(p)
except ValueError:
print ('load_pickle failed once, trying again')
p.close()
p = open(filename, 'r')
picklelicious = pk.load(p)
p.close()
return picklelicious
def save_pickle(data_object, filename):
pickle_file = open(filename, 'w')
pk.dump(data_object, pickle_file)
pickle_file.close()
def unnormalize(y, mean, std):
x = y.new(*y.size())
x[:, 0, :, :] = y[:, 0, :, :] * std[0] + mean[0]
x[:, 1, :, :] = y[:, 1, :, :] * std[1] + mean[1]
x[:, 2, :, :] = y[:, 2, :, :] * std[2] + mean[2]
return x
def data_mean_std(train_data_gen):
pop_mean = []
pop_std = []
for inputs in train_data_gen:
# shape (batch_size, 3, height, width)
data , _ = inputs
numpy_image = data.numpy()
# shape (3,)
batch_mean = np.mean(numpy_image, axis=(0,2,3))
batch_std = np.std(numpy_image, axis=(0,2,3), ddof=1)
pop_mean.append(batch_mean)
pop_std.append(batch_std)
# shape (num_iterations, 3) -> (mean across 0th axis) -> shape (3,)
pop_mean = np.array(pop_mean).mean(axis=0)
pop_std = np.array(pop_std).mean(axis=0)
return pop_mean, pop_std