Datasets:
Download load_example.py from kim1034/dacl10k-component-context: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/datasets/kim1034/dacl10k-component-context/resolve/main/load_example.py
- Command line
-
hf download hf://datasets/kim1034/dacl10k-component-context/load_example.py
-
curl -L -o load_example.py https://huggingface.co/datasets/kim1034/dacl10k-component-context/resolve/main/load_example.py
1.39 kB
| """split_3fold.json 으로 fold k 의 학습/inner_val/시험 목록을 만들고, 사진 1장과 마스크를 tar 에서 바로 읽는 예시. | |
| 사용법: python load_example.py [k] (필요: numpy, pillow) | |
| """ | |
| import io | |
| import json | |
| import sys | |
| import tarfile | |
| import numpy as np | |
| from PIL import Image | |
| k = int(sys.argv[1]) if len(sys.argv) > 1 else 0 | |
| split = json.load(open("split_3fold.json", encoding="utf-8")) | |
| classes = [c["name"] for c in json.load(open("classes.json", encoding="utf-8"))["classes"]] | |
| inner = set(split["inner_val"][str(k)]) | |
| test = sorted(n for n, v in split["images"].items() if v["fold"] == k) | |
| train = sorted(n for n, v in split["images"].items() if v["fold"] != k and n not in inner) | |
| print(f"fold {k}: train {len(train)}, inner_val {len(inner)}, test {len(test)}") | |
| name = test[0] | |
| with tarfile.open(f"masks/fold{k}.tar") as t: | |
| mask = np.load(io.BytesIO(t.extractfile(f"masks/{name}.npz").read()))["mask"] # (19, H, W) bool | |
| with tarfile.open(f"images/fold{k}.tar") as t: | |
| img = Image.open(io.BytesIO(t.extractfile(f"images/{name}.jpg").read())) | |
| print(f"{name}: image {img.size[0]}x{img.size[1]}, mask {mask.shape} {mask.dtype}") | |
| for i, c in enumerate(classes): | |
| if mask[i].any(): | |
| print(f" {i:2d} {c:14s} {int(mask[i].sum()):>10d} px") | |
| print(f" multi-label pixels (2+ classes): {int((mask.sum(0) > 1).sum())}") | |