| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| """TODO: Add a description here.""" |
|
|
|
|
| import csv |
| import json |
| import os |
| import math |
| import requests |
| from io import BytesIO |
| from zipfile import ZipFile |
| from urllib.request import urlopen |
| import pandas as pd |
|
|
| import datasets |
|
|
| |
| |
| _CITATION = """\ |
| @InProceedings{huggingface:dataset, |
| title = {A great new dataset}, |
| author={huggingface, Inc. |
| }, |
| year={2020} |
| } |
| """ |
|
|
| |
| |
| _DESCRIPTION = """\ |
| This new dataset is designed to solve this great NLP task and is crafted with a lot of care. |
| """ |
|
|
| |
| _HOMEPAGE = "" |
|
|
| |
| _LICENSE = "" |
|
|
| _LILA_SAS_URLS = pd.read_csv("https://lila.science/wp-content/uploads/2020/03/lila_sas_urls.txt") |
| _LILA_SAS_URLS.rename(columns={"# name": "name"}, inplace=True) |
|
|
| _METADATA_BASE_URL = "https://huggingface.co/datasets/NimaBoscarino/LILA/resolve/main/data/" |
|
|
| |
| _LILA_URLS = { |
| "Caltech Camera Traps": "Caltech_Camera_Traps.jsonl", |
| "ENA24": "ENA24.jsonl", |
| "Missouri Camera Traps": "Missouri_Camera_Traps.jsonl", |
| "NACTI": "NACTI.jsonl.zip", |
| "WCS Camera Traps": "WCS_Camera_Traps.jsonl.zip", |
| "Wellington Camera Traps": "Wellington_Camera_Traps.jsonl.zip", |
| "Island Conservation Camera Traps": "Island_Conservation_Camera_Traps.jsonl.zip", |
| "Channel Islands Camera Traps": "Channel_Islands_Camera_Traps.jsonl.zip", |
| "Idaho Camera Traps": "Idaho_Camera_Traps.jsonl.zip", |
| "Snapshot Serengeti": "Snapshot_Serengeti.jsonl.zip", |
| "Snapshot Karoo": "Snapshot_Karoo.jsonl.zip", |
| "Snapshot Kgalagadi": "Snapshot_Kgalagadi.jsonl", |
| "Snapshot Enonkishu": "Snapshot_Enonkishu.jsonl.zip", |
| "Snapshot Camdeboo": "Snapshot_Camdeboo.jsonl.zip", |
| "Snapshot Mountain Zebra": "Snapshot_Mountain_Zebra.jsonl.zip", |
| "Snapshot Kruger": "Snapshot_Kruger.jsonl", |
| "SWG Camera Traps": "SWG_Camera_Traps.jsonl.zip", |
| "Orinoquia Camera Traps": "Orinoquia_Camera_Traps.jsonl.zip", |
| } |
|
|
| class LILAConfig(datasets.BuilderConfig): |
| """Builder Config for LILA""" |
|
|
| def __init__(self, image_base_url, metadata_url, **kwargs): |
| """BuilderConfig for LILA. |
| Args: |
| **kwargs: keyword arguments forwarded to super. |
| """ |
| super(LILAConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs) |
| self.image_base_url = image_base_url |
| self.metadata_url = metadata_url |
|
|
|
|
| class LILA(datasets.GeneratorBasedBuilder): |
| """TODO: Short description of my dataset.""" |
|
|
| VERSION = datasets.Version("1.1.0") |
|
|
| BUILDER_CONFIGS = [ |
| LILAConfig( |
| name=row.name, |
| |
| image_base_url=row.image_base_url, |
| metadata_url=_METADATA_BASE_URL + _LILA_URLS[row.name] |
| ) for row in _LILA_SAS_URLS.itertuples() |
| ] |
|
|
| def _get_features(self) -> datasets.Features: |
| |
| |
|
|
| if self.config.name == 'Caltech Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "seq_num_frames": datasets.Value("int32"), |
| "date_captured": datasets.Value("date32"), |
| "seq_id": datasets.Value("string"), |
| "location": datasets.Value("string"), |
| "rights_holder": datasets.Value("string"), |
| "frame_num": datasets.Value("int32"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| }), |
| "bboxes": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'ENA24': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Missouri Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "seq_id": datasets.Value("string"), "seq_num_frames": datasets.Value("int32"), |
| "frame_num": datasets.Value("int32"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "sequence_level_annotation": datasets.Value("bool"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'NACTI': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "study": datasets.Value("string"), "location": datasets.Value("string"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| }), |
| "bboxes": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'WCS Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "wcs_id": datasets.Value("string"), "location": datasets.Value("string"), |
| "frame_num": datasets.Value("int32"), "match_level": datasets.Value("int32"), |
| "seq_id": datasets.Value("string"), "country_code": datasets.Value("string"), |
| "seq_num_frames": datasets.Value("int32"), |
| "status": datasets.Value("string"), |
| "datetime": datasets.Value("date32"), |
| "corrupt": datasets.Value("bool"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "count": datasets.Value("int32"), |
| "sex": datasets.Value("string"), |
| "age": datasets.Value("string"), |
| }), |
| "bboxes": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Wellington Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "frame_num": datasets.Value("int32"), "seq_id": datasets.Value("string"), |
| "site": datasets.Value("string"), "camera": datasets.Value("string"), |
| "datetime": datasets.Value("date32"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Island Conservation Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Channel Islands Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "frame_num": datasets.Value("int32"), "seq_id": datasets.Value("string"), |
| "seq_num_frames": datasets.Value("int32"), |
| "original_relative_path": datasets.Value("string"), |
| "location": datasets.Value("string"), |
| "temperature": datasets.Value("string"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "sequence_level_annotation": datasets.Value("bool"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Idaho Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "frame_num": datasets.Value("int32"), "seq_id": datasets.Value("string"), |
| "seq_num_frames": datasets.Value("int32"), |
| "original_relative_path": datasets.Value("string"), |
| "datetime": datasets.Value("date32"), |
| "location": datasets.Value("string"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "sequence_level_annotation": datasets.Value("bool"), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Snapshot Serengeti': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "frame_num": datasets.Value("int32"), "seq_id": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "seq_num_frames": datasets.Value("int32"), |
| "datetime": datasets.Value("date32"), |
| "corrupt": datasets.Value("bool"), |
| "location": datasets.Value("string"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "sequence_level_annotation": datasets.Value("bool"), |
| "seq_id": datasets.Value("string"), |
| "season": datasets.Value("string"), |
| "datetime": datasets.Value("date32"), |
| "subject_id": datasets.Value("string"), |
| "count": datasets.Value("string"), |
| "standing": datasets.Value("float32"), |
| "resting": datasets.Value("float32"), |
| "moving": datasets.Value("float32"), |
| "interacting": datasets.Value("float32"), |
| "young_present": datasets.Value("float32"), |
| "location": datasets.Value("string"), |
| }), |
| "bboxes": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name in [ |
| 'Snapshot Karoo', 'Snapshot Kgalagadi', 'Snapshot Enonkishu', 'Snapshot Camdeboo', |
| 'Snapshot Mountain Zebra', 'Snapshot Kruger' |
| ]: |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "frame_num": datasets.Value("int32"), "seq_id": datasets.Value("string"), |
| "width": datasets.Value("int32"), "height": datasets.Value("int32"), |
| "seq_num_frames": datasets.Value("int32"), |
| "datetime": datasets.Value("date32"), |
| "corrupt": datasets.Value("bool"), |
| "location": datasets.Value("string"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "category_id": datasets.Value("int32"), |
| "sequence_level_annotation": datasets.Value("bool"), |
| "seq_id": datasets.Value("string"), |
| "season": datasets.Value("string"), |
| "datetime": datasets.Value("date32"), |
| "subject_id": datasets.Value("string"), |
| "count": datasets.Value("string"), |
| "standing": datasets.Value("float32"), |
| "resting": datasets.Value("float32"), |
| "moving": datasets.Value("float32"), |
| "interacting": datasets.Value("float32"), |
| "young_present": datasets.Value("float32"), |
| "location": datasets.Value("string"), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
| elif self.config.name == 'Orinoquia Camera Traps': |
| return datasets.Features({ |
| "id": datasets.Value("string"), "file_name": datasets.Value("string"), |
| "frame_num": datasets.Value("int32"), "seq_id": datasets.Value("string"), |
| "seq_num_frames": datasets.Value("int32"), "datetime": datasets.Value("date32"), |
| "location": datasets.Value("string"), |
| "annotations": datasets.Sequence({ |
| "id": datasets.Value("string"), |
| "sequence_level_annotation": datasets.Value("bool"), |
| "category_id": datasets.Value("int32"), |
| }), |
| "image": datasets.Image(decode=False), |
| }) |
|
|
| def _info(self): |
| features = self._get_features() |
|
|
| return datasets.DatasetInfo( |
| |
| description=_DESCRIPTION, |
| |
| features=features, |
| |
| |
| |
| |
| homepage=_HOMEPAGE, |
| |
| license=_LICENSE, |
| |
| citation=_CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| archive_path = dl_manager.download_and_extract(self.config.metadata_url) |
| if archive_path.endswith(".zip"): |
| archive_path = os.path.join(archive_path, os.listdir(archive_path)[0]) |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={ |
| "filepath": archive_path, |
| "split": "train", |
| }, |
| ), |
| ] |
|
|
| def _generate_examples(self, filepath, split): |
| with open(filepath) as f: |
| for line in f: |
| example = json.loads(line) |
| image_url = f"{self.config.image_base_url}/{example['file_name']}" |
| yield example["id"], { |
| **example, |
| "image": image_url |
| } |