Support for PNG files at 1K as a fallback. Error messages.
Browse files- td01_natural-ground-textures.py +31 -15
td01_natural-ground-textures.py
CHANGED
@@ -35,13 +35,13 @@ class NaturalGroundTextures(datasets.GeneratorBasedBuilder):
|
|
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 = "
|
45 |
|
46 |
def _info(self):
|
47 |
return datasets.DatasetInfo(
|
@@ -58,12 +58,18 @@ class NaturalGroundTextures(datasets.GeneratorBasedBuilder):
|
|
58 |
)
|
59 |
|
60 |
def _split_generators(self, dl_manager):
|
61 |
-
data_dir = dl_manager.
|
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 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
return [
|
69 |
datasets.SplitGenerator(datasets.Split.TRAIN, dict(lines=train_lines, filenames=train_files)),
|
@@ -71,18 +77,28 @@ class NaturalGroundTextures(datasets.GeneratorBasedBuilder):
|
|
71 |
]
|
72 |
|
73 |
def _generate_examples(self, lines, filenames):
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
for key, (data, filename) in enumerate(zip(lines, filenames)):
|
78 |
-
|
|
|
79 |
sz = image.size
|
80 |
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
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"]}
|
|
|
35 |
VERSION = datasets.Version("0.1.0")
|
36 |
|
37 |
BUILDER_CONFIGS = [
|
38 |
+
datasets.BuilderConfig(name="JXL@4K", version=VERSION, description="The original resolution dataset."),
|
39 |
+
datasets.BuilderConfig(name="JXL@2K", version=VERSION, description="Half-resolution dataset."),
|
40 |
+
datasets.BuilderConfig(name="JXL@1K", version=VERSION, description="Quarter-resolution dataset."),
|
41 |
+
datasets.BuilderConfig(name="PNG@1K", version=VERSION, description="Fallback version of the dataset."),
|
42 |
]
|
43 |
|
44 |
+
DEFAULT_CONFIG_NAME = "JXL@2K"
|
45 |
|
46 |
def _info(self):
|
47 |
return datasets.DatasetInfo(
|
|
|
58 |
)
|
59 |
|
60 |
def _split_generators(self, dl_manager):
|
61 |
+
data_dir = dl_manager.download(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 |
+
def _get_file_url(row, split):
|
66 |
+
file_name = row['file_name']
|
67 |
+
if self.config.name[:3] == "PNG":
|
68 |
+
file_name = file_name.replace('.jxl', '.png')
|
69 |
+
return (DOWNLOAD_PREFIX + '/' + split + '/' + file_name)
|
70 |
+
|
71 |
+
train_files = dl_manager.download([_get_file_url(row, split='train') for row in train_lines])
|
72 |
+
test_files = dl_manager.download([_get_file_url(row, split='test') for row in test_lines])
|
73 |
|
74 |
return [
|
75 |
datasets.SplitGenerator(datasets.Split.TRAIN, dict(lines=train_lines, filenames=train_files)),
|
|
|
77 |
]
|
78 |
|
79 |
def _generate_examples(self, lines, filenames):
|
80 |
+
try:
|
81 |
+
if self.config.name[:3] == "JXL":
|
82 |
+
from jxlpy import JXLImagePlugin
|
83 |
+
import PIL.Image
|
84 |
+
except (ImportError, ModuleNotFoundError):
|
85 |
+
logger = datasets.logging.get_logger()
|
86 |
+
logger.critical('\n\n\nERROR: Please install `jxlpy` from PyPI to use JPEG-XL images.\n')
|
87 |
+
raise SystemExit
|
88 |
|
89 |
for key, (data, filename) in enumerate(zip(lines, filenames)):
|
90 |
+
# Load the images from disk with the correct format.
|
91 |
+
image = PIL.Image.open(filename, formats=[self.config.name[:3].lower()])
|
92 |
sz = image.size
|
93 |
|
94 |
+
# PNG files only available at quarter resolution.
|
95 |
+
if self.config.name[:3] == "PNG":
|
96 |
+
sz = (sz*4, sz*4)
|
97 |
+
|
98 |
+
# Resize the images as specified by the user.
|
99 |
+
if self.config.name[-2:] == "1K":
|
100 |
image = image.resize(size=(sz[0]//4, sz[1]//4), resample=PIL.Image.Resampling.LANCZOS)
|
101 |
+
elif self.config.name[-2:] == "2K":
|
102 |
image = image.resize(size=(sz[0]//2, sz[1]//2), resample=PIL.Image.Resampling.LANCZOS)
|
|
|
|
|
103 |
|
104 |
yield key, {"image": image, "set": data["set"]}
|