alexjc's picture
Prototype of dataset loading script that supports JXL.
79c4b52
raw
history blame
3.3 kB
# Copyright (c) 2022, texture.design.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json
import datasets
DESCRIPTION = """\
"""
REPO_PREFIX = "https://huggingface.co/datasets/texturedesign/td01_natural-ground-textures"
DOWNLOAD_PREFIX = REPO_PREFIX + "/resolve/main"
INDEX_URLS = {
"train": REPO_PREFIX + "/raw/main/train/metadata.jsonl",
"test": REPO_PREFIX + "/raw/main/test/metadata.jsonl",
}
class NaturalGroundTextures(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("0.1.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="4K", version=VERSION, description="The original resolution dataset."),
datasets.BuilderConfig(name="2K", version=VERSION, description="Half-resolution dataset."),
datasets.BuilderConfig(name="1K", version=VERSION, description="Quarter-resolution dataset."),
]
DEFAULT_CONFIG_NAME = "2k"
def _info(self):
return datasets.DatasetInfo(
description=DESCRIPTION,
citation="",
homepage="",
license="",
features=datasets.Features(
{
"image": datasets.Image(),
"set": datasets.Value("uint8"),
}
)
)
def _split_generators(self, dl_manager):
data_dir = dl_manager.download_and_extract(INDEX_URLS)
train_lines = [json.loads(l) for l in open(data_dir["train"], "r", encoding="utf-8").readlines()]
test_lines = [json.loads(l) for l in open(data_dir["test"], "r", encoding="utf-8").readlines()]
train_files = dl_manager.download_and_extract([(DOWNLOAD_PREFIX + '/train/' + row['file_name']) for row in train_lines])
test_files = dl_manager.download_and_extract([(DOWNLOAD_PREFIX + '/test/' + row['file_name']) for row in test_lines])
return [
datasets.SplitGenerator(datasets.Split.TRAIN, dict(lines=train_lines, filenames=train_files)),
datasets.SplitGenerator(datasets.Split.TEST, dict(lines=test_lines, filenames=test_files)),
]
def _generate_examples(self, lines, filenames):
from jxlpy import JXLImagePlugin
import PIL.Image
for key, (data, filename) in enumerate(zip(lines, filenames)):
image = PIL.Image.open(filename, formats=["jxl"])
sz = image.size
if self.config.name == "1K":
image = image.resize(size=(sz[0]//4, sz[1]//4), resample=PIL.Image.Resampling.LANCZOS)
elif self.config.name == "2K":
image = image.resize(size=(sz[0]//2, sz[1]//2), resample=PIL.Image.Resampling.LANCZOS)
else:
assert self.config.name == "4K"
yield key, {"image": image, "set": data["set"]}