Spaces:
Sleeping
Sleeping
File size: 7,357 Bytes
c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b 948c799 c3905ef e8577ab e8074db 586661b 2a15372 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 81c6237 c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef 586661b c3905ef | 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | import random
from collections import Counter
from typing import Dict, List, Tuple
import pandas as pd
import torch
from PIL import Image
from datasets import load_dataset, DatasetDict
from torch.utils.data import Dataset, DataLoader, Subset
from torchvision import transforms
from config import HF_DATASET_REPO, HF_TOKEN, IMAGE_SIZE, RANDOM_SEED
_RAW_DATASET = None
_CLASS_NAMES = None
_SPLITS = None
class HFDatasetWrapper(Dataset):
def __init__(self, hf_dataset, transform):
self.dataset = hf_dataset
self.transform = transform
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
item = self.dataset[idx]
image = item["image"]
if not isinstance(image, Image.Image):
image = Image.open(image)
image = image.convert("RGB")
label = int(item["label"])
if self.transform:
image = self.transform(image)
return image, label
def get_train_transform():
return transforms.Compose(
[
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomRotation(degrees=5),
transforms.ToTensor(),
transforms.Normalize(
mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225),
),
]
)
def get_eval_transform():
return transforms.Compose(
[
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
transforms.ToTensor(),
transforms.Normalize(
mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225),
),
]
)
def load_raw_dataset():
global _RAW_DATASET, _CLASS_NAMES
if _RAW_DATASET is not None:
return _RAW_DATASET, _CLASS_NAMES
if not HF_TOKEN:
raise RuntimeError(
"HF_TOKEN est manquant. Ajoutez-le dans les Secrets du Space Hugging Face."
)
raw = load_dataset(HF_DATASET_REPO, token=HF_TOKEN)
if "train" not in raw:
raise RuntimeError("Le dataset Hugging Face doit contenir au moins un split 'train'.")
label_feature = raw["train"].features["label"]
if hasattr(label_feature, "names") and label_feature.names:
class_names = label_feature.names
else:
labels = list(raw["train"]["label"])
class_names = [str(x) for x in sorted(set(labels))]
_RAW_DATASET = raw
_CLASS_NAMES = class_names
return _RAW_DATASET, _CLASS_NAMES
def prepare_splits(
train_ratio: float = 0.70,
val_ratio: float = 0.15,
test_ratio: float = 0.15,
):
global _SPLITS
if _SPLITS is not None:
return _SPLITS
raw, class_names = load_raw_dataset()
if "validation" in raw and "test" in raw:
_SPLITS = {
"train": raw["train"],
"validation": raw["validation"],
"test": raw["test"],
}
return _SPLITS
if "test" in raw:
train_val = raw["train"]
test = raw["test"]
relative_val_ratio = val_ratio / (train_ratio + val_ratio)
try:
split_train_val = train_val.train_test_split(
test_size=relative_val_ratio,
seed=RANDOM_SEED,
stratify_by_column="label",
)
except Exception:
split_train_val = train_val.train_test_split(
test_size=relative_val_ratio,
seed=RANDOM_SEED,
)
_SPLITS = {
"train": split_train_val["train"],
"validation": split_train_val["test"],
"test": test,
}
return _SPLITS
full = raw["train"]
try:
first_split = full.train_test_split(
test_size=(val_ratio + test_ratio),
seed=RANDOM_SEED,
stratify_by_column="label",
)
except Exception:
first_split = full.train_test_split(
test_size=(val_ratio + test_ratio),
seed=RANDOM_SEED,
)
temp = first_split["test"]
relative_test_ratio = test_ratio / (val_ratio + test_ratio)
try:
second_split = temp.train_test_split(
test_size=relative_test_ratio,
seed=RANDOM_SEED,
stratify_by_column="label",
)
except Exception:
second_split = temp.train_test_split(
test_size=relative_test_ratio,
seed=RANDOM_SEED,
)
_SPLITS = {
"train": first_split["train"],
"validation": second_split["train"],
"test": second_split["test"],
}
return _SPLITS
def get_class_names() -> List[str]:
_, class_names = load_raw_dataset()
return class_names
def make_loaders(batch_size: int):
splits = prepare_splits()
class_names = get_class_names()
train_dataset = HFDatasetWrapper(splits["train"], get_train_transform())
val_dataset = HFDatasetWrapper(splits["validation"], get_eval_transform())
test_dataset = HFDatasetWrapper(splits["test"], get_eval_transform())
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
return train_loader, val_loader, test_loader, class_names
def dataset_overview() -> Tuple[dict, pd.DataFrame]:
splits = prepare_splits()
class_names = get_class_names()
rows = []
total = 0
for split_name, split_data in splits.items():
labels = list(split_data["label"])
counter = Counter(labels)
split_total = len(labels)
total += split_total
for label_id, count in sorted(counter.items()):
rows.append(
{
"split": split_name,
"classe": class_names[int(label_id)],
"nombre_images": count,
}
)
df = pd.DataFrame(rows)
summary = {
"dataset": HF_DATASET_REPO,
"nombre_total_images": total,
"nombre_classes": len(class_names),
"train": len(splits["train"]),
"validation": len(splits["validation"]),
"test": len(splits["test"]),
}
return summary, df
def get_images_for_gallery(split_name: str, class_name: str, max_images: int = 24):
splits = prepare_splits()
class_names = get_class_names()
if split_name not in splits:
split_name = "train"
dataset = splits[split_name]
if class_name and class_name != "Toutes les classes":
class_id = class_names.index(class_name)
indices = [i for i, x in enumerate(dataset["label"]) if int(x) == class_id]
else:
indices = list(range(len(dataset)))
if not indices:
return []
sample_indices = random.sample(indices, min(max_images, len(indices)))
gallery = []
for idx in sample_indices:
item = dataset[idx]
image = item["image"]
if not isinstance(image, Image.Image):
image = Image.open(image)
image = image.convert("RGB")
label_id = int(item["label"])
label_name = class_names[label_id]
gallery.append((image, label_name))
return gallery |