diff --git a/nusacrowd/nusa_datasets/coco_id/__init__.py b/nusacrowd/nusa_datasets/coco_id/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/nusacrowd/nusa_datasets/coco_id/__init__.py @@ -0,0 +1 @@ + diff --git a/nusacrowd/nusa_datasets/coco_id/coco_id.py b/nusacrowd/nusa_datasets/coco_id/coco_id.py new file mode 100644 index 00000000..5590ae0e --- /dev/null +++ b/nusacrowd/nusa_datasets/coco_id/coco_id.py @@ -0,0 +1,323 @@ +# coding=utf-8 +# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor. +# +# 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. + +""" +This template serves as a starting point for contributing a dataset to the Nusantara Dataset repo. + +When modifying it for your dataset, look for TODO items that offer specific instructions. + +Full documentation on writing dataset loading scripts can be found here: +https://huggingface.co/docs/datasets/add_dataset.html + +To create a dataset loading script you will create a class and implement 3 methods: + * `_info`: Establishes the schema for the dataset, and returns a datasets.DatasetInfo object. + * `_split_generators`: Downloads and extracts data for each split (e.g. train/val/test) or associate local data with each split. + * `_generate_examples`: Creates examples from data on disk that conform to each schema defined in `_info`. + +TODO: Before submitting your script, delete this doc string and replace it with a description of your dataset. + +image_text = (kb, pairs, qa, text, t2t, entailment) +""" +import json +import os +from pathlib import Path +from typing import Dict, List, Tuple + +import datasets + +from nusacrowd.utils.configs import NusantaraConfig +from nusacrowd.utils import schemas + +# TODO: Add BibTeX citation +from nusacrowd.utils.constants import Tasks + +_CITATION = """\ +@article{, + author = "Sinurat, Ray Andrew Obaja and Kistijantoro, Achmad Imam and Purwarianti, Ayu", + title = "Pembangkitan Deskripsi Gambar dalam Bahasa Indonesia Dengan Pendekatan Semantic Compositional Networks", + journal = {}, + volume = {}, + year = "2019", + url = "https://digilib.itb.ac.id/index.php/gdl/view/40093", + doi = {}, + biburl = {}, + bibsource = {}, + abstract = "Pembangkitan deskripsi gambar otomatis adalah salah satu tantangan populer yang tengah berkembang pesat dalam bidang computer vision dan natural language processing. Ketiadaan pembangkitan deskripsi gambar dalam bahasa Indonesia mendorong dilakukannya penelitian pembangkitan deskripsi gambar berbahasa Indonesia. Penelitian pembuatan model pembangkitan deskripsi gambar berbahasa Indonesia dilakukan dengan menggunakan topologi Semantic Compositional Network (SCN). Pada tugas akhir ini, model SCN dimodifikasikan dengan menambahkan Attention Network guna menambah kualitas deskripsi gambar yang dihasilkan. Pembangkitan deskripsi gambar dilakukan pada dua bahasa yaitu bahasa Inggris dan bahasa Indonesia. Pembangkitan deskripsi bahasa Inggris ditujukan untuk mendapatkan model dengan kinerja terbaik di antara SCN murni dan SCN dengan Attention. Model pembangkitan deskrispi gambar bahasa Inggris dilatih pada dataset COCO dan Flickr30k yang kemudian dievaluasi menggunakan metrik evaluasi BLEU, CIDEr-D, METEOR, dan ROUGE. Berdasarkan eksperimen yang dilakukan, model SCN dengan Attention merupakan model dengan kinerja terbaik dibandingkan dengan model SCN murni. Model tersebut dipilih untuk membangkitkan deskripsi gambar dalam bahasa Indonesia. Pembangunan dataset deskrispi gambar bahasa Indonesia dilakukan dengan menerjemahkan dataset COCO dan Flickr8k menggunakan Google Translate. Selain itu, dilakukan juga koreksi manual oleh manusia sebanyak 3000 deskripsi gambar. Kinerja deskripsi gambar dalam bahasa Indonesia ditinjau menggunakan metrik evaluasi BLEU dan ROUGE. Metrik evaluasi menunjukkan hasil BLEU-4 pada dataset COCO 0.2403 dan Flickr8k 0.2276 sedangkan ROUGE_L pada dataset COCO 0.4689 dan Flickr8k 0.5361." +} +""" + +# TODO: create a module level variable with your dataset name (should match script name) +# E.g. Hallmarks of Cancer: coco_id --> hallmarks_of_cancer +_DATASETNAME = "coco_id" + +# TODO: Add description of the dataset here +# You can copy an official description +_DESCRIPTION = """\ +This dataset is designed for XXX NLP task. COCO ID is an Image Captioning Dataset containing image URL and its caption along with its tags. +""" + +# TODO: Add a link to an official homepage for the dataset here (if possible) +_HOMEPAGE = "" + +# TODO: Add the licence for the dataset here (if possible) +# Note that this doesn't have to be a common open source license. +# Some datasets have custom licenses. In this case, simply put the full license terms +# into `_LICENSE` +_LICENSE = "CC-BY 4.0" + +# TODO: Add links to the urls needed to download your dataset files. +# For local datasets, this variable can be an empty dictionary. + +# For publicly available datasets you will most likely end up passing these URLs to dl_manager in _split_generators. +# In most cases the URLs will be the same for the source and nusantara config. +# However, if you need to access different files for each config you can have multiple entries in this dict. +# This can be an arbitrarily nested dict/list of URLs (see below in `_split_generators` method) +_URLS = { + _DATASETNAME: "https://drive.google.com/uc?export=download&id=1y4COEzzO2nmn1yetOCqxTjNsiSVvRMpv", +} + +# TODO: add supported task by dataset. One dataset may support multiple tasks +_SUPPORTED_TASKS = [Tasks.IMAGE_CAPTIONING] # example: [Tasks.TRANSLATION, Tasks.NAMED_ENTITY_RECOGNITION, Tasks.RELATION_EXTRACTION] + +# TODO: set this to a version that is associated with the dataset. if none exists use "1.0.0" +# This version doesn't have to be consistent with semantic versioning. Anything that is +# provided by the original dataset as a version goes. +_SOURCE_VERSION = "1.0.0" + +_NUSANTARA_VERSION = "1.0.0" + + +# TODO: Name the dataset class to match the script name using CamelCase instead of snake_case +class CocoId(datasets.GeneratorBasedBuilder): + """TODO: Short description of my dataset. COCO ID is an Image Captioning Dataset containing image URL and its caption along with its tags""" + + SOURCE_VERSION = datasets.Version(_SOURCE_VERSION) + NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION) + + # You will be able to load the "source" or "nusantara" configurations with + # ds_source = datasets.load_dataset('my_dataset', name='source') + # ds_nusantara = datasets.load_dataset('my_dataset', name='nusantara') + + # For local datasets you can make use of the `data_dir` and `data_files` kwargs + # https://huggingface.co/docs/datasets/add_dataset.html#downloading-data-files-and-organizing-splits + # ds_source = datasets.load_dataset('my_dataset', name='source', data_dir="/path/to/data/files") + # ds_nusantara = datasets.load_dataset('my_dataset', name='nusantara', data_dir="/path/to/data/files") + + # TODO: For each dataset, implement Config for Source and Nusantara; + # If dataset contains more than one subset (see nusantara/nusa_datasets/smsa.py) implement for EACH of them. + # Each of them should contain: + # - name: should be unique for each dataset config eg. smsa_(source|nusantara)_image_text + # - version: option = (SOURCE_VERSION|NUSANTARA_VERSION) + # - description: one line description for the dataset + # - schema: options = (source|nusantara_image_text) + # - subset_id: subset id is the canonical name for the dataset (eg. smsa) + # where image_text = (kb, pairs, qa, text, t2t) + + BUILDER_CONFIGS = [ + NusantaraConfig( + name="coco_id_source", + version=SOURCE_VERSION, + description="coco_id source schema", + schema="source", + subset_id="coco_id", + ), + NusantaraConfig( + name="coco_id_nusantara_image_text", + version=NUSANTARA_VERSION, + description="coco_id Nusantara schema", + schema="nusantara_image_text", + subset_id="coco_id", + ), + ] + + DEFAULT_CONFIG_NAME = "coco_id_source" + + def _info(self) -> datasets.DatasetInfo: + + # Create the source schema; this schema will keep all keys/information/labels as close to the original dataset as possible. + + # You can arbitrarily nest lists and dictionaries. + # For iterables, use lists over tuples or `datasets.Sequence` + + if self.config.schema == "source": + features = datasets.Features( + { + "id": datasets.Value("string"), + "image_paths": datasets.Sequence(datasets.Value("string")), + "texts": datasets.Value("string"), + "metadata": { + "context": datasets.Value("string"), + "labels": datasets.Value("string"), + } + } + ) + # TODO: Create your source schema here + # raise NotImplementedError() + + # EX: Arbitrary NER type dataset + # features = datasets.Features( + # { + # "doc_id": datasets.Value("string"), + # "text": datasets.Value("string"), + # "entities": [ + # { + # "offsets": [datasets.Value("int64")], + # "text": datasets.Value("string"), + # "type": datasets.Value("string"), + # "entity_id": datasets.Value("string"), + # } + # ], + # } + # ) + + # Choose the appropriate nusantara schema for your task and copy it here. You can find information on the schemas in the CONTRIBUTING guide. + + # In rare cases you may get a dataset that supports multiple tasks requiring multiple schemas. In that case you can define multiple nusantara configs with a nusantara_image_text format. + + # For example nusantara_kb, nusantara_t2t + elif self.config.schema == "nusantara_image_text": + features = schemas.image_text_features + # e.g. features = schemas.kb_features + # TODO: Choose your nusantara schema here + # raise NotImplementedError() + + return datasets.DatasetInfo( + description=_DESCRIPTION, + features=features, + homepage=_HOMEPAGE, + license=_LICENSE, + citation=_CITATION, + ) + + def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: + """Returns SplitGenerators.""" + # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration + + # If you need to access the "source" or "nusantara" config choice, that will be in self.config.name + + # LOCAL DATASETS: You do not need the dl_manager; you can ignore this argument. Make sure `gen_kwargs` in the return gets passed the right filepath + + # PUBLIC DATASETS: Assign your data-dir based on the dl_manager. + + # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs; many examples use the download_and_extract method; see the DownloadManager docs here: https://huggingface.co/docs/datasets/package_reference/builder_classes.html#datasets.DownloadManager + + # dl_manager can accept any type of nested list/dict and will give back the same structure with the url replaced with the path to local files. + + # base_path = Path(dl_manager.download_and_extract(_URLS[_DATASETNAME])) + + # TODO: KEEP if your dataset is PUBLIC; remove if not + urls = _URLS[_DATASETNAME] + data_dir = dl_manager.download_and_extract(urls) + base_path = Path(data_dir) + + # TODO: KEEP if your dataset is LOCAL; remove if NOT + if self.config.data_dir is None: + raise ValueError("This is a local dataset. Please pass the data_dir kwarg to load_dataset.") + else: + data_dir = self.config.data_dir + + # Not all datasets have predefined canonical train/val/test splits. + # If your dataset has no predefined splits, use datasets.Split.TRAIN for all of the data. + + return [ + datasets.SplitGenerator( + name=datasets.Split.TRAIN, gen_kwargs={"filepath": base_path} + ), + # datasets.SplitGenerator( + # name=datasets.Split.TRAIN, + # # Whatever you put in gen_kwargs will be passed to _generate_examples + # gen_kwargs={ + # "filepath": os.path.join(data_dir, "train.jsonl"), + # "split": "train", + # }, + # ), + # datasets.SplitGenerator( + # name=datasets.Split.TEST, + # gen_kwargs={ + # "filepath": os.path.join(data_dir, "test.jsonl"), + # "split": "test", + # }, + # ), + # datasets.SplitGenerator( + # name=datasets.Split.VALIDATION, + # gen_kwargs={ + # "filepath": os.path.join(data_dir, "dev.jsonl"), + # "split": "dev", + # }, + # ), + ] + + # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` + + # TODO: change the args of this function to match the keys in `gen_kwargs`. You may add any necessary kwargs. + + def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]: + """Yields examples as (key, example) tuples.""" + # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset. + + # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example. + + # NOTE: For local datasets you will have access to self.config.data_dir and self.config.data_files + + data = json.load(open(filepath, "r")) + + train = [item for item in data if item['split'] == 'train'] + test = [item for item in data if item['split'] == 'test'] + val = [item for item in data if item['split'] == 'val'] + restval = [item for item in data if item['split'] == 'restval'] + + if self.config.schema == "source": + # TODO: yield (key, example) tuples in the original dataset schema + for row in data['images']: + ex = { + "cocoid": row["cocoid"], + "filepath": row["filepath"], + "imgid": row["imgid"], + "split": row["split"], + "filename": row["filename"], + "tags": row["tags"], + "sentids": row["sentids"], + "sentences": [{ + "sentid": sentence["sentid"], + "imgid": sentence["imgid"], + "raw": sentence["raw"], + "tokens": sentence["token"] + } for sentence in row["sentences"]] + } + yield row["cocoid"], ex + + elif self.config.schema == "nusantara_image_text": + # TODO: yield (key, example) tuples in the nusantara schema + for row in data['images']: + ex = { + "id": row["cocoid"], + "image_paths": row["filename"], + "texts": row["sentences"], + "metadata": { + "context": row["tags"], + "labels": row["sentids"], + } + } + yield row["cocoid"], ex + + +# This template is based on the following template from the datasets package: +# https://github.com/huggingface/datasets/blob/master/templates/new_dataset_script.py + + +# This allows you to run your dataloader with `python coco_id.py` during development +# TODO: Remove this before making your PR +# if __name__ == "__main__": +# datasets.load_dataset(__file__)