Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a452268
feat: add video url and tensors to proto
Jan 3, 2023
3ccb697
feat: add video url and video ndarray
Jan 3, 2023
dc957d1
feat: add video torch tensor and tests
Jan 4, 2023
fc86920
fix: mypy checks
Jan 4, 2023
8a55e0b
chore: add av to video extra
Jan 4, 2023
5cb098a
fix: allow dim 3
Jan 4, 2023
3ba1f78
test: wip video load and save
Jan 5, 2023
be63926
refactor: move to numpy to computational backend
Jan 6, 2023
395a495
fix: video load and save
Jan 11, 2023
406ec80
test: adjust tests
Jan 11, 2023
091e79a
fix: video load and save and add docstrings
Jan 11, 2023
dee1146
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
Jan 11, 2023
e4106a8
fix: fix some imports after merging
Jan 11, 2023
23ee930
docs: add doc strings and fix example urls
Jan 11, 2023
7ab8dbd
docs: small fixes in docs
Jan 11, 2023
ecf01d8
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
Jan 11, 2023
5295dd1
refactor: rename save to mp4 file to save
Jan 11, 2023
b3f2ccb
feat: add shape method to comp backend
Jan 16, 2023
20ecf2c
refactor: move validate shape to video tensor mixin
Jan 16, 2023
711d105
refactor: extract private load and make separate methods for frames
Jan 16, 2023
0c9c1fd
fix: use torch shape instead of size method
Jan 16, 2023
e3a465c
fix: add typehint to shape in comp backend
Jan 16, 2023
40eac93
docs: add supported strings for skip type
Jan 16, 2023
a700f30
fix: apply suggestions from code review
Jan 17, 2023
94572fd
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
Jan 17, 2023
07ceae8
fix: small change to trigger ci again
Jan 17, 2023
c2e129d
fix: extract shape var
Jan 17, 2023
d50ae67
fix: introduce compbackendinterface
Jan 17, 2023
2e365e6
fix: revert previous pr and fix for mypy
Jan 17, 2023
c44a035
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
Jan 17, 2023
95b0b81
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
Jan 17, 2023
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
Prev Previous commit
Next Next commit
fix: introduce compbackendinterface
Signed-off-by: anna-charlotte <[email protected]>
  • Loading branch information
anna-charlotte committed Jan 17, 2023
commit d50ae67fcb2b9462304a1b99d8fd622b431680e6
2 changes: 1 addition & 1 deletion docarray/typing/tensor/video/video_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def validate(
config: 'BaseConfig',
) -> T:
tensor = super().validate(value=value, field=field, config=config)
return VideoTensorMixin.validate_shape(cls, value=tensor)
return cls.validate_shape(value=tensor)
26 changes: 16 additions & 10 deletions docarray/typing/tensor/video/video_tensor_mixin.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import abc
from typing import TYPE_CHECKING, BinaryIO, Optional, Type, TypeVar, Union

import numpy as np

from docarray.typing.tensor.audio.audio_tensor import AudioTensor

if TYPE_CHECKING:
from docarray.typing import VideoNdArray, VideoTorchTensor

from docarray.typing.tensor.abstract_tensor import AbstractTensor

T = TypeVar('T', bound='VideoTensorMixin')
TT = TypeVar('TT', bound='AbstractTensor')


class VideoTensorMixin:
class CompBackendInterface(abc.ABC):
@staticmethod
Copy link
Member

Choose a reason for hiding this comment

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

maybe briefly explain why we went for a static method here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In .validate() of VideoNdArray we want to call the validation for general VideoNdArray (by calling super().validate()), and secondly we want to add a video specific shape check (and respectively for torch). As far as I know it's not possible to specify in super() which parent class to go to, instead it just goes to through all the parent classes and stops when it find a corresponding method. Therefore we have to call the second check explicitly. When calling it as as VideoTensorMixin.validate() the information of the child class gets lost though, which we need to get the corresponding comp backend. Therefore the changes in the signature and to staticmethod.

Copy link
Member

Choose a reason for hiding this comment

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

If you wan to use a specific superclass implementation, instead of using super() you can do something like:

SuperclassThatYouWant.foo(*inputs)

Or if you need to pass the subclass cls:

SuperclassThatYouWant.foo.__func__(cls, *inputs)

This unbinds foo from the class, so you can give it any cls argument that you want.

You can see this in the __class_getitem__ implementation of NdArray.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh nice, thanks! This works, but mypy complains:

docarray/typing/tensor/video/video_ndarray.py:34: error: "Callable[[VideoTensorMixin], VideoTensorMixin]" has no attribute "__func__"  [attr-defined]

Do u know what to do here to satisfy mypy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok just saw that in __class_getitem__ of NdArray it is being ignored ( # type: ignore), will add that here too then

def validate_shape(
cls: Union[Type['VideoTorchTensor'], Type['VideoNdArray']], value: 'T'
) -> 'T':
comp_backend = cls.get_comp_backend()
shape = comp_backend.shape(value) # type: ignore
if comp_backend.n_dim(value) not in [3, 4] or shape[-1] != 3: # type: ignore
@abc.abstractmethod
def get_comp_backend():
"""The computational backend compatible with this tensor type."""
...


class VideoTensorMixin(CompBackendInterface, abc.ABC):
@classmethod
def validate_shape(cls: Type['T'], value: 'T') -> 'T':
comp_be = cls.get_comp_backend()
if comp_be.n_dim(value) not in [3, 4] or comp_be.shape(value)[-1] != 3:
raise ValueError(
f'Expects tensor with 3 or 4 dimensions and the last dimension equal '
f'to 3, but received {shape}.'
f'to 3, but received {comp_be.shape(value)}.'
)
else:
return value
Expand Down
2 changes: 1 addition & 1 deletion docarray/typing/tensor/video/video_torch_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def validate(
config: 'BaseConfig',
) -> T:
tensor = super().validate(value=value, field=field, config=config)
return VideoTensorMixin.validate_shape(cls, value=tensor)
return cls.validate_shape(value=tensor)