add hhd get_force gpu

This commit is contained in:
bluepulsar
2025-12-27 11:44:28 +08:00
parent 473e383385
commit 3f231c2c42
4 changed files with 564 additions and 8 deletions
+55 -3
View File
@@ -173,35 +173,87 @@ class Sensor:
self.imgupdate = True
return self.img
def compute_deformation(self):
def compute_deformation(self, log_timing=False):
"""
Track optical flow and decompose into force field.
Args:
log_timing: If True, print timing information for each computation step (default: False)
Returns:
tuple: (flow, force) where:
- flow: Optical flow field (2D vector field)
- force: Decomposed force components from nHHD, ([normal, shear_x, shear_y, torque]). Note: torque is experimental.
"""
if log_timing:
timings = {}
t_start = time.perf_counter()
# Step 1: Optical flow tracking
if log_timing:
t0 = time.perf_counter()
flow = self.tracker.track(self.img)
if log_timing:
timings['optical_flow'] = (time.perf_counter() - t0) * 1000
if hasattr(flow, 'download'):
flow = flow.download()
if self.hhd is None:
self.hhd = nHHD(grid=(flow.shape[0], flow.shape[1]), spacings=(18, 18))
self.hhd.decompose(flow)
if log_timing:
t0 = time.perf_counter()
# Use GPU-accelerated force computation if CUDA is enabled
if self.cuda and self.CUPY_AVAILABLE:
# Use the most optimized GPU method with custom kernel
flag, [self.fnormal,self.fshearx,self.fsheary,self.ftor] = self.hhd.get_force_gpu_v2(flow)
else:
flag, [self.fnormal,self.fshearx,self.fsheary,self.ftor] = self.hhd.get_force(flow)
# self.hhd.decompose(flow)
if log_timing:
timings['hhd_decompose'] = (time.perf_counter() - t0) * 1000
self.imgupdate = False
self.vupdate = True
self.flow = flow
if log_timing:
t0 = time.perf_counter()
self.normal = self.hhd.get_normal()
self.shear = self.hhd.get_shear()
flag, [self.fnormal,self.fshearx,self.fsheary,self.ftor] = self.hhd.get_force(flow)
# Step 8: Compute depth/density
if log_timing:
t0 = time.perf_counter()
self.depth = getFilteredDensity(self.flow, use_cuda=self.cuda)
if log_timing:
timings['get_density'] = (time.perf_counter() - t0) * 1000
# Calculate FPS
self.fps = 1/(time.time() - self.last_log_time)
self.last_log_time = time.time()
# Print timing summary
if log_timing:
timings['total'] = (time.perf_counter() - t_start) * 1000
print("\n[compute_deformation timing]")
print(f" optical_flow: {timings['optical_flow']:7.2f} ms")
print(f" hhd_decompose: {timings['hhd_decompose']:7.2f} ms")
print(f" get_density: {timings['get_density']:7.2f} ms")
print(f" ─────────────────────────")
print(f" TOTAL: {timings['total']:7.2f} ms ({1000/timings['total']:.1f} FPS)")
# Store timings for external access
self._last_timings = timings
return self.flow