File size: 11,340 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
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
import json
import os
import datasets
from datasets.download.download_manager import DownloadManager
import dotenv
from urllib.parse import urlparse
import re

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]},
}

import json
import os
import datasets
import dotenv
from pycocotools.coco import COCO

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 VisualGenomeGRiTLocalConfig(datasets.BuilderConfig):
    def __init__(
        self,
        name,
        splits,
        with_image: bool = True,
        with_mask: bool = False,
        base_dir: str = None,
        base_annotation_dir: str = None,
        task_type: str = "caption",
        **kwargs,
    ):
        super().__init__(name=name, **kwargs)
        self.splits = splits
        self.with_image = with_image
        self.with_mask = with_mask
        self.base_dir = base_dir
        self.base_annotation_dir = base_annotation_dir
        self.task_type = task_type

    @property
    def features(self):
        if self.with_mask is True:
            raise ValueError("with_mask=True is not supported yet in COCO caption.")

        annoation_type = "mask_region_descriptions" if self.with_mask else "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 VisualGenomeGRiTLocalDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("0.0.0")

    BUILDER_CONFIG_CLASS = VisualGenomeGRiTLocalConfig
    BUILDER_CONFIGS = [
        # NOTE: we do not need test as it lacks visual promptsc
        # COCOBuilderConfig(name="2017", splits=["train", "valid", "test"]),
        VisualGenomeGRiTLocalConfig(name="grit", splits=["train", "test"]),
    ]
    DEFAULT_CONFIG_NAME = "grit"
    config: VisualGenomeGRiTLocalConfig

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=self.config.features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
            version=self.config.version,
        )

    def _split_generators(self, dl_manager: DownloadManager):
        """
        The data file structure:
        base_dir:
        - VG_100K/%d.jpg
        - VG_100K_2/%d.jpg

        annotation_dir:
        - image_data.json (the image meta data)
        - densecap_splits.json (from densecap github repo)
        - region_descriptions.json (from region descriptions)
        - {train,test}.json (from grit github repo)

        NOTE: Compare `grit` and `densecap`:
        1. Image split: the `test` is the same, the `val` is not used, for `train` the `densecap` has two more images than `grit`: `{1650, 1684}`
        2. Texts are different, the `densecap` is raw, the `grit` is processed
        3. The `image_id` is the same as that in `file url` or `file path`

        Args:
            dl_manager (DownloadManager): _description_

        Returns:
            _type_: _description_
        """
        base_dir = self.config.base_dir
        base_annotation_dir = self.config.base_annotation_dir

        if base_dir is None:
            raise ValueError("base_dir is not provided.")
        if base_annotation_dir is None:
            raise ValueError("vg_annotation_dir is not provided.")

        PATHS = {
            "base_dir": base_dir,
            "base_annotation_dir": base_annotation_dir,
        }
        split_kwargs_ls = []
        splits = self.config.splits
        for split in splits:
            if split not in ["train", "test"]:
                raise ValueError(f"Split {split} is not supported yet.")
            if split == "train":
                dataset = datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={**PATHS, "split": "train"})
            elif split == "test":
                dataset = datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={**PATHS, "split": "test"})
            split_kwargs_ls.append(dataset)
        return split_kwargs_ls

    def _generate_examples(
        self,
        base_dir,
        base_annotation_dir,
        split,
    ):
        grit_annot_file = os.path.join(base_annotation_dir, f"{split}.json")
        grit_image_meta_file = os.path.join(base_annotation_dir, "image_data.json")

        with open(grit_annot_file, "r") as f:
            grit_annot = json.load(f)
        with open(grit_image_meta_file, "r") as f:
            grit_image_meta = json.load(f)

        grit_img_id_to_region = self.build_grit_img_id_to_regions(grit_annot)
        grit_img_id_to_img = self.build_densecap_img_id_to_img(grit_image_meta)
        grit_split_img_id = [i["id"] for i in grit_annot["images"]]

        for idx, img_id in enumerate(grit_split_img_id):
            img = grit_img_id_to_img[img_id]
            image_metadata = {
                "coco_url": img["url"],
                "file_name": img["file_name"],
                "height": img["height"],
                "width": img["width"],
                "image_id": img["image_id"],
            }

            image_dict = {"image": os.path.join(base_dir, img["file_name"])} if self.config.with_image else {}

            regions = []
            for ann in grit_img_id_to_region[img_id]:
                x, y, w, h = ann["bbox"]  # NOTE: follow Detectron, [x, y, w, h]
                region_id = ann["id"]  # NOTE: follow Detectron
                phrase = ann["caption"]
                region = {
                    "region_id": region_id,
                    "image_id": img_id,
                    "phrases": [phrase],
                    "x": x,
                    "y": y,
                    "width": w,
                    "height": h,
                }
                if self.config.with_mask is True:
                    region["mask"] = {
                        "size": ann["mask"]["size"],
                        "counts": ann["mask"]["counts"],
                    }
                regions.append(region)

            yield idx, {
                **image_dict,
                **image_metadata,
                "regions": regions,
                "task_type": self.config.task_type,
            }

    def build_densecap_img_id_to_img(self, densecap_image_meta):
        img_id_to_img = {}
        for img in densecap_image_meta:
            img_id = img["image_id"]
            img["file_name"] = self.convert_url_to_path(img["url"])
            img_id_to_img[img_id] = img
        return img_id_to_img

    @staticmethod
    def convert_url_to_path(img_url):
        """Obtain image folder given an image url.

        For example:
        Given `https://cs.stanford.edu/people/rak248/VG_100K_2/1.jpg` as an image url, this method returns the local path for that image.
        """
        matches = re.fullmatch(
            r"^https://cs.stanford.edu/people/rak248/(VG_100K(?:_2)?)/([0-9]+\.jpg)$",
            img_url,
        )
        assert matches is not None, f"Got img_url: {img_url}, matched: {matches}"
        folder, filename = matches.group(1), matches.group(2)
        return os.path.join(folder, filename)

    @staticmethod
    def build_grit_img_id_to_regions(grit_annot):
        img_id_to_regions = {}
        for annot in grit_annot["annotations"]:
            img_id = annot["image_id"]
            if img_id not in img_id_to_regions:
                img_id_to_regions[img_id] = []
            img_id_to_regions[img_id].append(annot)
        return img_id_to_regions


_CITATION = """\
@inproceedings{krishnavisualgenome,
  title={Visual Genome: Connecting Language and Vision Using Crowdsourced Dense Image Annotations},
  author={Krishna, Ranjay and Zhu, Yuke and Groth, Oliver and Johnson, Justin and Hata, Kenji and Kravitz, Joshua and Chen, Stephanie and Kalantidis, Yannis and Li, Li-Jia and Shamma, David A and Bernstein, Michael and Fei-Fei, Li},
  year = {2016},
  url = {https://arxiv.org/abs/1602.07332},
}
"""

_DESCRIPTION = """\
Visual Genome enable to model objects and relationships between objects.
They collect dense annotations of objects, attributes, and relationships within each image.
Specifically, the dataset contains over 108K images where each image has an average of 35 objects, 26 attributes, and 21 pairwise relationships between objects.
"""

_HOMEPAGE = "https://homes.cs.washington.edu/~ranjay/visualgenome/"

_LICENSE = "Creative Commons Attribution 4.0 International License"