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

Make VideoMAEImageProcessor much faster #28221

Closed
wants to merge 9 commits into from
7 changes: 6 additions & 1 deletion src/transformers/feature_extraction_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ def _get_is_as_tensor_fns(self, tensor_type: Optional[Union[str, TensorType]] =
raise ImportError("Unable to convert output to PyTorch tensors format, PyTorch is not installed.")
import torch # noqa

def recursive_ndarray_check(value):
if isinstance(value, (list, tuple)) and len(value) > 0:
return recursive_ndarray_check(value[0])
return isinstance(value, np.ndarray)

def as_tensor(value):
if isinstance(value, (list, tuple)) and len(value) > 0 and isinstance(value[0], np.ndarray):
if recursive_ndarray_check(value):
value = np.array(value)
return torch.tensor(value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,7 @@ def preprocess(
for video in videos
]

data = {"pixel_values": videos}
# Speeds up tensor conversion - see: https://github.com/huggingface/transformers/pull/28221/files
data = {"pixel_values": np.asarray(videos) if return_tensors is not None else videos}

return BatchFeature(data=data, tensor_type=return_tensors)