File size: 6,437 Bytes
002bd9b |
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 |
import json
import os
import datasets
import dotenv
logger = datasets.logging.get_logger(__name__)
_BASE_IMAGE_METADATA_FEATURES = {
"image_id": datasets.Value("int32"),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"file_name": datasets.Value("string"),
"coco_url": datasets.Value("string"),
"task_type": datasets.Value("string"),
}
_BASE_REGION_FEATURES = {
# NOTE: one of them is 900100184613, which is out of the range of int32
"region_id": datasets.Value("int64"),
"image_id": datasets.Value("int32"),
"phrases": [datasets.Value("string")],
"x": datasets.Value("int32"),
"y": datasets.Value("int32"),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
}
_BASE_MASK_FEATURES = {
"size": [datasets.Value("int32")],
"counts": datasets.Value("string"),
}
_BASE_MASK_REGION_FEATURES = {
"region_id": datasets.Value("int64"),
"image_id": datasets.Value("int32"),
"phrases": [datasets.Value("string")],
"x": datasets.Value("int32"),
"y": datasets.Value("int32"),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"mask": _BASE_MASK_FEATURES,
# "area": datasets.Value("int32"),
# "phrase_conf": datasets.Value("float32"),
}
_ANNOTATION_FEATURES = {
"region_descriptions": {"regions": [_BASE_REGION_FEATURES]},
# "mask_region_descriptions": {"regions": [_BASE_MASK_REGION_FEATURES]},
}
class SBUBuilderConfig(datasets.BuilderConfig):
def __init__(
self,
name,
splits,
with_image: bool = True,
with_mask: bool = False,
base_dir: str = None,
base_annotations_dir: str = None,
task_type: "str" = "caption",
**kwargs,
):
super().__init__(name, **kwargs)
self.splits = splits
self.with_image = with_image
self.with_mask = with_mask
self.base_dir = base_dir
self.base_annotations_dir = base_annotations_dir
self.task_type = task_type
@property
def features(self):
# annoation_type = (
# "mask_region_descriptions" if self.with_mask else "region_descriptions"
# )
annoation_type = "region_descriptions"
logger.info(f"Using annotation type: {annoation_type} due to with_mask={self.with_mask}")
return datasets.Features(
{
**({"image": datasets.Image()} if self.with_image else {}),
**_BASE_IMAGE_METADATA_FEATURES,
**_ANNOTATION_FEATURES[annoation_type],
}
)
class SBUPseudoRegionDataset(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("0.0.0")
BUILDER_CONFIG_CLASS = SBUBuilderConfig
BUILDER_CONFIGS = [
SBUBuilderConfig(name="pseudo_region", splits=["train"]),
]
DEFAULT_CONFIG_NAME = "pseudo_region"
config: SBUBuilderConfig
def _info(self):
return datasets.DatasetInfo(features=self.config.features)
def _split_generators(self, dl_manager):
if self.config.with_mask is True:
raise ValueError("This sbu does not support `with_mask=True`.")
base_dir = self.config.base_dir
base_annotations_dir = self.config.base_annotations_dir
if base_dir is None:
raise ValueError("base_dir must be specified.")
if base_annotations_dir is None:
raise ValueError("base_annotations_dir must be specified.")
_DL_URLS = {
"train": os.path.join(base_dir, "images.zip"),
"annotations_train": os.path.join(base_annotations_dir, "sbu.json"),
}
# NOTE(xiaoke): load sas_key from .env
logger.info(f"Try to load sas_key from .env file: {dotenv.load_dotenv('.env')}.")
sbu_url_sas_key = os.getenv("SBU_URL_SAS_KEY", "")
sbu_annotations_url_sas_key = os.getenv("SBU_ANNOTATIONS_URL_SAS_KEY", "")
if not os.path.exists(_DL_URLS["train"]):
_DL_URLS["train"] += sbu_url_sas_key
if not os.path.exists(_DL_URLS["annotations_train"]):
_DL_URLS["annotations_train"] += sbu_annotations_url_sas_key
_DL_URLS = dl_manager.download_and_extract(_DL_URLS)
logger.warning(f"Downloaded to {_DL_URLS}.")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"image_dir": _DL_URLS["train"],
"annotations_path": _DL_URLS["annotations_train"],
"split": "train",
},
)
]
def _generate_examples(self, image_dir, annotations_path, split):
with open(annotations_path, "r") as f:
annotations = json.load(f)
from PIL import Image
failed_to_load = 0
for i, annotation in enumerate(annotations):
# NOTE: the annotation file "sbu.json" is from LAVIS download script.
image_path = os.path.join(image_dir, annotation["image"])
url = annotation["url"]
try:
image = Image.open(image_path)
except Exception as e:
logger.debug(f"Failed to open image {image_path} with url {url}: {e}")
failed_to_load += 1
continue
width, height = image.size
image_metadata = {
"image_id": i,
"width": width,
"height": height,
"file_name": annotation["image"],
"coco_url": url,
}
image_dict = {"image": image_path} if self.config.with_image else {}
annotation = {
"regions": [
{
"region_id": i,
"image_id": i,
"x": 0,
"y": 0,
"width": width,
"height": height,
"phrases": [annotation["caption"]],
}
]
}
yield i, {
**image_metadata,
**image_dict,
**annotation,
"task_type": self.config.task_type,
}
logger.info(
f"Total images: {len(annotations)}, successfully loaded: {len(annotations) - failed_to_load}, failed to load: {failed_to_load}"
)
|