|
import csv |
|
|
|
import datasets |
|
from datasets import DatasetDict |
|
|
|
LABELS = {"aerial", "interior", "exterior", "upshot", "skyline", "night"} |
|
|
|
_DATA_URL = { |
|
"train": [f"data/train_images.tar.gz" for i in range(5)], |
|
"validation": ["data/validation_images.tar.gz"], |
|
"test": ["data/test_images.tar.gz"], |
|
} |
|
|
|
|
|
class Sample(datasets.GeneratorBasedBuilder): |
|
DEFAULT_WRITER_BATCH_SIZE = 1000 |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description="A sample dataset to illustrate how to use HF APIs", |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"label": datasets.ClassLabel(names=list(LABELS)), |
|
} |
|
), |
|
homepage="github.com/SOM-Enterprise/hf-dataset-sample-representation", |
|
citation="None", |
|
task_templates=[ |
|
datasets.ImageClassification(image_column="image", label_column="label")], |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
archives = dl_manager.download(_DATA_URL) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"archives": [dl_manager.iter_archive(archive) for archive in archives["train"]], |
|
"split": "train", |
|
} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={ |
|
"archives": [dl_manager.iter_archive(archive) for archive in archives["validation"]], |
|
"split": "validation", |
|
} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"archives": [dl_manager.iter_archive(archive) for archive in archives["test"]], |
|
"split": "test", |
|
} |
|
) |
|
] |
|
|
|
def _generate_examples(self, archives, split): |
|
labels_dict = {} |
|
with open('metadata.csv', newline='') as csvfile: |
|
reader = csv.DictReader(csvfile) |
|
for row in reader: |
|
labels_dict[row['id']] = set(row['label'].split('|')) |
|
|
|
idx = 0 |
|
for archive in archives: |
|
for path, file in archive: |
|
if path.endswith(".jpeg"): |
|
if split != "test": |
|
labels = labels_dict.get(path.split('/')[-1]) |
|
label = labels if labels else [''] |
|
else: |
|
label = -1 |
|
ex = {"image": {"path": path, "bytes": file.read()}, "label": label} |
|
yield idx, ex |
|
idx += 1 |