|
"""CC6204-Hackaton-Cub-Dataset: Multimodal""" |
|
import os |
|
import re |
|
import datasets |
|
|
|
import pandas as pd |
|
|
|
from requests import get |
|
|
|
datasets.logging.set_verbosity_debug() |
|
logger = datasets.logging.get_logger(__name__) |
|
|
|
datasets.logging.set_verbosity_debug() |
|
|
|
|
|
_DESCRIPTION = "XYZ" |
|
_CITATION = "XYZ" |
|
_HOMEPAGE = "https://github.com/ivansipiran/CC6204-Deep-Learning/blob/main/Hackaton/hackaton.md" |
|
|
|
_REPO = "https://huggingface.co/datasets/alkzar90/CC6204-Hackaton-Cub-Dataset/resolve/main/data" |
|
|
|
_URLS = { |
|
"train_test_split": f"{_REPO}/train_test_split.txt", |
|
"classes": f"{_REPO}/classes.txt", |
|
"image_class_labels": f"{_REPO}/image_class_labels.txt", |
|
"images": f"{_REPO}/images.txt", |
|
"image_urls": f"{_REPO}/images.zip", |
|
"text_urls": f"{_REPO}/text.zip", |
|
"mini_images_urls": f"{_REPO}/dummy/mini_images.zip" |
|
} |
|
|
|
|
|
classes = get(_URLS["classes"]).iter_lines() |
|
_ID2LABEL = {} |
|
for row in classes: |
|
row = row.decode("UTF8") |
|
if row != "": |
|
idx, label = row.split(" ") |
|
_ID2LABEL[int(idx)] = re.search("[^\d\.\_+].+", label).group(0).replace("_", " ") |
|
|
|
|
|
_NAMES = list(_ID2LABEL.values()) |
|
|
|
|
|
|
|
imgpath_to_ids = get(_URLS["images"]).iter_lines() |
|
_IMGNAME2ID = {} |
|
for row in imgpath_to_ids: |
|
row = row.decode("UTF8") |
|
if row != "": |
|
idx, img_name = row.split(" ") |
|
_IMGNAME2ID[os.path.basename(img_name)] = idx |
|
|
|
|
|
|
|
train_test_split = get(_URLS["train_test_split"]).iter_lines() |
|
_TRAIN_IDX_SET = [] |
|
for row in train_test_split: |
|
row = row.decode("UTF8") |
|
if row != "": |
|
idx, train_bool = row.split(" ") |
|
|
|
if train_bool == "1": |
|
_TRAIN_IDX_SET.append(idx) |
|
|
|
_TRAIN_IDX_SET = set(_TRAIN_IDX_SET) |
|
|
|
|
|
class CubDataset(datasets.GeneratorBasedBuilder): |
|
"""Cub Dataset""" |
|
|
|
def _info(self): |
|
features = datasets.Features({ |
|
"image": datasets.Image(), |
|
"labels": datasets.features.ClassLabel(names=_NAMES), |
|
}) |
|
keys = ("image", "labels") |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
supervised_keys=keys, |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
train_files = [] |
|
test_files = [] |
|
|
|
|
|
data_files = dl_manager.download_and_extract(_URLS["mini_images_urls"]) |
|
path_files = dl_manager.iter_files(data_files) |
|
for img in path_files: |
|
img_idx = _IMGNAME2ID[os.path.basename(img)] |
|
if img_idx in _TRAIN_IDX_SET: |
|
train_files.append(img) |
|
else: |
|
test_files.append(img) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"files": train_files |
|
} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"files": test_files |
|
} |
|
) |
|
] |
|
|
|
|
|
def _generate_examples(self, files): |
|
|
|
for i, path in enumerate(files): |
|
file_name = os.path.basename(path) |
|
if file_name.endswith(".jpg"): |
|
yield i, { |
|
"image": path, |
|
"labels": _ID2LABEL[int(re.search("^\d+", file_name).group(0))], |
|
} |
|
|