forked from Xinjie-Q/GaussianImage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
128 lines (109 loc) · 4.25 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
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
import os
import torch.nn.functional as F
from pytorch_msssim import ms_ssim, ssim
import torch
import numpy as np
class LogWriter:
def __init__(self, file_path, train=True):
os.makedirs(file_path, exist_ok=True)
self.file_path = os.path.join(file_path, "train.txt" if train else "test.txt")
def write(self, text):
# 打印到控制台
print(text)
# 追加到文件
with open(self.file_path, 'a') as file:
file.write(text + '\n')
def loss_fn(pred, target, loss_type='L2', lambda_value=0.7):
target = target.detach()
pred = pred.float()
target = target.float()
if loss_type == 'L2':
loss = F.mse_loss(pred, target)
elif loss_type == 'L1':
loss = F.l1_loss(pred, target)
elif loss_type == 'SSIM':
loss = 1 - ssim(pred, target, data_range=1, size_average=True)
elif loss_type == 'Fusion1':
loss = lambda_value * F.mse_loss(pred, target) + (1-lambda_value) * (1 - ssim(pred, target, data_range=1, size_average=True))
elif loss_type == 'Fusion2':
loss = lambda_value * F.l1_loss(pred, target) + (1-lambda_value) * (1 - ssim(pred, target, data_range=1, size_average=True))
elif loss_type == 'Fusion3':
loss = lambda_value * F.mse_loss(pred, target) + (1-lambda_value) * F.l1_loss(pred, target)
elif loss_type == 'Fusion4':
loss = lambda_value * F.l1_loss(pred, target) + (1-lambda_value) * (1 - ms_ssim(pred, target, data_range=1, size_average=True))
elif loss_type == 'Fusion_hinerv':
loss = lambda_value * F.l1_loss(pred, target) + (1-lambda_value) * (1 - ms_ssim(pred, target, data_range=1, size_average=True, win_size=5))
return loss
def strip_lowerdiag(L):
if L.shape[1] == 3:
uncertainty = torch.zeros((L.shape[0], 6), dtype=torch.float, device="cuda")
uncertainty[:, 0] = L[:, 0, 0]
uncertainty[:, 1] = L[:, 0, 1]
uncertainty[:, 2] = L[:, 0, 2]
uncertainty[:, 3] = L[:, 1, 1]
uncertainty[:, 4] = L[:, 1, 2]
uncertainty[:, 5] = L[:, 2, 2]
elif L.shape[1] == 2:
uncertainty = torch.zeros((L.shape[0], 3), dtype=torch.float, device="cuda")
uncertainty[:, 0] = L[:, 0, 0]
uncertainty[:, 1] = L[:, 0, 1]
uncertainty[:, 2] = L[:, 1, 1]
return uncertainty
def strip_symmetric(sym):
return strip_lowerdiag(sym)
def build_rotation(r):
norm = torch.sqrt(r[:,0]*r[:,0] + r[:,1]*r[:,1] + r[:,2]*r[:,2] + r[:,3]*r[:,3])
q = r / norm[:, None]
R = torch.zeros((q.size(0), 3, 3), device='cuda')
r = q[:, 0]
x = q[:, 1]
y = q[:, 2]
z = q[:, 3]
R[:, 0, 0] = 1 - 2 * (y*y + z*z)
R[:, 0, 1] = 2 * (x*y - r*z)
R[:, 0, 2] = 2 * (x*z + r*y)
R[:, 1, 0] = 2 * (x*y + r*z)
R[:, 1, 1] = 1 - 2 * (x*x + z*z)
R[:, 1, 2] = 2 * (y*z - r*x)
R[:, 2, 0] = 2 * (x*z - r*y)
R[:, 2, 1] = 2 * (y*z + r*x)
R[:, 2, 2] = 1 - 2 * (x*x + y*y)
return R
def build_scaling_rotation(s, r):
L = torch.zeros((s.shape[0], 3, 3), dtype=torch.float, device="cuda")
R = build_rotation(r)
L[:,0,0] = s[:,0]
L[:,1,1] = s[:,1]
L[:,2,2] = s[:,2]
L = R @ L
return L
def build_rotation_2d(r):
'''
Build rotation matrix in 2D.
'''
R = torch.zeros((r.size(0), 2, 2), device='cuda')
R[:, 0, 0] = torch.cos(r)[:, 0]
R[:, 0, 1] = -torch.sin(r)[:, 0]
R[:, 1, 0] = torch.sin(r)[:, 0]
R[:, 1, 1] = torch.cos(r)[:, 0]
return R
def build_scaling_rotation_2d(s, r, device):
L = torch.zeros((s.shape[0], 2, 2), dtype=torch.float, device='cuda')
R = build_rotation_2d(r, device)
L[:,0,0] = s[:,0]
L[:,1,1] = s[:,1]
L = R @ L
return L
def build_covariance_from_scaling_rotation_2d(scaling, scaling_modifier, rotation, device):
'''
Build covariance metrix from rotation and scale matricies.
'''
L = build_scaling_rotation_2d(scaling_modifier * scaling, rotation, device)
actual_covariance = L @ L.transpose(1, 2)
return actual_covariance
def build_triangular(r):
R = torch.zeros((r.size(0), 2, 2), device=r.device)
R[:, 0, 0] = r[:, 0]
R[:, 1, 0] = r[:, 1]
R[:, 1, 1] = r[:, 2]
return R