Datasets:
v3: deep clean via per-source cross-val + manual curation
Browse files- Per-source iterative cross-val cleaning (3 rounds each)
- racing-original: 99.6% OOF, removed all mislabeled 4s (were actually 1s)
- sebring: 82% OOF, removed 91 noisy transition frames
- mnist: 99% OOF, removed 30 ambiguous digits
- Fixed Paul Ricard gear_4 label CSV (was pointing to a 3)
- Fixed gear_4 example image
- Per-label verification composites in composites/{train,validation}/
- 5,391 train / 903 val
- composites/train/label_1.png +2 -2
- composites/train/label_2.png +2 -2
- composites/train/label_3.png +2 -2
- composites/train/label_4.png +2 -2
- composites/train/label_5.png +2 -2
- composites/train/label_6.png +2 -2
- composites/train/label_7.png +2 -2
- composites/validation/label_1.png +2 -2
- composites/validation/label_2.png +2 -2
- composites/validation/label_3.png +2 -2
- composites/validation/label_4.png +2 -2
- composites/validation/label_5.png +2 -2
- composites/validation/label_6.png +2 -2
- composites/validation/label_7.png +2 -2
- data/train-00000-of-00001.parquet +2 -2
- data/validation-00000-of-00001.parquet +2 -2
- examples/paul-ricard-alpine/gear_4.png +2 -2
- labels/paul-ricard-alpine.csv +14 -13
- scripts/relabel_clean.py +166 -0
composites/train/label_1.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/train/label_2.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/train/label_3.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/train/label_4.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/train/label_5.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/train/label_6.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/train/label_7.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_1.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_2.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_3.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_4.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_5.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_6.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
composites/validation/label_7.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
data/train-00000-of-00001.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:40864d586b97dd25b57946e2eae95232fe1fabfff874a23a930545d5ac6007ef
|
| 3 |
+
size 2898683
|
data/validation-00000-of-00001.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c8124233735f356a205b1d96dde0c0c0ab075366725461a444c0c6464989b8ee
|
| 3 |
+
size 482795
|
examples/paul-ricard-alpine/gear_4.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
labels/paul-ricard-alpine.csv
CHANGED
|
@@ -6,33 +6,34 @@ start,end,label
|
|
| 6 |
44,47,6
|
| 7 |
48,79,3
|
| 8 |
80,84,3
|
| 9 |
-
85,
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
105,117,3
|
| 14 |
118,127,3
|
| 15 |
128,137,2
|
| 16 |
-
138,
|
| 17 |
-
140,158,2
|
| 18 |
159,166,2
|
| 19 |
167,175,2
|
| 20 |
176,181,2
|
| 21 |
182,189,2
|
| 22 |
190,199,3
|
| 23 |
-
200,
|
| 24 |
-
|
| 25 |
-
|
| 26 |
238,242,5
|
| 27 |
243,246,6
|
| 28 |
247,258,7
|
| 29 |
259,278,7
|
| 30 |
279,296,7
|
| 31 |
297,298,7
|
| 32 |
-
299,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
320,334,3
|
| 37 |
335,337,3
|
| 38 |
338,345,1
|
|
|
|
| 6 |
44,47,6
|
| 7 |
48,79,3
|
| 8 |
80,84,3
|
| 9 |
+
85,86,3
|
| 10 |
+
87,91,5
|
| 11 |
+
92,96,5
|
| 12 |
+
97,97,4
|
| 13 |
+
98,104,3
|
| 14 |
105,117,3
|
| 15 |
118,127,3
|
| 16 |
128,137,2
|
| 17 |
+
138,158,2
|
|
|
|
| 18 |
159,166,2
|
| 19 |
167,175,2
|
| 20 |
176,181,2
|
| 21 |
182,189,2
|
| 22 |
190,199,3
|
| 23 |
+
200,204,3
|
| 24 |
+
205,210,5
|
| 25 |
+
211,237,5
|
| 26 |
238,242,5
|
| 27 |
243,246,6
|
| 28 |
247,258,7
|
| 29 |
259,278,7
|
| 30 |
279,296,7
|
| 31 |
297,298,7
|
| 32 |
+
299,299,6
|
| 33 |
+
300,301,5
|
| 34 |
+
302,302,4
|
| 35 |
+
303,303,4
|
| 36 |
+
304,319,3
|
| 37 |
320,334,3
|
| 38 |
335,337,3
|
| 39 |
338,345,1
|
scripts/relabel_clean.py
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Clean the dataset by training on MNIST (known-clean labels) + Paul Ricard
|
| 2 |
+
(visually verified, very clear digits), then using that model to filter/relabel
|
| 3 |
+
all racing data.
|
| 4 |
+
|
| 5 |
+
Strategy:
|
| 6 |
+
1. Train a strong CNN on MNIST + Paul Ricard (both have clean, unambiguous labels)
|
| 7 |
+
2. Run inference on ALL training data
|
| 8 |
+
3. Keep only samples where model agrees with label (or relabel if model is very confident)
|
| 9 |
+
4. Do the same for val
|
| 10 |
+
5. Rebuild parquet files and composites
|
| 11 |
+
|
| 12 |
+
Usage:
|
| 13 |
+
uv run python scripts/relabel_clean.py
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import numpy as np
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import io
|
| 19 |
+
import os
|
| 20 |
+
|
| 21 |
+
import torch
|
| 22 |
+
import torch.nn as nn
|
| 23 |
+
import torch.optim as optim
|
| 24 |
+
from torch.utils.data import DataLoader, TensorDataset
|
| 25 |
+
from PIL import Image
|
| 26 |
+
from datasets import Dataset, Image as HFImage
|
| 27 |
+
|
| 28 |
+
if __name__ != "__main__":
|
| 29 |
+
import sys; sys.exit(0)
|
| 30 |
+
|
| 31 |
+
device = torch.device("cuda" if torch.cuda.is_available()
|
| 32 |
+
else "mps" if torch.backends.mps.is_available()
|
| 33 |
+
else "cpu")
|
| 34 |
+
print(f"Using device: {device}")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class CNN(nn.Module):
|
| 38 |
+
def __init__(self):
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.features = nn.Sequential(
|
| 41 |
+
nn.Conv2d(1, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2),
|
| 42 |
+
nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2),
|
| 43 |
+
nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d(4))
|
| 44 |
+
self.classifier = nn.Sequential(
|
| 45 |
+
nn.Flatten(), nn.Linear(128 * 16, 256), nn.ReLU(), nn.Dropout(0.4), nn.Linear(256, 10))
|
| 46 |
+
|
| 47 |
+
def forward(self, x):
|
| 48 |
+
return self.classifier(self.features(x))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def load_images(df):
|
| 52 |
+
imgs = []
|
| 53 |
+
for _, row in df.iterrows():
|
| 54 |
+
img = Image.open(io.BytesIO(row["image"]["bytes"])).convert("L")
|
| 55 |
+
imgs.append(np.array(img, dtype=np.float32) / 255.0)
|
| 56 |
+
return np.stack(imgs)[:, np.newaxis, :, :]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def train_model(X, y, epochs=40):
|
| 60 |
+
model = CNN().to(device)
|
| 61 |
+
opt = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-4)
|
| 62 |
+
scheduler = optim.lr_scheduler.CosineAnnealingLR(opt, epochs)
|
| 63 |
+
crit = nn.CrossEntropyLoss()
|
| 64 |
+
loader = DataLoader(
|
| 65 |
+
TensorDataset(torch.tensor(X), torch.tensor(y, dtype=torch.long)),
|
| 66 |
+
batch_size=64, shuffle=True)
|
| 67 |
+
|
| 68 |
+
model.train()
|
| 69 |
+
for epoch in range(epochs):
|
| 70 |
+
total_loss = 0
|
| 71 |
+
for xb, yb in loader:
|
| 72 |
+
xb, yb = xb.to(device), yb.to(device)
|
| 73 |
+
opt.zero_grad()
|
| 74 |
+
loss = crit(model(xb), yb)
|
| 75 |
+
loss.backward()
|
| 76 |
+
opt.step()
|
| 77 |
+
total_loss += loss.item()
|
| 78 |
+
scheduler.step()
|
| 79 |
+
if (epoch + 1) % 10 == 0:
|
| 80 |
+
print(f" epoch {epoch+1}/{epochs} loss={total_loss/len(loader):.4f}")
|
| 81 |
+
|
| 82 |
+
return model
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def predict(model, X):
|
| 86 |
+
model.eval()
|
| 87 |
+
with torch.no_grad():
|
| 88 |
+
probs = torch.softmax(model(torch.tensor(X).to(device)), dim=1).cpu().numpy()
|
| 89 |
+
return probs
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# --- Step 1: Build clean seed dataset ---
|
| 93 |
+
print("\n=== Building clean seed dataset ===")
|
| 94 |
+
train_df = pd.read_parquet("data/train-00000-of-00001.parquet")
|
| 95 |
+
val_df = pd.read_parquet("data/validation-00000-of-00001.parquet")
|
| 96 |
+
|
| 97 |
+
# MNIST from train (known clean)
|
| 98 |
+
mnist = train_df[train_df["source"] == "mnist"]
|
| 99 |
+
print(f"MNIST: {len(mnist)} samples")
|
| 100 |
+
|
| 101 |
+
# Paul Ricard from train (visually verified, very clear white-on-black)
|
| 102 |
+
paul_ricard = train_df[train_df["source"] == "paul-ricard-alpine"]
|
| 103 |
+
print(f"Paul Ricard: {len(paul_ricard)} samples")
|
| 104 |
+
|
| 105 |
+
seed = pd.concat([mnist, paul_ricard], ignore_index=True)
|
| 106 |
+
X_seed = load_images(seed)
|
| 107 |
+
y_seed = seed["label"].values
|
| 108 |
+
print(f"Seed dataset: {len(seed)} samples")
|
| 109 |
+
|
| 110 |
+
# --- Step 2: Train ensemble on seed ---
|
| 111 |
+
print("\n=== Training 5-model ensemble on seed data ===")
|
| 112 |
+
models = []
|
| 113 |
+
for i in range(5):
|
| 114 |
+
print(f"Model {i+1}/5:")
|
| 115 |
+
# Bootstrap
|
| 116 |
+
idx = np.random.choice(len(X_seed), len(X_seed), replace=True)
|
| 117 |
+
model = train_model(X_seed[idx], y_seed[idx])
|
| 118 |
+
models.append(model)
|
| 119 |
+
|
| 120 |
+
# --- Step 3: Predict on all data ---
|
| 121 |
+
print("\n=== Predicting on all data ===")
|
| 122 |
+
|
| 123 |
+
for split_name, df in [("train", train_df), ("validation", val_df)]:
|
| 124 |
+
X = load_images(df)
|
| 125 |
+
y = df["label"].values
|
| 126 |
+
sources = df["source"].values
|
| 127 |
+
|
| 128 |
+
# Ensemble predict
|
| 129 |
+
probs = np.zeros((len(X), 10))
|
| 130 |
+
for model in models:
|
| 131 |
+
probs += predict(model, X)
|
| 132 |
+
probs /= len(models)
|
| 133 |
+
|
| 134 |
+
preds = probs.argmax(axis=1)
|
| 135 |
+
conf = probs.max(axis=1)
|
| 136 |
+
|
| 137 |
+
# For MNIST: keep as-is (they're the seed)
|
| 138 |
+
# For racing: keep only where model agrees OR model is not confident
|
| 139 |
+
keep = np.ones(len(df), dtype=bool)
|
| 140 |
+
|
| 141 |
+
for i in range(len(df)):
|
| 142 |
+
if sources[i] == "mnist":
|
| 143 |
+
continue # always keep
|
| 144 |
+
if preds[i] != y[i] and conf[i] > 0.5:
|
| 145 |
+
keep[i] = False
|
| 146 |
+
|
| 147 |
+
dropped = (~keep).sum()
|
| 148 |
+
print(f"\n{split_name}: {len(df)} -> {len(df) - dropped} (dropping {dropped})")
|
| 149 |
+
|
| 150 |
+
# Per-label breakdown
|
| 151 |
+
for label in range(10):
|
| 152 |
+
mask = y == label
|
| 153 |
+
drop_mask = mask & ~keep
|
| 154 |
+
if drop_mask.sum() > 0:
|
| 155 |
+
pred_dist = pd.Series(preds[drop_mask]).value_counts().to_dict()
|
| 156 |
+
print(f" label={label}: drop {drop_mask.sum()}/{mask.sum()} -> model says {pred_dist}")
|
| 157 |
+
|
| 158 |
+
df_clean = df[keep].reset_index(drop=True)
|
| 159 |
+
print(f" Final distribution:")
|
| 160 |
+
print(pd.crosstab(df_clean["label"], df_clean["source"]))
|
| 161 |
+
|
| 162 |
+
ds = Dataset.from_pandas(df_clean)
|
| 163 |
+
ds = ds.cast_column("image", HFImage())
|
| 164 |
+
ds.to_parquet(f"data/{split_name}-00000-of-00001.parquet")
|
| 165 |
+
|
| 166 |
+
print("\nDone! Now run: uv run python scripts/make_composites.py")
|