未验证 提交 fc71459f 编写于 作者: D David Nicolas 提交者: GitHub

[xdoctest][task 167-170] reformat example code with google style in...

[xdoctest][task 167-170] reformat example code with google style in /paddle/vision/datasets/*; test=docs_preview (#56906)

* reformat example code with google style

* udpate

* update

* add timeout for dataset download

* update cifar timeout

* update cifar timeout and fix an output

* update cifar timeout

* add a blank line

---------
Co-authored-by: NSigureMo <sigure.qaq@gmail.com>
上级 8aaceba5
...@@ -55,8 +55,9 @@ def is_float16_supported(device=None): ...@@ -55,8 +55,9 @@ def is_float16_supported(device=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.amp.is_float16_supported() # True or False >>> paddle.amp.is_float16_supported() # True or False
False
""" """
device = ( device = (
...@@ -81,8 +82,9 @@ def is_bfloat16_supported(device=None): ...@@ -81,8 +82,9 @@ def is_bfloat16_supported(device=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.amp.is_bfloat16_supported() # True or False >>> paddle.amp.is_bfloat16_supported() # True or False
True
""" """
device = ( device = (
......
...@@ -61,46 +61,46 @@ class Cifar10(Dataset): ...@@ -61,46 +61,46 @@ class Cifar10(Dataset):
.. code-block:: python .. code-block:: python
import itertools >>> # doctest: +TIMEOUT(60)
import paddle.vision.transforms as T >>> import itertools
from paddle.vision.datasets import Cifar10 >>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import Cifar10
cifar10 = Cifar10() >>> cifar10 = Cifar10()
print(len(cifar10)) >>> print(len(cifar10))
# 50000 50000
for i in range(5): # only show first 5 images >>> for i in range(5): # only show first 5 images
img, label = cifar10[i] ... img, label = cifar10[i]
# do something with img and label ... # do something with img and label
print(type(img), img.size, label) ... print(type(img), img.size, label)
# <class 'PIL.Image.Image'> (32, 32) 6 ... # <class 'PIL.Image.Image'> (32, 32) 6
transform = T.Compose( >>> transform = T.Compose(
[ ... [
T.Resize(64), ... T.Resize(64),
T.ToTensor(), ... T.ToTensor(),
T.Normalize( ... T.Normalize(
mean=[0.5, 0.5, 0.5], ... mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5], ... std=[0.5, 0.5, 0.5],
to_rgb=True, ... to_rgb=True,
), ... ),
] ... ]
) ... )
>>> cifar10_test = Cifar10(
cifar10_test = Cifar10( ... mode="test",
mode="test", ... transform=transform, # apply transform to every image
transform=transform, # apply transform to every image ... backend="cv2", # use OpenCV as image transform backend
backend="cv2", # use OpenCV as image transform backend ... )
) >>> print(len(cifar10_test))
print(len(cifar10_test)) 10000
# 10000
>>> for img, label in itertools.islice(iter(cifar10_test), 5): # only show first 5 images
... # do something with img and label
... print(type(img), img.shape, label)
... # <class 'paddle.Tensor'> [3, 64, 64] 3
for img, label in itertools.islice(iter(cifar10_test), 5): # only show first 5 images
# do something with img and label
print(type(img), img.shape, label)
# <class 'paddle.Tensor'> [3, 64, 64] 3
""" """
def __init__( def __init__(
...@@ -210,46 +210,47 @@ class Cifar100(Cifar10): ...@@ -210,46 +210,47 @@ class Cifar100(Cifar10):
.. code-block:: python .. code-block:: python
import itertools >>> # doctest: +TIMEOUT(60)
import paddle.vision.transforms as T >>> import itertools
from paddle.vision.datasets import Cifar100 >>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import Cifar100
cifar100 = Cifar100() >>> cifar100 = Cifar100()
print(len(cifar100)) >>> print(len(cifar100))
# 50000 50000
for i in range(5): # only show first 5 images >>> for i in range(5): # only show first 5 images
img, label = cifar100[i] ... img, label = cifar100[i]
# do something with img and label ... # do something with img and label
print(type(img), img.size, label) ... print(type(img), img.size, label)
# <class 'PIL.Image.Image'> (32, 32) 19 ... # <class 'PIL.Image.Image'> (32, 32) 19
transform = T.Compose( >>> transform = T.Compose(
[ ... [
T.Resize(64), ... T.Resize(64),
T.ToTensor(), ... T.ToTensor(),
T.Normalize( ... T.Normalize(
mean=[0.5, 0.5, 0.5], ... mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5], ... std=[0.5, 0.5, 0.5],
to_rgb=True, ... to_rgb=True,
), ... ),
] ... ]
) ... )
cifar100_test = Cifar100( >>> cifar100_test = Cifar100(
mode="test", ... mode="test",
transform=transform, # apply transform to every image ... transform=transform, # apply transform to every image
backend="cv2", # use OpenCV as image transform backend ... backend="cv2", # use OpenCV as image transform backend
) ... )
print(len(cifar100_test)) >>> print(len(cifar100_test))
# 10000 10000
>>> for img, label in itertools.islice(iter(cifar100_test), 5): # only show first 5 images
... # do something with img and label
... print(type(img), img.shape, label)
... # <class 'paddle.Tensor'> [3, 64, 64] 49
for img, label in itertools.islice(iter(cifar100_test), 5): # only show first 5 images
# do something with img and label
print(type(img), img.shape, label)
# <class 'paddle.Tensor'> [3, 64, 64] 49
""" """
def __init__( def __init__(
......
...@@ -65,46 +65,44 @@ class Flowers(Dataset): ...@@ -65,46 +65,44 @@ class Flowers(Dataset):
.. code-block:: python .. code-block:: python
import itertools >>> # doctest: +TIMEOUT(60)
import paddle.vision.transforms as T >>> import itertools
from paddle.vision.datasets import Flowers >>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import Flowers
flowers = Flowers() >>> flowers = Flowers()
print(len(flowers)) >>> print(len(flowers))
# 6149 6149
for i in range(5): # only show first 5 images >>> for i in range(5): # only show first 5 images
img, label = flowers[i] ... img, label = flowers[i]
# do something with img and label ... # do something with img and label
print(type(img), img.size, label) ... print(type(img), img.size, label)
# <class 'PIL.JpegImagePlugin.JpegImageFile'> (523, 500) [1] ... # <class 'PIL.JpegImagePlugin.JpegImageFile'> (523, 500) [1]
>>> transform = T.Compose(
transform = T.Compose( ... [
[ ... T.Resize(64),
T.Resize(64), ... T.ToTensor(),
T.ToTensor(), ... T.Normalize(
T.Normalize( ... mean=[0.5, 0.5, 0.5],
mean=[0.5, 0.5, 0.5], ... std=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5], ... to_rgb=True,
to_rgb=True, ... ),
), ... ]
] ... )
) >>> flowers_test = Flowers(
... mode="test",
flowers_test = Flowers( ... transform=transform, # apply transform to every image
mode="test", ... backend="cv2", # use OpenCV as image transform backend
transform=transform, # apply transform to every image ... )
backend="cv2", # use OpenCV as image transform backend >>> print(len(flowers_test))
) 1020
print(len(flowers_test))
# 1020 >>> for img, label in itertools.islice(iter(flowers_test), 5): # only show first 5 images
... # do something with img and label
for img, label in itertools.islice(iter(flowers_test), 5): # only show first 5 images ... print(type(img), img.shape, label)
# do something with img and label ... # <class 'paddle.Tensor'> [3, 64, 96] [1]
print(type(img), img.shape, label)
# <class 'paddle.Tensor'> [3, 64, 96] [1]
""" """
def __init__( def __init__(
......
...@@ -91,16 +91,16 @@ class Compose: ...@@ -91,16 +91,16 @@ class Compose:
.. code-block:: python .. code-block:: python
from paddle.vision.datasets import Flowers >>> from paddle.vision.datasets import Flowers
from paddle.vision.transforms import Compose, ColorJitter, Resize >>> from paddle.vision.transforms import Compose, ColorJitter, Resize
>>> transform = Compose([ColorJitter(), Resize(size=608)])
transform = Compose([ColorJitter(), Resize(size=608)]) >>> flowers = Flowers(mode='test', transform=transform)
flowers = Flowers(mode='test', transform=transform) >>> for i in range(3):
... sample = flowers[i]
for i in range(10): ... print(sample[0].size, sample[1])
sample = flowers[i] (916, 608) [1]
print(sample[0].size, sample[1]) (758, 608) [1]
(811, 608) [1]
""" """
def __init__(self, transforms): def __init__(self, transforms):
...@@ -166,72 +166,72 @@ class BaseTransform: ...@@ -166,72 +166,72 @@ class BaseTransform:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
import paddle.vision.transforms.functional as F >>> import paddle.vision.transforms.functional as F
from paddle.vision.transforms import BaseTransform >>> from paddle.vision.transforms import BaseTransform
def _get_image_size(img): >>> def _get_image_size(img):
if F._is_pil_image(img): ... if F._is_pil_image(img):
return img.size ... return img.size
elif F._is_numpy_image(img): ... elif F._is_numpy_image(img):
return img.shape[:2][::-1] ... return img.shape[:2][::-1]
else: ... else:
raise TypeError("Unexpected type {}".format(type(img))) ... raise TypeError("Unexpected type {}".format(type(img)))
...
class CustomRandomFlip(BaseTransform): >>> class CustomRandomFlip(BaseTransform):
def __init__(self, prob=0.5, keys=None): ... def __init__(self, prob=0.5, keys=None):
super().__init__(keys) ... super().__init__(keys)
self.prob = prob ... self.prob = prob
...
def _get_params(self, inputs): ... def _get_params(self, inputs):
image = inputs[self.keys.index('image')] ... image = inputs[self.keys.index('image')]
params = {} ... params = {}
params['flip'] = np.random.random() < self.prob ... params['flip'] = np.random.random() < self.prob
params['size'] = _get_image_size(image) ... params['size'] = _get_image_size(image)
return params ... return params
...
def _apply_image(self, image): ... def _apply_image(self, image):
if self.params['flip']: ... if self.params['flip']:
return F.hflip(image) ... return F.hflip(image)
return image ... return image
...
# if you only want to transform image, do not need to rewrite this function ... # if you only want to transform image, do not need to rewrite this function
def _apply_coords(self, coords): ... def _apply_coords(self, coords):
if self.params['flip']: ... if self.params['flip']:
w = self.params['size'][0] ... w = self.params['size'][0]
coords[:, 0] = w - coords[:, 0] ... coords[:, 0] = w - coords[:, 0]
return coords ... return coords
...
# if you only want to transform image, do not need to rewrite this function ... # if you only want to transform image, do not need to rewrite this function
def _apply_boxes(self, boxes): ... def _apply_boxes(self, boxes):
idxs = np.array([(0, 1), (2, 1), (0, 3), (2, 3)]).flatten() ... idxs = np.array([(0, 1), (2, 1), (0, 3), (2, 3)]).flatten()
coords = np.asarray(boxes).reshape(-1, 4)[:, idxs].reshape(-1, 2) ... coords = np.asarray(boxes).reshape(-1, 4)[:, idxs].reshape(-1, 2)
coords = self._apply_coords(coords).reshape((-1, 4, 2)) ... coords = self._apply_coords(coords).reshape((-1, 4, 2))
minxy = coords.min(axis=1) ... minxy = coords.min(axis=1)
maxxy = coords.max(axis=1) ... maxxy = coords.max(axis=1)
trans_boxes = np.concatenate((minxy, maxxy), axis=1) ... trans_boxes = np.concatenate((minxy, maxxy), axis=1)
return trans_boxes ... return trans_boxes
...
# if you only want to transform image, do not need to rewrite this function ... # if you only want to transform image, do not need to rewrite this function
def _apply_mask(self, mask): ... def _apply_mask(self, mask):
if self.params['flip']: ... if self.params['flip']:
return F.hflip(mask) ... return F.hflip(mask)
return mask ... return mask
...
# create fake inputs >>> # create fake inputs
fake_img = Image.fromarray((np.random.rand(400, 500, 3) * 255.).astype('uint8')) >>> fake_img = Image.fromarray((np.random.rand(400, 500, 3) * 255.).astype('uint8'))
fake_boxes = np.array([[2, 3, 200, 300], [50, 60, 80, 100]]) >>> fake_boxes = np.array([[2, 3, 200, 300], [50, 60, 80, 100]])
fake_mask = fake_img.convert('L') >>> fake_mask = fake_img.convert('L')
>>> # only transform for image:
# only transform for image: >>> flip_transform = CustomRandomFlip(1.0)
flip_transform = CustomRandomFlip(1.0) >>> converted_img = flip_transform(fake_img)
converted_img = flip_transform(fake_img) >>> # transform for image, boxes and mask
>>> flip_transform = CustomRandomFlip(1.0, keys=('image', 'boxes', 'mask'))
# transform for image, boxes and mask >>> (converted_img, converted_boxes, converted_mask) = flip_transform((fake_img, fake_boxes, fake_mask))
flip_transform = CustomRandomFlip(1.0, keys=('image', 'boxes', 'mask')) >>> converted_boxes
(converted_img, converted_boxes, converted_mask) = flip_transform((fake_img, fake_boxes, fake_mask)) array([[300, 3, 498, 300],
print('converted boxes', converted_boxes) [420, 60, 450, 100]])
""" """
...@@ -319,23 +319,18 @@ class ToTensor(BaseTransform): ...@@ -319,23 +319,18 @@ class ToTensor(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
>>> import paddle.vision.transforms as T
import paddle.vision.transforms as T >>> import paddle.vision.transforms.functional as F
import paddle.vision.transforms.functional as F
>>> fake_img = Image.fromarray((np.random.rand(4, 5, 3) * 255.).astype(np.uint8))
fake_img = Image.fromarray((np.random.rand(4, 5, 3) * 255.).astype(np.uint8)) >>> transform = T.ToTensor()
>>> tensor = transform(fake_img)
transform = T.ToTensor() >>> print(tensor.shape)
[3, 4, 5]
tensor = transform(fake_img) >>> print(tensor.dtype)
paddle.float32
print(tensor.shape)
# [3, 4, 5]
print(tensor.dtype)
# paddle.float32
""" """
def __init__(self, data_format='CHW', keys=None): def __init__(self, data_format='CHW', keys=None):
...@@ -389,21 +384,19 @@ class Resize(BaseTransform): ...@@ -389,21 +384,19 @@ class Resize(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import Resize >>> from paddle.vision.transforms import Resize
fake_img = Image.fromarray((np.random.rand(256, 300, 3) * 255.).astype(np.uint8)) >>> fake_img = Image.fromarray((np.random.rand(256, 300, 3) * 255.).astype(np.uint8))
>>> transform = Resize(size=224)
transform = Resize(size=224) >>> converted_img = transform(fake_img)
converted_img = transform(fake_img) >>> print(converted_img.size)
print(converted_img.size) (262, 224)
# (262, 224) >>> transform = Resize(size=(200,150))
>>> converted_img = transform(fake_img)
transform = Resize(size=(200,150)) >>> print(converted_img.size)
converted_img = transform(fake_img) (150, 200)
print(converted_img.size)
# (150, 200)
""" """
def __init__(self, size, interpolation='bilinear', keys=None): def __init__(self, size, interpolation='bilinear', keys=None):
...@@ -456,16 +449,15 @@ class RandomResizedCrop(BaseTransform): ...@@ -456,16 +449,15 @@ class RandomResizedCrop(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import RandomResizedCrop >>> from paddle.vision.transforms import RandomResizedCrop
transform = RandomResizedCrop(224)
fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) >>> transform = RandomResizedCrop(224)
>>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> fake_img = transform(fake_img)
print(fake_img.size) >>> print(fake_img.size)
(224, 224)
""" """
...@@ -643,16 +635,16 @@ class CenterCrop(BaseTransform): ...@@ -643,16 +635,16 @@ class CenterCrop(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import CenterCrop >>> from paddle.vision.transforms import CenterCrop
transform = CenterCrop(224)
fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) >>> transform = CenterCrop(224)
>>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)
fake_img = transform(fake_img)
print(fake_img.size)
""" """
def __init__(self, size, keys=None): def __init__(self, size, keys=None):
...@@ -684,16 +676,15 @@ class RandomHorizontalFlip(BaseTransform): ...@@ -684,16 +676,15 @@ class RandomHorizontalFlip(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import RandomHorizontalFlip >>> from paddle.vision.transforms import RandomHorizontalFlip
transform = RandomHorizontalFlip(0.5)
fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> transform = RandomHorizontalFlip(0.5)
print(fake_img.size) >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(320, 300)
""" """
def __init__(self, prob=0.5, keys=None): def __init__(self, prob=0.5, keys=None):
...@@ -738,16 +729,14 @@ class RandomVerticalFlip(BaseTransform): ...@@ -738,16 +729,14 @@ class RandomVerticalFlip(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import RandomVerticalFlip >>> from paddle.vision.transforms import RandomVerticalFlip
>>> transform = RandomVerticalFlip()
transform = RandomVerticalFlip() >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) >>> print(fake_img.size)
(320, 300)
fake_img = transform(fake_img)
print(fake_img.size)
""" """
...@@ -800,20 +789,21 @@ class Normalize(BaseTransform): ...@@ -800,20 +789,21 @@ class Normalize(BaseTransform):
.. code-block:: python .. code-block:: python
:name: code-example :name: code-example
import paddle
from paddle.vision.transforms import Normalize
normalize = Normalize(mean=[127.5, 127.5, 127.5],
std=[127.5, 127.5, 127.5],
data_format='HWC')
fake_img = paddle.rand([300,320,3]).numpy() * 255. >>> import paddle
>>> from paddle.vision.transforms import Normalize
fake_img = normalize(fake_img) >>> paddle.seed(2023)
print(fake_img.shape)
# (300, 320, 3) >>> normalize = Normalize(mean=[127.5, 127.5, 127.5],
print(fake_img.max(), fake_img.min()) ... std=[127.5, 127.5, 127.5],
# 0.99999905 -0.999974 ... data_format='HWC')
...
>>> fake_img = paddle.rand([300,320,3]).numpy() * 255.
>>> fake_img = normalize(fake_img)
>>> print(fake_img.shape)
(300, 320, 3)
>>> print(fake_img.max(), fake_img.min())
0.99999464 -0.9999929
""" """
...@@ -860,16 +850,15 @@ class Transpose(BaseTransform): ...@@ -860,16 +850,15 @@ class Transpose(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import Transpose >>> from paddle.vision.transforms import Transpose
transform = Transpose()
fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) >>> transform = Transpose()
>>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> fake_img = transform(fake_img)
print(fake_img.shape) >>> print(fake_img.shape)
(3, 300, 320)
""" """
...@@ -908,15 +897,19 @@ class BrightnessTransform(BaseTransform): ...@@ -908,15 +897,19 @@ class BrightnessTransform(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import BrightnessTransform >>> from paddle.vision.transforms import BrightnessTransform
>>> np.random.seed(2023)
transform = BrightnessTransform(0.4)
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) >>> transform = BrightnessTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> print(fake_img.load()[1,1])
(60, 169, 34)
>>> # doctest: +SKIP('random sample in Brightness function')
>>> fake_img = transform(fake_img)
>>> print(fake_img.load()[1,1])
(68, 192, 38)
""" """
...@@ -951,15 +944,15 @@ class ContrastTransform(BaseTransform): ...@@ -951,15 +944,15 @@ class ContrastTransform(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import ContrastTransform >>> from paddle.vision.transforms import ContrastTransform
transform = ContrastTransform(0.4)
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) >>> transform = ContrastTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)
""" """
...@@ -996,16 +989,15 @@ class SaturationTransform(BaseTransform): ...@@ -996,16 +989,15 @@ class SaturationTransform(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import SaturationTransform >>> from paddle.vision.transforms import SaturationTransform
transform = SaturationTransform(0.4)
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img)
>>> transform = SaturationTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)
""" """
def __init__(self, value, keys=None): def __init__(self, value, keys=None):
...@@ -1039,15 +1031,15 @@ class HueTransform(BaseTransform): ...@@ -1039,15 +1031,15 @@ class HueTransform(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import HueTransform >>> from paddle.vision.transforms import HueTransform
transform = HueTransform(0.4)
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> transform = HueTransform(0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(224, 224)
""" """
...@@ -1090,15 +1082,15 @@ class ColorJitter(BaseTransform): ...@@ -1090,15 +1082,15 @@ class ColorJitter(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import ColorJitter >>> from paddle.vision.transforms import ColorJitter
transform = ColorJitter(0.4, 0.4, 0.4, 0.4) >>> transform = ColorJitter(0.4, 0.4, 0.4, 0.4)
>>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) >>> fake_img = transform(fake_img)
>>> print(fake_img.size)
fake_img = transform(fake_img) (224, 224)
""" """
...@@ -1197,15 +1189,17 @@ class RandomCrop(BaseTransform): ...@@ -1197,15 +1189,17 @@ class RandomCrop(BaseTransform):
.. code-block:: python .. code-block:: python
:name: code-example1 :name: code-example1
import paddle >>> import paddle
from paddle.vision.transforms import RandomCrop >>> from paddle.vision.transforms import RandomCrop
transform = RandomCrop(224) >>> transform = RandomCrop(224)
fake_img = paddle.randint(0, 255, shape=(3, 324,300), dtype = 'int32') >>> fake_img = paddle.randint(0, 255, shape=(3, 324,300), dtype = 'int32')
print(fake_img.shape) # [3, 324, 300] >>> print(fake_img.shape)
[3, 324, 300]
crop_img = transform(fake_img) >>> crop_img = transform(fake_img)
print(crop_img.shape) # [3, 224, 224] >>> print(crop_img.shape)
[3, 224, 224]
""" """
def __init__( def __init__(
...@@ -1313,16 +1307,15 @@ class Pad(BaseTransform): ...@@ -1313,16 +1307,15 @@ class Pad(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import Pad >>> from paddle.vision.transforms import Pad
transform = Pad(2)
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> transform = Pad(2)
print(fake_img.size) >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(228, 228)
""" """
def __init__(self, padding, fill=0, padding_mode='constant', keys=None): def __init__(self, padding, fill=0, padding_mode='constant', keys=None):
...@@ -1429,15 +1422,14 @@ class RandomAffine(BaseTransform): ...@@ -1429,15 +1422,14 @@ class RandomAffine(BaseTransform):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.vision.transforms import RandomAffine >>> from paddle.vision.transforms import RandomAffine
transform = RandomAffine([-90, 90], translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[-10, 10]) >>> transform = RandomAffine([-90, 90], translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[-10, 10])
>>> fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32)
fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) >>> fake_img = transform(fake_img)
>>> print(fake_img.shape)
fake_img = transform(fake_img) [3, 256, 300]
print(fake_img.shape)
""" """
def __init__( def __init__(
...@@ -1583,16 +1575,15 @@ class RandomRotation(BaseTransform): ...@@ -1583,16 +1575,15 @@ class RandomRotation(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import RandomRotation >>> from paddle.vision.transforms import RandomRotation
transform = RandomRotation(90)
fake_img = Image.fromarray((np.random.rand(200, 150, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> transform = RandomRotation(90)
print(fake_img.size) >>> fake_img = Image.fromarray((np.random.rand(200, 150, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(fake_img.size)
(150, 200)
""" """
def __init__( def __init__(
...@@ -1683,15 +1674,14 @@ class RandomPerspective(BaseTransform): ...@@ -1683,15 +1674,14 @@ class RandomPerspective(BaseTransform):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.vision.transforms import RandomPerspective >>> from paddle.vision.transforms import RandomPerspective
transform = RandomPerspective(prob=1.0, distortion_scale=0.9) >>> transform = RandomPerspective(prob=1.0, distortion_scale=0.9)
>>> fake_img = paddle.randn((3, 200, 150)).astype(paddle.float32)
fake_img = paddle.randn((3, 200, 150)).astype(paddle.float32) >>> fake_img = transform(fake_img)
>>> print(fake_img.shape)
fake_img = transform(fake_img) [3, 200, 150]
print(fake_img.shape)
""" """
def __init__( def __init__(
...@@ -1806,16 +1796,15 @@ class Grayscale(BaseTransform): ...@@ -1806,16 +1796,15 @@ class Grayscale(BaseTransform):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from PIL import Image >>> from PIL import Image
from paddle.vision.transforms import Grayscale >>> from paddle.vision.transforms import Grayscale
transform = Grayscale()
fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
fake_img = transform(fake_img) >>> transform = Grayscale()
print(np.array(fake_img).shape) >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
>>> fake_img = transform(fake_img)
>>> print(np.array(fake_img).shape)
(224, 224)
""" """
def __init__(self, num_output_channels=1, keys=None): def __init__(self, num_output_channels=1, keys=None):
...@@ -1861,13 +1850,20 @@ class RandomErasing(BaseTransform): ...@@ -1861,13 +1850,20 @@ class RandomErasing(BaseTransform):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
fake_img = paddle.randn((3, 10, 10)).astype(paddle.float32) >>> fake_img = paddle.randn((1, 5, 5)).astype(paddle.float32)
transform = paddle.vision.transforms.RandomErasing() >>> transform = paddle.vision.transforms.RandomErasing()
result = transform(fake_img) >>> result = transform(fake_img)
>>> # doctest: +SKIP('random sample')
>>> print(result)
Tensor(shape=[1, 5, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[[-0.22141267, -0.71004093, 1.71224928, 2.99622107, -0.82959402],
[ 0.36916021, -0.25601348, 0.86669374, 1.27504587, -0.56462914],
[-0.45704395, -0.87613666, 1.12195814, -0.87974882, 0.04902615],
[-0.91549885, -0.15066874, 1.26381516, 0. , 0. ],
[ 0.87887472, -1.59914243, -0.73970413, 0. , 0. ]]])
print(result)
""" """
def __init__( def __init__(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册