未验证 提交 5356f2e0 编写于 作者: C cyberslack_lee 提交者: GitHub

[xdoctest] reformat example code with google style in No.150-160 (#56178)

* test=docs_preview

* test=docs_preview
上级 dcfe2f1a
......@@ -59,13 +59,13 @@ class WMT16(Dataset):
Args:
data_file(str): path to data tar file, can be set None if
:attr:`download` is True. Default None
mode(str): 'train', 'test' or 'val'. Default 'train'
:attr:`download` is True. Default None.
mode(str): 'train', 'test' or 'val'. Default 'train'.
src_dict_size(int): word dictionary size for source language word. Default -1.
trg_dict_size(int): word dictionary size for target language word. Default -1.
lang(str): source language, 'en' or 'de'. Default 'en'.
download(bool): whether to download dataset automatically if
:attr:`data_file` is not set. Default True
:attr:`data_file` is not set. Default True.
Returns:
Dataset: Instance of WMT16 dataset. The instance of dataset has 3 fields:
......@@ -77,30 +77,37 @@ class WMT16(Dataset):
.. code-block:: python
import paddle
from paddle.text.datasets import WMT16
class SimpleNet(paddle.nn.Layer):
def __init__(self):
super().__init__()
def forward(self, src_ids, trg_ids, trg_ids_next):
return paddle.sum(src_ids), paddle.sum(trg_ids), paddle.sum(trg_ids_next)
paddle.disable_static()
wmt16 = WMT16(mode='train', src_dict_size=50, trg_dict_size=50)
for i in range(10):
src_ids, trg_ids, trg_ids_next = wmt16[i]
src_ids = paddle.to_tensor(src_ids)
trg_ids = paddle.to_tensor(trg_ids)
trg_ids_next = paddle.to_tensor(trg_ids_next)
model = SimpleNet()
src_ids, trg_ids, trg_ids_next = model(src_ids, trg_ids, trg_ids_next)
print(src_ids.numpy(), trg_ids.numpy(), trg_ids_next.numpy())
>>> import paddle
>>> from paddle.text.datasets import WMT16
>>> class SimpleNet(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
...
... def forward(self, src_ids, trg_ids, trg_ids_next):
... return paddle.sum(src_ids), paddle.sum(trg_ids), paddle.sum(trg_ids_next)
>>> wmt16 = WMT16(mode='train', src_dict_size=50, trg_dict_size=50)
>>> for i in range(10):
... src_ids, trg_ids, trg_ids_next = wmt16[i]
... src_ids = paddle.to_tensor(src_ids)
... trg_ids = paddle.to_tensor(trg_ids)
... trg_ids_next = paddle.to_tensor(trg_ids_next)
...
... model = SimpleNet()
... src_ids, trg_ids, trg_ids_next = model(src_ids, trg_ids, trg_ids_next)
... print(src_ids.item(), trg_ids.item(), trg_ids_next.item())
89 32 33
79 18 19
55 26 27
147 36 37
106 22 23
135 50 51
54 43 44
217 30 31
146 51 52
55 24 25
"""
def __init__(
......@@ -257,9 +264,9 @@ class WMT16(Dataset):
.. code-block:: python
from paddle.text.datasets import WMT16
wmt16 = WMT16(mode='train', src_dict_size=50, trg_dict_size=50)
en_dict = wmt16.get_dict('en')
>>> from paddle.text.datasets import WMT16
>>> wmt16 = WMT16(mode='train', src_dict_size=50, trg_dict_size=50)
>>> en_dict = wmt16.get_dict('en')
"""
dict_size = (
......
......@@ -42,20 +42,27 @@ def viterbi_decode(
Returns:
scores(Tensor): The output tensor containing the score for the Viterbi sequence. The shape is [batch_size]
and the data type is float32 or float64.
paths(Tensor): The output tensor containing the highest scoring tag indices. The shape is [batch_size, sequence_length]
and the data type is int64.
paths(Tensor): The output tensor containing the highest scoring tag indices. The shape is [batch_size, sequence_length]
and the data type is int64.
Example:
Examples:
.. code-block:: python
import paddle
paddle.seed(102)
batch_size, seq_len, num_tags = 2, 4, 3
emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
length = paddle.randint(1, seq_len + 1, [batch_size])
tags = paddle.randint(0, num_tags, [batch_size, seq_len])
transition = paddle.rand((num_tags, num_tags), dtype='float32')
scores, path = paddle.text.viterbi_decode(emission, transition, length, False) # scores: [3.37089300, 1.56825531], path: [[1, 0, 0], [1, 1, 0]]
>>> import paddle
>>> paddle.seed(2023)
>>> batch_size, seq_len, num_tags = 2, 4, 3
>>> emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
>>> length = paddle.randint(1, seq_len + 1, [batch_size])
>>> tags = paddle.randint(0, num_tags, [batch_size, seq_len])
>>> transition = paddle.rand((num_tags, num_tags), dtype='float32')
>>> scores, path = paddle.text.viterbi_decode(emission, transition, length, False)
>>> print(scores)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.57385254, 2.04533720])
>>> print(path)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0],
[1, 1]])
"""
if in_dygraph_mode():
return _C_ops.viterbi_decode(
......@@ -95,7 +102,7 @@ class ViterbiDecoder(Layer):
Decode the highest scoring sequence of tags computed by transitions and potentials and get the viterbi path.
Args:
transitions (`Tensor`): The transition matrix. Its dtype is float32 and has a shape of `[num_tags, num_tags]`.
transitions (`Tensor`): The transition matrix. Its dtype is float32 and has a shape of `[num_tags, num_tags]`.
include_bos_eos_tag (`bool`, optional): If set to True, the last row and the last column of transitions will be considered
as start tag, the second to last row and the second to last column of transitions will be considered as stop tag. Defaults to ``True``.
name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please
......@@ -104,27 +111,34 @@ class ViterbiDecoder(Layer):
Shape:
potentials (Tensor): The input tensor of unary emission. This is a 3-D tensor with shape of
[batch_size, sequence_length, num_tags]. The data type is float32 or float64.
lengths (Tensor): The input tensor of length of each sequence. This is a 1-D tensor with shape of
lengths (Tensor): The input tensor of length of each sequence. This is a 1-D tensor with shape of
[batch_size]. The data type is int64.
Returns:
scores(Tensor): The output tensor containing the score for the Viterbi sequence. The shape is [batch_size]
and the data type is float32 or float64.
paths(Tensor): The output tensor containing the highest scoring tag indices. The shape is [batch_size, sequence_length]
paths(Tensor): The output tensor containing the highest scoring tag indices. The shape is [batch_size, sequence_length]
and the data type is int64.
Example:
Examples:
.. code-block:: python
import paddle
paddle.seed(102)
batch_size, seq_len, num_tags = 2, 4, 3
emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
length = paddle.randint(1, seq_len + 1, [batch_size])
tags = paddle.randint(0, num_tags, [batch_size, seq_len])
transition = paddle.rand((num_tags, num_tags), dtype='float32')
decoder = paddle.text.ViterbiDecoder(transition, include_bos_eos_tag=False)
scores, path = decoder(emission, length) # scores: [3.37089300, 1.56825531], path: [[1, 0, 0], [1, 1, 0]]
>>> import paddle
>>> paddle.seed(2023)
>>> batch_size, seq_len, num_tags = 2, 4, 3
>>> emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
>>> length = paddle.randint(1, seq_len + 1, [batch_size])
>>> tags = paddle.randint(0, num_tags, [batch_size, seq_len])
>>> transition = paddle.rand((num_tags, num_tags), dtype='float32')
>>> decoder = paddle.text.ViterbiDecoder(transition, include_bos_eos_tag=False)
>>> scores, path = decoder(emission, length)
>>> print(scores)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.57385254, 2.04533720])
>>> print(path)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0],
[1, 1]])
"""
def __init__(self, transitions, include_bos_eos_tag=True, name=None):
......
......@@ -21,13 +21,13 @@ from paddle import nn
def _make_divisible(v, divisor=8, min_value=None):
"""
This function ensures that all layers have a channel number that is divisible by divisor
This function ensures that all layers have a channel number that is divisible by divisor.
You can also see at https://github.com/keras-team/keras/blob/8ecef127f70db723c158dbe9ed3268b3d610ab55/keras/applications/mobilenet_v2.py#L505
Args:
divisor (int): The divisor for number of channels. Default: 8.
divisor (int, optional): The divisor for number of channels. Default: 8.
min_value (int, optional): The minimum value of number of channels, if it is None,
the default is divisor. Default: None.
the default is divisor. Default: None.
"""
if min_value is None:
min_value = divisor
......@@ -50,22 +50,25 @@ class IntermediateLayerGetter(nn.LayerDict):
So if `model` is passed, `model.feature1` can be returned, but not `model.feature1.layer2`.
Args:
model (nn.Layer): model on which we will extract the features
return_layers (Dict[name, new_name]): a dict containing the names of the layers for
model (nn.Layer): Model on which we will extract the features.
return_layers (Dict[name, new_name]): A dict containing the names of the layers for
which the activations will be returned as the key of the dict, and the value of the
dict is the name of the returned activation (which the user can specify).
Examples:
.. code-block:: python
import paddle
m = paddle.vision.models.resnet18(pretrained=False)
# extract layer1 and layer3, giving as names `feat1` and feat2`
new_m = paddle.vision.models._utils.IntermediateLayerGetter(m,
{'layer1': 'feat1', 'layer3': 'feat2'})
out = new_m(paddle.rand([1, 3, 224, 224]))
print([(k, v.shape) for k, v in out.items()])
# [('feat1', [1, 64, 56, 56]), ('feat2', [1, 256, 14, 14])]
>>> import paddle
>>> m = paddle.vision.models.resnet18(pretrained=False)
>>> # extract layer1 and layer3, giving as names `feat1` and feat2`
>>> new_m = paddle.vision.models._utils.IntermediateLayerGetter(m,
... {'layer1': 'feat1', 'layer3': 'feat2'})
>>> out = new_m(paddle.rand([1, 3, 224, 224]))
>>> print([(k, v.shape) for k, v in out.items()])
[('feat1', [1, 64, 56, 56]), ('feat2', [1, 256, 14, 14])]
"""
__annotations__ = {
......
......@@ -75,7 +75,7 @@ class AlexNet(nn.Layer):
Args:
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
Returns:
:ref:`api_paddle_nn_Layer`. An instance of AlexNet model.
......@@ -83,16 +83,14 @@ class AlexNet(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import AlexNet
>>> import paddle
>>> from paddle.vision.models import AlexNet
alexnet = AlexNet()
x = paddle.rand([1, 3, 224, 224])
out = alexnet(x)
print(out.shape)
# [1, 1000]
>>> alexnet = AlexNet()
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = alexnet(x)
>>> print(out.shape)
[1, 1000]
"""
def __init__(self, num_classes=1000):
......@@ -197,7 +195,7 @@ def alexnet(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`AlexNet <api_paddle_vision_AlexNet>`.
Returns:
......@@ -206,19 +204,19 @@ def alexnet(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import alexnet
>>> import paddle
>>> from paddle.vision.models import alexnet
# build model
model = alexnet()
>>> # Build model
>>> model = alexnet()
# build model and load imagenet pretrained weight
# model = alexnet(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = alexnet(pretrained=True)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
return _alexnet('alexnet', pretrained, **kwargs)
......@@ -209,7 +209,7 @@ class DenseNet(nn.Layer):
bn_size (int, optional): Expansion of growth rate in the middle layer. Default: 4.
dropout (float, optional): Dropout rate. Default: :math:`0.0`.
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -218,17 +218,17 @@ class DenseNet(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import DenseNet
>>> import paddle
>>> from paddle.vision.models import DenseNet
# build model
densenet = DenseNet()
>>> # Build model
>>> densenet = DenseNet()
x = paddle.rand([1, 3, 224, 224])
out = densenet(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = densenet(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
def __init__(
......@@ -360,7 +360,7 @@ def densenet121(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_DenseNet>`.
Returns:
......@@ -369,20 +369,20 @@ def densenet121(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import densenet121
>>> import paddle
>>> from paddle.vision.models import densenet121
# build model
model = densenet121()
>>> # Build model
>>> model = densenet121()
# build model and load imagenet pretrained weight
# model = densenet121(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = densenet121(pretrained=True)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
return _densenet('densenet121', 121, pretrained, **kwargs)
......@@ -393,7 +393,7 @@ def densenet161(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_DenseNet>`.
Returns:
......@@ -402,13 +402,20 @@ def densenet161(pretrained=False, **kwargs):
Examples:
.. code-block:: python
from paddle.vision.models import densenet161
>>> import paddle
>>> from paddle.vision.models import densenet161
# build model
model = densenet161()
>>> # Build model
>>> model = densenet161()
# build model and load imagenet pretrained weight
# model = densenet161(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = densenet161(pretrained=True)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
>>> print(out.shape)
[1, 1000]
"""
return _densenet('densenet161', 161, pretrained, **kwargs)
......@@ -419,7 +426,7 @@ def densenet169(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_DenseNet>`.
Returns:
......@@ -428,20 +435,20 @@ def densenet169(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import densenet169
>>> import paddle
>>> from paddle.vision.models import densenet169
# build model
model = densenet169()
>>> # Build model
>>> model = densenet169()
# build model and load imagenet pretrained weight
# model = densenet169(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = densenet169(pretrained=True)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
return _densenet('densenet169', 169, pretrained, **kwargs)
......@@ -452,7 +459,7 @@ def densenet201(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_DenseNet>`.
Returns:
......@@ -461,19 +468,19 @@ def densenet201(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import densenet201
>>> import paddle
>>> from paddle.vision.models import densenet201
# build model
model = densenet201()
>>> # Build model
>>> model = densenet201()
# build model and load imagenet pretrained weight
# model = densenet201(pretrained=True)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> # Build model and load imagenet pretrained weight
>>> # model = densenet201(pretrained=True)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
return _densenet('densenet201', 201, pretrained, **kwargs)
......@@ -484,7 +491,7 @@ def densenet264(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_DenseNet>`.
Returns:
......@@ -493,19 +500,19 @@ def densenet264(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import densenet264
>>> import paddle
>>> from paddle.vision.models import densenet264
# build model
model = densenet264()
>>> # Build model
>>> model = densenet264()
# build model and load imagenet pretrained weight
# model = densenet264(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = densenet264(pretrained=True)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
return _densenet('densenet264', 264, pretrained, **kwargs)
......@@ -110,7 +110,7 @@ class GoogLeNet(nn.Layer):
Args:
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -119,17 +119,17 @@ class GoogLeNet(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import GoogLeNet
>>> import paddle
>>> from paddle.vision.models import GoogLeNet
# build model
model = GoogLeNet()
>>> # Build model
>>> model = GoogLeNet()
x = paddle.rand([1, 3, 224, 224])
out, out1, out2 = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out, out1, out2 = model(x)
print(out.shape, out1.shape, out2.shape)
# [1, 1000] [1, 1000] [1, 1000]
>>> print(out.shape, out1.shape, out2.shape)
[1, 1000] [1, 1000] [1, 1000]
"""
def __init__(self, num_classes=1000, with_pool=True):
......@@ -236,7 +236,7 @@ def googlenet(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`GoogLeNet <api_paddle_vision_GoogLeNet>`.
Returns:
......@@ -245,20 +245,20 @@ def googlenet(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import googlenet
>>> import paddle
>>> from paddle.vision.models import googlenet
# build model
model = googlenet()
>>> # Build model
>>> model = googlenet()
# build model and load imagenet pretrained weight
# model = googlenet(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = googlenet(pretrained=True)
x = paddle.rand([1, 3, 224, 224])
out, out1, out2 = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out, out1, out2 = model(x)
print(out.shape, out1.shape, out2.shape)
# [1, 1000] [1, 1000] [1, 1000]
>>> print(out.shape, out1.shape, out2.shape)
[1, 1000] [1, 1000] [1, 1000]
"""
model = GoogLeNet(**kwargs)
arch = "googlenet"
......
......@@ -491,7 +491,7 @@ class InceptionV3(nn.Layer):
Args:
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -500,16 +500,16 @@ class InceptionV3(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import InceptionV3
>>> import paddle
>>> from paddle.vision.models import InceptionV3
inception_v3 = InceptionV3()
>>> inception_v3 = InceptionV3()
x = paddle.rand([1, 3, 299, 299])
out = inception_v3(x)
>>> x = paddle.rand([1, 3, 299, 299])
>>> out = inception_v3(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
def __init__(self, num_classes=1000, with_pool=True):
......@@ -591,7 +591,7 @@ def inception_v3(pretrained=False, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`InceptionV3 <api_paddle_vision_InceptionV3>`.
Returns:
......@@ -600,20 +600,20 @@ def inception_v3(pretrained=False, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import inception_v3
>>> import paddle
>>> from paddle.vision.models import inception_v3
# build model
model = inception_v3()
>>> # Build model
>>> model = inception_v3()
# build model and load imagenet pretrained weight
# model = inception_v3(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = inception_v3(pretrained=True)
x = paddle.rand([1, 3, 299, 299])
out = model(x)
>>> x = paddle.rand([1, 3, 299, 299])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
model = InceptionV3(**kwargs)
arch = "inception_v3"
......
......@@ -24,7 +24,7 @@ class LeNet(nn.Layer):
Args:
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 10.
will not be defined. Default: 10.
Returns:
:ref:`api_paddle_nn_Layer`. An instance of LeNet model.
......@@ -32,16 +32,16 @@ class LeNet(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import LeNet
>>> import paddle
>>> from paddle.vision.models import LeNet
model = LeNet()
>>> model = LeNet()
x = paddle.rand([1, 1, 28, 28])
out = model(x)
>>> x = paddle.rand([1, 1, 28, 28])
>>> out = model(x)
print(out.shape)
# [1, 10]
>>> print(out.shape)
[1, 10]
"""
def __init__(self, num_classes=10):
......
......@@ -70,7 +70,7 @@ class MobileNetV1(nn.Layer):
Args:
scale (float, optional): Scale of channels in each layer. Default: 1.0.
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -79,16 +79,16 @@ class MobileNetV1(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import MobileNetV1
>>> import paddle
>>> from paddle.vision.models import MobileNetV1
model = MobileNetV1()
>>> model = MobileNetV1()
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
......@@ -268,7 +268,7 @@ def mobilenet_v1(pretrained=False, scale=1.0, **kwargs):
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
on ImageNet. Default: False.
scale (float, optional): Scale of channels in each layer. Default: 1.0.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`MobileNetV1 <api_paddle_vision_MobileNetV1>`.
......@@ -278,23 +278,23 @@ def mobilenet_v1(pretrained=False, scale=1.0, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import mobilenet_v1
>>> import paddle
>>> from paddle.vision.models import mobilenet_v1
# build model
model = mobilenet_v1()
>>> # Build model
>>> model = mobilenet_v1()
# build model and load imagenet pretrained weight
# model = mobilenet_v1(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = mobilenet_v1(pretrained=True)
# build mobilenet v1 with scale=0.5
model_scale = mobilenet_v1(scale=0.5)
>>> # build mobilenet v1 with scale=0.5
>>> model_scale = mobilenet_v1(scale=0.5)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
model = _mobilenet(
'mobilenetv1_' + str(scale), pretrained, scale=scale, **kwargs
......
......@@ -81,7 +81,7 @@ class MobileNetV2(nn.Layer):
Args:
scale (float, optional): Scale of channels in each layer. Default: 1.0.
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -90,16 +90,16 @@ class MobileNetV2(nn.Layer):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import MobileNetV2
>>> import paddle
>>> from paddle.vision.models import MobileNetV2
model = MobileNetV2()
>>> model = MobileNetV2()
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
......@@ -206,8 +206,7 @@ def mobilenet_v2(pretrained=False, scale=1.0, **kwargs):
`"MobileNetV2: Inverted Residuals and Linear Bottlenecks" <https://arxiv.org/abs/1801.04381>`_.
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained on ImageNet. Default: False.
scale (float, optional): Scale of channels in each layer. Default: 1.0.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`MobileNetV2 <api_paddle_vision_MobileNetV2>`.
......@@ -217,23 +216,23 @@ def mobilenet_v2(pretrained=False, scale=1.0, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import mobilenet_v2
>>> import paddle
>>> from paddle.vision.models import mobilenet_v2
# build model
model = mobilenet_v2()
>>> # Build model
>>> model = mobilenet_v2()
# build model and load imagenet pretrained weight
# model = mobilenet_v2(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = mobilenet_v2(pretrained=True)
# build mobilenet v2 with scale=0.5
model = mobilenet_v2(scale=0.5)
>>> # Build mobilenet v2 with scale=0.5
>>> model = mobilenet_v2(scale=0.5)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
model = _mobilenet(
'mobilenetv2_' + str(scale), pretrained, scale=scale, **kwargs
......
......@@ -41,11 +41,12 @@ class SqueezeExcitation(nn.Layer):
Parameters ``activation``, and ``scale_activation`` correspond to ``delta`` and ``sigma`` in eq. 3.
This code is based on the torchvision code with modifications.
You can also see at https://github.com/pytorch/vision/blob/main/torchvision/ops/misc.py#L127
Args:
input_channels (int): Number of channels in the input image
squeeze_channels (int): Number of squeeze channels
activation (Callable[..., paddle.nn.Layer], optional): ``delta`` activation. Default: ``paddle.nn.ReLU``
scale_activation (Callable[..., paddle.nn.Layer]): ``sigma`` activation. Default: ``paddle.nn.Sigmoid``
input_channels (int): Number of channels in the input image.
squeeze_channels (int): Number of squeeze channels.
activation (Callable[..., paddle.nn.Layer], optional): ``delta`` activation. Default: ``paddle.nn.ReLU``.
scale_activation (Callable[..., paddle.nn.Layer]): ``sigma`` activation. Default: ``paddle.nn.Sigmoid``.
"""
def __init__(
......@@ -190,7 +191,7 @@ class MobileNetV3(nn.Layer):
last_channel (int): The number of channels on the penultimate layer.
scale (float, optional): Scale of channels in each layer. Default: 1.0.
num_classes (int, optional): Output dim of last fc layer. If num_classes <=0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
"""
......@@ -280,7 +281,7 @@ class MobileNetV3Small(MobileNetV3):
Args:
scale (float, optional): Scale of channels in each layer. Default: 1.0.
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -289,17 +290,17 @@ class MobileNetV3Small(MobileNetV3):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import MobileNetV3Small
>>> import paddle
>>> from paddle.vision.models import MobileNetV3Small
# build model
model = MobileNetV3Small(scale=1.0)
>>> # Build model
>>> model = MobileNetV3Small(scale=1.0)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
......@@ -333,7 +334,7 @@ class MobileNetV3Large(MobileNetV3):
Args:
scale (float, optional): Scale of channels in each layer. Default: 1.0.
num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
will not be defined. Default: 1000.
will not be defined. Default: 1000.
with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.
Returns:
......@@ -342,17 +343,17 @@ class MobileNetV3Large(MobileNetV3):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import MobileNetV3Large
>>> import paddle
>>> from paddle.vision.models import MobileNetV3Large
# build model
model = MobileNetV3Large(scale=1.0)
>>> # Build model
>>> model = MobileNetV3Large(scale=1.0)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
......@@ -427,8 +428,7 @@ def mobilenet_v3_small(pretrained=False, scale=1.0, **kwargs):
`"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained on ImageNet. Default: False.
scale (float, optional): Scale of channels in each layer. Default: 1.0.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`MobileNetV3Small <api_paddle_vision_MobileNetV3Small>`.
......@@ -438,23 +438,23 @@ def mobilenet_v3_small(pretrained=False, scale=1.0, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import mobilenet_v3_small
>>> import paddle
>>> from paddle.vision.models import mobilenet_v3_small
# build model
model = mobilenet_v3_small()
>>> # Build model
>>> model = mobilenet_v3_small()
# build model and load imagenet pretrained weight
# model = mobilenet_v3_small(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = mobilenet_v3_small(pretrained=True)
# build mobilenet v3 small model with scale=0.5
model = mobilenet_v3_small(scale=0.5)
>>> # Build mobilenet v3 small model with scale=0.5
>>> model = mobilenet_v3_small(scale=0.5)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
model = _mobilenet_v3(
"mobilenet_v3_small", scale=scale, pretrained=pretrained, **kwargs
......@@ -467,8 +467,7 @@ def mobilenet_v3_large(pretrained=False, scale=1.0, **kwargs):
`"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.
Args:
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
on ImageNet. Default: False.
pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained on ImageNet. Default: False.
scale (float, optional): Scale of channels in each layer. Default: 1.0.
**kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`MobileNetV3Large <api_paddle_vision_MobileNetV3Large>`.
......@@ -478,23 +477,23 @@ def mobilenet_v3_large(pretrained=False, scale=1.0, **kwargs):
Examples:
.. code-block:: python
import paddle
from paddle.vision.models import mobilenet_v3_large
>>> import paddle
>>> from paddle.vision.models import mobilenet_v3_large
# build model
model = mobilenet_v3_large()
>>> # Build model
>>> model = mobilenet_v3_large()
# build model and load imagenet pretrained weight
# model = mobilenet_v3_large(pretrained=True)
>>> # Build model and load imagenet pretrained weight
>>> # model = mobilenet_v3_large(pretrained=True)
# build mobilenet v3 large model with scale=0.5
model = mobilenet_v3_large(scale=0.5)
>>> # Build mobilenet v3 large model with scale=0.5
>>> model = mobilenet_v3_large(scale=0.5)
x = paddle.rand([1, 3, 224, 224])
out = model(x)
>>> x = paddle.rand([1, 3, 224, 224])
>>> out = model(x)
print(out.shape)
# [1, 1000]
>>> print(out.shape)
[1, 1000]
"""
model = _mobilenet_v3(
"mobilenet_v3_large", scale=scale, pretrained=pretrained, **kwargs
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册