-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
155 lines (119 loc) · 4.72 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
import torch
from torch._C import dtype
from typing import Dict
DTYPE_BIT_SIZE: Dict[dtype, int] = {
torch.float32: 32,
torch.float: 32,
torch.float64: 64,
torch.double: 64,
torch.float16: 16,
torch.half: 16,
torch.bfloat16: 16,
torch.complex32: 32,
torch.complex64: 64,
torch.complex128: 128,
torch.cdouble: 128,
torch.uint8: 8,
torch.int8: 8,
torch.int16: 16,
torch.short: 16,
torch.int32: 32,
torch.int: 32,
torch.int64: 64,
torch.long: 64,
torch.bool: 1
}
def to_coordinates_and_features(img):
"""Converts an image to a set of coordinates and features.
Args:
img (torch.Tensor): Shape (channels, height, width).
"""
# Coordinates are indices of all non zero locations of a tensor of ones of
# same shape as spatial dimensions of image
coordinates = torch.ones(img.shape[1:]).nonzero(as_tuple=False).float()
# Normalize coordinates to lie in [-.5, .5]
coordinates = coordinates / (img.shape[1] - 1) - 0.5
# Convert to range [-1, 1]
coordinates *= 2
# Convert image to a tensor of features of shape (num_points, channels)
features = img.reshape(img.shape[0], -1).T
return coordinates, features
def to_coordinates_and_features_2D(img):
"""Converts an image to a set of coordinates and features.
Args:
img (torch.Tensor): Shape (channels, X, Y).
"""
# Coordinates are indices of all non zero locations of a tensor of ones of
# same shape as spatial dimensions of image
# Get coordinates
d1, d2 = np.mgrid[0:img.shape[1], 0:img.shape[2]]
d1 = np.reshape(d1, (img.shape[1] * img.shape[2], 1))
d2 = np.reshape(d2, (img.shape[1] * img.shape[2], 1))
d1 = 2 * (torch.from_numpy(d1.astype(np.float32)) / (img.shape[1] - 1) - 0.5)
d2 = 2 * (torch.from_numpy(d2.astype(np.float32)) / (img.shape[2] - 1) - 0.5)
coordinates = torch.ones(img.shape[1] * img.shape[2], 2)
coordinates[:, 0] = d1[:, 0]
coordinates[:, 1] = d2[:, 0]
# Get features
features = img.reshape(img.shape[0], -1).T
return coordinates, features
def to_coordinates_and_features_3D(img):
"""Converts an image to a set of coordinates and features.
Args:
img (torch.Tensor): Shape (channels, X, Y, Z).
"""
print("Get coordinates")
d1, d2, d3 = np.mgrid[0:img.shape[1], 0:img.shape[2], 0:img.shape[3]]
d1 = np.reshape(d1, (img.shape[1] * img.shape[2] * img.shape[3], 1))
d2 = np.reshape(d2, (img.shape[1] * img.shape[2] * img.shape[3], 1))
d3 = np.reshape(d3, (img.shape[1] * img.shape[2] * img.shape[3], 1))
d1 = 2 * (torch.from_numpy(d1.astype(np.float32)) / (img.shape[1] - 1) - 0.5)
d2 = 2 * (torch.from_numpy(d2.astype(np.float32)) / (img.shape[2] - 1) - 0.5)
d3 = 2 * (torch.from_numpy(d3.astype(np.float32)) / (img.shape[3] - 1) - 0.5)
coordinates = torch.ones(img.shape[1] * img.shape[2] * img.shape[3], 3)
coordinates[:, 0] = d1[:, 0]
coordinates[:, 1] = d2[:, 0]
coordinates[:, 2] = d3[:, 0]
print("Get features")
features = img.reshape(img.shape[0], -1).T
return coordinates, features
def model_size_in_bits(model):
"""Calculate total number of bits to store `model` parameters and buffers."""
return sum(sum(t.nelement() * DTYPE_BIT_SIZE[t.dtype] for t in tensors)
for tensors in (model.parameters(), model.buffers()))
def bpp(image, model):
"""Computes size in bits per pixel of model.
Args:
image (torch.Tensor): Image to be fitted by model.
model (torch.nn.Module): Model used to fit image.
"""
num_pixels = np.prod(image.shape) / 3 # Dividing by 3 because of RGB channels
return model_size_in_bits(model=model) / num_pixels
def psnr(img1, img2):
"""Calculates PSNR between two images.
Args:
img1 (torch.Tensor):
img2 (torch.Tensor):
"""
return 20. * np.log10(1.) - 10. * (img1 - img2).detach().pow(2).mean().log10().to('cpu').item()
def clamp_image(img):
"""Clamp image values to like in [0, 1] and convert to unsigned int.
Args:
img (torch.Tensor):
"""
# Values may lie outside [0, 1], so clamp input
img_ = torch.clamp(img, 0., 1.)
# Pixel values lie in {0, ..., 255}, so round float tensor
return torch.round(img_ * 255) / 255.
def get_clamped_psnr(img, img_recon):
"""Get PSNR between true image and reconstructed image. As reconstructed
image comes from output of neural net, ensure that values like in [0, 1] and
are unsigned ints.
Args:
img (torch.Tensor): Ground truth image.
img_recon (torch.Tensor): Image reconstructed by model.
"""
return psnr(img, clamp_image(img_recon))
def mean(list_):
return np.mean(list_)