Skip to content

Commit f30657e

Browse files
modified to use dl_manager instead of ZipFile.
1 parent 74b63a4 commit f30657e

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

tensorflow_datasets/image/coco.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import json
2525
import os
2626

27-
from zipfile import ZipFile
28-
2927
from absl import logging
3028
import tensorflow as tf
3129

@@ -541,7 +539,22 @@ def _split_generators(self, dl_manager):
541539
extracted_paths = dl_manager.download_and_extract({
542540
key: root_url + url for key, url in urls.items()
543541
})
544-
542+
543+
# Extract the panoptic_{}.zip folders containing the png images
544+
panoptic_image_filename = "panoptic_{}.zip"
545+
# Extracting panoptic train png images
546+
panoptic_train_image_path = os.path.join(
547+
extracted_paths["panoptic_annotations_trainval2017"],
548+
"annotations",
549+
panoptic_image_filename.format("train2017")
550+
)
551+
# Extracting panoptic val png images
552+
panoptic_val_image_path = os.path.join(
553+
extracted_paths["panoptic_annotations_trainval2017"],
554+
"annotations",
555+
panoptic_image_filename.format("val2017")
556+
)
557+
545558
return [
546559
tfds.core.SplitGenerator(
547560
name=tfds.Split.TRAIN,
@@ -550,6 +563,7 @@ def _split_generators(self, dl_manager):
550563
image_dir=extracted_paths["train_images"],
551564
annotation_dir=extracted_paths["panoptic_annotations_trainval2017"],
552565
split_type="train2017",
566+
panoptic_extracted_images=os.path.join(dl_manager.extract(panoptic_train_image_path), "panoptic_train2017"),
553567
)),
554568
tfds.core.SplitGenerator(
555569
name=tfds.Split.VALIDATION,
@@ -558,11 +572,12 @@ def _split_generators(self, dl_manager):
558572
image_dir=extracted_paths["val_images"],
559573
annotation_dir=extracted_paths["panoptic_annotations_trainval2017"],
560574
split_type="val2017",
575+
panoptic_extracted_images=os.path.join(dl_manager.extract(panoptic_val_image_path), "panoptic_val2017"),
561576
)),
562577
]
563578

564579
def _generate_examples(
565-
self, image_dir, annotation_dir, split_type):
580+
self, image_dir, annotation_dir, split_type, panoptic_extracted_images):
566581
"""Generate examples as dicts.
567582
568583
Args:
@@ -609,16 +624,6 @@ def _generate_examples(
609624

610625
categories_id2name = {c["id"]: c["name"] for c in categories}
611626

612-
# Extract the panoptic_{}.zip folder containing the png images
613-
panoptic_image_filename = "panoptic_{}.zip"
614-
panoptic_image_path = os.path.join(
615-
annotation_dir,
616-
"annotations",
617-
panoptic_image_filename.format(split_type)
618-
)
619-
with ZipFile(panoptic_image_path) as f:
620-
f.extractall(os.path.join(annotation_dir, "annotations"))
621-
622627
# Iterate over all images
623628
for image_info in sorted(images, key=lambda x: x["id"]):
624629
# Each instance annotation is a dict:
@@ -654,7 +659,7 @@ def build_bbox(x, y, width, height):
654659
"image": os.path.join(image_dir, split_type, image_info["file_name"]),
655660
"image_id": image_info["id"],
656661
"image/filename": image_info["file_name"],
657-
"panoptic_image": os.path.join(annotation_dir, "annotations", "panoptic_{}".format(split_type), instances["file_name"]),
662+
"panoptic_image": os.path.join(panoptic_extracted_images, instances["file_name"]),
658663
"panoptic_image/filename": instances["file_name"],
659664
"objects": [{
660665
"area": instance["area"],

0 commit comments

Comments
 (0)