Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
a9940fe4
Unverified
Commit
a9940fe4
authored
Oct 08, 2021
by
Sergii Khomenko
Committed by
GitHub
Oct 08, 2021
Browse files
Switch from np.frombuffer to torch.frombuffer (#4578)
Co-authored-by:
Prabhat Roy
<
prabhatroy@fb.com
>
parent
9948d4f3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
5 additions
and
8 deletions
+5
-8
test/test_image.py
test/test_image.py
+1
-3
test/test_video_reader.py
test/test_video_reader.py
+1
-1
torchvision/io/_video_opt.py
torchvision/io/_video_opt.py
+3
-4
No files found.
test/test_image.py
View file @
a9940fe4
...
@@ -476,9 +476,7 @@ def test_encode_jpeg(img_path):
...
@@ -476,9 +476,7 @@ def test_encode_jpeg(img_path):
buf
=
io
.
BytesIO
()
buf
=
io
.
BytesIO
()
pil_img
.
save
(
buf
,
format
=
"JPEG"
,
quality
=
75
)
pil_img
.
save
(
buf
,
format
=
"JPEG"
,
quality
=
75
)
# pytorch can't read from raw bytes so we go through numpy
encoded_jpeg_pil
=
torch
.
frombuffer
(
buf
.
getvalue
(),
dtype
=
torch
.
uint8
)
pil_bytes
=
np
.
frombuffer
(
buf
.
getvalue
(),
dtype
=
np
.
uint8
)
encoded_jpeg_pil
=
torch
.
as_tensor
(
pil_bytes
)
for
src_img
in
[
img
,
img
.
contiguous
()]:
for
src_img
in
[
img
,
img
.
contiguous
()]:
encoded_jpeg_torch
=
encode_jpeg
(
src_img
,
quality
=
75
)
encoded_jpeg_torch
=
encode_jpeg
(
src_img
,
quality
=
75
)
...
...
test/test_video_reader.py
View file @
a9940fe4
...
@@ -258,7 +258,7 @@ def _get_video_tensor(video_dir, video_file):
...
@@ -258,7 +258,7 @@ def _get_video_tensor(video_dir, video_file):
assert
os
.
path
.
exists
(
full_path
),
"File not found: %s"
%
full_path
assert
os
.
path
.
exists
(
full_path
),
"File not found: %s"
%
full_path
with
open
(
full_path
,
"rb"
)
as
fp
:
with
open
(
full_path
,
"rb"
)
as
fp
:
video_tensor
=
torch
.
from_numpy
(
np
.
frombuffer
(
fp
.
read
(),
dtype
=
np
.
uint8
)
)
video_tensor
=
torch
.
frombuffer
(
fp
.
read
(),
dtype
=
torch
.
uint8
)
return
full_path
,
video_tensor
return
full_path
,
video_tensor
...
...
torchvision/io/_video_opt.py
View file @
a9940fe4
...
@@ -3,7 +3,6 @@ import warnings
...
@@ -3,7 +3,6 @@ import warnings
from
fractions
import
Fraction
from
fractions
import
Fraction
from
typing
import
List
,
Tuple
from
typing
import
List
,
Tuple
import
numpy
as
np
import
torch
import
torch
from
.._internally_replaced_utils
import
_get_extension_path
from
.._internally_replaced_utils
import
_get_extension_path
...
@@ -338,7 +337,7 @@ def _read_video_from_memory(
...
@@ -338,7 +337,7 @@ def _read_video_from_memory(
_validate_pts
(
audio_pts_range
)
_validate_pts
(
audio_pts_range
)
if
not
isinstance
(
video_data
,
torch
.
Tensor
):
if
not
isinstance
(
video_data
,
torch
.
Tensor
):
video_data
=
torch
.
from_numpy
(
np
.
frombuffer
(
video_data
,
dtype
=
np
.
uint8
)
)
video_data
=
torch
.
frombuffer
(
video_data
,
dtype
=
torch
.
uint8
)
result
=
torch
.
ops
.
video_reader
.
read_video_from_memory
(
result
=
torch
.
ops
.
video_reader
.
read_video_from_memory
(
video_data
,
video_data
,
...
@@ -378,7 +377,7 @@ def _read_video_timestamps_from_memory(video_data):
...
@@ -378,7 +377,7 @@ def _read_video_timestamps_from_memory(video_data):
is much faster than read_video(...)
is much faster than read_video(...)
"""
"""
if
not
isinstance
(
video_data
,
torch
.
Tensor
):
if
not
isinstance
(
video_data
,
torch
.
Tensor
):
video_data
=
torch
.
from_numpy
(
np
.
frombuffer
(
video_data
,
dtype
=
np
.
uint8
)
)
video_data
=
torch
.
frombuffer
(
video_data
,
dtype
=
torch
.
uint8
)
result
=
torch
.
ops
.
video_reader
.
read_video_from_memory
(
result
=
torch
.
ops
.
video_reader
.
read_video_from_memory
(
video_data
,
video_data
,
0
,
# seek_frame_margin
0
,
# seek_frame_margin
...
@@ -415,7 +414,7 @@ def _probe_video_from_memory(video_data):
...
@@ -415,7 +414,7 @@ def _probe_video_from_memory(video_data):
This function is torchscriptable
This function is torchscriptable
"""
"""
if
not
isinstance
(
video_data
,
torch
.
Tensor
):
if
not
isinstance
(
video_data
,
torch
.
Tensor
):
video_data
=
torch
.
from_numpy
(
np
.
frombuffer
(
video_data
,
dtype
=
np
.
uint8
)
)
video_data
=
torch
.
frombuffer
(
video_data
,
dtype
=
torch
.
uint8
)
result
=
torch
.
ops
.
video_reader
.
probe_video_from_memory
(
video_data
)
result
=
torch
.
ops
.
video_reader
.
probe_video_from_memory
(
video_data
)
vtimebase
,
vfps
,
vduration
,
atimebase
,
asample_rate
,
aduration
=
result
vtimebase
,
vfps
,
vduration
,
atimebase
,
asample_rate
,
aduration
=
result
info
=
_fill_info
(
vtimebase
,
vfps
,
vduration
,
atimebase
,
asample_rate
,
aduration
)
info
=
_fill_info
(
vtimebase
,
vfps
,
vduration
,
atimebase
,
asample_rate
,
aduration
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment