Spaces:
Sleeping
Sleeping
File size: 24,482 Bytes
33d4721 |
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
import os
import shutil
import uuid
from dataclasses import dataclass
from typing import Optional
import pandas as pd
from datasets import ClassLabel, Features, Image, Sequence, Value, load_dataset
from sklearn.model_selection import train_test_split
ALLOWED_EXTENSIONS = ("jpeg", "png", "jpg", "JPG", "JPEG", "PNG")
@dataclass
class ImageClassificationPreprocessor:
"""
A class used to preprocess image data for classification tasks.
Attributes
----------
train_data : str
Path to the training data directory.
username : str
Username for the Hugging Face Hub.
project_name : str
Name of the project.
token : str
Authentication token for the Hugging Face Hub.
valid_data : Optional[str], optional
Path to the validation data directory, by default None.
test_size : Optional[float], optional
Proportion of the dataset to include in the validation split, by default 0.2.
seed : Optional[int], optional
Random seed for reproducibility, by default 42.
local : Optional[bool], optional
Whether to save the dataset locally or push to the Hugging Face Hub, by default False.
Methods
-------
__post_init__():
Validates the structure and contents of the training and validation data directories.
split(df: pd.DataFrame) -> Tuple[pd.DataFrame, pd.DataFrame]:
Splits the dataframe into training and validation sets.
prepare() -> str:
Prepares the dataset for training and either saves it locally or pushes it to the Hugging Face Hub.
"""
train_data: str
username: str
project_name: str
token: str
valid_data: Optional[str] = None
test_size: Optional[float] = 0.2
seed: Optional[int] = 42
local: Optional[bool] = False
def __post_init__(self):
# Check if train data path exists
if not os.path.exists(self.train_data):
raise ValueError(f"{self.train_data} does not exist.")
# Check if train data path contains at least 2 folders
subfolders = [f.path for f in os.scandir(self.train_data) if f.is_dir()]
# list subfolders
if len(subfolders) < 2:
raise ValueError(f"{self.train_data} should contain at least 2 subfolders.")
# Check if each subfolder contains at least 5 image files in jpeg, png or jpg format only
for subfolder in subfolders:
image_files = [f for f in os.listdir(subfolder) if f.endswith(ALLOWED_EXTENSIONS)]
if len(image_files) < 5:
raise ValueError(f"{subfolder} should contain at least 5 jpeg, png or jpg files.")
# Check if there are no other files except image files in the subfolder
if len(image_files) != len(os.listdir(subfolder)):
raise ValueError(f"{subfolder} should not contain any other files except image files.")
# Check if there are no subfolders inside subfolders
subfolders_in_subfolder = [f.path for f in os.scandir(subfolder) if f.is_dir()]
if len(subfolders_in_subfolder) > 0:
raise ValueError(f"{subfolder} should not contain any subfolders.")
if self.valid_data:
# Check if valid data path exists
if not os.path.exists(self.valid_data):
raise ValueError(f"{self.valid_data} does not exist.")
# Check if valid data path contains at least 2 folders
subfolders = [f.path for f in os.scandir(self.valid_data) if f.is_dir()]
# make sure that the subfolders in train and valid data are the same
train_subfolders = set(os.path.basename(f.path) for f in os.scandir(self.train_data) if f.is_dir())
valid_subfolders = set(os.path.basename(f.path) for f in os.scandir(self.valid_data) if f.is_dir())
if train_subfolders != valid_subfolders:
raise ValueError(f"{self.valid_data} should have the same subfolders as {self.train_data}.")
if len(subfolders) < 2:
raise ValueError(f"{self.valid_data} should contain at least 2 subfolders.")
# Check if each subfolder contains at least 5 image files in jpeg, png or jpg format only
for subfolder in subfolders:
image_files = [f for f in os.listdir(subfolder) if f.endswith(ALLOWED_EXTENSIONS)]
if len(image_files) < 5:
raise ValueError(f"{subfolder} should contain at least 5 jpeg, png or jpg files.")
# Check if there are no other files except image files in the subfolder
if len(image_files) != len(os.listdir(subfolder)):
raise ValueError(f"{subfolder} should not contain any other files except image files.")
# Check if there are no subfolders inside subfolders
subfolders_in_subfolder = [f.path for f in os.scandir(subfolder) if f.is_dir()]
if len(subfolders_in_subfolder) > 0:
raise ValueError(f"{subfolder} should not contain any subfolders.")
def split(self, df):
train_df, valid_df = train_test_split(
df,
test_size=self.test_size,
random_state=self.seed,
stratify=df["subfolder"],
)
train_df = train_df.reset_index(drop=True)
valid_df = valid_df.reset_index(drop=True)
return train_df, valid_df
def prepare(self):
random_uuid = uuid.uuid4()
cache_dir = os.environ.get("HF_HOME")
if not cache_dir:
cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "huggingface")
data_dir = os.path.join(cache_dir, "autotrain", str(random_uuid))
if self.valid_data:
shutil.copytree(self.train_data, os.path.join(data_dir, "train"))
shutil.copytree(self.valid_data, os.path.join(data_dir, "validation"))
dataset = load_dataset("imagefolder", data_dir=data_dir)
dataset = dataset.rename_columns({"image": "autotrain_image", "label": "autotrain_label"})
if self.local:
dataset.save_to_disk(f"{self.project_name}/autotrain-data")
else:
dataset.push_to_hub(
f"{self.username}/autotrain-data-{self.project_name}",
private=True,
token=self.token,
)
else:
subfolders = [f.path for f in os.scandir(self.train_data) if f.is_dir()]
image_filenames = []
subfolder_names = []
for subfolder in subfolders:
for filename in os.listdir(subfolder):
if filename.endswith(("jpeg", "png", "jpg")):
image_filenames.append(filename)
subfolder_names.append(os.path.basename(subfolder))
df = pd.DataFrame({"image_filename": image_filenames, "subfolder": subfolder_names})
train_df, valid_df = self.split(df)
for row in train_df.itertuples():
os.makedirs(os.path.join(data_dir, "train", row.subfolder), exist_ok=True)
shutil.copy(
os.path.join(self.train_data, row.subfolder, row.image_filename),
os.path.join(data_dir, "train", row.subfolder, row.image_filename),
)
for row in valid_df.itertuples():
os.makedirs(os.path.join(data_dir, "validation", row.subfolder), exist_ok=True)
shutil.copy(
os.path.join(self.train_data, row.subfolder, row.image_filename),
os.path.join(data_dir, "validation", row.subfolder, row.image_filename),
)
dataset = load_dataset("imagefolder", data_dir=data_dir)
dataset = dataset.rename_columns({"image": "autotrain_image", "label": "autotrain_label"})
if self.local:
dataset.save_to_disk(f"{self.project_name}/autotrain-data")
else:
dataset.push_to_hub(
f"{self.username}/autotrain-data-{self.project_name}",
private=True,
token=self.token,
)
if self.local:
return f"{self.project_name}/autotrain-data"
return f"{self.username}/autotrain-data-{self.project_name}"
@dataclass
class ObjectDetectionPreprocessor:
"""
A class to preprocess data for object detection tasks.
Attributes:
-----------
train_data : str
Path to the training data directory.
username : str
Username for the Hugging Face Hub.
project_name : str
Name of the project.
token : str
Authentication token for the Hugging Face Hub.
valid_data : Optional[str], default=None
Path to the validation data directory.
test_size : Optional[float], default=0.2
Proportion of the dataset to include in the validation split.
seed : Optional[int], default=42
Random seed for reproducibility.
local : Optional[bool], default=False
Whether to save the dataset locally or push to the Hugging Face Hub.
Methods:
--------
_process_metadata(data_path):
Processes the metadata.jsonl file and extracts required columns and categories.
__post_init__():
Validates the existence and content of the training and validation data directories.
split(df):
Splits the dataframe into training and validation sets.
prepare():
Prepares the dataset for training by processing metadata, splitting data, and saving or pushing the dataset.
"""
train_data: str
username: str
project_name: str
token: str
valid_data: Optional[str] = None
test_size: Optional[float] = 0.2
seed: Optional[int] = 42
local: Optional[bool] = False
@staticmethod
def _process_metadata(data_path):
metadata = pd.read_json(os.path.join(data_path, "metadata.jsonl"), lines=True)
# make sure that the metadata.jsonl file contains the required columns: file_name, objects
if "file_name" not in metadata.columns or "objects" not in metadata.columns:
raise ValueError(f"{data_path}/metadata.jsonl should contain 'file_name' and 'objects' columns.")
# keeo only file_name and objects columns
metadata = metadata[["file_name", "objects"]]
# inside metadata objects column, values should be bbox, area and category
# if area does not exist, it should be created by multiplying bbox width and height
categories = []
for _, row in metadata.iterrows():
obj = row["objects"]
if "bbox" not in obj or "category" not in obj:
raise ValueError(f"{data_path}/metadata.jsonl should contain 'bbox' and 'category' keys in 'objects'.")
# keep only bbox, area and category keys
obj = {k: obj[k] for k in ["bbox", "category"]}
categories.extend(obj["category"])
categories = set(categories)
return metadata, categories
def __post_init__(self):
# Check if train data path exists
if not os.path.exists(self.train_data):
raise ValueError(f"{self.train_data} does not exist.")
# check if self.train_data contains at least 5 image files in jpeg, png or jpg format only
train_image_files = [f for f in os.listdir(self.train_data) if f.endswith(ALLOWED_EXTENSIONS)]
if len(train_image_files) < 5:
raise ValueError(f"{self.train_data} should contain at least 5 jpeg, png or jpg files.")
# check if self.train_data contains a metadata.jsonl file
if "metadata.jsonl" not in os.listdir(self.train_data):
raise ValueError(f"{self.train_data} should contain a metadata.jsonl file.")
# Check if valid data path exists
if self.valid_data:
if not os.path.exists(self.valid_data):
raise ValueError(f"{self.valid_data} does not exist.")
# check if self.valid_data contains at least 5 image files in jpeg, png or jpg format only
valid_image_files = [f for f in os.listdir(self.valid_data) if f.endswith(ALLOWED_EXTENSIONS)]
if len(valid_image_files) < 5:
raise ValueError(f"{self.valid_data} should contain at least 5 jpeg, png or jpg files.")
# check if self.valid_data contains a metadata.jsonl file
if "metadata.jsonl" not in os.listdir(self.valid_data):
raise ValueError(f"{self.valid_data} should contain a metadata.jsonl file.")
def split(self, df):
train_df, valid_df = train_test_split(
df,
test_size=self.test_size,
random_state=self.seed,
)
train_df = train_df.reset_index(drop=True)
valid_df = valid_df.reset_index(drop=True)
return train_df, valid_df
def prepare(self):
random_uuid = uuid.uuid4()
cache_dir = os.environ.get("HF_HOME")
if not cache_dir:
cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "huggingface")
data_dir = os.path.join(cache_dir, "autotrain", str(random_uuid))
if self.valid_data:
shutil.copytree(self.train_data, os.path.join(data_dir, "train"))
shutil.copytree(self.valid_data, os.path.join(data_dir, "validation"))
train_metadata, train_categories = self._process_metadata(os.path.join(data_dir, "train"))
valid_metadata, valid_categories = self._process_metadata(os.path.join(data_dir, "validation"))
train_metadata.to_json(os.path.join(data_dir, "train", "metadata.jsonl"), orient="records", lines=True)
valid_metadata.to_json(
os.path.join(data_dir, "validation", "metadata.jsonl"), orient="records", lines=True
)
all_categories = train_categories.union(valid_categories)
features = Features(
{
"image": Image(),
"objects": Sequence(
{
"bbox": Sequence(Value("float32"), length=4),
"category": ClassLabel(names=list(all_categories)),
}
),
}
)
dataset = load_dataset("imagefolder", data_dir=data_dir, features=features)
dataset = dataset.rename_columns(
{
"image": "autotrain_image",
"objects": "autotrain_objects",
}
)
if self.local:
dataset.save_to_disk(f"{self.project_name}/autotrain-data")
else:
dataset.push_to_hub(
f"{self.username}/autotrain-data-{self.project_name}",
private=True,
token=self.token,
)
else:
metadata = pd.read_json(os.path.join(self.train_data, "metadata.jsonl"), lines=True)
train_df, valid_df = self.split(metadata)
# create train and validation folders
os.makedirs(os.path.join(data_dir, "train"), exist_ok=True)
os.makedirs(os.path.join(data_dir, "validation"), exist_ok=True)
# move images to train and validation folders
for row in train_df.iterrows():
shutil.copy(
os.path.join(self.train_data, row[1]["file_name"]),
os.path.join(data_dir, "train", row[1]["file_name"]),
)
for row in valid_df.iterrows():
shutil.copy(
os.path.join(self.train_data, row[1]["file_name"]),
os.path.join(data_dir, "validation", row[1]["file_name"]),
)
# save metadata.jsonl file to train and validation folders
train_df.to_json(os.path.join(data_dir, "train", "metadata.jsonl"), orient="records", lines=True)
valid_df.to_json(os.path.join(data_dir, "validation", "metadata.jsonl"), orient="records", lines=True)
train_metadata, train_categories = self._process_metadata(os.path.join(data_dir, "train"))
valid_metadata, valid_categories = self._process_metadata(os.path.join(data_dir, "validation"))
train_metadata.to_json(os.path.join(data_dir, "train", "metadata.jsonl"), orient="records", lines=True)
valid_metadata.to_json(
os.path.join(data_dir, "validation", "metadata.jsonl"), orient="records", lines=True
)
all_categories = train_categories.union(valid_categories)
features = Features(
{
"image": Image(),
"objects": Sequence(
{
"bbox": Sequence(Value("float32"), length=4),
"category": ClassLabel(names=list(all_categories)),
}
),
}
)
dataset = load_dataset("imagefolder", data_dir=data_dir, features=features)
dataset = dataset.rename_columns(
{
"image": "autotrain_image",
"objects": "autotrain_objects",
}
)
if self.local:
dataset.save_to_disk(f"{self.project_name}/autotrain-data")
else:
dataset.push_to_hub(
f"{self.username}/autotrain-data-{self.project_name}",
private=True,
token=self.token,
)
if self.local:
return f"{self.project_name}/autotrain-data"
return f"{self.username}/autotrain-data-{self.project_name}"
@dataclass
class ImageRegressionPreprocessor:
train_data: str
username: str
project_name: str
token: str
valid_data: Optional[str] = None
test_size: Optional[float] = 0.2
seed: Optional[int] = 42
local: Optional[bool] = False
@staticmethod
def _process_metadata(data_path):
metadata = pd.read_json(os.path.join(data_path, "metadata.jsonl"), lines=True)
# make sure that the metadata.jsonl file contains the required columns: file_name, target
if "file_name" not in metadata.columns or "target" not in metadata.columns:
raise ValueError(f"{data_path}/metadata.jsonl should contain 'file_name' and 'target' columns.")
# keep only file_name and target columns
metadata = metadata[["file_name", "target"]]
return metadata
def __post_init__(self):
# Check if train data path exists
if not os.path.exists(self.train_data):
raise ValueError(f"{self.train_data} does not exist.")
# check if self.train_data contains at least 5 image files in jpeg, png or jpg format only
train_image_files = [f for f in os.listdir(self.train_data) if f.endswith(ALLOWED_EXTENSIONS)]
if len(train_image_files) < 5:
raise ValueError(f"{self.train_data} should contain at least 5 jpeg, png or jpg files.")
# check if self.train_data contains a metadata.jsonl file
if "metadata.jsonl" not in os.listdir(self.train_data):
raise ValueError(f"{self.train_data} should contain a metadata.jsonl file.")
# Check if valid data path exists
if self.valid_data:
if not os.path.exists(self.valid_data):
raise ValueError(f"{self.valid_data} does not exist.")
# check if self.valid_data contains at least 5 image files in jpeg, png or jpg format only
valid_image_files = [f for f in os.listdir(self.valid_data) if f.endswith(ALLOWED_EXTENSIONS)]
if len(valid_image_files) < 5:
raise ValueError(f"{self.valid_data} should contain at least 5 jpeg, png or jpg files.")
# check if self.valid_data contains a metadata.jsonl file
if "metadata.jsonl" not in os.listdir(self.valid_data):
raise ValueError(f"{self.valid_data} should contain a metadata.jsonl file.")
def split(self, df):
train_df, valid_df = train_test_split(
df,
test_size=self.test_size,
random_state=self.seed,
)
train_df = train_df.reset_index(drop=True)
valid_df = valid_df.reset_index(drop=True)
return train_df, valid_df
def prepare(self):
random_uuid = uuid.uuid4()
cache_dir = os.environ.get("HF_HOME")
if not cache_dir:
cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "huggingface")
data_dir = os.path.join(cache_dir, "autotrain", str(random_uuid))
if self.valid_data:
shutil.copytree(self.train_data, os.path.join(data_dir, "train"))
shutil.copytree(self.valid_data, os.path.join(data_dir, "validation"))
train_metadata = self._process_metadata(os.path.join(data_dir, "train"))
valid_metadata = self._process_metadata(os.path.join(data_dir, "validation"))
train_metadata.to_json(os.path.join(data_dir, "train", "metadata.jsonl"), orient="records", lines=True)
valid_metadata.to_json(
os.path.join(data_dir, "validation", "metadata.jsonl"), orient="records", lines=True
)
dataset = load_dataset("imagefolder", data_dir=data_dir)
dataset = dataset.rename_columns(
{
"image": "autotrain_image",
"target": "autotrain_label",
}
)
if self.local:
dataset.save_to_disk(f"{self.project_name}/autotrain-data")
else:
dataset.push_to_hub(
f"{self.username}/autotrain-data-{self.project_name}",
private=True,
token=self.token,
)
else:
metadata = pd.read_json(os.path.join(self.train_data, "metadata.jsonl"), lines=True)
train_df, valid_df = self.split(metadata)
# create train and validation folders
os.makedirs(os.path.join(data_dir, "train"), exist_ok=True)
os.makedirs(os.path.join(data_dir, "validation"), exist_ok=True)
# move images to train and validation folders
for row in train_df.iterrows():
shutil.copy(
os.path.join(self.train_data, row[1]["file_name"]),
os.path.join(data_dir, "train", row[1]["file_name"]),
)
for row in valid_df.iterrows():
shutil.copy(
os.path.join(self.train_data, row[1]["file_name"]),
os.path.join(data_dir, "validation", row[1]["file_name"]),
)
# save metadata.jsonl file to train and validation folders
train_df.to_json(os.path.join(data_dir, "train", "metadata.jsonl"), orient="records", lines=True)
valid_df.to_json(os.path.join(data_dir, "validation", "metadata.jsonl"), orient="records", lines=True)
train_metadata = self._process_metadata(os.path.join(data_dir, "train"))
valid_metadata = self._process_metadata(os.path.join(data_dir, "validation"))
train_metadata.to_json(os.path.join(data_dir, "train", "metadata.jsonl"), orient="records", lines=True)
valid_metadata.to_json(
os.path.join(data_dir, "validation", "metadata.jsonl"), orient="records", lines=True
)
dataset = load_dataset("imagefolder", data_dir=data_dir)
dataset = dataset.rename_columns(
{
"image": "autotrain_image",
"target": "autotrain_label",
}
)
if self.local:
dataset.save_to_disk(f"{self.project_name}/autotrain-data")
else:
dataset.push_to_hub(
f"{self.username}/autotrain-data-{self.project_name}",
private=True,
token=self.token,
)
if self.local:
return f"{self.project_name}/autotrain-data"
return f"{self.username}/autotrain-data-{self.project_name}"
|