-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_minimal.py
More file actions
48 lines (39 loc) · 1.23 KB
/
Copy pathnumpy_minimal.py
File metadata and controls
48 lines (39 loc) · 1.23 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
import io
import numpy as np
from pydantic import BaseModel
import taco
class ML(BaseModel):
split: str
def encode(array: np.ndarray) -> bytes:
buffer = io.BytesIO()
np.save(buffer, array)
return buffer.getvalue()
contract = taco.Contract(
structure=["image.npy", "mask.npy"],
metadata=taco.MetadataSchema(taco.Level("sample", ml=ML)),
)
collection = taco.Collection(
contract=contract,
id="numpy-demo",
dataset_version="1.0.0",
description="Small NumPy dataset",
licenses=["MIT"],
providers=["me"],
tasks=["segmentation"],
)
with taco.open_writer(collection, "numpy-demo.zip", overwrite=True) as writer:
for index in range(10):
image = np.random.default_rng(index).random((3, 32, 32), dtype=np.float32)
writer.add(
taco.Sample(
metadata=taco.Metadata(ml=ML(split="train" if index < 8 else "test")),
assets=[
taco.Asset(encode(image), path="image.npy"),
taco.Asset(encode(image[0] > 0.5), path="mask.npy"),
],
)
)
writer.run()
dataset = taco.open_dataset("numpy-demo.zip")
assert taco.read(dataset).num_rows == 10
assert taco.validate("numpy-demo.zip").ok