forked from Lightning-AI/litData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjpeg.py
More file actions
33 lines (25 loc) · 897 Bytes
/
Copy pathjpeg.py
File metadata and controls
33 lines (25 loc) · 897 Bytes
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
"""Jpeg: always JPEG (quality defaults to 95), stream a CHW tensor."""
import numpy as np
from litdata import Jpeg, StreamingDataLoader, StreamingDataset, optimize
def make_sample(index: int) -> dict:
hwc = np.random.randint(0, 256, (64, 64, 3), np.uint8)
return {
"index": index,
# Jpeg(path="a.jpg")
"image": Jpeg(array=hwc, quality=95),
}
if __name__ == "__main__":
optimize(
fn=make_sample,
inputs=list(range(8)),
output_dir="example_optimize_dataset/jpeg",
num_workers=2,
chunk_bytes="64MB",
mode="overwrite",
)
dataset = StreamingDataset("example_optimize_dataset/jpeg")
sample = dataset[0]
image = sample["image"] # Tensor CHW
print(image.shape, image.dtype)
batch = next(iter(StreamingDataLoader(dataset, batch_size=4, num_workers=0)))
print(batch["image"].shape)