Skip to content

Commit 4fcfbe0

Browse files
committed
Update
1 parent d080d67 commit 4fcfbe0

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

scripts/benchmark.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import time
2+
import numpy as np
3+
4+
5+
def method1(points, R, t):
6+
return np.dot(points, R.T) + t
7+
8+
9+
def method2(hpoints, tf):
10+
transformed_points = np.dot(hpoints, tf.T)
11+
return transformed_points[:, :3]
12+
13+
14+
def benchmark(N, repetitions=100):
15+
# Generate random Nx3 points
16+
points = np.random.rand(N, 3)
17+
ones = np.ones((points.shape[0], 1))
18+
hpoints = np.hstack((points, ones))
19+
20+
# Create random rotation matrix and translation vector
21+
R = np.random.rand(3, 3)
22+
t = np.random.rand(3)
23+
24+
# Create a 4x4 homogeneous transformation matrix
25+
tf = np.eye(4)
26+
tf[:3, :3] = R
27+
tf[:3, 3] = t
28+
29+
# Benchmark Method 1
30+
start_time = time.time()
31+
for _ in range(repetitions):
32+
method1(points, R, t)
33+
time_method1 = (time.time() - start_time) / repetitions
34+
35+
# Benchmark Method 2
36+
start_time = time.time()
37+
for _ in range(repetitions):
38+
method2(hpoints, tf)
39+
time_method2 = (time.time() - start_time) / repetitions
40+
41+
print(f"Mean time for Method 1 (SO3 + R3): {time_method1:.2e} [s]")
42+
print(f"Mean time for Method 2 (SE3): {time_method2:.2e} [s]")
43+
44+
45+
# Run the benchmark
46+
benchmark(N=100000000)

scripts/fast_projector.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
import multiprocessing as mp
3+
4+
import numpy as np
5+
6+
7+
def project_points(hpts_world, K):
8+
uvd = hpts_world[:, :3] @ K.T
9+
# uvd /= uvd[:, 2]
10+
return uvd
11+
12+
13+
def process_chunk(start_idx, chunk_size, hpts_world, K):
14+
num_points = hpts_world.shape[0]
15+
end_idx = min(start_idx + chunk_size, num_points)
16+
return project_points(hpts_world[start_idx:end_idx, :], K)
17+
18+
19+
def project_points_multiprocessing(hpts_world, K, num_procs=3):
20+
num_points = hpts_world.shape[0]
21+
chunk_size = num_points // num_procs
22+
23+
with mp.Pool(processes=num_procs) as pool:
24+
results = [
25+
pool.apply_async(
26+
process_chunk,
27+
args=(i * chunk_size, chunk_size, hpts_world, K),
28+
) for i in range(num_procs)
29+
]
30+
output = np.vstack([result.get() for result in results])
31+
32+
return output
33+
34+
35+
if __name__ == "__main__":
36+
# Setup
37+
N = 100000
38+
pts_world = np.random.rand(N, 3)
39+
hpts_world = np.hstack((pts_world, np.ones((N, 1))))
40+
K = np.array([[1000, 0, 640], [0, 1000, 360], [0, 0, 1]])
41+
42+
# Project points in a multiprocessing way
43+
uvd = project_points_multiprocessing(hpts_world, K, num_procs=8)
44+
print(uvd)

0 commit comments

Comments
 (0)