|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""The ELEVATER benchmark""" |
|
|
|
import json |
|
import os |
|
|
|
import datasets |
|
|
|
from zipfile import ZipFile |
|
from io import BytesIO |
|
from PIL import Image |
|
|
|
_VERSION = "1.0.0" |
|
_BASE_URL = "https://cvinthewildeus.blob.core.windows.net/datasets/" |
|
|
|
_ELEVATER_CITATION = """\ |
|
@article{li2022elevater, |
|
title={ELEVATER: A Benchmark and Toolkit for Evaluating Language-Augmented Visual Models}, |
|
author={Li, Chunyuan and Liu, Haotian and Li, Liunian Harold and Zhang, Pengchuan and Aneja, Jyoti and Yang, Jianwei and Jin, Ping and Lee, Yong Jae and Hu, Houdong and Liu, Zicheng and Gao, Jianfeng}, |
|
journal={Neural Information Processing Systems}, |
|
year={2022} |
|
} |
|
Note that each ELEVATER dataset has its own citation. Please see the source to |
|
get the correct citation for each contained dataset. |
|
""" |
|
|
|
_VOC_2007_CLASSIFICATION_CITATION="""\ |
|
@misc{pascal-voc-2007, |
|
author = "Everingham, M. and Van~Gool, L. and Williams, C. K. I. and Winn, J. and Zisserman, A.", |
|
title = "The {PASCAL} {V}isual {O}bject {C}lasses {C}hallenge 2007 {(VOC2007)} {R}esults", |
|
howpublished = "http://www.pascal-network.org/challenges/VOC/voc2007/workshop/index.html" |
|
}""" |
|
|
|
_CIFAR_10_CITATION="""\ |
|
@article{krizhevsky2009learning, |
|
title={Learning multiple layers of features from tiny images}, |
|
author={Krizhevsky, Alex and Hinton, Geoffrey and others}, |
|
year={2009}, |
|
publisher={Toronto, ON, Canada} |
|
}""" |
|
|
|
class ELEVATERConfig(datasets.BuilderConfig): |
|
|
|
"""BuilderConfig for ELEVATER.""" |
|
def __init__(self, name, description, contact, version, type_, root_folder, labelmap, num_classes, train, test, citation, url, **kwargs): |
|
"""BuilderConfig for ELEVATER. |
|
Args: |
|
features: `list[string]`, list of the features that will appear in the |
|
feature dict. Should not include "label". |
|
data_url: `string`, url to download the zip file from. |
|
citation: `string`, citation for the data set. |
|
url: `string`, url for information about the data set. |
|
label_classes: `list[string]`, the list of classes for the label if the |
|
label is present as a string. Non-string labels will be cast to either |
|
'False' or 'True'. |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(ELEVATERConfig, self).__init__(**kwargs) |
|
self.name = name |
|
self.description = description |
|
self.contact = contact |
|
self.version = version |
|
self.type = type_ |
|
self.root_folder = root_folder |
|
self.labelmap = labelmap |
|
self.num_classes = num_classes |
|
self.train = train |
|
self.test = test |
|
self.citation = citation |
|
self.url = url |
|
|
|
|
|
class ELEVATER(datasets.GeneratorBasedBuilder): |
|
|
|
BUILDER_CONFIGS = [ |
|
ELEVATERConfig( |
|
name="cifar-10", |
|
description="The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.", |
|
contact="pinjin", |
|
version=_VERSION, |
|
type_="classification_multiclass", |
|
root_folder="classification/cifar_10_20211007", |
|
labelmap="labels.txt", |
|
num_classes=10, |
|
train={ |
|
"index_path": "train.txt", |
|
"files_for_local_usage": ["train.zip"], |
|
"num_images": 50000 |
|
}, |
|
test={ |
|
"index_path": "test.txt", |
|
"files_for_local_usage": ["val.zip"], |
|
"num_images": 10000 |
|
}, |
|
citation=_CIFAR_10_CITATION, |
|
url=_BASE_URL, |
|
), |
|
ELEVATERConfig( |
|
"name"="voc-2007-classification", |
|
"description"="Voc2007 classification dataset.", |
|
"contact"="pinjin", |
|
"version"=_VERSION, |
|
"type"="classification_multilabel", |
|
"root_folder"="classification/voc2007_20211007", |
|
"train": { |
|
"index_path": "train_ic.txt", |
|
"files_for_local_usage": ["train.zip"], |
|
"num_images": 2501 |
|
}, |
|
"val": { |
|
"index_path": "val_ic.txt", |
|
"files_for_local_usage": ["train.zip"], |
|
"num_images": 2510 |
|
}, |
|
"test": { |
|
"index_path": "test_ic.txt", |
|
"files_for_local_usage": ["test.zip"], |
|
"num_images": 4952 |
|
}, |
|
"labelmap": "labels.txt", |
|
"num_classes": 20, |
|
citation=_VOC_2007_CLASSIFICATION_CITATION, |
|
url=_BASE_URL, |
|
) |
|
] |
|
|
|
def _info(self): |
|
features = datasets.Features( |
|
{ |
|
"image_file_path": datasets.Value("string"), |
|
"image": datasets.Image(), |
|
"labels": datasets.Value("int32") |
|
} |
|
) |
|
return datasets.DatasetInfo( |
|
description=self.config.description, |
|
features=features, |
|
citation=self.config.citation + '\n' + _ELEVATER_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
_URL = self.config.url + self.config.root_folder |
|
print(self.config.val) |
|
print(good) |
|
urls_to_download = { |
|
"labelmap": os.path.join(_URL, self.config.labelmap), |
|
"train": { |
|
"images": os.path.join(_URL, self.config.train['files_for_local_usage'][0]), |
|
"index": os.path.join(_URL, self.config.train['index_path']), |
|
}, |
|
"test": { |
|
"images": os.path.join(_URL, self.config.test['files_for_local_usage'][0]), |
|
"index": os.path.join(_URL, self.config.test['index_path']), |
|
} |
|
} |
|
downloaded_files = dl_manager.download_and_extract(urls_to_download) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"images": downloaded_files["train"]["images"], |
|
"index": downloaded_files["train"]["index"], |
|
"split": datasets.Split.TRAIN, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"images": downloaded_files["test"]["images"], |
|
"index": downloaded_files["test"]["index"], |
|
"split": datasets.Split.TEST, |
|
}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, images, index, split): |
|
image_path_label_list = [] |
|
with open(index, "r") as f: |
|
lines = f.readlines() |
|
for i, line in enumerate(lines): |
|
line_split = line[:-1].split(" ") |
|
label = int(line_split[1]) |
|
image_path = line_split[0].split('@')[1] |
|
path = images + '/' + image_path |
|
yield i, { |
|
"image_file_path": path, |
|
"image": path, |
|
"labels": label, |
|
} |