opencv/opencv-python

[Feature request] Random Number state or generator

ternaus opened this issue · 1 comments

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.

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.