Rui6188 commited on
Commit
5d5ae2c
·
1 Parent(s): af481ca

add data loading script

Browse files
50States2K.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # TODO: Address all TODOs and remove all explanatory comments
15
+
16
+ import csv
17
+ import json
18
+ import os
19
+
20
+ import datasets
21
+ from PIL import Image
22
+
23
+ # Find for instance the citation on arxiv or on the dataset repo/website
24
+ _CITATION = """\
25
+ @InProceedings{huggingface:dataset,
26
+ title = {50States2K},
27
+ author={Rui
28
+ },
29
+ year={2023}
30
+ }
31
+ """
32
+
33
+ # You can copy an official description
34
+ _DESCRIPTION = """\
35
+ 50States2K dataset
36
+ """
37
+
38
+ _HOMEPAGE = "https://huggingface.co/datasets/Rui6188/50States2K"
39
+
40
+ _LICENSE = ""
41
+
42
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
43
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
44
+ _URLS = {
45
+ "Alabama": "Alabama",
46
+ "Alaska": "Alaska",
47
+ "Arizona": "Arizona",
48
+ "Arkansas": "Arkansas",
49
+ "California": "California",
50
+ "Colorado": "Colorado",
51
+ "Connecticut": "Connecticut",
52
+ "Delaware": "Delaware",
53
+ "Florida": "Florida",
54
+ "Georgia": "Georgia",
55
+ "Hawaii": "Hawaii",
56
+ "Idaho": "Idaho",
57
+ "Illinois": "Illinois",
58
+ "Indiana": "Indiana",
59
+ "Iowa": "Iowa",
60
+ "Kansas": "Kansas",
61
+ "Kentucky": "Kentucky",
62
+ "Louisiana": "Louisiana",
63
+ "Maine": "Maine",
64
+ "Maryland": "Maryland",
65
+ "Massachusetts": "Massachusetts",
66
+ "Michigan": "Michigan",
67
+ "Minnesota": "Minnesota",
68
+ "Mississippi": "Mississippi",
69
+ "Missouri": "Missouri",
70
+ "Montana": "Montana",
71
+ "Nebraska": "Nebraska",
72
+ "Nevada": "Nevada",
73
+ "New Hampshire": "New Hampshire",
74
+ "New Jersey": "New Jersey",
75
+ "New Mexico": "New Mexico",
76
+ "New York": "New York",
77
+ "North Carolina": "North Carolina",
78
+ "North Dakota": "North Dakota",
79
+ "Ohio": "Ohio",
80
+ "Oklahoma": "Oklahoma",
81
+ "Oregon": "Oregon",
82
+ "Pennsylvania": "Pennsylvania",
83
+ "Rhode Island": "Rhode Island",
84
+ "South Carolina": "South Carolina",
85
+ "South Dakota": "South Dakota",
86
+ "Tennessee": "Tennessee",
87
+ "Texas": "Texas",
88
+ "Utah": "Utah",
89
+ "Vermont": "Vermont",
90
+ "Virginia": "Virginia",
91
+ "Washington": "Washington",
92
+ "West Virginia": "West Virginia",
93
+ "Wisconsin": "Wisconsin",
94
+ "Wyoming": "Wyoming",
95
+ }
96
+
97
+
98
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
99
+ class NewDataset(datasets.GeneratorBasedBuilder):
100
+ """TODO: Short description of my dataset."""
101
+
102
+ VERSION = datasets.Version("1.1.0")
103
+
104
+ # This is an example of a dataset with multiple configurations.
105
+ # If you don't want/need to define several sub-sets in your dataset,
106
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
107
+
108
+ # If you need to make complex sub-parts in the datasets with configurable options
109
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
110
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
111
+
112
+ # You will be able to load one or the other configurations in the following list with
113
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
114
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
115
+ BUILDER_CONFIGS = [
116
+ datasets.BuilderConfig(name="Alabama", version=VERSION, description="images of Alamaba", data_dir="./Alabama"),
117
+ datasets.BuilderConfig(name="Alaska", version=VERSION, description="images of Alaska", data_dir="./Alaska"),
118
+ datasets.BuilderConfig(name="Arizona", version=VERSION, description="images of Arizona", data_dir="./Arizona"),
119
+ datasets.BuilderConfig(name="Arkansas", version=VERSION, description="images of Arkansas", data_dir="./Arkansas"),
120
+ datasets.BuilderConfig(name="California", version=VERSION, description="images of California", data_dir="./California"),
121
+ datasets.BuilderConfig(name="Colorado", version=VERSION, description="images of Colorado", data_dir="./Colorado"),
122
+ datasets.BuilderConfig(name="Connecticut", version=VERSION, description="images of Connecticut", data_dir="./Connecticut"),
123
+ datasets.BuilderConfig(name="Delaware", version=VERSION, description="images of Delaware", data_dir="./Delaware"),
124
+ datasets.BuilderConfig(name="Florida", version=VERSION, description="images of Florida", data_dir="./Florida"),
125
+ datasets.BuilderConfig(name="Georgia", version=VERSION, description="images of Georgia", data_dir="./Georgia"),
126
+ datasets.BuilderConfig(name="Hawaii", version=VERSION, description="images of Hawaii", data_dir="./Hawaii"),
127
+ datasets.BuilderConfig(name="Idaho", version=VERSION, description="images of Idaho", data_dir="./Idaho"),
128
+ datasets.BuilderConfig(name="Illinois", version=VERSION, description="images of Illinois", data_dir="./Illinois"),
129
+ datasets.BuilderConfig(name="Indiana", version=VERSION, description="images of Indiana", data_dir="./Indiana"),
130
+ datasets.BuilderConfig(name="Iowa", version=VERSION, description="images of Iowa", data_dir="./Iowa"),
131
+ datasets.BuilderConfig(name="Kansas", version=VERSION, description="images of Kansas", data_dir="./Kansas"),
132
+ datasets.BuilderConfig(name="Kentucky", version=VERSION, description="images of Kentucky", data_dir="./Kentucky"),
133
+ datasets.BuilderConfig(name="Louisiana", version=VERSION, description="images of Louisiana", data_dir="./Louisiana"),
134
+ datasets.BuilderConfig(name="Maine", version=VERSION, description="images of Maine", data_dir="./Maine"),
135
+ datasets.BuilderConfig(name="Maryland", version=VERSION, description="images of Maryland", data_dir="./Maryland"),
136
+ datasets.BuilderConfig(name="Massachusetts", version=VERSION, description="images of Massachusetts", data_dir="./Massachusetts"),
137
+ datasets.BuilderConfig(name="Michigan", version=VERSION, description="images of Michigan", data_dir="./Michigan"),
138
+ datasets.BuilderConfig(name="Minnesota", version=VERSION, description="images of Minnesota", data_dir="./Minnesota"),
139
+ datasets.BuilderConfig(name="Mississippi", version=VERSION, description="images of Mississippi", data_dir="./Mississippi"),
140
+ datasets.BuilderConfig(name="Missouri", version=VERSION, description="images of Missouri", data_dir="./Missouri"),
141
+ datasets.BuilderConfig(name="Montana", version=VERSION, description="images of Montana", data_dir="./Montana"),
142
+ datasets.BuilderConfig(name="Nebraska", version=VERSION, description="images of Nebraska", data_dir="./Nebraska"),
143
+ datasets.BuilderConfig(name="Nevada", version=VERSION, description="images of Nevada", data_dir="./Nevada"),
144
+ datasets.BuilderConfig(name="New Hampshire", version=VERSION, description="images of New Hampshire", data_dir="./New Hampshire"),
145
+ datasets.BuilderConfig(name="New Jersey", version=VERSION, description="images of New Jersey", data_dir="./New Jersey"),
146
+ datasets.BuilderConfig(name="New Mexico", version=VERSION, description="images of New Mexico", data_dir="./New Mexico"),
147
+ datasets.BuilderConfig(name="New York", version=VERSION, description="images of New York", data_dir="./New York"),
148
+ datasets.BuilderConfig(name="North Carolina", version=VERSION, description="images of North Carolina", data_dir="./North Carolina"),
149
+ datasets.BuilderConfig(name="North Dakota", version=VERSION, description="images of North Dakota", data_dir="./North Dakota"),
150
+ datasets.BuilderConfig(name="Ohio", version=VERSION, description="images of Ohio", data_dir="./Ohio"),
151
+ datasets.BuilderConfig(name="Oklahoma", version=VERSION, description="images of Oklahoma", data_dir="./Oklahoma"),
152
+ datasets.BuilderConfig(name="Oregon", version=VERSION, description="images of Oregon", data_dir="./Oregon"),
153
+ datasets.BuilderConfig(name="Pennsylvania", version=VERSION, description="images of Pennsylvania", data_dir="./Pennsylvania"),
154
+ datasets.BuilderConfig(name="Rhode Island", version=VERSION, description="images of Rhode Island", data_dir="./Rhode Island"),
155
+ datasets.BuilderConfig(name="South Carolina", version=VERSION, description="images of South Carolina", data_dir="./South Carolina"),
156
+ datasets.BuilderConfig(name="South Dakota", version=VERSION, description="images of South Dakota", data_dir="./South Dakota"),
157
+ datasets.BuilderConfig(name="Tennessee", version=VERSION, description="images of Tennessee", data_dir="./Tennessee"),
158
+ datasets.BuilderConfig(name="Texas", version=VERSION, description="images of Texas", data_dir="./Texas"),
159
+ datasets.BuilderConfig(name="Utah", version=VERSION, description="images of Utah", data_dir="./Utah"),
160
+ datasets.BuilderConfig(name="Vermont", version=VERSION, description="images of Vermont", data_dir="./Vermont"),
161
+ datasets.BuilderConfig(name="Virginia", version=VERSION, description="images of Virginia", data_dir="./Virginia"),
162
+ datasets.BuilderConfig(name="Washington", version=VERSION, description="images of Washington", data_dir="./Washington"),
163
+ datasets.BuilderConfig(name="West Virginia", version=VERSION, description="images of West Virginia", data_dir="./West Virginia"),
164
+ datasets.BuilderConfig(name="Wisconsin", version=VERSION, description="images of Wisconsin", data_dir="./Wisconsin"),
165
+ datasets.BuilderConfig(name="Wyoming", version=VERSION, description="images of Wyoming", data_dir="./Wyoming"),
166
+ ]
167
+
168
+
169
+ DEFAULT_CONFIG_NAME = "Alabama" # It's not mandatory to have a default configuration. Just use one if it make sense.
170
+
171
+ def _info(self):
172
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
173
+ # if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
174
+ # features = datasets.Features(
175
+ # {
176
+ # "sentence": datasets.Value("string"),
177
+ # "option1": datasets.Value("string"),
178
+ # "answer": datasets.Value("string")
179
+ # # These are the features of your dataset like images, labels ...
180
+ # }
181
+ # )
182
+ # else: # This is an example to show how to have different features for "first_domain" and "second_domain"
183
+ # features = datasets.Features(
184
+ # {
185
+ # "sentence": datasets.Value("string"),
186
+ # "option2": datasets.Value("string"),
187
+ # "second_domain_answer": datasets.Value("string")
188
+ # # These are the features of your dataset like images, labels ...
189
+ # }
190
+ # )
191
+ features = datasets.Features(
192
+ {
193
+ "image0": datasets.Image(),
194
+ "image1": datasets.Image(),
195
+ "image2": datasets.Image(),
196
+ "image3": datasets.Image(),
197
+ "label": datasets.Value(dtype='string', id=None),
198
+ }
199
+ )
200
+ return datasets.DatasetInfo(
201
+ # This is the description that will appear on the datasets page.
202
+ description=_DESCRIPTION,
203
+ # This defines the different columns of the dataset and their types
204
+ features=features, # Here we define them above because they are different between the two configurations
205
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
206
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
207
+ # supervised_keys=("sentence", "label"),
208
+ # Homepage of the dataset for documentation
209
+ homepage=_HOMEPAGE,
210
+ # License for the dataset if available
211
+ license=_LICENSE,
212
+ # Citation for the dataset
213
+ citation=_CITATION,
214
+ )
215
+
216
+ def _split_generators(self, dl_manager):
217
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
218
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
219
+
220
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
221
+ # 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.
222
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
223
+ urls = _URLS[self.config.name]
224
+
225
+ data_dir = dl_manager.download(urls)
226
+ print(f"\ndata_dir: {data_dir}")
227
+ return [
228
+ datasets.SplitGenerator(
229
+ name=datasets.Split.TEST,
230
+ # These kwargs will be passed to _generate_examples
231
+ gen_kwargs={
232
+ "filepath": os.path.join(data_dir),
233
+ "split": "test",
234
+ },
235
+ ),
236
+ ]
237
+
238
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
239
+ def _generate_examples(self, filepath, split):
240
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
241
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
242
+ # print(f"filepath: {filepath}")
243
+ file_list = os.listdir(filepath)
244
+ file_list = list(map(lambda x: os.path.join(filepath, x), filter(lambda x: x.endswith('.jpg'), file_list)))
245
+ # print(f"len(file_list): {len(file_list)}")
246
+
247
+ assert len(file_list) % 4 == 0, "image number cannot divided by 4"
248
+ for i in range(0, len(file_list), 4):
249
+ batch_files = file_list[i:i+4]
250
+ yield i, {
251
+ "image0": {"path": os.path.join(filepath, batch_files[0])},
252
+ "image1": {"path": os.path.join(filepath, batch_files[1])},
253
+ "image2": {"path": os.path.join(filepath, batch_files[2])},
254
+ "image3": {"path": os.path.join(filepath, batch_files[3])},
255
+ "label": filepath.split('/')[-1],
256
+ }
Colorado/info.txt DELETED
The diff for this file is too large to render. See raw diff
 
