File size: 7,422 Bytes
b065fc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89c644a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b065fc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89c644a
 
b065fc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89c644a
 
b065fc4
89c644a
b065fc4
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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.
"""Dataset for dating historical color images"""

from pathlib import Path
import datasets
import csv
import os

_CITATION = """@inproceedings{10.1007/978-3-642-33783-3_36,
author = {Palermo, Frank and Hays, James and Efros, Alexei A.},
title = {Dating Historical Color Images},
year = {2012},
isbn = {9783642337826},
publisher = {Springer-Verlag},
address = {Berlin, Heidelberg},
url = {https://doi.org/10.1007/978-3-642-33783-3_36},
doi = {10.1007/978-3-642-33783-3_36},
abstract = {We introduce the task of automatically estimating the age of historical color photographs. We suggest features which attempt to capture temporally discriminative information based on the evolution of color imaging processes over time and evaluate the performance of both these novel features and existing features commonly utilized in other problem domains on a novel historical image data set. For the challenging classification task of sorting historical color images into the decade during which they were photographed, we demonstrate significantly greater accuracy than that shown by untrained humans on the same data set. Additionally, we apply the concept of data-driven camera response function estimation to historical color imagery, demonstrating its relevance to both the age estimation task and the popular application of imitating the appearance of vintage color photography.},
booktitle = {Proceedings of the 12th European Conference on Computer Vision - Volume Part VI},
pages = {499–512},
numpages = {14},
location = {Florence, Italy},
series = {ECCV'12}
}
"""


_DESCRIPTION = """\
This dataset contains color photographs taken between the 1930s and 1970s. 
The goal of the dataset is to develop methods for dating historical color photographs
"""

_HOMEPAGE = "http://graphics.cs.cmu.edu/projects/historicalColor/"

_URLS = {
    "metadata": "https://storage.hpai.bsc.es/mame-dataset/MAMe_metadata.zip",
    "images": "https://storage.hpai.bsc.es/mame-dataset/MAMe_data_256.zip",
    "full_images": "https://storage.hpai.bsc.es/mame-dataset/MAMe_data.zip",
}


def generate_mapping_dict(csv_file):
    with open(csv_file) as f:
        mapping_dict = {}
        dictreater = csv.DictReader(f)
        for row in dictreater:
            split = row["Subset"]
            image_file = row["Image file"]
            del row["Image file"]
            del row["Subset"]
            # add row to subset dictionary
            row_dict = {image_file: row}
            if split not in mapping_dict:
                mapping_dict[split] = row_dict
            else:
                mapping_dict[split].update(row_dict)
    return mapping_dict


class MAMeConfig(datasets.BuilderConfig):
    """TODO"""

    def __init__(self, image_data_url, **kwargs):
        """TODO"""
        super().__init__(version=datasets.Version("1.0.2"), **kwargs)
        self.image_data_url = image_data_url


class MAMe(datasets.GeneratorBasedBuilder):
    """TODO"""

    VERSION = datasets.Version("1.1.0")

    BUILDER_CONFIGS = [
        MAMeConfig(name="256", image_data_url=_URLS["images"]),
        MAMeConfig(name="full", image_data_url=_URLS["full_images"]),
    ]

    DEFAULT_CONFIG_NAME = "256"

    def _info(self):
        features = datasets.Features(
            {
                "image": datasets.Image(),
                "label": datasets.ClassLabel(
                    names=[
                        "Albumen photograph",
                        "Bronze",
                        "Ceramic",
                        "Clay",
                        "Engraving",
                        "Etching",
                        "Faience",
                        "Glass",
                        "Gold",
                        "Graphite",
                        "Hand-colored engraving",
                        "Hand-colored etching",
                        "Iron",
                        "Ivory",
                        "Limestone",
                        "Lithograph",
                        "Marble",
                        "Oil on canvas",
                        "Pen and brown ink",
                        "Polychromed wood",
                        "Porcelain",
                        "Silk and metal thread",
                        "Silver",
                        "Steel",
                        "Wood",
                        "Wood engraving",
                        "Woodblock",
                        "Woodcut",
                        "Woven fabric",
                    ]
                ),
                "Museum": datasets.Value("string"),
                "Museum-based instance ID": datasets.Value("string"),
                "Width": datasets.Value("float32"),
                "Height": datasets.Value("float32"),
                "Product size": datasets.Value("float32"),
                "Aspect ratio": datasets.Value("float32"),
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        metadata = dl_manager.download_and_extract(_URLS["metadata"])
        metadata = os.path.join(metadata, "MAMe_dataset.csv")
        images = dl_manager.download_and_extract(self.config.image_data_url)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "metadata": metadata,
                    "images": images,
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "metadata": metadata,
                    "images": images,
                    "split": "val",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "metadata": metadata,
                    "images": images,
                    "split": "test",
                },
            ),
        ]

    def _generate_examples(self, metadata, images, split):
        if self.config.name == 'full':
            from PIL import Image # to prevent decompression bomb warnings
            Image.MAX_IMAGE_PIXELS = None
        mapping = generate_mapping_dict(metadata)
        subset = mapping[split]
        for i, kv in enumerate(subset.items()):
            k, v = kv
            if self.config.name == "256":
                im = f"{images}/data_256/{k}"
            if self.config.name == "full":
                im = f"{images}/data/{k}"
            v["label"] = v["Medium"]
            del v["Medium"]
            v["image"] = im
            yield i, v