mariosasko
commited on
Commit
•
d187349
1
Parent(s):
046a4f1
Fix dataset script
Browse filesMakes the dataset script faster (no need to extract the archives) and streamable
- danbooru2023.py +19 -25
danbooru2023.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import datasets
|
3 |
-
from
|
4 |
-
from
|
5 |
-
|
6 |
|
7 |
_EXTENSION = [".png", ".jpg", ".jpeg", ".webp", ".bmp"]
|
8 |
-
_NAME = "nyanko7/danbooru2023"
|
9 |
-
_REVISION = "main"
|
10 |
|
11 |
|
12 |
class DanbooruDataset(datasets.GeneratorBasedBuilder):
|
@@ -28,23 +28,17 @@ class DanbooruDataset(datasets.GeneratorBasedBuilder):
|
|
28 |
return info
|
29 |
|
30 |
def _split_generators(self, dl_manager: DownloadManager):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
for
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
all_fnames = {os.path.relpath(os.path.join(root, fname), start=path) for root, _dirs, files in os.walk(path) for fname in files}
|
46 |
-
image_fnames = sorted([fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION], reverse=True)
|
47 |
-
for image_fname in image_fnames:
|
48 |
-
image_path = os.path.join(path, image_fname)
|
49 |
-
post_id = int(os.path.splitext(os.path.basename(image_fname))[0])
|
50 |
-
yield image_fname, {"post_id": post_id}
|
|
|
1 |
import os
|
2 |
+
import posixpath
|
3 |
+
|
4 |
import datasets
|
5 |
+
from datasets import DatasetInfo, DownloadManager
|
6 |
+
from fsspec.core import url_to_fs
|
7 |
+
|
8 |
|
9 |
_EXTENSION = [".png", ".jpg", ".jpeg", ".webp", ".bmp"]
|
|
|
|
|
10 |
|
11 |
|
12 |
class DanbooruDataset(datasets.GeneratorBasedBuilder):
|
|
|
28 |
return info
|
29 |
|
30 |
def _split_generators(self, dl_manager: DownloadManager):
|
31 |
+
fs, path = url_to_fs(dl_manager._base_path)
|
32 |
+
urls = fs.glob(posixpath.join(path, "**/*.tar"), detail=False)
|
33 |
+
archives = dl_manager.download(["hf://" + url for url in urls])
|
34 |
+
archives = [dl_manager.iter_archive(archives) for archives in archives]
|
35 |
+
return [datasets.SplitGenerator(name="train", gen_kwargs={"archives": archives})]
|
36 |
+
|
37 |
+
def _generate_examples(self, archives):
|
38 |
+
for archive in archives:
|
39 |
+
for path, f in archive:
|
40 |
+
path_root, path_ext = os.path.splitext(path)
|
41 |
+
if path_ext.lower() not in _EXTENSION:
|
42 |
+
continue
|
43 |
+
post_id = int(os.path.basename(path_root))
|
44 |
+
yield path, {"image": {"bytes": f.read()}, "post_id": post_id}
|
|
|
|
|
|
|
|
|
|
|
|