Connecticut/2015_WZt1WCNs7uwpG-UUmeqA7Q_180.jpg DELETED

Git LFS Details

  • SHA256: 9385533f59dc8975b7ac58e1dabb554d5ceb947147955fa27ae5f5e325368ee9
  • Pointer size: 130 Bytes
  • Size of remote file: 11.1 kB
Connecticut/2015_WZt1WCNs7uwpG-UUmeqA7Q_270.jpg DELETED

Git LFS Details

  • SHA256: be57d27be3e29dbbf989626b243ee7f15afa9a324997662c534661624cd80a08
  • Pointer size: 130 Bytes
  • Size of remote file: 10.7 kB
Indiana/2016_wnWQakoqxd5cdBIpv_F-ow_180.jpg DELETED

Git LFS Details

  • SHA256: 984a2c733c6e2e5bf44e0744ddd14e70b5eed8f406992d4e1bd16de28c821ecc
  • Pointer size: 130 Bytes
  • Size of remote file: 14.8 kB
Indiana/2016_wnWQakoqxd5cdBIpv_F-ow_270.jpg DELETED

Git LFS Details

  • SHA256: f88949497f0a6f54423634e24ac7b059f03a0791617ff78e57e1294fadccf66e
  • Pointer size: 130 Bytes
  • Size of remote file: 11.6 kB
Indiana/2016_wnWQakoqxd5cdBIpv_F-ow_90.jpg DELETED

Git LFS Details

  • SHA256: 98562d257a2daade180ef8c86356fbead9815b7363fc7dbe001c23b5fa1171e8
  • Pointer size: 130 Bytes
  • Size of remote file: 11.8 kB
Kansas/info.txt DELETED
The diff for this file is too large to render. See raw diff
 
Kentucky/2016_rfyL_2mJyNWJgso36AKxyQ_180.jpg DELETED

Git LFS Details

  • SHA256: 801cb0814f8e9523e05c040c1691014ae38e746f17fe5d3e001ff591c8b78916
  • Pointer size: 129 Bytes
  • Size of remote file: 9.11 kB
Kentucky/2016_rfyL_2mJyNWJgso36AKxyQ_270.jpg DELETED

Git LFS Details

  • SHA256: aa4bc5388c78673dfa0f3d6d35b399bc60e09441e1fc05f4ef677f33c671f19e
  • Pointer size: 130 Bytes
  • Size of remote file: 10.9 kB
Kentucky/2016_rfyL_2mJyNWJgso36AKxyQ_90.jpg DELETED

Git LFS Details

  • SHA256: 182d07c9fdc4a06f75ea2f6477d60202821d29213de4d6823f614d64c7e8e011
  • Pointer size: 129 Bytes
  • Size of remote file: 8.88 kB
