Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typing][A-78] Add type annotations for python/paddle/vision/models/mobilenetv1.py #65323

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions python/paddle/vision/models/mobilenetv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,34 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import (
TYPE_CHECKING,
TypedDict,
)

from typing_extensions import NotRequired, Unpack

import paddle
from paddle import nn
from paddle._typing import Size2
from paddle.utils.download import get_weights_path_from_url

from ..ops import ConvNormActivation

if TYPE_CHECKING:
from paddle import Tensor

__all__ = []


class _MobileNetV1Options(TypedDict):
scale: NotRequired[float]
num_classes: NotRequired[int]
with_pool: NotRequired[bool]


model_urls = {
'mobilenetv1_1.0': (
'https://paddle-hapi.bj.bcebos.com/models/mobilenetv1_1.0.pdparams',
Expand All @@ -31,13 +51,13 @@
class DepthwiseSeparable(nn.Layer):
def __init__(
self,
in_channels,
out_channels1,
out_channels2,
num_groups,
stride,
scale,
):
in_channels: int,
out_channels1: int,
out_channels2: int,
num_groups: int,
stride: Size2,
scale: float,
) -> None:
super().__init__()

self._depthwise_conv = ConvNormActivation(
Expand All @@ -57,7 +77,7 @@ def __init__(
padding=0,
)

def forward(self, x):
def forward(self, x: Tensor) -> Tensor:
x = self._depthwise_conv(x)
x = self._pointwise_conv(x)
return x
Expand Down Expand Up @@ -91,7 +111,12 @@ class MobileNetV1(nn.Layer):
[1, 1000]
"""

def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
def __init__(
self,
scale: float = 1.0,
num_classes: int = 1000,
with_pool: bool = True,
) -> None:
super().__init__()
self.scale = scale
self.dwsl = []
Expand Down Expand Up @@ -230,7 +255,7 @@ def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
if num_classes > 0:
self.fc = nn.Linear(int(1024 * scale), num_classes)

def forward(self, x):
def forward(self, x: Tensor) -> Tensor:
x = self.conv1(x)
for dws in self.dwsl:
x = dws(x)
Expand All @@ -244,7 +269,9 @@ def forward(self, x):
return x


def _mobilenet(arch, pretrained=False, **kwargs):
def _mobilenet(
arch: str, pretrained: bool = False, **kwargs: Unpack[_MobileNetV1Options]
) -> MobileNetV1:
model = MobileNetV1(**kwargs)
if pretrained:
assert (
Expand All @@ -260,7 +287,11 @@ def _mobilenet(arch, pretrained=False, **kwargs):
return model


def mobilenet_v1(pretrained=False, scale=1.0, **kwargs):
def mobilenet_v1(
pretrained: bool = False,
scale: float = 1.0,
**kwargs: Unpack[_MobileNetV1Options],
) -> MobileNetV1:
"""MobileNetV1 from
`"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_.

Expand Down