|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""covr dataset.""" |
| 17 | + |
| 18 | +import json |
| 19 | + |
| 20 | +from etils import epath |
| 21 | +import tensorflow_datasets.public_api as tfds |
| 22 | + |
| 23 | + |
| 24 | +class Builder(tfds.core.GeneratorBasedBuilder): |
| 25 | + """DatasetBuilder for covr dataset.""" |
| 26 | + |
| 27 | + VERSION = tfds.core.Version('1.0.0') |
| 28 | + RELEASE_NOTES = { |
| 29 | + '1.0.0': 'Initial release.', |
| 30 | + } |
| 31 | + |
| 32 | + def _info(self) -> tfds.core.DatasetInfo: |
| 33 | + """Returns the dataset metadata.""" |
| 34 | + return self.dataset_info_from_configs( |
| 35 | + features=tfds.features.FeaturesDict({ |
| 36 | + 'utterance': tfds.features.Text(), |
| 37 | + 'scenes': tfds.features.Sequence( |
| 38 | + feature=tfds.features.Text(), |
| 39 | + ), |
| 40 | + 'properties': tfds.features.Sequence( |
| 41 | + feature=tfds.features.Text(), |
| 42 | + ), |
| 43 | + 'pattern_name': tfds.features.Text(), |
| 44 | + 'program': tfds.features.Text(), |
| 45 | + 'label': tfds.features.Text(), |
| 46 | + 'images': tfds.features.Sequence( |
| 47 | + feature=tfds.features.Image(), |
| 48 | + ), |
| 49 | + }), |
| 50 | + supervised_keys=None, |
| 51 | + homepage='https://covr-dataset.github.io/', |
| 52 | + ) |
| 53 | + |
| 54 | + def _split_generators(self, dl_manager: tfds.download.DownloadManager): |
| 55 | + """Returns SplitGenerators.""" |
| 56 | + extracted_dirs = dl_manager.download_and_extract({ |
| 57 | + 'covr_dir': ( |
| 58 | + 'https://drive.google.com/uc?export=download&' |
| 59 | + 'id=10xlQ6isRdGX94BypoqN6klniGeqdLBJA' |
| 60 | + ), |
| 61 | + 'imsitu_dir': ( |
| 62 | + 'https://s3.amazonaws.com/my89-frame-annotation' |
| 63 | + '/public/of500_images.tar' |
| 64 | + ), |
| 65 | + 'vg1_dir': 'https://cs.stanford.edu/people/rak248/VG_100K_2/images.zip', |
| 66 | + 'vg2_dir': ( |
| 67 | + 'https://cs.stanford.edu/people/rak248/VG_100K_2/images2.zip' |
| 68 | + ), |
| 69 | + }) |
| 70 | + |
| 71 | + # Each name is the image file name without the ".jpg" extension, which is |
| 72 | + # also used as the scene id in COVR. |
| 73 | + image_path_by_scene_id: dict[str, epath.Path] = {} |
| 74 | + image_globs = [ |
| 75 | + extracted_dirs['vg1_dir'].glob('*/*.jpg'), |
| 76 | + extracted_dirs['vg2_dir'].glob('*/*.jpg'), |
| 77 | + extracted_dirs['imsitu_dir'].glob('of500_images/*/*.jpg'), |
| 78 | + ] |
| 79 | + for image_glob in image_globs: |
| 80 | + for image_path in image_glob: |
| 81 | + name = image_path.stem |
| 82 | + image_path_by_scene_id[name] = image_path |
| 83 | + path = extracted_dirs['covr_dir'] |
| 84 | + return { |
| 85 | + 'train': self._generate_examples( |
| 86 | + path / 'train.jsonl', image_path_by_scene_id |
| 87 | + ), |
| 88 | + 'test': self._generate_examples( |
| 89 | + path / 'test.jsonl', image_path_by_scene_id |
| 90 | + ), |
| 91 | + 'validation': self._generate_examples( |
| 92 | + path / 'val.jsonl', image_path_by_scene_id |
| 93 | + ), |
| 94 | + } |
| 95 | + |
| 96 | + def _generate_examples( |
| 97 | + self, path: epath.Path, image_path_by_scene_id: dict[str, epath.Path] |
| 98 | + ): |
| 99 | + """Yields examples.""" |
| 100 | + with path.open() as f: |
| 101 | + for line in f: |
| 102 | + item = json.loads(line) |
| 103 | + images = [ |
| 104 | + image_path_by_scene_id[scene_id] for scene_id in item['scenes'] |
| 105 | + ] |
| 106 | + yield item['qid'], { |
| 107 | + 'utterance': item['utterance'], |
| 108 | + 'scenes': item['scenes'], |
| 109 | + 'properties': item['properties'], |
| 110 | + 'pattern_name': item['pattern_name'], |
| 111 | + 'program': str(item['program']), |
| 112 | + 'label': str(item.get('answer')), |
| 113 | + 'images': images, |
| 114 | + } |
0 commit comments