-
Notifications
You must be signed in to change notification settings - Fork 16
/
inference.py
215 lines (168 loc) · 6.53 KB
/
inference.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import argparse
import math
import numpy as np
import socket
import importlib
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import sys
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.backends import cudnn
import cv2
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import KDTree
from torchvision import transforms, utils
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
from loading_pointclouds import *
import models.DiSCO as SC
from tensorboardX import SummaryWriter
import loss.loss_function
import gputransform
import config as cfg
import scipy.io as scio
cudnn.enabled = True
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def infer(input_filename):
query = load_pc_file_infer(input_filename)
query = np.array(query, dtype=np.float32)
model = SC.DiSCO(output_dim=cfg.FEATURE_OUTPUT_DIM)
corr2soft = SC.Corr2Softmax(200., 0.)
corr2soft = corr2soft.to(device)
model = model.to(device)
resume_filename = cfg.LOG_DIR + cfg.MODEL_FILENAME
print("Resuming From ", resume_filename)
checkpoint = torch.load(resume_filename)
saved_state_dict = checkpoint['state_dict']
saved_corr2soft_dict = checkpoint['corr2soft']
model.load_state_dict(saved_state_dict)
corr2soft.load_state_dict(saved_corr2soft_dict)
model = nn.DataParallel(model)
out, _, _, _ = infer_model(model, corr2soft, query)
out_show = out.reshape(1, 32, 32)
imshow(out_show)
# print("output descriptor: ",out)
# np.save("./output_des.npy", out.cpu().numpy())
return out
def infer_model(model, corr2soft, query):
model.eval()
corr2soft.eval()
is_training = False
with torch.no_grad():
feed_tensor = torch.from_numpy(query).float()
feed_tensor = feed_tensor.to(device)
feed_tensor = feed_tensor.view((-1, cfg.num_height, cfg.num_ring, cfg.num_sector))
out, outfft, fft_result, unet_out = model(feed_tensor)
model.train()
return out, outfft, fft_result, unet_out
def imshow(tensor, title=None):
unloader = transforms.ToPILImage()
image = tensor.cpu().clone() # we clone the tensor to not do changes on it
image = image.squeeze(0) # remove the fake batch dimension
image = unloader(image)
plt.imshow(image, cmap='jet')
plt.show()
def GT_sc_angle_convert(gt_yaw, size):
gt_yaw = gt_yaw % 360
if gt_yaw > 180:
gt_yaw -= 360
elif gt_yaw < -180:
gt_yaw += 360
gt_angle = gt_yaw
for batch_num in range(gt_angle.shape[0]):
if gt_angle[batch_num] <= -180.:
gt_angle[batch_num] = gt_angle[batch_num] + 540.
elif gt_angle[batch_num] >= 180.:
gt_angle[batch_num] = gt_angle[batch_num] - 180.
else:
gt_angle[batch_num] = gt_angle[batch_num] + 180.
gt_angle = np.ceil(gt_angle * float(cfg.num_sector) / 360.) - 1.
return gt_angle
def fftshift2d(x):
for dim in range(1, len(x.size())):
n_shift = x.size(dim)//2
if x.size(dim) % 2 != 0:
n_shift = n_shift + 1 # for odd-sized images
x = roll_n(x, axis=dim, n=n_shift)
return x # last dim=2 (real&imag)
def roll_n(X, axis, n):
f_idx = tuple(slice(None, None, None) if i != axis else slice(0, n, None) for i in range(X.dim()))
b_idx = tuple(slice(None, None, None) if i != axis else slice(n, None, None) for i in range(X.dim()))
front = X[f_idx]
back = X[b_idx]
return torch.cat([back, front], axis)
def phase_corr(a, b, device, corr2soft):
# a: template; b: source
# imshow(a.squeeze(0).float())
# [B, 1, cfg.num_ring, cfg.num_sector, 2]
eps = 1e-15
real_a = torch.from_numpy(a[...,0]).to(device)
real_b = torch.from_numpy(b[...,0]).to(device)
imag_a = torch.from_numpy(a[...,1]).to(device)
imag_b = torch.from_numpy(b[...,1]).to(device)
# compute a * b.conjugate; shape=[B,H,W,C]
R = torch.FloatTensor(1, 1, cfg.num_ring, cfg.num_sector, 2).to(device)
R[...,0] = real_a * real_b + imag_a * imag_b
R[...,1] = real_a * imag_b - real_b * imag_a
r0 = torch.sqrt(real_a ** 2 + imag_a ** 2 + eps) * torch.sqrt(real_b ** 2 + imag_b ** 2 + eps).to(device)
R[...,0] = R[...,0].clone()/(r0 + eps).to(device)
R[...,1] = R[...,1].clone()/(r0 + eps).to(device)
corr = torch.ifft(R, 2)
corr_real = corr[...,0]
corr_imag = corr[...,1]
corr = torch.sqrt(corr_real ** 2 + corr_imag ** 2 + eps)
corr = fftshift2d(corr)
corr = corr.squeeze(1)
corr_wb = corr2soft(corr)
corr_ang = torch.sum(corr_wb, 1, keepdim=False)
angle = torch.argmax(corr)
angle = angle % cfg.num_sector
return angle, corr
def rotation_on_SCI(sc, rotation):
# rotation to translation [-180:180] -> [-cfg.num_sector//2:cfg.num_sector//2]
if rotation > 0:
t = rotation / 180. * (cfg.num_sector // 2)
t = np.floor(t).astype(int)
patch = sc[:, (cfg.num_sector-t):cfg.num_sector]
col, row = cfg.num_sector, cfg.num_ring
center = (col // 2, row // 2)
t_x, t_y = t, 0.
M = cv2.getRotationMatrix2D(center, 0.0, 1.0)
sc = cv2.warpAffine(sc, M, (col, row))
N = np.float32([[1,0,t_x],[0,1,t_y]])
sc = cv2.warpAffine(sc, N, (col, row))
sc[:, 0:t] = patch
else:
t = -rotation / 180. * (cfg.num_sector // 2)
t = np.floor(t).astype(int)
patch = sc[:, 0:t]
col, row = cfg.num_sector, cfg.num_ring
center = (col // 2, row // 2)
t_x, t_y = -t, 0.
M = cv2.getRotationMatrix2D(center, 0.0, 1.0)
sc = cv2.warpAffine(sc, M, (col, row))
N = np.float32([[1,0,t_x],[0,1,t_y]])
sc = cv2.warpAffine(sc, N, (col, row))
sc[:, (cfg.num_sector-t):cfg.num_sector] = patch
return sc
if __name__ == "__main__":
# params
parser = argparse.ArgumentParser()
parser.add_argument('--input_filename', default='./test.bin',
help='input file name [default: ./test.bin]')
parser.add_argument('--dimension', type=int, default=1024)
parser.add_argument('--input_type', default='point',
help='Input of the network, can be [point] or scan [image], [default: point]')
FLAGS = parser.parse_args()
cfg.INPUT_FILENAME = FLAGS.input_filename
cfg.FEATURE_OUTPUT_DIM = 1024
cfg.num_ring = 40
cfg.num_sector = 120
cfg.num_height = 20
cfg.max_length = 1
cfg.LOG_DIR = './log/'
cfg.MODEL_FILENAME = "model.ckpt"
cfg.INPUT_TYPE = FLAGS.input_type
disco = infer(cfg.INPUT_FILENAME)