NVlabs / curobo

CUDA Accelerated Robot Library
https://curobo.org
Other
796 stars 125 forks source link

Inconsistent Data Handling in get_spline_interpolated_trajectory Function #365

Open robertkim1221 opened 3 months ago

robertkim1221 commented 3 months ago

If it’s not a bug, please use discussions: https://github.com/NVlabs/curobo/discussions

Please provide the below information in addition to your issue:

  1. cuRobo installation mode (choose from [python, isaac sim, docker python, docker isaac sim]): Isaac Sim
  2. python version: Python 3.10.13
  3. Isaac Sim version (if using): 2023.1.1

While using the get_spline_interpolated_trajectory function in the CuRobo library, I encountered an issue where the code converts tensors to numpy arrays and then expects a tensor object later on. This leads to errors when the function tries to access attributes like .device which are not available for numpy arrays.

Expected Behavior

The function should handle the trajectory data as tensors throughout its execution, maintaining compatibility with PyTorch's tensor operations.

Actual Behavior

The function converts the trajectory data to a numpy array using .cpu().numpy(), which results in the following error when attempting to access .device on the array:

Relevant Code

The issue occurs in the get_spline_interpolated_trajectory function in the trajectory.py file and bspline function in the sample_lib.py file:


def get_spline_interpolated_trajectory(raw_traj, des_horizon, degree=5):
    retimed_traj = torch.zeros((des_horizon, raw_traj.shape[-1]))
    tensor_args = TensorDeviceType(device=raw_traj.device, dtype=raw_traj.dtype)
    cpu_traj = raw_traj.cpu().numpy()

    for i in range(cpu_traj.shape[-1]):
        retimed_traj[:, i] = bspline(cpu_traj[:, i], n=des_horizon, degree=degree)
    retimed_traj = retimed_traj.to(**(tensor_args.as_torch_dict()))
    return retimed_traj

def bspline(c_arr, t_arr=None, n=100, degree=3):
    sample_device = c_arr.device
    sample_dtype = c_arr.dtype
    cv = c_arr.cpu().numpy()

    if t_arr is None:
        t_arr = np.linspace(0, cv.shape[0], cv.shape[0])
    else:
        t_arr = t_arr.cpu().numpy()
    spl = si.splrep(t_arr, cv, k=degree, s=0.5)

    xx = np.linspace(0, cv.shape[0], n)
    samples = si.splev(xx, spl, ext=3)
    samples = torch.as_tensor(samples, device=sample_device, dtype=sample_dtype)

    return samples