alexjc commited on
Commit
79c4b52
1 Parent(s): 9b511b4

Prototype of dataset loading script that supports JXL.

Browse files
Files changed (1) hide show
  1. td01.py +88 -0
td01.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2022, texture.design.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import os
16
+ import json
17
+
18
+ import datasets
19
+
20
+
21
+ DESCRIPTION = """\
22
+ """
23
+
24
+ REPO_PREFIX = "https://huggingface.co/datasets/texturedesign/td01_natural-ground-textures"
25
+ DOWNLOAD_PREFIX = REPO_PREFIX + "/resolve/main"
26
+
27
+ INDEX_URLS = {
28
+ "train": REPO_PREFIX + "/raw/main/train/metadata.jsonl",
29
+ "test": REPO_PREFIX + "/raw/main/test/metadata.jsonl",
30
+ }
31
+
32
+
33
+ class NaturalGroundTextures(datasets.GeneratorBasedBuilder):
34
+
35
+ VERSION = datasets.Version("0.1.0")
36
+
37
+ BUILDER_CONFIGS = [
38
+ datasets.BuilderConfig(name="4K", version=VERSION, description="The original resolution dataset."),
39
+ datasets.BuilderConfig(name="2K", version=VERSION, description="Half-resolution dataset."),
40
+ datasets.BuilderConfig(name="1K", version=VERSION, description="Quarter-resolution dataset."),
41
+
42
+ ]
43
+
44
+ DEFAULT_CONFIG_NAME = "2k"
45
+
46
+ def _info(self):
47
+ return datasets.DatasetInfo(
48
+ description=DESCRIPTION,
49
+ citation="",
50
+ homepage="",
51
+ license="",
52
+ features=datasets.Features(
53
+ {
54
+ "image": datasets.Image(),
55
+ "set": datasets.Value("uint8"),
56
+ }
57
+ )
58
+ )
59
+
60
+ def _split_generators(self, dl_manager):
61
+ data_dir = dl_manager.download_and_extract(INDEX_URLS)
62
+ train_lines = [json.loads(l) for l in open(data_dir["train"], "r", encoding="utf-8").readlines()]
63
+ test_lines = [json.loads(l) for l in open(data_dir["test"], "r", encoding="utf-8").readlines()]
64
+
65
+ train_files = dl_manager.download_and_extract([(DOWNLOAD_PREFIX + '/train/' + row['file_name']) for row in train_lines])
66
+ test_files = dl_manager.download_and_extract([(DOWNLOAD_PREFIX + '/test/' + row['file_name']) for row in test_lines])
67
+
68
+ return [
69
+ datasets.SplitGenerator(datasets.Split.TRAIN, dict(lines=train_lines, filenames=train_files)),
70
+ datasets.SplitGenerator(datasets.Split.TEST, dict(lines=test_lines, filenames=test_files)),
71
+ ]
72
+
73
+ def _generate_examples(self, lines, filenames):
74
+ from jxlpy import JXLImagePlugin
75
+ import PIL.Image
76
+
77
+ for key, (data, filename) in enumerate(zip(lines, filenames)):
78
+ image = PIL.Image.open(filename, formats=["jxl"])
79
+ sz = image.size
80
+
81
+ if self.config.name == "1K":
82
+ image = image.resize(size=(sz[0]//4, sz[1]//4), resample=PIL.Image.Resampling.LANCZOS)
83
+ elif self.config.name == "2K":
84
+ image = image.resize(size=(sz[0]//2, sz[1]//2), resample=PIL.Image.Resampling.LANCZOS)
85
+ else:
86
+ assert self.config.name == "4K"
87
+
88
+ yield key, {"image": image, "set": data["set"]}