-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatasets.py
More file actions
157 lines (125 loc) · 5.64 KB
/
Copy pathdatasets.py
File metadata and controls
157 lines (125 loc) · 5.64 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
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
"""datasets.py – utilidades de carga y división de datasets para Continual Learning
(1) Carga datasets comunes mediante ``torchvision``.
(2) Genera divisiones por tareas *task‑incremental* (p.ej. Split CIFAR‑10 → 5 tasks * 2 clases).
(3) Expone un pequeño *smoke‑test* en el bloque *main* para verificar que todo corre.
Uso rápido (smoke test)
-----------------------
$ python datasets.py --dataset split_cifar10
Mostrará algo como:
```
[split_cifar10] task=0 classes=[0, 1] train=10000 test=2000
[split_cifar10] task=1 classes=[2, 3] train=10000 test=2000
...
```
"""
from __future__ import annotations
import argparse
from pathlib import Path
from typing import Dict, List, Tuple
import torch
from torch.utils.data import Dataset, Subset
from torchvision.datasets import CIFAR10, CIFAR100
import torchvision.transforms as T
__all__ = [
"load_cifar10",
"load_cifar100",
"make_class_splits",
"build_split_datasets",
]
# ---------------------------------------------------------------------------
# 1. Carga de datasets base
# ---------------------------------------------------------------------------
def _default_transform(img_size: int | None = None):
tx = [T.ToTensor()]
if img_size is not None:
tx.insert(0, T.Resize(img_size))
return T.Compose(tx)
def load_cifar10(root: str | Path = "~/.torchvision", *, img_size: int | None = None):
"""Devuelve (trainset, testset) de CIFAR‑10 con transform básica."""
root = Path(root).expanduser()
tfm = _default_transform(img_size)
trainset = CIFAR10(root, train=True, download=True, transform=tfm)
testset = CIFAR10(root, train=False, download=True, transform=tfm)
return trainset, testset
def load_cifar100(root: str | Path = "~/.torchvision", *, img_size: int | None = None):
root = Path(root).expanduser()
tfm = _default_transform(img_size)
trainset = CIFAR100(root, train=True, download=True, transform=tfm)
testset = CIFAR100(root, train=False, download=True, transform=tfm)
return trainset, testset
# ---------------------------------------------------------------------------
# 2. Construcción de splits por clases
# ---------------------------------------------------------------------------
def make_class_splits(
trainset: Dataset,
testset: Dataset,
classes_per_task: int,
) -> Tuple[List[List[int]], List[List[int]]]:
"""Agrupa índices por tareas con *classes_per_task* etiquetas cada una.
Devuelve:
train_idx_tasks, test_idx_tasks – listas de lista de índices.
"""
num_classes = len(set(trainset.targets)) # CIFAR = 10/100
assert num_classes % classes_per_task == 0, "El nº de clases debe dividirse exacto en las tareas"
# Mapear etiqueta → índices
idx_by_class_train: Dict[int, List[int]] = {c: [] for c in range(num_classes)}
for i, lbl in enumerate(trainset.targets):
idx_by_class_train[lbl].append(i)
idx_by_class_test: Dict[int, List[int]] = {c: [] for c in range(num_classes)}
for i, lbl in enumerate(testset.targets):
idx_by_class_test[lbl].append(i)
train_tasks, test_tasks = [], []
for start in range(0, num_classes, classes_per_task):
cls_slice = list(range(start, start + classes_per_task))
train_tasks.append(sum((idx_by_class_train[c] for c in cls_slice), []))
test_tasks.append(sum((idx_by_class_test[c] for c in cls_slice), []))
return train_tasks, test_tasks
# ---------------------------------------------------------------------------
# 3. Helper de alto nivel: build_split_datasets
# ---------------------------------------------------------------------------
def build_split_datasets(
dataset: str,
classes_per_task: int,
root: str | Path = "~/.torchvision",
img_size: int | None = None,
):
"""Devuelve listas de *Subset* PyTorch por task.
Ejemplo:
>>> train_tasks, test_tasks = build_split_datasets("cifar10", 2)
"""
if dataset == "cifar10":
trainset, testset = load_cifar10(root, img_size=img_size)
elif dataset == "cifar100":
trainset, testset = load_cifar100(root, img_size=img_size)
else:
raise ValueError(f"Dataset no soportado: {dataset}")
train_idx_tasks, test_idx_tasks = make_class_splits(trainset, testset, classes_per_task)
train_subsets = [Subset(trainset, idxs) for idxs in train_idx_tasks]
test_subsets = [Subset(testset, idxs) for idxs in test_idx_tasks]
return train_subsets, test_subsets
# ---------------------------------------------------------------------------
# 4. Smoke‑test CLI
# ---------------------------------------------------------------------------
def _smoke_test(args):
train_ts, test_ts = build_split_datasets(args.dataset, args.cpt, img_size=args.img)
for t, (tr, te) in enumerate(zip(train_ts, test_ts)):
cls_start = t * args.cpt
cls_end = cls_start + args.cpt - 1
print(
f"[{args.dataset}] task={t} classes=[{cls_start}, {cls_end}] "
f"train={len(tr)} test={len(te)}"
)
def _parse_args():
p = argparse.ArgumentParser(description="Smoke test de datasets para CL")
p.add_argument("--dataset", choices=["cifar10", "cifar100", "split_cifar10"], default="split_cifar10")
p.add_argument("--cpt", type=int, default=2, help="Clases por tarea")
p.add_argument("--img", type=int, default=None, help="Resize opcional de imagen (px)")
return p.parse_args()
if __name__ == "__main__":
args = _parse_args()
# Alias para conveniencia – split_cifar10 ≡ cifar10 con 2 clases/tarea
if args.dataset == "split_cifar10":
args.dataset = "cifar10"
args.cpt = 5
_smoke_test(args)