Skip to content

Commit

Permalink
[xdoctest][task 167-170] reformat example code with google style in /…
Browse files Browse the repository at this point in the history
…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: SigureMo <[email protected]>
  • Loading branch information
liyongchao911 and SigureMo authored Sep 8, 2023
1 parent 8aaceba commit fc71459
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 389 deletions.
14 changes: 8 additions & 6 deletions python/paddle/amp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def is_float16_supported(device=None):
Examples:
.. code-block:: python
.. code-block:: python
import paddle
paddle.amp.is_float16_supported() # True or False
>>> import paddle
>>> paddle.amp.is_float16_supported() # True or False
False
"""

device = (
Expand All @@ -79,10 +80,11 @@ def is_bfloat16_supported(device=None):
Examples:
.. code-block:: python
.. code-block:: python
import paddle
paddle.amp.is_bfloat16_supported() # True or False
>>> import paddle
>>> paddle.amp.is_bfloat16_supported() # True or False
True
"""

device = (
Expand Down
157 changes: 79 additions & 78 deletions python/paddle/vision/datasets/cifar.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,46 @@ class Cifar10(Dataset):
.. code-block:: python
import itertools
import paddle.vision.transforms as T
from paddle.vision.datasets import Cifar10
cifar10 = Cifar10()
print(len(cifar10))
# 50000
for i in range(5): # only show first 5 images
img, label = cifar10[i]
# do something with img and label
print(type(img), img.size, label)
# <class 'PIL.Image.Image'> (32, 32) 6
transform = T.Compose(
[
T.Resize(64),
T.ToTensor(),
T.Normalize(
mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5],
to_rgb=True,
),
]
)
cifar10_test = Cifar10(
mode="test",
transform=transform, # apply transform to every image
backend="cv2", # use OpenCV as image transform backend
)
print(len(cifar10_test))
# 10000
>>> # doctest: +TIMEOUT(60)
>>> import itertools
>>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import Cifar10
>>> cifar10 = Cifar10()
>>> print(len(cifar10))
50000
>>> for i in range(5): # only show first 5 images
... img, label = cifar10[i]
... # do something with img and label
... print(type(img), img.size, label)
... # <class 'PIL.Image.Image'> (32, 32) 6
>>> transform = T.Compose(
... [
... T.Resize(64),
... T.ToTensor(),
... T.Normalize(
... mean=[0.5, 0.5, 0.5],
... std=[0.5, 0.5, 0.5],
... to_rgb=True,
... ),
... ]
... )
>>> cifar10_test = Cifar10(
... mode="test",
... transform=transform, # apply transform to every image
... backend="cv2", # use OpenCV as image transform backend
... )
>>> print(len(cifar10_test))
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__(
Expand Down Expand Up @@ -210,46 +210,47 @@ class Cifar100(Cifar10):
.. code-block:: python
import itertools
import paddle.vision.transforms as T
from paddle.vision.datasets import Cifar100
cifar100 = Cifar100()
print(len(cifar100))
# 50000
for i in range(5): # only show first 5 images
img, label = cifar100[i]
# do something with img and label
print(type(img), img.size, label)
# <class 'PIL.Image.Image'> (32, 32) 19
transform = T.Compose(
[
T.Resize(64),
T.ToTensor(),
T.Normalize(
mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5],
to_rgb=True,
),
]
)
cifar100_test = Cifar100(
mode="test",
transform=transform, # apply transform to every image
backend="cv2", # use OpenCV as image transform backend
)
print(len(cifar100_test))
# 10000
>>> # doctest: +TIMEOUT(60)
>>> import itertools
>>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import Cifar100
>>> cifar100 = Cifar100()
>>> print(len(cifar100))
50000
>>> for i in range(5): # only show first 5 images
... img, label = cifar100[i]
... # do something with img and label
... print(type(img), img.size, label)
... # <class 'PIL.Image.Image'> (32, 32) 19
>>> transform = T.Compose(
... [
... T.Resize(64),
... T.ToTensor(),
... T.Normalize(
... mean=[0.5, 0.5, 0.5],
... std=[0.5, 0.5, 0.5],
... to_rgb=True,
... ),
... ]
... )
>>> cifar100_test = Cifar100(
... mode="test",
... transform=transform, # apply transform to every image
... backend="cv2", # use OpenCV as image transform backend
... )
>>> print(len(cifar100_test))
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__(
Expand Down
78 changes: 38 additions & 40 deletions python/paddle/vision/datasets/flowers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,46 +65,44 @@ class Flowers(Dataset):
.. code-block:: python
import itertools
import paddle.vision.transforms as T
from paddle.vision.datasets import Flowers
flowers = Flowers()
print(len(flowers))
# 6149
for i in range(5): # only show first 5 images
img, label = flowers[i]
# do something with img and label
print(type(img), img.size, label)
# <class 'PIL.JpegImagePlugin.JpegImageFile'> (523, 500) [1]
transform = T.Compose(
[
T.Resize(64),
T.ToTensor(),
T.Normalize(
mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5],
to_rgb=True,
),
]
)
flowers_test = Flowers(
mode="test",
transform=transform, # apply transform to every image
backend="cv2", # use OpenCV as image transform backend
)
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
print(type(img), img.shape, label)
# <class 'paddle.Tensor'> [3, 64, 96] [1]
>>> # doctest: +TIMEOUT(60)
>>> import itertools
>>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import Flowers
>>> flowers = Flowers()
>>> print(len(flowers))
6149
>>> for i in range(5): # only show first 5 images
... img, label = flowers[i]
... # do something with img and label
... print(type(img), img.size, label)
... # <class 'PIL.JpegImagePlugin.JpegImageFile'> (523, 500) [1]
>>> transform = T.Compose(
... [
... T.Resize(64),
... T.ToTensor(),
... T.Normalize(
... mean=[0.5, 0.5, 0.5],
... std=[0.5, 0.5, 0.5],
... to_rgb=True,
... ),
... ]
... )
>>> flowers_test = Flowers(
... mode="test",
... transform=transform, # apply transform to every image
... backend="cv2", # use OpenCV as image transform backend
... )
>>> 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
... print(type(img), img.shape, label)
... # <class 'paddle.Tensor'> [3, 64, 96] [1]
"""

def __init__(
Expand Down
Loading

0 comments on commit fc71459

Please sign in to comment.