|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""DIV2K dataset: DIVerse 2K resolution high quality images. |
| 17 | +
|
| 18 | +As used for the challenges @ NTIRE (CVPR 2017 and CVPR 2018) |
| 19 | +and @ PIRM (ECCV 2018) |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import absolute_import |
| 23 | +from __future__ import division |
| 24 | +from __future__ import print_function |
| 25 | + |
| 26 | +import os.path |
| 27 | + |
| 28 | +import tensorflow.compat.v2 as tf |
| 29 | +import tensorflow_datasets.public_api as tfds |
| 30 | + |
| 31 | +_CITATION = """@InProceedings{Ignatov_2018_ECCV_Workshops, |
| 32 | +author = {Ignatov, Andrey and Timofte, Radu and others}, |
| 33 | +title = {PIRM challenge on perceptual image enhancement on smartphones: report}, |
| 34 | +booktitle = {European Conference on Computer Vision (ECCV) Workshops}, |
| 35 | +url = "http://www.vision.ee.ethz.ch/~timofter/publications/Agustsson-CVPRW-2017.pdf", |
| 36 | +month = {January}, |
| 37 | +year = {2019} |
| 38 | +} |
| 39 | +""" |
| 40 | + |
| 41 | +_DESCRIPTION = """ |
| 42 | +DIV2K dataset: DIVerse 2K resolution high quality images as used for the challenges @ NTIRE (CVPR 2017 and CVPR 2018) and @ PIRM (ECCV 2018) |
| 43 | +""" |
| 44 | + |
| 45 | +_DL_URL = "https://data.vision.ee.ethz.ch/cvl/DIV2K/" |
| 46 | + |
| 47 | +_DL_URLS = { |
| 48 | + "train_hr": _DL_URL + "DIV2K_train_HR.zip", |
| 49 | + "valid_hr": _DL_URL + "DIV2K_valid_HR.zip", |
| 50 | + "train_bicubic_x2": _DL_URL + "DIV2K_train_LR_bicubic_X2.zip", |
| 51 | + "train_unknown_x2": _DL_URL + "DIV2K_train_LR_unknown_X2.zip", |
| 52 | + "valid_bicubic_x2": _DL_URL + "DIV2K_valid_LR_bicubic_X2.zip", |
| 53 | + "valid_unknown_x2": _DL_URL + "DIV2K_valid_LR_unknown_X2.zip", |
| 54 | + "train_bicubic_x3": _DL_URL + "DIV2K_train_LR_bicubic_X3.zip", |
| 55 | + "train_unknown_x3": _DL_URL + "DIV2K_train_LR_unknown_X3.zip", |
| 56 | + "valid_bicubic_x3": _DL_URL + "DIV2K_valid_LR_bicubic_X3.zip", |
| 57 | + "valid_unknown_x3": _DL_URL + "DIV2K_valid_LR_unknown_X3.zip", |
| 58 | + "train_bicubic_x4": _DL_URL + "DIV2K_train_LR_bicubic_X4.zip", |
| 59 | + "train_unknown_x4": _DL_URL + "DIV2K_train_LR_unknown_X4.zip", |
| 60 | + "valid_bicubic_x4": _DL_URL + "DIV2K_valid_LR_bicubic_X4.zip", |
| 61 | + "valid_unknown_x4": _DL_URL + "DIV2K_valid_LR_unknown_X4.zip", |
| 62 | + "train_bicubic_x8": _DL_URL + "DIV2K_train_LR_x8.zip", |
| 63 | + "valid_bicubic_x8": _DL_URL + "DIV2K_valid_LR_x8.zip", |
| 64 | + "train_realistic_mild_x4": _DL_URL + "DIV2K_train_LR_mild.zip", |
| 65 | + "valid_realistic_mild_x4": _DL_URL + "DIV2K_valid_LR_mild.zip", |
| 66 | + "train_realistic_difficult_x4": _DL_URL + "DIV2K_train_LR_difficult.zip", |
| 67 | + "valid_realistic_difficult_x4": _DL_URL + "DIV2K_valid_LR_difficult.zip", |
| 68 | + "train_realistic_wild_x4": _DL_URL + "DIV2K_train_LR_wild.zip", |
| 69 | + "valid_realistic_wild_x4": _DL_URL + "DIV2K_valid_LR_wild.zip", |
| 70 | +} |
| 71 | + |
| 72 | +_DATA_OPTIONS = [ |
| 73 | + "bicubic_x2", "bicubic_x3", "bicubic_x4", "bicubic_x8", "unknown_x2", |
| 74 | + "unknown_x3", "unknown_x4", "realistic_mild_x4", "realistic_difficult_x4", |
| 75 | + "realistic_wild_x4" |
| 76 | +] |
| 77 | + |
| 78 | + |
| 79 | +class Div2kConfig(tfds.core.BuilderConfig): |
| 80 | + """BuilderConfig for Div2k.""" |
| 81 | + |
| 82 | + def __init__(self, name, **kwargs): |
| 83 | + """Constructs a Div2kConfig.""" |
| 84 | + if name not in _DATA_OPTIONS: |
| 85 | + raise ValueError("data must be one of %s" % _DATA_OPTIONS) |
| 86 | + |
| 87 | + description = kwargs.get("description", "Uses %s data." % name) |
| 88 | + kwargs["description"] = description |
| 89 | + |
| 90 | + super(Div2kConfig, self).__init__(name=name, **kwargs) |
| 91 | + self.data = name |
| 92 | + self.download_urls = { |
| 93 | + "train_lr_url": _DL_URLS["train_" + self.data], |
| 94 | + "valid_lr_url": _DL_URLS["valid_" + self.data], |
| 95 | + "train_hr_url": _DL_URLS["train_hr"], |
| 96 | + "valid_hr_url": _DL_URLS["valid_hr"], |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +def _make_builder_configs(): |
| 101 | + configs = [] |
| 102 | + for data in _DATA_OPTIONS: |
| 103 | + configs.append(Div2kConfig(version=tfds.core.Version("2.0.0"), name=data)) |
| 104 | + return configs |
| 105 | + |
| 106 | + |
| 107 | +class Div2k(tfds.core.GeneratorBasedBuilder): |
| 108 | + """DIV2K dataset: DIVerse 2K resolution high quality images.""" |
| 109 | + |
| 110 | + BUILDER_CONFIGS = _make_builder_configs() |
| 111 | + |
| 112 | + def _info(self): |
| 113 | + return tfds.core.DatasetInfo( |
| 114 | + builder=self, |
| 115 | + description=_DESCRIPTION, |
| 116 | + features=tfds.features.FeaturesDict({ |
| 117 | + "lr": tfds.features.Image(), |
| 118 | + "hr": tfds.features.Image(), |
| 119 | + }), |
| 120 | + supervised_keys=("lr", "hr"), |
| 121 | + homepage=_DL_URL, |
| 122 | + citation=_CITATION, |
| 123 | + ) |
| 124 | + |
| 125 | + def _split_generators(self, dl_manager): |
| 126 | + """Returns SplitGenerators.""" |
| 127 | + print("EXTRACTING", self.builder_config.download_urls) |
| 128 | + extracted_paths = dl_manager.download_and_extract( |
| 129 | + self.builder_config.download_urls) |
| 130 | + |
| 131 | + return [ |
| 132 | + tfds.core.SplitGenerator( |
| 133 | + name=tfds.Split.TRAIN, |
| 134 | + gen_kwargs={ |
| 135 | + "lr_path": extracted_paths["train_lr_url"], |
| 136 | + "hr_path": os.path.join(extracted_paths["train_hr_url"], |
| 137 | + "DIV2K_train_HR"), |
| 138 | + }), |
| 139 | + tfds.core.SplitGenerator( |
| 140 | + name=tfds.Split.VALIDATION, |
| 141 | + gen_kwargs={ |
| 142 | + "lr_path": extracted_paths["valid_lr_url"], |
| 143 | + "hr_path": os.path.join(extracted_paths["valid_hr_url"], |
| 144 | + "DIV2K_valid_HR"), |
| 145 | + }), |
| 146 | + ] |
| 147 | + |
| 148 | + def _generate_examples(self, lr_path, hr_path): |
| 149 | + """Yields examples.""" |
| 150 | + for root, _, files in tf.io.gfile.walk(lr_path): |
| 151 | + for file_path in files: |
| 152 | + # Select only png files. |
| 153 | + if file_path.endswith(".png"): |
| 154 | + yield file_path, { |
| 155 | + "lr": os.path.join(root, file_path), |
| 156 | + # Extract the image id from the filename: "0001x2.png" |
| 157 | + "hr": os.path.join(hr_path, file_path[:4] + ".png") |
| 158 | + } |
0 commit comments