Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An issue in voxel.py #8

Open
aric0103 opened this issue May 24, 2022 · 2 comments
Open

An issue in voxel.py #8

aric0103 opened this issue May 24, 2022 · 2 comments

Comments

@aric0103
Copy link

Hi, I found a bug in the function voxalize that leads to incorrect results. I checked the value of np.sum(pixel) before return which should be equal to the number of points in the frame, but it is always smaller than it. So I guess some points are not correctly taken into account. I checked the code line by line, and I found line 78 does not work well when the point is close to the maximum boundary(in one of x, y, z dimension). Could you please fix this issue?

@lucagessi
Copy link

lucagessi commented Aug 28, 2023

I think that you are right. I think that the function always lose 3 points, which are the point related to maximum of x, y and z axis. In the better hypothesis if only one point is the fairest it will lose only one.
A part from this the implementation of the function is really slow. I have write a function that, for a particular cloud from 6 seconds of elaboration it goes down to 20 ms.
I leave below some lines:

# Generate index of points axis
# based on axis resolution
# If x is minimum - x min equals 0! It should be OK
x_i = np.int16( np.round((x-x_min) / x_res) )
y_i = np.int16( np.round((y-y_min) / y_res) )
z_i = np.int16( np.round((z-z_min) / z_res) )
for i in range(y.shape[0]):
    # print(f"Indices{x_i[i]},{y_i[i]},{z_i[i]}")
    if( x_i[i] < x_max and y_i[i] < y_max and z_i[i] < z_max ):
        pixel[x_i[i],y_i[i],z_i[i]] = pixel[x_i[i],y_i[i],z_i[i]] + 1
return pixel

It should work fine and it's less code.

@parmahadi
Copy link

is my voxels.py code correct?
def voxalize(x_points, y_points, z_points, x, y, z, velocity):
x_min, x_max = np.min(x), np.max(x)
y_min, y_max = np.min(y), np.max(y)
z_min, z_max = np.min(z), np.max(z)

x_res = (x_max - x_min) / x_points
y_res = (y_max - y_min) / y_points
z_res = (z_max - z_min) / z_points

pixel = np.zeros([x_points, y_points, z_points])

for i in range(len(x)):
    x_idx = min(int((x[i] - x_min) / x_res), x_points - 1)
    y_idx = min(int((y[i] - y_min) / y_res), y_points - 1)
    z_idx = min(int((z[i] - z_min) / z_res), z_points - 1)
    pixel[x_idx, y_idx, z_idx] += 1

return pixel

Data extraction function

def get_data(file_path):
with open(file_path) as f:
lines = f.readlines()

frame_num, x, y, z, velocity, intensity = [], [], [], [], [], []
frame_num_count = -1
wordlist = [word for line in lines for word in line.split()]

for i in range(0, len(wordlist)):
    if wordlist[i] == "point_id:" and wordlist[i + 1] == "0":
        frame_num_count += 1
    if wordlist[i] == "point_id:": frame_num.append(frame_num_count)
    if wordlist[i] == "x:": x.append(wordlist[i + 1])
    if wordlist[i] == "y:": y.append(wordlist[i + 1])
    if wordlist[i] == "z:": z.append(wordlist[i + 1])
    if wordlist[i] == "velocity:": velocity.append(wordlist[i + 1])
    if wordlist[i] == "intensity:": intensity.append(wordlist[i + 1])

x, y, z = map(lambda coords: np.asarray(coords, dtype=float), [x, y, z])
frame_num = np.asarray(frame_num, dtype=int)
velocity = np.asarray(velocity, dtype=float)

data = {}
for i in range(len(frame_num)):
    if frame_num[i] in data:
        data[frame_num[i]].append([x[i], y[i], z[i], velocity[i]])
    else:
        data[frame_num[i]] = [[x[i], y[i], z[i], velocity[i]]]

data_pro1 = {}
together_frames, sliding_frames = 1, 1
frames_number = np.array(list(data.keys()))
total_frames = frames_number.max()

i = j = 0
while together_frames + i <= total_frames:
    curr_j_data = []
    for k in range(together_frames):
        curr_j_data.extend(data[i + k])
    data_pro1[j] = curr_j_data
    j += 1
    i += sliding_frames

pixels = []
for i in data_pro1:
    f = np.array(data_pro1[i])
    x_c, y_c, z_c, vel_c = f[:, 0], f[:, 1], f[:, 2], f[:, 3]
    pix = voxalize(10, 32, 32, x_c, y_c, z_c, vel_c)
    pixels.append(pix)

pixels = np.array(pixels)

train_data = []
frames_together, sliding = 60, 10
i = 0
while i + frames_together <= pixels.shape[0]:
    local_data = [pixels[i + j] for j in range(frames_together)]
    train_data.append(local_data)
    i += sliding

return np.array(train_data)

Parsing and saving data

def parse_RF_files(parent_dir, sub_dirs, file_ext='*.txt'):
for sub_dir in sub_dirs:
features = np.empty((0, 60, 10, 32, 32))
labels = []

    # Process each file in the subdirectory
    files = sorted(glob.glob(os.path.join(parent_dir, sub_dir, file_ext)))
    for fn in files:
        print(f'Processing file: {fn}')
        train_data = get_data(fn)
        features = np.vstack([features, train_data])

        # Append label for each sample in the file
        labels.extend([sub_dir] * train_data.shape[0])

        print(f'Current shape of features: {features.shape}, labels count: {len(labels)}')

    # Save the data for each activity in separate .npz files
    save_path = f"{extract_path}{sub_dir}"
    np.savez(save_path, features=features, labels=labels)
    print(f'Saved {sub_dir} data to {save_path}.npz')

Run the parser

parse_RF_files(parent_dir, sub_dirs)
print("Data extraction and saving completed successfully.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants