-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
147 lines (119 loc) · 5.34 KB
/
Copy pathdata.py
File metadata and controls
147 lines (119 loc) · 5.34 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""QuickDraw (google/tinyquickdraw) -> PyTorch DataLoaders.
The Kaggle dataset ships one `<category>.ndjson` file per class. Every line is a
JSON object with a `"drawing"` field: a list of strokes, each stroke being
`[[x0, x1, ...], [y0, y1, ...]]` with coordinates already simplified to 0..255.
This module only builds the datasets: it rasterizes those strokes to
`IMG_SIZE x IMG_SIZE` grayscale images and exposes `train_DS` / `Test_DS`
(plus `CLASSES` / `NUM_CLASSES`). DataLoaders live in `dataloader.py`.
"""
import io
import json
import zipfile
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import Dataset
import kagglehub
# ---------------------------------------------------------------------------
# config (tweak these; the rasterized cache is keyed off IMG_SIZE / MAX_PER_CLASS)
# ---------------------------------------------------------------------------
IMG_SIZE = 28 # output raster size (IMG_SIZE x IMG_SIZE)
MAX_PER_CLASS = 3000 # cap drawings loaded per category (RAM / speed)
TEST_RATIO = 0.1 # fraction of each class held out for the test set
SEED = 42
CACHE_NAME = f"quickdraw_{IMG_SIZE}px_{MAX_PER_CLASS}.npz"
# ---------------------------------------------------------------------------
# loading + rasterization
# ---------------------------------------------------------------------------
def _iter_ndjson(root: Path):
"""Yield (class_name, binary_file_handle) for every .ndjson in the dataset.
Handles both an already-extracted folder and a single downloaded archive.
"""
files = sorted(root.rglob("*.ndjson"))
if files:
for p in files:
with p.open("rb") as fh:
yield p.stem, fh
return
archives = sorted(root.rglob("*.archive")) + sorted(root.rglob("*.zip"))
if not archives:
raise FileNotFoundError(f"no .ndjson or archive found under {root}")
with zipfile.ZipFile(archives[0]) as zf:
for info in sorted(zf.infolist(), key=lambda i: i.filename):
if info.filename.endswith(".ndjson"):
with zf.open(info) as fh:
yield Path(info.filename).stem, fh
def _draw_line(img, x0, y0, x1, y1):
n = int(max(abs(x1 - x0), abs(y1 - y0))) + 1
xs = np.clip(np.round(np.linspace(x0, x1, n)), 0, img.shape[1] - 1).astype(int)
ys = np.clip(np.round(np.linspace(y0, y1, n)), 0, img.shape[0] - 1).astype(int)
img[ys, xs] = 255
def _rasterize(drawing, size=IMG_SIZE):
img = np.zeros((size, size), dtype=np.uint8)
scale = (size - 1) / 255.0
for stroke in drawing:
xs = np.asarray(stroke[0], dtype=np.float32) * scale
ys = np.asarray(stroke[1], dtype=np.float32) * scale
if len(xs) == 1:
_draw_line(img, xs[0], ys[0], xs[0], ys[0])
for i in range(len(xs) - 1):
_draw_line(img, xs[i], ys[i], xs[i + 1], ys[i + 1])
return img
def _build_arrays():
"""Return (images uint8 [N,H,W], labels int64 [N], class_names list)."""
root = Path(kagglehub.dataset_download("google/tinyquickdraw"))
cache = root / CACHE_NAME
if cache.exists():
d = np.load(cache, allow_pickle=True)
return d["X"], d["y"], list(d["classes"])
X_parts, y_parts, classes = [], [], []
for label, (name, fh) in enumerate(_iter_ndjson(root)):
classes.append(name)
imgs = []
for line in io.TextIOWrapper(fh, encoding="utf-8"):
line = line.strip()
if not line:
continue
imgs.append(_rasterize(json.loads(line)["drawing"]))
if len(imgs) >= MAX_PER_CLASS:
break
arr = np.stack(imgs).astype(np.uint8)
X_parts.append(arr)
y_parts.append(np.full(len(arr), label, dtype=np.int64))
print(f"[{label:>3}] {name:<28} {len(arr)}")
X = np.concatenate(X_parts)
y = np.concatenate(y_parts)
np.savez_compressed(cache, X=X, y=y, classes=np.array(classes, dtype=object))
print(f"cached -> {cache}")
return X, y, classes
# ---------------------------------------------------------------------------
# dataset
# ---------------------------------------------------------------------------
class QuickDrawDataset(Dataset):
def __init__(self, images_u8: torch.Tensor, labels: torch.Tensor):
self.images = images_u8 # uint8 (N, H, W)
self.labels = labels # int64 (N,)
def __len__(self):
return len(self.labels)
def __getitem__(self, i):
img = self.images[i].float().div_(255.0).unsqueeze(0) # (1, H, W) in [0, 1]
return img, self.labels[i]
def _make_datasets():
X, y, classes = _build_arrays()
images = torch.from_numpy(np.ascontiguousarray(X))
labels = torch.from_numpy(np.ascontiguousarray(y)).long()
rng = np.random.default_rng(SEED)
train_idx, test_idx = [], []
for c in range(len(classes)):
idx = np.where(y == c)[0]
rng.shuffle(idx)
cut = int(len(idx) * TEST_RATIO)
test_idx.append(idx[:cut])
train_idx.append(idx[cut:])
train_idx = torch.from_numpy(np.concatenate(train_idx))
test_idx = torch.from_numpy(np.concatenate(test_idx))
train_DS = QuickDrawDataset(images[train_idx], labels[train_idx])
Test_DS = QuickDrawDataset(images[test_idx], labels[test_idx])
return train_DS, Test_DS, classes
train_DS, Test_DS, CLASSES = _make_datasets()
NUM_CLASSES = len(CLASSES)