Montana/info.txt DELETED
The diff for this file is too large to render. See raw diff
 
Nevada/2016_y9loWswQLqVpc6srkRusPQ_180.jpg DELETED

Git LFS Details

  • SHA256: acdb7a9a02dc3cddc45ee6589f3384861b71e1cf1e52e8edd66ceba35f027dbc
  • Pointer size: 129 Bytes
  • Size of remote file: 7.71 kB
Nevada/2016_y9loWswQLqVpc6srkRusPQ_270.jpg DELETED

Git LFS Details

  • SHA256: 4519da90064c749264cb6caca8399ca4d5533773c8ae99dcdcd93167d925ca00
  • Pointer size: 129 Bytes
  • Size of remote file: 7.2 kB
Ohio/info.txt DELETED
The diff for this file is too large to render. See raw diff
 
Pennsylvania/2016_XXsDx9Av5DY8QfRLaklrXA_270.jpg DELETED

Git LFS Details

  • SHA256: 475f6052a0d01d16da2dfffc96f34ea87c73cf34ed82176ca9437e0b8b6d2123
  • Pointer size: 130 Bytes
  • Size of remote file: 16.1 kB
Vermont/info.txt DELETED
The diff for this file is too large to render. See raw diff
 
Wisconsin/2016_qOof7SObFdsnL_O1mFfmmQ_180.jpg DELETED

Git LFS Details

  • SHA256: 98c0fb93f348c971b0c9f2d72128d9c7933f3ead4071725e32336231796843ed
  • Pointer size: 129 Bytes
  • Size of remote file: 8.9 kB
Wisconsin/2016_qOof7SObFdsnL_O1mFfmmQ_270.jpg DELETED

Git LFS Details

  • SHA256: b7083617936a4fdf274aad2e6fb2c2c2356d727048fa488698c6b35c29a9e34d
  • Pointer size: 129 Bytes
  • Size of remote file: 7.74 kB