Eugene Siow commited on
Commit
bcb97ac
1 Parent(s): 325646f

Add dataset builder.

Browse files
Files changed (1) hide show
  1. Set5.py +133 -0
Set5.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+ """Set5 dataset: An evaluation dataset for the image super resolution task"""
16
+
17
+
18
+ import datasets
19
+ from pathlib import Path
20
+
21
+
22
+ _CITATION = """
23
+ @article{bevilacqua2012low,
24
+ title={Low-complexity single-image super-resolution based on nonnegative neighbor embedding},
25
+ author={Bevilacqua, Marco and Roumy, Aline and Guillemot, Christine and Alberi-Morel, Marie Line},
26
+ year={2012},
27
+ publisher={BMVA press}
28
+ }
29
+ """
30
+
31
+ _DESCRIPTION = """
32
+ Set5 is a evaluation dataset with 5 RGB images for the image super resolution task.
33
+ """
34
+
35
+ _HOMEPAGE = "http://people.rennes.inria.fr/Aline.Roumy/results/SR_BMVC12.html"
36
+
37
+ _LICENSE = "UNK"
38
+
39
+ _DL_URL = "https://huggingface.co/datasets/eugenesiow/Set5/resolve/main/data/"
40
+
41
+ _DEFAULT_CONFIG = "bicubic_x2"
42
+
43
+ _DATA_OPTIONS = {
44
+ "bicubic_x2": {
45
+ "hr": _DL_URL + "Set5_HR.tar.gz",
46
+ "lr": _DL_URL + "Set5_LR_x2.tar.gz",
47
+ },
48
+ "bicubic_x3": {
49
+ "hr": _DL_URL + "Set5_HR.tar.gz",
50
+ "lr": _DL_URL + "Set5_LR_x3.tar.gz",
51
+ },
52
+ "bicubic_x4": {
53
+ "hr": _DL_URL + "Set5_HR.tar.gz",
54
+ "lr": _DL_URL + "Set5_LR_x4.tar.gz",
55
+ }
56
+ }
57
+
58
+
59
+ class Set5Config(datasets.BuilderConfig):
60
+ """BuilderConfig for Set5."""
61
+
62
+ def __init__(
63
+ self,
64
+ name,
65
+ hr_url,
66
+ lr_url,
67
+ **kwargs,
68
+ ):
69
+ if name not in _DATA_OPTIONS:
70
+ raise ValueError("data must be one of %s" % _DATA_OPTIONS)
71
+ super(Set5Config, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
72
+ self.hr_url = hr_url
73
+ self.lr_url = lr_url
74
+
75
+
76
+ class Set5(datasets.GeneratorBasedBuilder):
77
+ """Set5 dataset for single image super resolution evaluation."""
78
+
79
+ BUILDER_CONFIGS = [
80
+ Set5Config(
81
+ name=option,
82
+ hr_url=values['hr'],
83
+ lr_url=values['lr']
84
+ ) for option, values in _DATA_OPTIONS.items()
85
+ ]
86
+
87
+ DEFAULT_CONFIG_NAME = _DEFAULT_CONFIG
88
+
89
+ def _info(self):
90
+ features = datasets.Features(
91
+ {
92
+ "hr": datasets.Value("string"),
93
+ "lr": datasets.Value("string"),
94
+ }
95
+ )
96
+ return datasets.DatasetInfo(
97
+ description=_DESCRIPTION,
98
+ features=features,
99
+ supervised_keys=None,
100
+ homepage=_HOMEPAGE,
101
+ license=_LICENSE,
102
+ citation=_CITATION,
103
+ )
104
+
105
+ def _split_generators(self, dl_manager):
106
+ """Returns SplitGenerators."""
107
+ hr_data_dir = dl_manager.download_and_extract(self.config.hr_url)
108
+ lr_data_dir = dl_manager.download_and_extract(self.config.lr_url)
109
+ return [
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.VALIDATION,
112
+ # These kwargs will be passed to _generate_examples
113
+ gen_kwargs={
114
+ "lr_path": lr_data_dir,
115
+ "hr_path": hr_data_dir
116
+ },
117
+ )
118
+ ]
119
+
120
+ def _generate_examples(
121
+ self, hr_path, lr_path
122
+ ):
123
+ """ Yields examples as (key, example) tuples. """
124
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
125
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
126
+ extensions = {'.jpg', '.jpeg', '.png'}
127
+ for file_path in sorted(Path(hr_path).glob(".")):
128
+ if file_path.suffix in extensions:
129
+ file_path_str = str(file_path.as_posix())
130
+ yield file_path_str, {
131
+ 'hr': file_path_str,
132
+ 'lr': str((Path(lr_path) / file_path.name).as_posix())
133
+ }