zhlds / zhlds.py
Yanpeng Yuan
update
7debf8d
raw history blame
No virus
6.53 kB
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets 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.
# Lint as: python3
"""The ELEVATER benchmark"""
import json
import os
import datasets
from zipfile import ZipFile
from io import BytesIO
from PIL import Image
_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.
"""
_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."""
_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."""
print(f"zhlds/zhlds.py, line 50")
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="1.0.0",
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="https://cvinthewildeus.blob.core.windows.net/datasets/",
),
]
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
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,
}