Skip to content

Commit e49ca71

Browse files
Merge pull request #1276 from WilliamHYZhang:master
PiperOrigin-RevId: 291883668
2 parents 8551888 + 6643cf2 commit e49ca71

File tree

28 files changed

+329
-2
lines changed

28 files changed

+329
-2
lines changed

docs/release_notes.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
[VTAB benchmark](https://arxiv.org/abs/1910.04867).
1818
* Add e-SNLI dataset from the paper
1919
[e-SNLI](http://papers.nips.cc/paper/8163-e-snli-natural-language-inference-with-natural-language-explanations.pdf).
20-
* Add SCAN dataset introduced
21-
[here](https://arxiv.org/pdf/1711.00350.pdf).
20+
* Add SCAN dataset introduced [here](https://arxiv.org/pdf/1711.00350.pdf).
21+
* Add DIV2K dataset from the paper
22+
[DIV2K](http://www.vision.ee.ethz.ch/~timofter/publications/Agustsson-CVPRW-2017.pdf)

tensorflow_datasets/image/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from tensorflow_datasets.image.cycle_gan import CycleGAN
4545
from tensorflow_datasets.image.deep_weeds import DeepWeeds
4646
from tensorflow_datasets.image.diabetic_retinopathy_detection import DiabeticRetinopathyDetection
47+
from tensorflow_datasets.image.div2k import Div2k
4748
from tensorflow_datasets.image.dmlab import Dmlab
4849
from tensorflow_datasets.image.downsampled_imagenet import DownsampledImagenet
4950
from tensorflow_datasets.image.dsprites import Dsprites

tensorflow_datasets/image/div2k.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
"""Test for div2k dataset."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
from tensorflow_datasets import testing
23+
from tensorflow_datasets.image import div2k
24+
25+
26+
class Div2kTestBicubicX(testing.DatasetBuilderTestCase):
27+
DATASET_CLASS = div2k.Div2k
28+
BUILDER_CONFIG_NAMES_TO_TEST = ["bicubic_x2"]
29+
SPLITS = {
30+
"train": 1,
31+
"validation": 1,
32+
}
33+
34+
DL_EXTRACT_RESULT = {
35+
"train_hr_url": "",
36+
"valid_hr_url": "",
37+
"train_lr_url": "DIV2K_train_LR_bicubic_X2",
38+
"valid_lr_url": "DIV2K_valid_LR_bicubic_X2",
39+
}
40+
41+
42+
class Div2kTestUnknownX(testing.DatasetBuilderTestCase):
43+
DATASET_CLASS = div2k.Div2k
44+
BUILDER_CONFIG_NAMES_TO_TEST = ["unknown_x2"]
45+
SPLITS = {
46+
"train": 1,
47+
"validation": 1,
48+
}
49+
50+
DL_EXTRACT_RESULT = {
51+
"train_hr_url": "",
52+
"valid_hr_url": "",
53+
"train_lr_url": "DIV2K_train_LR_unknown_X2",
54+
"valid_lr_url": "DIV2K_valid_LR_unknown_X2",
55+
}
56+
57+
58+
if __name__ == "__main__":
59+
testing.test_main()

tensorflow_datasets/testing/div2k.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
"""Generates DIV2K like files with random data for testing."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
import os
23+
24+
from absl import app
25+
from absl import flags
26+
27+
import tensorflow as tf
28+
29+
from tensorflow_datasets.core.utils import py_utils
30+
from tensorflow_datasets.testing import fake_data_utils
31+
32+
flags.DEFINE_string("tfds_dir", py_utils.tfds_dir(),
33+
"Path to tensorflow_datasets directory")
34+
35+
FLAGS = flags.FLAGS
36+
37+
DATA = {
38+
"DIV2K_train_HR": "0001.png",
39+
"DIV2K_train_LR_bicubic_X2": "0001x2.png",
40+
"DIV2K_train_LR_bicubic_X3": "0001x3.png",
41+
"DIV2K_train_LR_bicubic_X4": "0001x4.png",
42+
"DIV2K_train_LR_difficult": "0001x4d.png",
43+
"DIV2K_train_LR_mild": "0001x4m.png",
44+
"DIV2K_train_LR_unknown_X2": "0001x2.png",
45+
"DIV2K_train_LR_unknown_X3": "0001x3.png",
46+
"DIV2K_train_LR_unknown_X4": "0001x4.png",
47+
"DIV2K_train_LR_wild": "0001x4w.png",
48+
"DIV2K_train_LR_x8": "0001x8.png",
49+
"DIV2K_valid_HR": "0002.png",
50+
"DIV2K_valid_LR_bicubic_X2": "0002x2.png",
51+
"DIV2K_valid_LR_bicubic_X3": "0002x3.png",
52+
"DIV2K_valid_LR_bicubic_X4": "0002x4.png",
53+
"DIV2K_valid_LR_difficult": "0002x4d.png",
54+
"DIV2K_valid_LR_mild": "0002x4m.png",
55+
"DIV2K_valid_LR_unknown_X2": "0002x2.png",
56+
"DIV2K_valid_LR_unknown_X3": "0002x3.png",
57+
"DIV2K_valid_LR_unknown_X4": "0002x4.png",
58+
"DIV2K_valid_LR_wild": "0002x4w.png",
59+
"DIV2K_valid_LR_x8": "0002x8.png",
60+
}
61+
62+
63+
def _output_dir():
64+
"""Returns output directory."""
65+
return os.path.join(FLAGS.tfds_dir, "testing", "test_data", "fake_examples",
66+
"div2k")
67+
68+
69+
def _generate_image(fdir, fname):
70+
dirname = os.path.join(_output_dir(), fdir)
71+
if not os.path.exists(dirname):
72+
os.makedirs(dirname)
73+
tf.io.gfile.copy(
74+
fake_data_utils.get_random_png(1, 1),
75+
os.path.join(dirname, fname),
76+
overwrite=True)
77+
78+
79+
def main(argv):
80+
del argv
81+
for fdir, fname in DATA.items():
82+
_generate_image(fdir, fname)
83+
84+
85+
if __name__ == "__main__":
86+
app.run(main)
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)