Skip to content

Commit 31b8c4d

Browse files
committed
Switch from np.frombuffer to torch.frombuffer
1 parent aaee8ff commit 31b8c4d

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

test/test_image.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,7 @@ def test_encode_jpeg(img_path):
476476
buf = io.BytesIO()
477477
pil_img.save(buf, format="JPEG", quality=75)
478478

479-
# pytorch can't read from raw bytes so we go through numpy
480-
pil_bytes = np.frombuffer(buf.getvalue(), dtype=np.uint8)
481-
encoded_jpeg_pil = torch.as_tensor(pil_bytes)
479+
encoded_jpeg_pil = torch.frombuffer(buf.getvalue(), dtype=torch.uint8)
482480

483481
for src_img in [img, img.contiguous()]:
484482
encoded_jpeg_torch = encode_jpeg(src_img, quality=75)

test/test_video_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def _get_video_tensor(video_dir, video_file):
258258
assert os.path.exists(full_path), "File not found: %s" % full_path
259259

260260
with open(full_path, "rb") as fp:
261-
video_tensor = torch.from_numpy(np.frombuffer(fp.read(), dtype=np.uint8))
261+
video_tensor = torch.frombuffer(fp.read(), dtype=torch.uint8)
262262

263263
return full_path, video_tensor
264264

torchvision/io/_video_opt.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from fractions import Fraction
44
from typing import List, Tuple
55

6-
import numpy as np
76
import torch
87

98
from .._internally_replaced_utils import _get_extension_path
@@ -338,7 +337,7 @@ def _read_video_from_memory(
338337
_validate_pts(audio_pts_range)
339338

340339
if not isinstance(video_data, torch.Tensor):
341-
video_data = torch.from_numpy(np.frombuffer(video_data, dtype=np.uint8))
340+
video_data = torch.frombuffer(video_data, dtype=torch.uint8)
342341

343342
result = torch.ops.video_reader.read_video_from_memory(
344343
video_data,
@@ -378,7 +377,7 @@ def _read_video_timestamps_from_memory(video_data):
378377
is much faster than read_video(...)
379378
"""
380379
if not isinstance(video_data, torch.Tensor):
381-
video_data = torch.from_numpy(np.frombuffer(video_data, dtype=np.uint8))
380+
video_data = torch.frombuffer(video_data, dtype=torch.uint8)
382381
result = torch.ops.video_reader.read_video_from_memory(
383382
video_data,
384383
0, # seek_frame_margin
@@ -415,7 +414,7 @@ def _probe_video_from_memory(video_data):
415414
This function is torchscriptable
416415
"""
417416
if not isinstance(video_data, torch.Tensor):
418-
video_data = torch.from_numpy(np.frombuffer(video_data, dtype=np.uint8))
417+
video_data = torch.frombuffer(video_data, dtype=torch.uint8)
419418
result = torch.ops.video_reader.probe_video_from_memory(video_data)
420419
vtimebase, vfps, vduration, atimebase, asample_rate, aduration = result
421420
info = _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration)

0 commit comments

Comments
 (0)