-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerf_cone_motion.py
executable file
·179 lines (169 loc) · 8.69 KB
/
nerf_cone_motion.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# It is still in BETA!
# By Wangyukang Mar 1, 2024
import numpy as np
import torch
import torch.nn as nn
import tqdm
import time
from JITSelfCalibration import differentiableConeGradient as dffg
import ConeBeamLayers.BeijingGeometry as geometry
from ConeBeamLayers.BeijingGeometry import ForwardProjection
from model import model
from options import beijingVolumeSize
from options import beijingAngleNum, beijingPlanes
DEBUG_NERF = True
device = "cuda:1"
for m in model:
m = m.to(device)
m.train()
def getProjectionMatrix(projectionMatrix):
s,d,u,v = projectionMatrix[:,0:3], projectionMatrix[:,3:6], projectionMatrix[:,6:9], projectionMatrix[:,9:12]
posTrans = lambda x: x[:,[1,2,0]]
normU = torch.norm(u, dim=1).unsqueeze(-1)
normV = torch.norm(v, dim=1).unsqueeze(-1)
s = posTrans(s)
d = posTrans(d)
u = posTrans(u) / normU
v = posTrans(v) / normV
sod = torch.cross(u, v, dim=1)
sod /= torch.norm(sod, dim=1, keepdim=True)
mr = torch.inverse(torch.stack((u, v, sod), dim=2))
vt = torch.matmul(-mr, s.unsqueeze(-1)).squeeze(-1)
mrt = torch.cat((mr, vt.unsqueeze(-1)), dim=2)
ktemp = torch.matmul(mr, d.unsqueeze(-1)).squeeze(-1) + vt
mk = torch.zeros_like(mr)
mk[:,0,2] = -ktemp[:,0]
mk[:,1,2] = -ktemp[:,1]
mk[:,0,0] = ktemp[:,2]
mk[:,1,1] = ktemp[:,2]
mk[:,2,2] = 1
return mk @ mrt, normU, normV
def getResultParams(projectionMatrix, normU, normV):
N = projectionMatrix.shape[0]
_, r = torch.linalg.qr(torch.inverse(projectionMatrix[:,:,:3]), mode="r")
mk = torch.inverse(r)
mk /= mk[:,2,2].clone().unsqueeze(-1).unsqueeze(-1)
mk[:,:,0] *= torch.sign(mk[:,0,0]).unsqueeze(1)
mk[:,:,1] *= torch.sign(mk[:,1,1]).unsqueeze(1)
mrt = torch.inverse(mk) @ projectionMatrix
s = torch.linalg.solve(mrt[:,:,:3], -mrt[:,:,3])
d = torch.linalg.solve(mrt[:,:,:3], torch.stack((-mk[:,0,2], -mk[:,1,2], mk[:,0,0]), dim=1)-mrt[:,:,3])
u = torch.linalg.solve(mrt[:,:,:3], torch.stack((torch.ones(N), torch.zeros(N), torch.zeros(N)), dim=1).to(projectionMatrix.device))
v = torch.linalg.solve(mrt[:,:,:3], torch.stack((torch.zeros(N), torch.ones(N), torch.zeros(N)), dim=1).to(projectionMatrix.device))
posTrans = lambda x: x[:,[2,0,1]]
s = posTrans(s)
d = posTrans(d)
u = posTrans(u) * normU
v = posTrans(v) * normV
return torch.cat((s,d,u,v), dim=1)
def plotResult(params, path):
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(params[:,0],params[:,1],params[:,2], color='red', label='Sources')
ax.scatter(params[:,3],params[:,4],params[:,5], color='blue', label='Detectors')
ax.set_axis_off()
ax.legend()
plt.savefig(path)
plt.close('all')
class ProjectionGeom(torch.autograd.Function):
normU = 1
normV = 1
@staticmethod
def forward(ctx, input, projectionMatrix, label):
geometry.parameters = getResultParams(projectionMatrix, ProjectionGeom.normU, ProjectionGeom.normV)
result = ForwardProjection.apply(output.reshape(1, 1,beijingVolumeSize[2],beijingVolumeSize[1],beijingVolumeSize[0]))
residual = label - input.reshape(label.shape)
ctx.save_for_backward(projectionMatrix, result, residual)
return torch.autograd.Variable(result, requires_grad=True)
@staticmethod
def backward(ctx, grad):
# Beta
projectionMatrix, sino, residual = ctx.saved_tensors
gMatrix = torch.zeros_like(projectionMatrix)
gsinoX, = torch.gradient(sino, dim=3)
gsinoY, = torch.gradient(sino, dim=4)
gsinoX = gsinoX[...,::beijingPlanes,:,:].contiguous()
gsinoY = gsinoY[...,::beijingPlanes,:,:].contiguous()
projectionMatrix = projectionMatrix[...,::beijingPlanes,:,:]
residual = residual[...,20:36,:,:].contiguous()
gMatrix[...,::beijingPlanes,:,:] = dffg(gsinoX, gsinoY, residual, projectionMatrix.contiguous())
# gMatrix = dffg(gsinoX, gsinoY, residual, projectionMatrix.contiguous())
return None, gMatrix, None
# Beta
def constuctProjectionMatrix(angles, trans):
return torch.stack([torch.cos(angles), -torch.sin(angles), torch.zeros_like(angles) * trans[:,0],torch.zeros_like(angles) * trans[:,0],
torch.sin(angles), torch.cos(angles), torch.zeros_like(angles) * trans[:,1],torch.zeros_like(angles) * trans[:,0],
torch.zeros_like(angles), torch.zeros_like(angles), torch.ones_like(angles),torch.zeros_like(angles) * trans[:,1],
torch.zeros_like(angles), torch.zeros_like(angles), torch.zeros_like(angles),torch.ones_like(angles) * trans[:,1]
]).view(4,4,-1).permute(2,0,1).to(torch.float32).to(device)
def build_coordinate_test(L):
input = torch.zeros(L, L, 2)
value_xy = np.linspace(-1,1,L)
for x in range(L):
for y in range(L):
input[x,y,0] = value_xy[x]
input[x,y,1] = value_xy[y]
return input.reshape(-1, 2).to(device)
if __name__ == '__main__':
# projectionPath = "/home/nv/wyk/Data/lz/projection.raw"
labelPath = "/home/nv/wyk/Data/SheppLogan.raw"
outputPath = "/home/nv/wyk/Data/output.raw"
label = np.fromfile(labelPath, dtype="float32").reshape(1,1,64,512,512)
label = torch.from_numpy(label).to(device)
geometry.parameters[0:1000,:6] += 10
projection = ForwardProjection.apply(label)
np.save("/home/nv/wyk/Data/std_params.npy", geometry.parameters.detach().cpu().numpy())
geometry.parameters[0:1000,:6] -= 10
# projection = np.fromfile(projectionPath, dtype="float32")
# projection = torch.from_numpy(projection).reshape(1,1,1080*21,128,80).to(device)
# projection[torch.isnan(projection)] = 0
# projection = projection[...,2:78]
bytelen = beijingVolumeSize[0]*beijingVolumeSize[1]
input = build_coordinate_test(beijingVolumeSize[0])
lossFunction = nn.MSELoss()
optimizer = [torch.optim.Adam(m.parameters(), lr=1e-3) for m in model]
scheduler = [torch.optim.lr_scheduler.StepLR(o, step_size=50, gamma=0.95) for o in optimizer]
projectionMatrixInit, normU, normV = getProjectionMatrix(geometry.parameters)
projectionMatrixInit = projectionMatrixInit.to(device)
ProjectionGeom.normU = normU.to(device)
ProjectionGeom.normV = normV.to(device)
anglesNum = beijingAngleNum * beijingPlanes
projectionMatrixCorrAngle = torch.autograd.Variable(torch.tensor([0.0] * anglesNum).to(device), requires_grad=True)
projectionMatrixCorrTrans = torch.autograd.Variable(torch.tensor([[1.0,1.0]] * anglesNum).to(device), requires_grad=True)
optimizerCorr = torch.optim.SGD([projectionMatrixCorrAngle, projectionMatrixCorrTrans], lr=1e-2)
tic = time.time()
for iph in range(2):
with tqdm.trange(51) as epochs:
for e in epochs:
output = torch.zeros(beijingVolumeSize[2], bytelen, 1).to(device)
for s in range(beijingVolumeSize[2]):
output[s,...] = model[s](input).float()
output_projection = ForwardProjection.apply(output.reshape(1, 1,beijingVolumeSize[2],beijingVolumeSize[1],beijingVolumeSize[0]))
loss = lossFunction(output_projection, projection)
loss.backward()
for s in range(beijingVolumeSize[2]):
optimizer[s].step()
optimizer[s].zero_grad()
scheduler[s].step()
if e%10==0:
output.detach().cpu().numpy().astype("float32").tofile(outputPath)
epochs.set_description(f"Learning projections ...")
epochs.set_postfix({"loss": loss.item()})
print("geometry finetune ...")
projectionMatrixCorr = constuctProjectionMatrix(projectionMatrixCorrAngle, projectionMatrixCorrTrans)
projectionMatrixCorr = projectionMatrixCorr.reshape(anglesNum, 4, 4)
projectionMatrix = projectionMatrixInit @ projectionMatrixCorr
output_sino = ProjectionGeom.apply(output, projectionMatrix, label)
loss = lossFunction(output_sino, projection)
loss.backward()
optimizerCorr.step()
optimizerCorr.zero_grad()
geometry.parameters[:1000, :6] = getResultParams(projectionMatrixInit @ projectionMatrixCorr, ProjectionGeom.normU, ProjectionGeom.normV)[:1000, :6]
print("Training successfully. Now we are going to render...")
output = torch.zeros(beijingVolumeSize[2], bytelen, 1).to(device)
for s in range(beijingVolumeSize[2]):
output[s,...] = model[s](input).float()
output.detach().cpu().numpy().astype("float32").tofile(outputPath)
np.save("/home/nv/wyk/Data/params.npy", geometry.parameters.detach().cpu().numpy())
print(f"Success. Render output is saved in {outputPath}. Total time cost:{time.time() - tic}s")