File size: 6,219 Bytes
74e8f2f |
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 |
# Copyright 2024 Big Vision Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=line-too-long
r"""Implements RSVQA-LR dataset in TFDS.
Remote sensing visual question answering task, using low-resolution satellite
(Sentinel-2) RGB channels data at 10m resolution per pixel.
It's small dataset at source (200M), so simple to run locally.
First, download and unzip the dataset from https://zenodo.org/records/6344334
and place it in /tmp/data/rsvqa_lr.
Then, run conversion locally (make sure to install tensorflow-datasets for the `tfds` util):
cd third_party/py/big_vision/datasets
env TFDS_DATA_DIR=/tmp/tfds tfds build --datasets=rsvqa_lr
Example to load:
import tensorflow_datasets as tfds
dataset = tfds.load('rsvqa_lr', split='train', data_dir='/tmp/tfds')
Dataset splits:
train: 57223 examples/questions
val: 10005 examples/questions
test: 10004 examples/questions
And the same splits are available excluding numeric questions:
train_nonum: 39441 examples/questions
val_nonum: 6782 examples/questions
test_nonum: 6782 examples/questions
Note: due to image duplication with each question, the dataset size is
significatnly increased by the number of questions per image.
Recommended training splits:
train: train
minitrain: train[:5%]
eval: val
full_train: train+val
test: test
Image sizes: 256x256
Number of answers per question: 1
Question types distribution in train split:
- Comparison(comp): 39.4%
- Count (count): 29.9% (integers, binned at evaluation into
{0, 1-10, 11-100, 101-1000, >10000})
- Presence (presence): 29.7%
- Rural/Urban (rural_urban): 1%
"""
import io
import json
import os
import numpy as np
import tensorflow_datasets as tfds
_DESCRIPTION = """RSVQA-LR dataset."""
# pylint: disable=line-too-long
_CITATION = """
@article{Lobry_2020,
title={RSVQA: Visual Question Answering for Remote Sensing Data},
volume={58},
ISSN={1558-0644},
url={http://dx.doi.org/10.1109/TGRS.2020.2988782},
DOI={10.1109/tgrs.2020.2988782},
number={12},
journal={IEEE Transactions on Geoscience and Remote Sensing},
publisher={Institute of Electrical and Electronics Engineers (IEEE)},
author={Lobry, Sylvain and Marcos, Diego and Murray, Jesse and Tuia, Devis},
year={2020},
month=dec, pages={8555–8566} }
"""
# pylint: enable=line-too-long
# When running locally (recommended), copy files as above an use these:
PATH = '/tmp/data/rsvqa_lr/'
class RsvqaLrConfig(tfds.core.BuilderConfig):
"""Config to specify each variant."""
def __init__(self, nonum, **kwargs):
name = 'nonum' if nonum else 'all'
super(RsvqaLrConfig, self).__init__(name=name, **kwargs)
self.nonum = nonum
class RsvqaLr(tfds.core.GeneratorBasedBuilder):
"""DatasetBuilder for RSVQA-LR dataset."""
VERSION = tfds.core.Version('1.0.2')
RELEASE_NOTES = {
'1.0.0': 'First release.',
'1.0.1': 'Rename binned values.',
'1.0.2': 'Removed explicit png image encoding.',
}
BUILDER_CONFIGS = [
RsvqaLrConfig(nonum=False),
RsvqaLrConfig(nonum=True),
]
def _info(self):
"""Returns the metadata."""
return tfds.core.DatasetInfo(
builder=self,
description=_DESCRIPTION,
features=tfds.features.FeaturesDict({
'question_id': tfds.features.Scalar(np.int32),
'filename': tfds.features.Text(),
'image': tfds.features.Image(),
'question': tfds.features.Text(),
'question_type': tfds.features.Text(),
'answers': tfds.features.Sequence(tfds.features.Text()),
'raw_answers': tfds.features.Sequence(tfds.features.Text()),
}),
supervised_keys=None,
homepage='https://rsvqa.sylvainlobry.com/',
citation=_CITATION,
)
def _split_generators(self, dl_manager: tfds.download.DownloadManager):
"""Returns SplitGenerators."""
return {
split: self._generate_examples(split)
for split in ('train', 'val', 'test')
}
def _generate_examples(self, split):
"""Yields (key, example) tuples."""
questions_path = os.path.join(PATH + f'LR_split_{split}_questions.json')
answers_path = os.path.join(PATH + f'LR_split_{split}_answers.json')
images_path = os.path.join(PATH + 'Images_LR')
with open(questions_path, 'r') as f:
questions = json.loads(f.read())['questions']
with open(answers_path, 'r') as f:
answers = json.loads(f.read())['answers']
for q, a in zip(questions, answers):
assert q['active'] == a['active']
if not q['active']:
continue
if self.builder_config.nonum and q['type'] == 'count':
continue
assert q['answers_ids'] == [a['id']]
assert q['id'] == a['question_id']
filename = f'{q["img_id"]}.tif'
img = read_tif(os.path.join(images_path, filename))
yield q['id'], {
'question_id': q['id'],
'filename': filename,
'image': img,
'question': q['question'],
'question_type': q['type'],
'answers': [bin_answer(a['answer'], q['type'])],
'raw_answers': [a['answer']],
}
def bin_answer(answer, question_type):
"""Bins answers into expected ranges."""
if question_type == 'count':
count = int(answer)
if count == 0:
return '0'
elif count <= 10:
return 'between 1 and 10'
elif count <= 100:
return 'between 11 and 100'
elif count <= 1000:
return 'between 101 and 1000'
else:
return 'more than 1000'
return answer
def read_tif(path):
with open(path, 'rb') as f:
img = tfds.core.lazy_imports.tifffile.imread(io.BytesIO(f.read()))
return img.astype(np.uint8)
|