File size: 4,515 Bytes
600666e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
"""BSD100 dataset: An evaluation dataset for the image super resolution task"""


import datasets
from pathlib import Path


_CITATION = """
@inproceedings{martin2001database,
  title={A database of human segmented natural images and its application to evaluating segmentation algorithms and measuring ecological statistics},
  author={Martin, David and Fowlkes, Charless and Tal, Doron and Malik, Jitendra},
  booktitle={Proceedings Eighth IEEE International Conference on Computer Vision. ICCV 2001},
  volume={2},
  pages={416--423},
  year={2001},
  organization={IEEE}
}
"""

_DESCRIPTION = """
BSD is a dataset used frequently for image denoising and super-resolution. 
BSD100 is the testing set of the Berkeley segmentation dataset BSD300.
"""

_HOMEPAGE = "https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/"

_LICENSE = "UNK"

_DL_URL = "https://huggingface.co/datasets/eugenesiow/BSD100/resolve/main/data/"

_DEFAULT_CONFIG = "bicubic_x2"

_DATA_OPTIONS = {
    "bicubic_x2": {
        "hr": _DL_URL + "BSD100_HR.tar.gz",
        "lr": _DL_URL + "BSD100_LR_x2.tar.gz",
    },
    "bicubic_x3": {
        "hr": _DL_URL + "BSD100_HR.tar.gz",
        "lr": _DL_URL + "BSD100_LR_x3.tar.gz",
    },
    "bicubic_x4": {
        "hr": _DL_URL + "BSD100_HR.tar.gz",
        "lr": _DL_URL + "BSD100_LR_x4.tar.gz",
    }
}


class Bsd100Config(datasets.BuilderConfig):
    """BuilderConfig for BSD100."""

    def __init__(
        self,
        name,
        hr_url,
        lr_url,
        **kwargs,
    ):
        if name not in _DATA_OPTIONS:
            raise ValueError("data must be one of %s" % _DATA_OPTIONS)
        super(Bsd100Config, self).__init__(name=name, version=datasets.Version("1.0.0"), **kwargs)
        self.hr_url = hr_url
        self.lr_url = lr_url


class Bsd100(datasets.GeneratorBasedBuilder):
    """BSD100 dataset for single image super resolution evaluation."""

    BUILDER_CONFIGS = [
        Bsd100Config(
            name=key,
            hr_url=values['hr'],
            lr_url=values['lr']
        ) for key, values in _DATA_OPTIONS.items()
    ]

    DEFAULT_CONFIG_NAME = _DEFAULT_CONFIG

    def _info(self):
        features = datasets.Features(
            {
                "hr": datasets.Value("string"),
                "lr": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        hr_data_dir = dl_manager.download_and_extract(self.config.hr_url)
        lr_data_dir = dl_manager.download_and_extract(self.config.lr_url)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "lr_path": lr_data_dir,
                    "hr_path": str(Path(hr_data_dir) / 'BSD100_HR')
                },
            )
        ]

    def _generate_examples(
        self, hr_path, lr_path
    ):
        """ Yields examples as (key, example) tuples. """
        # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is here for legacy reason (tfds) and is not important in itself.
        extensions = {'.png'}
        for file_path in sorted(Path(lr_path).glob("**/*")):
            if file_path.suffix in extensions:
                file_path_str = str(file_path.as_posix())
                yield file_path_str, {
                    'lr': file_path_str,
                    'hr': str((Path(hr_path) / file_path.name).as_posix())
                }