Download LoadingScriptPractice.py from mickylan2367/LoadingScriptPractice: direct link, hf CLI and curl.
- Browser
- Download file 5.69 kB
-
https://huggingface.co/datasets/mickylan2367/LoadingScriptPractice/resolve/b5679b75ea6c4c77879a5caa2d03d2aea3313856/LoadingScriptPractice.py
- Command line
-
hf download hf://datasets/mickylan2367/LoadingScriptPractice@b5679b75ea6c4c77879a5caa2d03d2aea3313856/LoadingScriptPractice.py
-
curl -L -o LoadingScriptPractice.py https://huggingface.co/datasets/mickylan2367/LoadingScriptPractice/resolve/b5679b75ea6c4c77879a5caa2d03d2aea3313856/LoadingScriptPractice.py
5.69 kB
| import datasets | |
| from huggingface_hub import HfApi | |
| from datasets import DownloadManager, DatasetInfo | |
| from datasets.data_files import DataFilesDict | |
| import os | |
| import json | |
| # ここに設定を記入 | |
| _NAME = "mickylan2367/LoadingScriptPractice" | |
| _EXTENSION = [".png"] | |
| _REVISION = "main" | |
| # _HOMEPAGE = "https://github.com/fastai/imagenette" | |
| # プログラムを置く場所が決まったら、ここにホームページURLつける | |
| # _HOMEPAGE = "https://huggingface.co/datasets/mickylan2367/spectrogram_musicCaps" | |
| _DESCRIPTION = f"""\ | |
| {_NAME} Datasets including spectrogram.png file from Google MusicCaps Datasets! | |
| Using for Project Learning... | |
| """ | |
| # え...なにこれ(;´・ω・) | |
| # _IMAGES_DIR = "mickylan2367/images/data/" | |
| # _REPO = "https://huggingface.co/datasets/frgfm/imagenette/resolve/main/metadata" | |
| # 参考になりそうなURL集 | |
| # https://huggingface.co/docs/datasets/v1.1.1/_modules/datasets/utils/download_manager.html | |
| # https://huggingface.co/datasets/animelover/danbooru2022/blob/main/danbooru2022.py | |
| # https://huggingface.co/datasets/food101/blob/main/food101.py | |
| # https://huggingface.co/docs/datasets/about_dataset_load | |
| # class LoadingScriptPracticeConfig(datasets.BuilderConfig): | |
| # """Builder Config for spectrogram_MusicCaps""" | |
| # def __init__(self, **kwargs): | |
| # """BuilderConfig | |
| # Args: | |
| # data_url: `string`, url to download the zip file from. | |
| # metadata_urls: dictionary with keys 'train' and 'validation' containing the archive metadata URLs | |
| # **kwargs: keyword arguments forwarded to super. | |
| # """ | |
| # super(LoadingScriptPracticeConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs) | |
| # # self.data_url = data_url | |
| # # self.metadata_urls = metadata_urls | |
| class LoadingScriptPractice(datasets.GeneratorBasedBuilder): | |
| # データのサブセットはここで用意 | |
| BUILDER_CONFIGS = [ | |
| datasets.BuilderConfig( | |
| name="MusicCaps data 0_3", | |
| description="this Datasets is personal practice for using loadingScript. Data is from Google/MusicCaps", | |
| ) | |
| ] | |
| def _info(self) -> DatasetInfo: | |
| return datasets.DatasetInfo( | |
| description = self.config.description, | |
| features=datasets.Features( | |
| { | |
| "image": datasets.Image(), | |
| "caption": datasets.Value("string"), | |
| "data_idx": datasets.Value("int32"), | |
| "number" : datasets.Value("int32"), | |
| "label" : datasets.Value("string") | |
| } | |
| ), | |
| supervised_keys=("image", "caption", "data_idx", "number", "label"), | |
| # homepage=_HOMEPAGE, | |
| citation= "", | |
| # license=_LICENSE, | |
| # task_templates=[ImageClassification(image_column="image", label_column="label")], | |
| ) | |
| def _split_generators(self, dl_manager: DownloadManager): | |
| # huggingfaceのディレクトリからデータを取ってくる | |
| hfh_dataset_info = HfApi().dataset_info(_NAME, revision=_REVISION, timeout=100.0) | |
| split_metadata_paths = DataFilesDict.from_hf_repo( | |
| {datasets.Split.TRAIN: ["**"]}, | |
| dataset_info=hfh_dataset_info, | |
| allowed_extensions=["jsonl", ".jsonl"], | |
| ) | |
| # **.zipのURLをDict型として取得? | |
| data_path = DataFilesDict.from_hf_repo( | |
| {datasets.Split.TRAIN: ["**"]}, | |
| dataset_info=hfh_dataset_info, | |
| allowed_extensions=["zip", ".zip"], | |
| ) | |
| gs = [] | |
| for split, files in data_path.items(): | |
| ''' | |
| split : "train" or "test" or "val" | |
| files : zip files | |
| ''' | |
| # リポジトリからダウンロードしてとりあえずキャッシュしたURLリストを取得 | |
| split_metadata_path = split_metadata_paths[split][0] | |
| downloaded_files_path = dl_manager.download_and_extract(files) | |
| # 元のコードではzipファイルの中身を"filepath"としてそのまま_generate_exampleに引き渡している? | |
| gs.append( | |
| datasets.SplitGenerator( | |
| name = split, | |
| gen_kwargs={ | |
| "images" : dl_manager.iter_archive(downloaded_files_path), | |
| "metadata_path": split_metadata_path | |
| } | |
| ) | |
| ) | |
| return gs | |
| def _generate_examples(self, images, metadata_path, dl_manager:DownloadManager): | |
| """Generate images and captions for splits.""" | |
| # with open(metadata_path, encoding="utf-8") as f: | |
| # files_to_keep = set(f.read().split("\n")) | |
| file_list = list() | |
| caption_list = list() | |
| dataIDX_list = list() | |
| num_list = list() | |
| label_list = list() | |
| with open(dl_manager.download_and_extract(metadata_path)) as fin: | |
| for line in fin: | |
| data = json.loads(line) | |
| file_list.append(data["file_name"]) | |
| caption_list.append(data["caption"]) | |
| dataIDX_list.append(data["data_idx"]) | |
| num_list.append(data["number"]) | |
| label_list.append(data["label"]) | |
| for idx, (file_path, file_obj) in enumerate(images): | |
| yield file_path, { | |
| "image": { | |
| "path": file_path, | |
| "bytes": file_obj.read() | |
| }, | |
| "caption" : caption_list[idx], | |
| "data_idx" : dataIDX_list[idx], | |
| "number" : num_list[idx], | |
| "label": label_list[idx] | |
| } | |