-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathnumpy_array.py
More file actions
50 lines (37 loc) · 1.33 KB
/
Copy pathnumpy_array.py
File metadata and controls
50 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""NumPy: load .npy files, stream ndarrays.
A 2-D+ array keeps its shape. A 1-D array uses the no-header layout
(same idea as token ids). Use Tensor(...) for torch tensors.
"""
from pathlib import Path
import numpy as np
from litdata import StreamingDataLoader, StreamingDataset, optimize
def make_sample(path: str) -> dict:
return {
"array": np.array(np.load(path), copy=True),
"caption": Path(path).stem,
}
def seed_folder(root: Path) -> list[str]:
root.mkdir(parents=True, exist_ok=True)
rng = np.random.default_rng(0)
paths = []
for index in range(8):
path = root / f"feat_{index}.npy"
np.save(path, rng.standard_normal((3, 4, 4)).astype(np.float32))
paths.append(str(path))
return paths
if __name__ == "__main__":
paths = seed_folder(Path("example_optimize_dataset/source/numpy"))
optimize(
fn=make_sample,
inputs=paths,
output_dir="example_optimize_dataset/numpy",
num_workers=2,
chunk_bytes="64MB",
mode="overwrite",
)
dataset = StreamingDataset("example_optimize_dataset/numpy")
sample = dataset[0]
array = sample["array"] # NumPy
print(sample["caption"], array.shape, array.dtype)
batch = next(iter(StreamingDataLoader(dataset, batch_size=4, num_workers=0)))
print(batch["array"].shape)