我正在尝试为我的硕士论文在 C++ xtensor 库中重新实现 numpy 代码。但我无法理解为什么 numpy 零初始化数组给出 number(10,20,43,28,....)。请参阅下面的代码。
这是代码:
import numpy as np
scene_size = np.array([4, 40, 48], dtype=np.float32)
voxel_size = np.array([0.4, 0.2, 0.2], dtype=np.float32)
grid_size = np.array([10, 200, 240], dtype=np.int64)
lidar_coord = np.array([0, 20, 3], dtype=np.float32)
max_point_number = 45
#lidar point clouds, each point has 4 fields (x,y,z,intensity)
np.random.shuffle(point_cloud) #(N,4) N number of points
shifted_coord = point_cloud[:, :3] + lidar_coord
# reverse the point cloud coordinate (X, Y, Z) -> (Z, Y, X)
voxel_index = np.floor(
shifted_coord[:, ::-1] / voxel_size).astype(np.int) #(N, 3)
bound_x = np.logical_and(
voxel_index[:, 2] >= 0, voxel_index[:, 2] < grid_size[2]) #(N, )
bound_y = np.logical_and(
voxel_index[:, 1] >= 0, voxel_index[:, 1] < grid_size[1]) #(N, )
bound_z = np.logical_and(
voxel_index[:, 0] >= 0, voxel_index[:, 0] < grid_size[0]) #(N,)
bound_box = np.logical_and(np.logical_and(bound_x, bound_y), bound_z) #(N, )
point_cloud = point_cloud[bound_box] ##(dynmaic_value, 4)
voxel_index = voxel_index[bound_box] ##(dynmaic_value, 3)
# [K, 3] coordinate buffer as described in the paper
coordinate_buffer = np.unique(voxel_index, axis=0) #removing the duplicates in axis 0
#which means for an example voxel_index[10] = (1,2,3) and voxel_index[20] = (1,2,3), remove one of them
K = len(coordinate_buffer)
T = max_point_number
#Not storing any points, but zeros initialized array of shape K
# [K, 1] store number of points in each voxel grid
number_buffer = np.zeros(shape=(K), dtype=np.int64)
# [K, T, 7] feature buffer as described in the paper
feature_buffer = np.zeros(shape=(K, T, 7), dtype=np.float32)
# build a reverse index for coordinate buffer
index_buffer = {}
for i in range(K):
index_buffer[tuple(coordinate_buffer[i])] = i
for voxel, point in zip(voxel_index, point_cloud):
index = index_buffer[tuple(voxel)]
number = number_buffer[index]
print(number) #according to my understand it should be zero, but giving number like 4,19,34,23,26
#Because number buffer is zeros initialized
if number < T:
feature_buffer[index, number, :4] = point
number_buffer[index] += 1
你可以在上面代码的注释部分看到,
- 我不明白,为什么 number_buffer 给出的实际值不是 0 且大于零?
我正在使用第三方库在 C++ 中实现 numpy 功能。
谁能解释一下代码的行为?
零初始化数组如何给出大于 0 的实际值(这意味着存储在每个体素中的点数)?
使用 xtensor 在 C++ 中实现的代码可以正常工作,直到满足以下条件:
if(number <T):
在 C++ 中,number 将始终为零,因为 number_buffer 在 numpy 和 C++ 中都初始化为零。所以 C++ 代码给出 0,但 numpy 代码给出的某些索引不是零(比如大于零的值,这意味着存储在每个体素中的点数)。
我很高兴提供我需要的更多信息。请帮我解决这个问题。
提前致谢