Birchlabs commited on
Commit
cc11703
1 Parent(s): 6731a01

webdataset loading script

Browse files
openai-guided-diffusion-256-classcond-unguided-samples-50k.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import tarfile
3
+ from datasets import Features, Value
4
+ from datasets.download.download_manager import DownloadManager
5
+ from typing import List, NamedTuple, TypedDict, Generator
6
+ from PIL import Image
7
+
8
+ Example = TypedDict('Example', {
9
+ 'index': int,
10
+ 'tar': str,
11
+ 'tar_path': str,
12
+ 'img': Image.Image,
13
+ })
14
+
15
+ class KeyedExample(NamedTuple):
16
+ key: int
17
+ example: Example
18
+
19
+ tar_count = 5
20
+ files = [f'./{ix:05d}.tar' for ix in range(tar_count)]
21
+
22
+ class MyWebdataset(datasets.GeneratorBasedBuilder):
23
+ VERSION = '1.0.0'
24
+
25
+ def _info(self) -> datasets.DatasetInfo:
26
+ print(__file__)
27
+ return datasets.DatasetInfo(
28
+ description="OpenAI guided-diffusion 256px class-conditional unguided samples (50k)",
29
+ features=Features({
30
+ 'index': Value('uint32'),
31
+ 'tar': Value('string'),
32
+ 'tar_path': Value('string'),
33
+ 'img': datasets.Image(),
34
+ }),
35
+ )
36
+
37
+ def _split_generators(self, dl_manager: DownloadManager) -> List[datasets.SplitGenerator]:
38
+ return [
39
+ datasets.SplitGenerator(
40
+ name=datasets.Split.TRAIN,
41
+ gen_kwargs={"filepaths": dl_manager.download(files)},
42
+ )
43
+ ]
44
+
45
+ def _generate_examples(self, filepaths: List[str]) -> Generator[KeyedExample, None, None]:
46
+ index = 0
47
+ for filepath in filepaths:
48
+ with tarfile.open(filepath, "r:") as tar:
49
+ for member in tar.getmembers():
50
+ with tar.extractfile(member.name) as f:
51
+ pil: Image.Image = Image.open(f)
52
+ yield KeyedExample(index, Example(
53
+ index=index,
54
+ tar=filepath,
55
+ tar_path=member.path,
56
+ img=pil,
57
+ ))
58
+ index += 1