Support is_one and is_ten for noface-inet
Browse files- face-obfuscated-imagenet.py +44 -21
face-obfuscated-imagenet.py
CHANGED
@@ -135,6 +135,8 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
135 |
{
|
136 |
"image": datasets.Image(),
|
137 |
"label": datasets.ClassLabel(names=formatted_class_names),
|
|
|
|
|
138 |
}
|
139 |
),
|
140 |
supervised_keys=("image", "label"), # type: ignore
|
@@ -149,8 +151,10 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
149 |
|
150 |
def _split_generators(self, dl_manager):
|
151 |
if dl_manager.is_streaming:
|
152 |
-
raise NotImplementedError(
|
153 |
-
|
|
|
|
|
154 |
# cache location for the dataset downloads
|
155 |
inet_train_path = (
|
156 |
"/gpfs/data/shared/imagenet/ILSVRC2012/ILSVRC2012_img_train.tar"
|
@@ -347,6 +351,8 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
347 |
yield example_idx, {
|
348 |
"image": img,
|
349 |
"label": label,
|
|
|
|
|
350 |
}
|
351 |
example_idx += 1
|
352 |
|
@@ -385,6 +391,8 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
385 |
yield example_idx, {
|
386 |
"image": img,
|
387 |
"label": label,
|
|
|
|
|
388 |
}
|
389 |
|
390 |
def _generate_nonblur_subset(
|
@@ -421,6 +429,8 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
421 |
yield example_idx, {
|
422 |
"image": img,
|
423 |
"label": new_label,
|
|
|
|
|
424 |
}
|
425 |
example_idx += 1
|
426 |
|
@@ -469,70 +479,81 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
469 |
yield example_idx, {
|
470 |
"image": img,
|
471 |
"label": new_label,
|
|
|
|
|
472 |
}
|
473 |
example_idx += 1
|
474 |
|
475 |
def _generate_noface(self, archive_path, split, train_txt_url, val_txt_url):
|
476 |
def is_valid_file(path):
|
477 |
return os.path.isfile(path) and os.path.getsize(path) > 0
|
478 |
-
|
479 |
num_failed = 0
|
480 |
-
|
481 |
if split == "train":
|
482 |
with urllib.request.urlopen(train_txt_url) as f:
|
483 |
train_ids = [line.decode("utf-8").strip() for line in f if line.strip()]
|
484 |
-
|
485 |
if not hasattr(self, "_mapping"):
|
486 |
with urllib.request.urlopen(self._CLASS_INDEX_URL) as response:
|
487 |
self._mapping = json.load(response)
|
488 |
-
|
489 |
wnid_to_label = {
|
490 |
wnid: idx for idx, wnid in enumerate(self._get_class_names())
|
491 |
}
|
492 |
-
|
493 |
valid_entries = []
|
494 |
for file_id in train_ids:
|
495 |
-
|
496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
497 |
found = False
|
498 |
for ext in [".JPEG", ".jpg"]:
|
499 |
candidate_path = os.path.join(
|
500 |
archive_path, "train_blurred", wnid, img_name + ext
|
501 |
)
|
502 |
if is_valid_file(candidate_path):
|
503 |
-
valid_entries.append((candidate_path, wnid))
|
504 |
found = True
|
505 |
break
|
506 |
if not found:
|
507 |
num_failed += 1
|
508 |
-
|
509 |
print(f"[train] Skipped {num_failed} invalid or zero-byte files.")
|
510 |
-
|
511 |
example_idx = 0
|
512 |
-
for img_path, wnid in valid_entries:
|
513 |
label = wnid_to_label[wnid]
|
514 |
img = Image.open(img_path)
|
515 |
if img.mode != "RGB":
|
516 |
img = img.convert("RGB")
|
517 |
-
|
518 |
yield example_idx, {
|
519 |
"image": img,
|
520 |
"label": label,
|
|
|
|
|
521 |
}
|
522 |
example_idx += 1
|
523 |
-
|
524 |
if split == "validation":
|
525 |
with urllib.request.urlopen(val_txt_url) as f:
|
526 |
val_ids = [line.decode("utf-8").strip() for line in f if line.strip()]
|
527 |
-
|
528 |
if not hasattr(self, "_mapping"):
|
529 |
with urllib.request.urlopen(self._CLASS_INDEX_URL) as response:
|
530 |
self._mapping = json.load(response)
|
531 |
-
|
532 |
wnid_to_label = {
|
533 |
wnid: idx for idx, wnid in enumerate(self._get_class_names())
|
534 |
}
|
535 |
-
|
536 |
valid_entries = []
|
537 |
for file_id in val_ids:
|
538 |
wnid = file_id.split("_")[0]
|
@@ -548,19 +569,21 @@ class FaceObfuscatedImagenet(datasets.GeneratorBasedBuilder):
|
|
548 |
break
|
549 |
if not found:
|
550 |
num_failed += 1
|
551 |
-
|
552 |
print(f"[validation] Skipped {num_failed} invalid or zero-byte files.")
|
553 |
-
|
554 |
example_idx = 0
|
555 |
for img_path, wnid in valid_entries:
|
556 |
label = wnid_to_label[wnid]
|
557 |
img = Image.open(img_path)
|
558 |
if img.mode != "RGB":
|
559 |
img = img.convert("RGB")
|
560 |
-
|
561 |
yield example_idx, {
|
562 |
"image": img,
|
563 |
"label": label,
|
|
|
|
|
564 |
}
|
565 |
example_idx += 1
|
566 |
|
|
|
135 |
{
|
136 |
"image": datasets.Image(),
|
137 |
"label": datasets.ClassLabel(names=formatted_class_names),
|
138 |
+
"is_one": datasets.Value("bool"),
|
139 |
+
"is_ten": datasets.Value("bool"),
|
140 |
}
|
141 |
),
|
142 |
supervised_keys=("image", "label"), # type: ignore
|
|
|
151 |
|
152 |
def _split_generators(self, dl_manager):
|
153 |
if dl_manager.is_streaming:
|
154 |
+
raise NotImplementedError(
|
155 |
+
"Streaming is not yet supported for this dataset."
|
156 |
+
)
|
157 |
+
|
158 |
# cache location for the dataset downloads
|
159 |
inet_train_path = (
|
160 |
"/gpfs/data/shared/imagenet/ILSVRC2012/ILSVRC2012_img_train.tar"
|
|
|
351 |
yield example_idx, {
|
352 |
"image": img,
|
353 |
"label": label,
|
354 |
+
"is_one": False,
|
355 |
+
"is_ten": False,
|
356 |
}
|
357 |
example_idx += 1
|
358 |
|
|
|
391 |
yield example_idx, {
|
392 |
"image": img,
|
393 |
"label": label,
|
394 |
+
"is_one": False,
|
395 |
+
"is_ten": False,
|
396 |
}
|
397 |
|
398 |
def _generate_nonblur_subset(
|
|
|
429 |
yield example_idx, {
|
430 |
"image": img,
|
431 |
"label": new_label,
|
432 |
+
"is_one": False,
|
433 |
+
"is_ten": False,
|
434 |
}
|
435 |
example_idx += 1
|
436 |
|
|
|
479 |
yield example_idx, {
|
480 |
"image": img,
|
481 |
"label": new_label,
|
482 |
+
"is_one": False,
|
483 |
+
"is_ten": False,
|
484 |
}
|
485 |
example_idx += 1
|
486 |
|
487 |
def _generate_noface(self, archive_path, split, train_txt_url, val_txt_url):
|
488 |
def is_valid_file(path):
|
489 |
return os.path.isfile(path) and os.path.getsize(path) > 0
|
490 |
+
|
491 |
num_failed = 0
|
492 |
+
|
493 |
if split == "train":
|
494 |
with urllib.request.urlopen(train_txt_url) as f:
|
495 |
train_ids = [line.decode("utf-8").strip() for line in f if line.strip()]
|
496 |
+
|
497 |
if not hasattr(self, "_mapping"):
|
498 |
with urllib.request.urlopen(self._CLASS_INDEX_URL) as response:
|
499 |
self._mapping = json.load(response)
|
500 |
+
|
501 |
wnid_to_label = {
|
502 |
wnid: idx for idx, wnid in enumerate(self._get_class_names())
|
503 |
}
|
504 |
+
|
505 |
valid_entries = []
|
506 |
for file_id in train_ids:
|
507 |
+
# inputs are like n01440764_2708-0-0
|
508 |
+
# Split to get all parts: ["n01440764_2708", "0", "0"]
|
509 |
+
parts = file_id.split("-")
|
510 |
+
base_file_id = parts[0] # "n01440764_2708"
|
511 |
+
is_one = parts[1] == "1" if len(parts) > 1 else False
|
512 |
+
is_ten = parts[2] == "1" if len(parts) > 2 else False
|
513 |
+
|
514 |
+
wnid = base_file_id.split("_")[0]
|
515 |
+
img_name = base_file_id
|
516 |
found = False
|
517 |
for ext in [".JPEG", ".jpg"]:
|
518 |
candidate_path = os.path.join(
|
519 |
archive_path, "train_blurred", wnid, img_name + ext
|
520 |
)
|
521 |
if is_valid_file(candidate_path):
|
522 |
+
valid_entries.append((candidate_path, wnid, is_one, is_ten))
|
523 |
found = True
|
524 |
break
|
525 |
if not found:
|
526 |
num_failed += 1
|
527 |
+
|
528 |
print(f"[train] Skipped {num_failed} invalid or zero-byte files.")
|
529 |
+
|
530 |
example_idx = 0
|
531 |
+
for img_path, wnid, is_one, is_ten in valid_entries:
|
532 |
label = wnid_to_label[wnid]
|
533 |
img = Image.open(img_path)
|
534 |
if img.mode != "RGB":
|
535 |
img = img.convert("RGB")
|
536 |
+
|
537 |
yield example_idx, {
|
538 |
"image": img,
|
539 |
"label": label,
|
540 |
+
"is_one": is_one,
|
541 |
+
"is_ten": is_ten,
|
542 |
}
|
543 |
example_idx += 1
|
544 |
+
|
545 |
if split == "validation":
|
546 |
with urllib.request.urlopen(val_txt_url) as f:
|
547 |
val_ids = [line.decode("utf-8").strip() for line in f if line.strip()]
|
548 |
+
|
549 |
if not hasattr(self, "_mapping"):
|
550 |
with urllib.request.urlopen(self._CLASS_INDEX_URL) as response:
|
551 |
self._mapping = json.load(response)
|
552 |
+
|
553 |
wnid_to_label = {
|
554 |
wnid: idx for idx, wnid in enumerate(self._get_class_names())
|
555 |
}
|
556 |
+
|
557 |
valid_entries = []
|
558 |
for file_id in val_ids:
|
559 |
wnid = file_id.split("_")[0]
|
|
|
569 |
break
|
570 |
if not found:
|
571 |
num_failed += 1
|
572 |
+
|
573 |
print(f"[validation] Skipped {num_failed} invalid or zero-byte files.")
|
574 |
+
|
575 |
example_idx = 0
|
576 |
for img_path, wnid in valid_entries:
|
577 |
label = wnid_to_label[wnid]
|
578 |
img = Image.open(img_path)
|
579 |
if img.mode != "RGB":
|
580 |
img = img.convert("RGB")
|
581 |
+
|
582 |
yield example_idx, {
|
583 |
"image": img,
|
584 |
"label": label,
|
585 |
+
"is_one": False,
|
586 |
+
"is_ten": False,
|
587 |
}
|
588 |
example_idx += 1
|
589 |
|