File size: 3,579 Bytes
068458a 48d8d15 068458a f608c8a 068458a f1f47d6 068458a cdba063 068458a bb64b59 068458a 48d8d15 068458a 48d8d15 068458a cdba063 068458a 15a6da2 068458a b8f144b cdba063 b8f144b |
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 |
# USAGE: this script is used to create an image dataset that is NOT hosted on HuggingFace but points to the original files
# to download and generate the dataset.
import os
import datasets
from datasets.tasks import ImageClassification
_DESCRIPTION = """\
Images collected using Wild Sage Nodes to detect wild fires.
"""
_HOMEPAGE = "https://sagecontinuum.org/"
_LICENSE = "MIT"
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = "https://web.lcrc.anl.gov/public/waggle/datasets/smoke-example.tar"
_NAMES = [
"cloud",
"other",
"smoke"
]
_PROMPT = "What is shown in the image?"
_CHOICES = _NAMES
class smokedataset_QA(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features= datasets.Features(
{
"image": datasets.Image(),
"label": datasets.ClassLabel(names=_NAMES),
"prompt": datasets.Value(dtype='string'),
"choices": datasets.Sequence(datasets.Value("string"))
}
),
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
supervised_keys=None,
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE
# Citation for the dataset
# citation=_CITATION,
)
# This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
def _split_generators(self, dl_manager):
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
data_dir = dl_manager.download(_URLS)
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"images": dl_manager.iter_archive(data_dir),
"split": "test",
"prompt": _PROMPT,
"choices": _CHOICES
},
)
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, images, prompt, choices, split):
for file_path, file_obj in images:
label = file_path.split("/")[1]
yield file_path,{
"image": {"path": file_path, "bytes": file_obj.read()},
"label": label,
"prompt": prompt,
"choices": choices
} |