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

Random seed works at 0.15.1 segment_plane but not works at 0.16.0 #5647

Closed
3 tasks done
mcmingchang opened this issue Nov 4, 2022 · 13 comments
Closed
3 tasks done

Random seed works at 0.15.1 segment_plane but not works at 0.16.0 #5647

mcmingchang opened this issue Nov 4, 2022 · 13 comments
Assignees
Labels
bug Not a build issue, this is likely a bug.

Comments

@mcmingchang
Copy link

Checklist

Describe the issue

Random seed works at 0.15.1 segment_plane but not works at 0.16.0

Steps to reproduce the bug

#### in 0.15.1
import numpy as np
import open3d as o3d
pcd = o3d.io.read_point_cloud('1.ply')
pcd_down = pcd.voxel_down_sample(voxel_size=0.01)
plane_model, inliers = pcd_down.segment_plane(distance_threshold=0.02,
                                              ransac_n=5,
                                              num_iterations=5000, seed=1)
plane_model, inliers2 = pcd_down.segment_plane(distance_threshold=0.02,
                                              ransac_n=5,
                                              num_iterations=5000, seed=1)
print(np.all(np.array(inliers) == np.array(inliers2)))


##### in 0.16.0
import numpy as np
import open3d as o3d
o3d.utility.random.seed(1)
pcd = o3d.io.read_point_cloud('1.ply')
pcd_down = pcd.voxel_down_sample(voxel_size=0.01)
plane_model, inliers = pcd_down.segment_plane(distance_threshold=0.02,
                                              ransac_n=5,
                                              num_iterations=5000)
plane_model, inliers2 = pcd_down.segment_plane(distance_threshold=0.02,
                                              ransac_n=5,
                                              num_iterations=5000)
print(np.all(np.array(inliers) == np.array(inliers2)))

Error message

None

Expected behavior

The result of 0.16 should be the same as that of 0.15

Open3D, Python and System information

None

Additional information

Random seed works at 0.15.1 segment_plane but not works at 0.16.0

@mcmingchang mcmingchang added the bug Not a build issue, this is likely a bug. label Nov 4, 2022
@yuecideng
Copy link
Collaborator

Since 0.16.0, the segment_plane function is parallel and the iteration is changed with the estimated inliers ratio, which may lead to inconsistent results under the same random seed.

@mcmingchang
Copy link
Author

You mean it's not a bug?But setting seed is to achieve a stable result.

@yuecideng
Copy link
Collaborator

You may try to set the new arg probability=1, which will force the iteration to be executed equals to num_iterations. Then the results will be consistent with the seed you set.

@mcmingchang
Copy link
Author

After experiments, the results are different each time.

@yuecideng
Copy link
Collaborator

The random seed is singleton so you should reset the random seed before segment_plane at the second time.

import numpy as np
import open3d as o3d
o3d.utility.random.seed(1)
pcd = o3d.io.read_point_cloud('1.ply')
pcd_down = pcd.voxel_down_sample(voxel_size=0.01)
plane_model, inliers = pcd_down.segment_plane(distance_threshold=0.02,
                                              ransac_n=5,
                                              num_iterations=5000,
                                              probability=1)
o3d.utility.random.seed(1)
plane_model, inliers2 = pcd_down.segment_plane(distance_threshold=0.02,
                                              ransac_n=5,
                                              num_iterations=5000,
                                              probability=1)
print(np.all(np.array(inliers) == np.array(inliers2)))

@mcmingchang
Copy link
Author

thank you

@mcmingchang
Copy link
Author

For large plane fitting, it is not work at 0.16

@conorsim
Copy link

I have also observed this issue with 0.16 and 0.17

@TheExDeus
Copy link

TheExDeus commented Jul 26, 2023

I set seed before each call and probability=1.0 - and it still doesn't make this deterministic. I also get different value every time (and in some cases it can vary a lot if the points are sparse). This means I cannot make tests. Any idea on how to fix this?

This means I might need to revert to 0.15, but that doesn't seem like a long term solution.

@yuecideng
Copy link
Collaborator

Hi @TheExDeus @conorsim, could you provide the data or the code snippet to reproduce the bug?

@conorsim
Copy link

@yuecideng if I test your code above with version 0.17.0 I don't get the same values. I made a couple of changes:

  1. To initialize a point cloud of random points since I don't have a .ply file handy
  2. To compare plane_model since inliers doesn't always have the same shape (due to the this issue)
import numpy as np
import open3d as o3d

pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.random.uniform(1,10,(1000,3)))

for _ in range(10):
    o3d.utility.random.seed(1)
    pcd_down = pcd.voxel_down_sample(voxel_size=0.01)
    plane_model, inliers = pcd_down.segment_plane(distance_threshold=0.02,
                                                  ransac_n=5,
                                                  num_iterations=5000,
                                                  probability=1)
    o3d.utility.random.seed(1)
    plane_model2, inliers2 = pcd_down.segment_plane(distance_threshold=0.02,
                                                  ransac_n=5,
                                                  num_iterations=5000,
                                                  probability=1)
    print(np.all(np.array(plane_model) == np.array(plane_model2)))

When I run this, the output I got was

False
False
False
False
True
False
False
False
False
False

So, in this run it was correct only 1/10 times.

@yuecideng
Copy link
Collaborator

Hi @conorsim, you are right! this may be caused by the update of best estimated model among multi threading. I will try to investigate it.

@yuecideng
Copy link
Collaborator

The issue has been solved by PR #6308.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Not a build issue, this is likely a bug.
Projects
None yet
Development

No branches or pull requests

4 participants