Yarroudh/segment-lidar

ValueError: could not broadcast input array from shape into shape

binaryannamolly opened this issue · 1 comments

Any idea why I get the following error?

ValueError: could not broadcast input array from shape (3114735,) into shape (8392998,)

This is my code,

from segment_lidar import samlidar, view
import torch

device_set = 'cuda:0' if torch.cuda.is_available() else 'cpu'

model = samlidar.SamLidar(ckpt_path='segment_anything/checkpoints/sam_vit_h_4b8939.pth',
                        device=device_set, # 'cuda:0',
                        algorithm='segment-geospatial',
                        model_type = "vit_h",
                        resolution=1,
                        sam_kwargs=False) 

points = model.read("L4.las")

cloud, non_ground, ground = model.csf(points, class_threshold=0.1)

labels, *_ = model.segment(points=cloud,
                        image_path="results/raster_L4.tif",
                        labels_path="results/labeled_L4.tif")

model.write(points=points, segment_ids=labels, save_path="results/segmented_L4.las")

Below is the full output when running the script,

  • Classification value is not provided. Reading all points...
  • Reading RGB values...
    File reading is completed in 0.96 seconds. The point cloud contains 8392998 points.

Applying CSF algorithm...
[0] Configuring terrain...
[0] Configuring cloth...
[0] - width: 5004 height: 5004
[0] Rasterizing...
[0] Simulating...
CSF algorithm is completed in 552.40 seconds. The filtered non-ground cloud contains 8392998 points.

  • Generating raster image...
  • Saving raster image...
  • Applying segment-geospatial to raster image...
  • Saving segmented image...
  • Generating segment IDs...
    Segmentation is completed in 305.05 seconds. Number of instances: 212

Writing the segmented point cloud to results/segmented_L4.las...
Traceback (most recent call last):
File "/samlidar/segmentLiDAR_playground_nv.py", line 73, in
model.write(points=points, segment_ids=labels, save_path="results/segmented_L4.las")
File "/samlidar/lib/python3.10/site-packages/segment_lidar/samlidar.py", line 454, in write
lidar.segment_id = segment_ids
File "/samlidar/lib/python3.10/site-packages/laspy/lasdata.py", line 403, in setattr
self[key] = value
File "/samlidar/lib/python3.10/site-packages/laspy/lasdata.py", line 435, in setitem
self.points[key] = value
File "/samlidar/lib/python3.10/site-packages/laspy/point/record.py", line 222, in setitem
self[key][:] = value
ValueError: could not broadcast input array from shape (3114735,) into shape (8392998,)

Solved it.
For anyone having the same issue, the mistake was in my code.

I changed this,
model.write(points=points, segment_ids=labels, save_path="results/segmented_L4.las")

To this,
model.write(points=points, non_ground=non_ground, ground=ground, segment_ids=labels, save_path="results/segmented_L4.las")