-
Notifications
You must be signed in to change notification settings - Fork 15
/
wire_occupancy.py
186 lines (142 loc) · 5.46 KB
/
wire_occupancy.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
180
181
182
183
184
185
186
#!/usr/bin/env python
import os
import sys
import glob
import tqdm
import importlib
import time
import pdb
import copy
import numpy as np
from scipy import io
from scipy import ndimage
import cv2
import torch
from torch.optim.lr_scheduler import LambdaLR
import matplotlib.pyplot as plt
plt.gray()
from modules import models
from modules import utils
from modules import volutils
if __name__ == '__main__':
nonlin = 'wire' # type of nonlinearity, 'wire', 'siren', 'mfn', 'relu', 'posenc', 'gauss'
niters = 200 # Number of SGD iterations
learning_rate = 5e-3 # Learning rate
expname = 'thai_statue' # Volume to load
scale = 1.0 # Run at lower scales to testing
mcubes_thres = 0.5 # Threshold for marching cubes
# Gabor filter constants
# These settings work best for 3D occupancies
omega0 = 10.0 # Frequency of sinusoid
sigma0 = 40.0 # Sigma of Gaussian
# Network constants
hidden_layers = 2 # Number of hidden layers in the mlp
hidden_features = 256 # Number of hidden units per layer
maxpoints = int(2e5) # Batch size
if expname == 'thai_statue':
occupancy = True
else:
occupancy = False
# Load image and scale
im = io.loadmat('data/%s.mat'%expname)['hypercube'].astype(np.float32)
im = ndimage.zoom(im/im.max(), [scale, scale, scale], order=0)
# If the volume is an occupancy, clip to tightest bounding box
if occupancy:
hidx, widx, tidx = np.where(im > 0.99)
im = im[hidx.min():hidx.max(),
widx.min():widx.max(),
tidx.min():tidx.max()]
print(im.shape)
H, W, T = im.shape
maxpoints = min(H*W*T, maxpoints)
imten = torch.tensor(im).cuda().reshape(H*W*T, 1)
if nonlin == 'posenc':
nonlin = 'relu'
posencode = True
else:
posencode = False
# Create model
model = models.get_INR(
nonlin=nonlin,
in_features=3,
out_features=1,
hidden_features=hidden_features,
hidden_layers=hidden_layers,
first_omega_0=omega0,
hidden_omega_0=omega0,
scale=sigma0,
pos_encode=posencode,
sidelength=max(H, W, T)).cuda()
# Optimizer
optim = torch.optim.Adam(lr=learning_rate, params=model.parameters())
# Schedule to 0.1 times the initial rate
scheduler = LambdaLR(optim, lambda x: 0.2**min(x/niters, 1))
criterion = torch.nn.MSELoss()
# Create inputs
coords = utils.get_coords(H, W, T)
mse_array = np.zeros(niters)
time_array = np.zeros(niters)
best_mse = float('inf')
best_img = None
tbar = tqdm.tqdm(range(niters))
im_estim = torch.zeros((H*W*T, 1), device='cuda')
tic = time.time()
print('Running %s nonlinearity'%nonlin)
for idx in tbar:
indices = torch.randperm(H*W*T)
train_loss = 0
nchunks = 0
for b_idx in range(0, H*W*T, maxpoints):
b_indices = indices[b_idx:min(H*W*T, b_idx+maxpoints)]
b_coords = coords[b_indices, ...].cuda()
b_indices = b_indices.cuda()
pixelvalues = model(b_coords[None, ...]).squeeze()[:, None]
with torch.no_grad():
im_estim[b_indices, :] = pixelvalues
loss = criterion(pixelvalues, imten[b_indices, :])
optim.zero_grad()
loss.backward()
optim.step()
lossval = loss.item()
train_loss += lossval
nchunks += 1
if occupancy:
mse_array[idx] = volutils.get_IoU(im_estim, imten, mcubes_thres)
else:
mse_array[idx] = train_loss/nchunks
time_array[idx] = time.time()
scheduler.step()
im_estim_vol = im_estim.reshape(H, W, T)
if lossval < best_mse:
best_mse = lossval
best_img = copy.deepcopy(im_estim)
if sys.platform == 'win32':
cv2.imshow('GT', im[..., idx%T])
cv2.imshow('Estim', im_estim_vol[..., idx%T].detach().cpu().numpy())
cv2.waitKey(1)
tbar.set_description('%.4e'%mse_array[idx])
tbar.refresh()
total_time = time.time() - tic
nparams = utils.count_parameters(model)
best_img = best_img.reshape(H, W, T).detach().cpu().numpy()
if posencode:
nonlin = 'posenc'
# Save data
os.makedirs('results/%s'%expname, exist_ok=True)
indices, = np.where(time_array > 0)
time_array = time_array[indices]
mse_array = mse_array[indices]
mdict = {'mse_array': mse_array,
'time_array': time_array-time_array[0],
'nparams': utils.count_parameters(model)}
io.savemat('results/%s/%s.mat'%(expname, nonlin), mdict)
# Generate a mesh with marching cubes if it is an occupancy volume
if occupancy:
savename = 'results/%s/%s.dae'%(expname, nonlin)
volutils.march_and_save(best_img, mcubes_thres, savename, True)
print('Total time %.2f minutes'%(total_time/60))
if occupancy:
print('IoU: ', volutils.get_IoU(best_img, im, mcubes_thres))
else:
print('PSNR: ', utils.psnr(im, best_img))
print('Total pararmeters: %.2f million'%(nparams/1e6))