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

[Feature request] Random Number state or generator #1082

Open
ternaus opened this issue Feb 11, 2025 · 1 comment
Open

[Feature request] Random Number state or generator #1082

ternaus opened this issue Feb 11, 2025 · 1 comment
Assignees

Comments

@ternaus
Copy link

ternaus commented Feb 11, 2025

Context of the request

cv2.randnis materially faster than np.random to generate arrays of random numbers, would love to use in Albumentations library everywhere.

One limitation is reproducibility.

I prefer to fix seed for random number generator at the level of instance, and not global level.

I.e.

transform = A.Compose([A.HorizontalFlip(p=0.5)], seed=137)

=> get same sequence of random transforms every time I run the code.

To make it possible Compose has under the hood:

        self.random_generator = np.random.default_rng(seed)
        self.py_random = random.Random(seed)

and all random things are sampled using either of these generators.


Request:

I would like to do similar thing with cv2.randn. It would be great to have something like cv2.random.default_rng(seed) or cv2.random.Random(seed),

or at least to be able to pass seed like cv2.randn(dst=gaussian_sampled_arr, mean=mean_vector, stddev=std_dev_vector, seed=seed) to the functions.

Currently we can fix seed in opencv globally with cv2.setRNGSeed(cv2_seed), but it could mess things up if other parts of the pipeline depend on the seed as well.

@asmorkalov
Copy link
Collaborator

C++ allows to create dedicated RNG object for each sequence as you want, but the RNG class is not wrapped to Python and other languages. I'll take a look if it's feasible without API change.

@asmorkalov asmorkalov self-assigned this Feb 12, 2025
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

2 participants