oneflow.random

Random

oneflow.random.CoinFlip(batch_size: int = 1, seed: Optional[int] = None, probability: float = 0.5, name: str = 'CoinFlip') → oneflow_api.BlobDesc

This operator performs the horizontal flip.

Parameters
  • batch_size (int, optional) – The batch size. Defaults to 1.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • probability (float, optional) – The flip probability. Defaults to 0.5.

  • name (str, optional) – The name for the operation. Defaults to “CoinFlip”.

Returns

[description]

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp
from typing import Tuple


@flow.global_function(type="predict")
def coin_flip_job() -> Tuple[tp.Numpy, tp.Numpy]:
    batch_size = 1
    color_space = "RGB"
    # our ofrecord file path is "./dataset/part-0"
    ofrecord = flow.data.ofrecord_reader(
        "./imgdataset",
        batch_size=batch_size,
        data_part_num=1,
        part_name_suffix_length=-1,
        part_name_prefix='part-',
        shuffle_after_epoch=True,
    )
    image = flow.data.OFRecordImageDecoder(
            ofrecord, "encoded", color_space=color_space
        )
    res_image, scale, new_size = flow.image.Resize(
            image, target_size=(512, 512)
        )
    label = flow.data.OFRecordRawDecoder(
        ofrecord, "class/label", shape=(1, ), dtype=flow.int32
    )
    coin_flip = flow.random.CoinFlip(
        batch_size=batch_size,
        probability=0.8
    )
    normal = flow.image.CropMirrorNormalize(
            res_image,
            mirror_blob=coin_flip,
            color_space=color_space,
            crop_h= 256,
            crop_w= 256,
            crop_pos_y=0.5,
            crop_pos_x=0.5,
            mean=[123.68, 116.779, 103.939],
            std=[58.393, 57.12, 57.375],
            output_dtype=flow.float,
        )

    return normal, label

if __name__ == "__main__":
    images, labels = coin_flip_job()
oneflow.random.bernoulli(x: oneflow_api.BlobDesc, seed: Optional[int] = None, dtype: Optional[oneflow.python.framework.dtype.dtype] = None, name: str = 'Bernoulli') → oneflow_api.BlobDesc

This operator returns a Blob with binaray random numbers (0 / 1) from a Bernoulli distribution.

Parameters
  • x (oneflow_api.BlobDesc) – The input Blob.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • dtype (Optional[dtype_util.dtype], optional) – The data type. Defaults to None.

  • name (str, optional) – The name for the operation. Defaults to “Bernoulli”.

Returns

The result Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def bernoulli_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    out = flow.random.bernoulli(x)
    return out


x = np.array([[0.25, 0.45, 0.3],
            [0.55, 0.32, 0.13],
            [0.75, 0.15, 0.1]]).astype(np.float32)
out = bernoulli_Job(x)

# Because our random seed is not fixed, so the return value is different each time.
# out [[1. 0. 0.]
#      [0. 0. 1.]
#      [0. 0. 0.]]
oneflow.random.coin_flip(batch_size: int = 1, seed: Optional[int] = None, probability: float = 0.5, name: str = 'CoinFlip') → oneflow_api.BlobDesc

This operator performs the horizontal flip.

Parameters
  • batch_size (int, optional) – The batch size. Defaults to 1.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • probability (float, optional) – The flip probability. Defaults to 0.5.

  • name (str, optional) – The name for the operation. Defaults to “CoinFlip”.

Returns

[description]

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp
from typing import Tuple


@flow.global_function(type="predict")
def coin_flip_job() -> Tuple[tp.Numpy, tp.Numpy]:
    batch_size = 1
    color_space = "RGB"
    # our ofrecord file path is "./dataset/part-0"
    ofrecord = flow.data.ofrecord_reader(
        "./imgdataset",
        batch_size=batch_size,
        data_part_num=1,
        part_name_suffix_length=-1,
        part_name_prefix='part-',
        shuffle_after_epoch=True,
    )
    image = flow.data.OFRecordImageDecoder(
            ofrecord, "encoded", color_space=color_space
        )
    res_image, scale, new_size = flow.image.Resize(
            image, target_size=(512, 512)
        )
    label = flow.data.OFRecordRawDecoder(
        ofrecord, "class/label", shape=(1, ), dtype=flow.int32
    )
    coin_flip = flow.random.CoinFlip(
        batch_size=batch_size,
        probability=0.8
    )
    normal = flow.image.CropMirrorNormalize(
            res_image,
            mirror_blob=coin_flip,
            color_space=color_space,
            crop_h= 256,
            crop_w= 256,
            crop_pos_y=0.5,
            crop_pos_x=0.5,
            mean=[123.68, 116.779, 103.939],
            std=[58.393, 57.12, 57.375],
            output_dtype=flow.float,
        )

    return normal, label

if __name__ == "__main__":
    images, labels = coin_flip_job()
oneflow.random.gen_seed(seed: Optional[int] = None)
oneflow.random.generate_random_batch_permutation_indices(value: oneflow_api.BlobDesc, seed: Optional[int] = None, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator generates a random permutation of indices in batch axis.

Parameters
  • value (oneflow_api.BlobDesc) – The input Blob.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def random_indice_Job(x: tp.Numpy.Placeholder(shape=(4, 3), dtype=flow.int32),
) -> tp.ListNumpy:
    return flow.random.generate_random_batch_permutation_indices(value=x)

x = np.array([[1, 1, 1],
            [2, 2, 2],
            [3, 3, 3],
            [4, 4, 4]]).astype(np.int32)
out = random_indice_Job(x)

# out [array([3, 0, 2, 1], dtype=int32)]
oneflow.random.shuffle(value: oneflow_api.BlobDesc, seed: Optional[int] = None, name: Optional[str] = None) → oneflow_api.BlobDesc

This operator shuffle the elements in input Blob.

Parameters
  • value (oneflow_api.BlobDesc) – The input Blob.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow_api.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp


@flow.global_function()
def shuffle_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.int32),
) -> tp.Numpy:
    return flow.random.shuffle(x)

x = np.array([[1, 1, 1],
            [2, 2, 2],
            [3, 3, 3]]).astype(np.int32)
out = shuffle_Job(x)

# out [[3 3 3]
#      [1 1 1]
#      [2 2 2]]