Create DocTamper.py
Browse files- DocTamper.py +169 -0
DocTamper.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
"""
|
16 |
+
DocTamper dataset
|
17 |
+
"""
|
18 |
+
|
19 |
+
|
20 |
+
import csv
|
21 |
+
import json
|
22 |
+
import os
|
23 |
+
import lmdb
|
24 |
+
import io
|
25 |
+
from PIL import Image
|
26 |
+
|
27 |
+
import datasets
|
28 |
+
|
29 |
+
from datasets.tasks import ImageClassification
|
30 |
+
|
31 |
+
|
32 |
+
# TODO: Add BibTeX citation
|
33 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
34 |
+
_CITATION = """\
|
35 |
+
@InProceedings{huggingface:dataset,
|
36 |
+
title = {A great new dataset},
|
37 |
+
author={huggingface, Inc.
|
38 |
+
},
|
39 |
+
year={2020}
|
40 |
+
}
|
41 |
+
"""
|
42 |
+
|
43 |
+
# TODO: Add description of the dataset here
|
44 |
+
# You can copy an official description
|
45 |
+
_DESCRIPTION = """\
|
46 |
+
This dataset was designed to train models to determine image tampering on documents.
|
47 |
+
"""
|
48 |
+
|
49 |
+
# TODO: Add a link to an official homepage for the dataset here
|
50 |
+
_HOMEPAGE = "https://github.com/qcf-568/DocTamper"
|
51 |
+
|
52 |
+
# TODO: Add the licence for the dataset here if you can find it
|
53 |
+
_LICENSE = ""
|
54 |
+
|
55 |
+
# TODO: Add link to the official dataset URLs here
|
56 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
57 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
58 |
+
_URL = "https://storage.googleapis.com/document-image-tampering-dataset/DocTamperV1.zip"
|
59 |
+
|
60 |
+
|
61 |
+
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
62 |
+
class DocTamper(datasets.GeneratorBasedBuilder):
|
63 |
+
"""TODO: Short description of my dataset."""
|
64 |
+
|
65 |
+
VERSION = datasets.Version("1.1.0")
|
66 |
+
|
67 |
+
# This is an example of a dataset with multiple configurations.
|
68 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
69 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
70 |
+
|
71 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
72 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
73 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
74 |
+
|
75 |
+
# You will be able to load one or the other configurations in the following list with
|
76 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
77 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
78 |
+
# BUILDER_CONFIGS = [
|
79 |
+
# datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
|
80 |
+
# # datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
|
81 |
+
# ]
|
82 |
+
|
83 |
+
# DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
84 |
+
|
85 |
+
def _info(self):
|
86 |
+
return datasets.DatasetInfo(
|
87 |
+
description=_DESCRIPTION,
|
88 |
+
features=datasets.Features(
|
89 |
+
{
|
90 |
+
"image": datasets.Image(),
|
91 |
+
"label": datasets.Image(),
|
92 |
+
}
|
93 |
+
),
|
94 |
+
homepage=_HOMEPAGE,
|
95 |
+
citation=_CITATION,
|
96 |
+
license=_LICENSE
|
97 |
+
)
|
98 |
+
|
99 |
+
|
100 |
+
def _split_generators(self, dl_manager):
|
101 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
102 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
103 |
+
|
104 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
105 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
106 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
107 |
+
urls = _URL
|
108 |
+
data_dir = dl_manager.download_and_extract(urls)
|
109 |
+
return [
|
110 |
+
datasets.SplitGenerator(
|
111 |
+
name=datasets.Split.TRAIN,
|
112 |
+
# These kwargs will be passed to _generate_examples
|
113 |
+
gen_kwargs={
|
114 |
+
"filepath": os.path.join(data_dir, "DocTamperV1/DocTamperV1-TrainingSet"),
|
115 |
+
"split": "train",
|
116 |
+
},
|
117 |
+
),
|
118 |
+
datasets.SplitGenerator(
|
119 |
+
name=datasets.Split.VALIDATION,
|
120 |
+
# These kwargs will be passed to _generate_examples
|
121 |
+
gen_kwargs={
|
122 |
+
"filepath": os.path.join(data_dir, "DocTamperV1/DocTamperV1-FCD"),
|
123 |
+
"split": "dev",
|
124 |
+
},
|
125 |
+
),
|
126 |
+
datasets.SplitGenerator(
|
127 |
+
name=datasets.Split.TEST,
|
128 |
+
# These kwargs will be passed to _generate_examples
|
129 |
+
gen_kwargs={
|
130 |
+
"filepath": os.path.join(data_dir, "DocTamperV1/DocTamperV1-TestingSet"),
|
131 |
+
"split": "test"
|
132 |
+
},
|
133 |
+
),
|
134 |
+
]
|
135 |
+
|
136 |
+
|
137 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
138 |
+
def _generate_examples(self, filepath, split):
|
139 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
140 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
141 |
+
lmdb_path = filepath
|
142 |
+
|
143 |
+
env = lmdb.open(lmdb_path, readonly=True)
|
144 |
+
txn = env.begin()
|
145 |
+
|
146 |
+
idx = 0
|
147 |
+
while True:
|
148 |
+
image_key = f"image-{idx:09}"
|
149 |
+
label_key = f"label-{idx:09}"
|
150 |
+
|
151 |
+
image_data = txn.get(image_key.encode())
|
152 |
+
label_data = txn.get(label_key.encode())
|
153 |
+
|
154 |
+
if not image_data or not label_data:
|
155 |
+
break
|
156 |
+
|
157 |
+
|
158 |
+
# Yields examples as (key, example) tuples
|
159 |
+
yield idx, {
|
160 |
+
"image": Image.open(io.BytesIO(image_data)),
|
161 |
+
"label": Image.open(io.BytesIO(label_data)),
|
162 |
+
}
|
163 |
+
|
164 |
+
idx += 1
|
165 |
+
|
166 |
+
env.close()
|
167 |
+
|
168 |
+
|
169 |
+
# datasets-cli shared/vision-transformers/doctamper_loading_script.py --save_info --all_configs
|