-
Notifications
You must be signed in to change notification settings - Fork 7.2k
add prototype utilities to read arbitrary numeric binary files #4882
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
f61a0b9
add FloReader datapipe
pmeier 675eaa0
add NumericBinaryReader
pmeier e0157bc
Merge branch 'main' into flow-reader-datapipe
pmeier 05e934f
revert unrelated change
pmeier 3a2d812
cleanup
pmeier 2d7111d
cleanup
pmeier f984983
add comment for byte reversal
pmeier c4b46b7
use numpy after all
pmeier ba362a7
appease mypy
pmeier 263b454
Merge branch 'main' into flow-reader-datapipe
pmeier 3bb9256
use .astype() with copy=False
pmeier 5e029a9
add docstring and cleanuo
pmeier e9c5584
reuse current _read_flo and revert MNIST changes
pmeier fa4fafb
cleanup
pmeier 61a71a1
revert demonstration
pmeier 950dc49
Merge branch 'main' into flow-reader-datapipe
pmeier 68f2d95
refactor
pmeier a3823ba
cleanup
pmeier de865cf
add support for mutable memory
pmeier c3fd445
add test
pmeier 7c3a33f
add comments
pmeier 2c62670
Merge branch 'main' into flow-reader-datapipe
pmeier aa780fd
catch more exceptions
pmeier e9031af
fix mypy
pmeier 1d55fc0
fix variable names
pmeier 5ebb5ae
hardcode flow sizes in test
pmeier ac3e4c2
add fix dtype docstring
pmeier 507681a
expand comment on different reading modes
pmeier c52c547
add comment about files in update mode
pmeier 80e8f25
add tests for fromfile
pmeier 1979d17
Merge branch 'main' into flow-reader-datapipe
pmeier 2bb491b
cleanup
pmeier 388ccb1
Merge branch 'main' into flow-reader-datapipe
pmeier 0969cf9
cleanup
pmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import sys | ||
|
||
import numpy as np | ||
import pytest | ||
import torch | ||
from datasets_utils import make_fake_flo_file | ||
from torchvision.datasets._optical_flow import _read_flo as read_flo_ref | ||
from torchvision.prototype.datasets.utils._internal import read_flo, fromfile | ||
|
||
|
||
@pytest.mark.filterwarnings("error:The given NumPy array is not writeable:UserWarning") | ||
@pytest.mark.parametrize( | ||
("np_dtype", "torch_dtype", "byte_order"), | ||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ | ||
(">f4", torch.float32, "big"), | ||
("<f8", torch.float64, "little"), | ||
("<i4", torch.int32, "little"), | ||
(">i8", torch.int64, "big"), | ||
("|u1", torch.uint8, sys.byteorder), | ||
], | ||
) | ||
@pytest.mark.parametrize("count", (-1, 2)) | ||
@pytest.mark.parametrize("mode", ("rb", "r+b")) | ||
def test_fromfile(tmpdir, np_dtype, torch_dtype, byte_order, count, mode): | ||
path = tmpdir / "data.bin" | ||
rng = np.random.RandomState(0) | ||
rng.randn(5 if count == -1 else count + 1).astype(np_dtype).tofile(path) | ||
|
||
for count_ in (-1, count // 2): | ||
pmeier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected = torch.from_numpy(np.fromfile(path, dtype=np_dtype, count=count_).astype(np_dtype[1:])) | ||
|
||
with open(path, mode) as file: | ||
actual = fromfile(file, dtype=torch_dtype, byte_order=byte_order, count=count_) | ||
|
||
torch.testing.assert_close(actual, expected) | ||
|
||
|
||
def test_read_flo(tmpdir): | ||
path = tmpdir / "test.flo" | ||
make_fake_flo_file(3, 4, path) | ||
|
||
with open(path, "rb") as file: | ||
actual = read_flo(file) | ||
|
||
expected = torch.from_numpy(read_flo_ref(path).astype("f4", copy=False)) | ||
|
||
torch.testing.assert_close(actual, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.