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-26] Add type annotations for paddle/vision/transforms/transforms.py #65378

Merged
merged 9 commits into from
Jun 23, 2024

Conversation

ooooo-create
Copy link
Contributor

PR Category

User Experience

PR Types

Improvements

Description

类型标注:

  • paddle/vision/transforms/transforms.py

Related links

@SigureMo @megemini

Copy link

paddle-bot bot commented Jun 22, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

from paddle import Tensor
from paddle._typing import DataLayout1D

_DataT = TypeVar("_DataT", bound=Tensor | PILImage | npt.NDArray[Any])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有两个小问题:

  • python 3.8 不能用 |
  • 这里好像不需要 bound?需要继承关系?

以下代码本地验证通过:

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Literal, TypeVar, overload, Union, Sequence

import numpy as np
import numpy.typing as npt
from typing_extensions import TypeAlias

from PIL import Image
from PIL.Image import Image as PILImage

class Tensor: ...

_DataT = TypeVar("_DataT", Tensor, PILImage, npt.NDArray[Any])
_Keys: TypeAlias = Sequence[Literal["image", "coords", "boxes", "mask"]]


def test(d: _DataT, k: _Keys | None = None) -> _DataT:
    return d

t1 = Tensor()
t2 = np.array(123)
t3 = 123
t4 = Image.open('/home/shun/Pictures/Selection_147.png')

test(t1)
test(t2)
test(t2, ['coords'])
# test(t3) fail
test(t4)

data_format: DataLayout1D

def __init__(
self, data_format: DataLayout1D = 'CHW', keys: _Keys | None = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是应该用 DataLayoutImage: TypeAlias = Literal["HWC", "CHW"]

@@ -394,7 +444,15 @@ class Resize(BaseTransform):
(150, 200)
"""

def __init__(self, size, interpolation='bilinear', keys=None):
size: int | Sequence[int]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以用 Size2: TypeAlias = Union[int, Tuple[int, int], List[int]]

还有其他的几个 size 应该都有对应的别名 ~

padding: int | Sequence[int] | None
pad_if_needed: bool
fill: int | tuple[int, ...]
padding_mode: Literal['constant', 'edge', 'reflect', 'symmetric']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PaddingMode ?

@megemini
Copy link
Contributor

def get_params(self, width, height, distortion_scale): 貌似漏掉了?

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其余目测没啥问题~

from paddle._typing import DataLayoutImage, Size2, Size3, Size4

_DataT = TypeVar("_DataT", Tensor, PILImage, npt.NDArray[Any])
_Keys: TypeAlias = Sequence[Literal["image", "coords", "boxes", "mask"]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉 _Keys 有点太宽泛了,有更具体的名字嘛?比如 _TransformKeys

SigureMo
SigureMo previously approved these changes Jun 23, 2024
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@SigureMo SigureMo merged commit 4aefe1e into PaddlePaddle:develop Jun 23, 2024
32 of 33 checks passed
@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Jun 24, 2024
co63oc pushed a commit to co63oc/Paddle that referenced this pull request Jun 25, 2024
…nsforms.py` (PaddlePaddle#65378)


---------

Co-authored-by: SigureMo <sigure.qaq@gmail.com>
co63oc pushed a commit to co63oc/Paddle that referenced this pull request Jun 25, 2024
…nsforms.py` (PaddlePaddle#65378)


---------

Co-authored-by: SigureMo <sigure.qaq@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants