From 48409529b68b5767e2465222a235700ec25a367d Mon Sep 17 00:00:00 2001 From: David Nicolas <37790151+liyongchao911@users.noreply.github.com> Date: Tue, 31 May 2022 15:54:55 +0800 Subject: [PATCH] update RandomCrop class code annotation; test=document_fix (#42428) * update RandomCrop class code annotation; test=document_fix * update adjust_brightness api in functional.py test=document_fix * udpate uniform api in random.py * update transforms.py --- python/paddle/tensor/random.py | 28 ++++++-------- python/paddle/vision/transforms/functional.py | 9 ++++- python/paddle/vision/transforms/transforms.py | 38 ++++++++++++++----- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index 1194d81a360..49671d65b6d 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -30,7 +30,7 @@ __all__ = [] def bernoulli(x, name=None): """ - This OP returns a Tensor filled with random binary(0 or 1) number from a Bernoulli distribution. + Returns a Tensor filled with random binary(0 or 1) number from a Bernoulli distribution. The input ``x`` is a tensor with probabilities for generating the random binary number. Each element in ``x`` should be in [0, 1], and the out is generated by: @@ -86,7 +86,7 @@ def bernoulli(x, name=None): def poisson(x, name=None): r""" - This OP returns a tensor filled with random number from a Poisson Distribution. + Returns a tensor filled with random number from a Poisson Distribution. .. math:: @@ -129,7 +129,7 @@ def poisson(x, name=None): def multinomial(x, num_samples=1, replacement=False, name=None): """ - This OP returns a Tensor filled with random values sampled from a Multinomical + Returns a Tensor filled with random values sampled from a Multinomical distribution. The input ``x`` is a tensor with probabilities for generating the random number. Each element in ``x`` should be larger or equal to 0, but not all 0. ``replacement`` indicates whether it is a replaceable sample. If ``replacement`` @@ -278,7 +278,7 @@ def gaussian(shape, mean=0.0, std=1.0, dtype=None, name=None): def standard_normal(shape, dtype=None, name=None): """ - This OP returns a Tensor filled with random values sampled from a standard + Returns a Tensor filled with random values sampled from a standard normal distribution with mean 0 and standard deviation 1, with ``shape`` and ``dtype``. @@ -387,7 +387,7 @@ def randn(shape, dtype=None, name=None): def normal(mean=0.0, std=1.0, shape=None, name=None): """ - This OP returns a Tensor filled with random values sampled from a normal + Returns a Tensor filled with random values sampled from a normal distribution with ``mean`` and ``std`` (standard deviation) . If ``mean`` is a Tensor, the output Tensor has the same shape and data type as ``mean``. @@ -475,7 +475,7 @@ def normal(mean=0.0, std=1.0, shape=None, name=None): def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None): """ - This OP returns a Tensor filled with random values sampled from a uniform + Returns a Tensor filled with random values sampled from a uniform distribution in the range [``min``, ``max``), with ``shape`` and ``dtype``. Examples: @@ -505,20 +505,16 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None): it will use the seed of the global default generator (which can be set by paddle.seed). Note that if seed is not 0, this operator will always generate the same random numbers every time. Default is 0. - name(str, optional): The default value is None. Normally there is no - need for user to set this property. For more information, please - refer to :ref:`api_guide_Name`. + name(str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: A Tensor filled with random values sampled from a uniform distribution in the range [``min``, ``max``), with ``shape`` and ``dtype``. - Raises: - TypeError: If ``shape`` is not list, tuple, Tensor. - TypeError: If ``dtype`` is not float32, float64. - Examples: .. code-block:: python + :name: code-example1 import paddle @@ -625,7 +621,7 @@ def uniform_(x, min=-1.0, max=1.0, seed=0, name=None): def randint(low=0, high=None, shape=[1], dtype=None, name=None): """ - This OP returns a Tensor filled with random integers from a discrete uniform + Returns a Tensor filled with random integers from a discrete uniform distribution in the range [``low``, ``high``), with ``shape`` and ``dtype``. If ``high`` is None (the default), the range is [0, ``low``). @@ -731,7 +727,7 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None): def randint_like(x, low=0, high=None, dtype=None, name=None): """ - This OP returns a Tensor filled with random integers from a discrete uniform + Returns a Tensor filled with random integers from a discrete uniform distribution in the range [``low``, ``high``), with the same shape as ``x``. (use ``dtype`` if ``dtype`` is not None) If ``high`` is None (the default), the range is [0, ``low``). @@ -957,7 +953,7 @@ def randperm(n, dtype="int64", name=None): def rand(shape, dtype=None, name=None): """ - This OP returns a Tensor filled with random values sampled from a uniform + Returns a Tensor filled with random values sampled from a uniform distribution in the range [0, 1), with ``shape`` and ``dtype``. Args: diff --git a/python/paddle/vision/transforms/functional.py b/python/paddle/vision/transforms/functional.py index 90fba1c4130..7927e9faee3 100644 --- a/python/paddle/vision/transforms/functional.py +++ b/python/paddle/vision/transforms/functional.py @@ -380,6 +380,7 @@ def adjust_brightness(img, brightness_factor): Examples: .. code-block:: python + :name: code-example1 import numpy as np from PIL import Image @@ -388,9 +389,13 @@ def adjust_brightness(img, brightness_factor): fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') fake_img = Image.fromarray(fake_img) + print(fake_img.size) # (300, 256) + print(fake_img.load()[1,1]) # (95, 127, 202) + converted_img = F.adjust_brightness(fake_img, 0.5) + print(converted_img.size) # (300, 256) + print(converted_img.load()[1,1]) # (47, 63, 101) + - converted_img = F.adjust_brightness(fake_img, 0.4) - print(converted_img.size) """ if not (_is_pil_image(img) or _is_numpy_image(img) or _is_tensor_image(img)): diff --git a/python/paddle/vision/transforms/transforms.py b/python/paddle/vision/transforms/transforms.py index fea2efb1fb2..31f56e89055 100644 --- a/python/paddle/vision/transforms/transforms.py +++ b/python/paddle/vision/transforms/transforms.py @@ -1042,14 +1042,32 @@ class RandomCrop(BaseTransform): size (sequence|int): Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop (size, size) is made. - padding (int|sequence|optional): Optional padding on each border + padding (int|sequence, optional): Optional padding on each border of the image. If a sequence of length 4 is provided, it is used to pad left, - top, right, bottom borders respectively. Default: 0. - pad_if_needed (boolean|optional): It will pad the image if smaller than the + top, right, bottom borders respectively. Default: None, without padding. + pad_if_needed (boolean, optional): It will pad the image if smaller than the desired size to avoid raising an exception. Default: False. + fill (float|tuple, optional): Pixel fill value for constant fill. If a tuple of + length 3, it is used to fill R, G, B channels respectively. + This value is only used when the padding_mode is constant. Default: 0. + padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default: 'constant'. + + - constant: pads with a constant value, this value is specified with fill + + - edge: pads with the last value on the edge of the image + + - reflect: pads with reflection of image (without repeating the last value on the edge) + + padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode + will result in [3, 2, 1, 2, 3, 4, 3, 2] + + - symmetric: pads with reflection of image (repeating the last value on the edge) + + padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode + will result in [2, 1, 1, 2, 3, 4, 4, 3] keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - Shape: + Shape - img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C). - output(PIL.Image|np.ndarray|Paddle.Tensor): A random cropped image. @@ -1059,17 +1077,17 @@ class RandomCrop(BaseTransform): Examples: .. code-block:: python + :name: code-example1 - import numpy as np - from PIL import Image + import paddle from paddle.vision.transforms import RandomCrop - transform = RandomCrop(224) - fake_img = Image.fromarray((np.random.rand(324, 300, 3) * 255.).astype(np.uint8)) + fake_img = paddle.randint(0, 255, shape=(3, 324,300), dtype = 'int32') + print(fake_img.shape) # [3, 324, 300] - fake_img = transform(fake_img) - print(fake_img.size) + crop_img = transform(fake_img) + print(crop_img.shape) # [3, 224, 224] """ def __init__(self, -- GitLab