zhuchi76 commited on
Commit
7c6e3fa
·
verified ·
1 Parent(s): 573a1e4

Upload script to hub

Browse files
Files changed (1) hide show
  1. Boat_dataset.py +174 -0
Boat_dataset.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
17
+ import csv
18
+ import json
19
+ import os
20
+
21
+ import datasets
22
+
23
+
24
+ # Add BibTeX citation
25
+ # Find for instance the citation on arxiv or on the dataset repo/website
26
+ _CITATION = """\
27
+ @InProceedings{huggingface:dataset,
28
+ title = {Boat dataset},
29
+ author={huggingface, Inc.
30
+ },
31
+ year={2024}
32
+ }
33
+ """
34
+
35
+ # Add description of the dataset here
36
+ # You can copy an official description
37
+ _DESCRIPTION = """\
38
+ This new dataset is designed to solve this great object detection task.
39
+ """
40
+
41
+ # Add a link to an official homepage for the dataset here
42
+ _HOMEPAGE = ""
43
+
44
+ # Add the licence for the dataset here if you can find it
45
+ _LICENSE = ""
46
+
47
+ # Add link to the official dataset URLs here
48
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
49
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
50
+ _URLS = {
51
+ "Boat_dataset": "ftp://arg.lab.nycu.edu.tw/arg-projectfile-download/detr/dataset/Boat_dataset.zip",
52
+ }
53
+
54
+
55
+
56
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
57
+ class BoatDataset(datasets.GeneratorBasedBuilder):
58
+ """TODO: Short description of my dataset."""
59
+
60
+ VERSION = datasets.Version("1.1.0")
61
+
62
+ # This is an example of a dataset with multiple configurations.
63
+ # If you don't want/need to define several sub-sets in your dataset,
64
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
65
+
66
+ # If you need to make complex sub-parts in the datasets with configurable options
67
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
68
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
69
+
70
+ # You will be able to load one or the other configurations in the following list with
71
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
72
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
73
+ BUILDER_CONFIGS = [
74
+ datasets.BuilderConfig(name="Boat_dataset", version=VERSION, description="Images of real and virtual boats."),
75
+ ]
76
+
77
+ DEFAULT_CONFIG_NAME = "Boat_dataset" # It's not mandatory to have a default configuration. Just use one if it make sense.
78
+
79
+ def _info(self):
80
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
81
+
82
+ features=datasets.Features({
83
+ 'image_id': datasets.Value('int32'),
84
+ # 'image': datasets.Image(), # This is commented out because you can't directly store PIL images in the dataset.
85
+ 'file_name': datasets.Value('string'), # Store the path to the image file instead.
86
+ 'width': datasets.Value('int32'),
87
+ 'height': datasets.Value('int32'),
88
+ 'objects': datasets.Sequence({
89
+ 'id': datasets.Value('int32'),
90
+ 'area': datasets.Value('float32'),
91
+ 'bbox': datasets.Sequence(datasets.Value('float32'), length=4), # [x, y, width, height]
92
+ 'category': datasets.Value('int32')
93
+ }),
94
+ })
95
+
96
+ return datasets.DatasetInfo(
97
+ # This is the description that will appear on the datasets page.
98
+ description=_DESCRIPTION,
99
+ # This defines the different columns of the dataset and their types
100
+ features=features, # Here we define them above because they are different between the two configurations
101
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
102
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
103
+ # supervised_keys=("sentence", "label"),
104
+ # Homepage of the dataset for documentation
105
+ homepage=_HOMEPAGE,
106
+ # License for the dataset if available
107
+ license=_LICENSE,
108
+ # Citation for the dataset
109
+ citation=_CITATION,
110
+ )
111
+
112
+ def _split_generators(self, dl_manager):
113
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
114
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
115
+
116
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
117
+ # 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.
118
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
119
+ urls = _URLS[self.config.name]
120
+ data_dir = dl_manager.download_and_extract(urls)
121
+ return [
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TRAIN,
124
+ # These kwargs will be passed to _generate_examples
125
+ gen_kwargs={
126
+ "filepath": os.path.join(data_dir, "instances_train2023.jsonl"),
127
+ "split": "train",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.VALIDATION,
132
+ # These kwargs will be passed to _generate_examples
133
+ gen_kwargs={
134
+ "filepath": os.path.join(data_dir, "instances_val2023.jsonl"),
135
+ "split": "val",
136
+ },
137
+ ),
138
+ datasets.SplitGenerator(
139
+ name=datasets.Split.VALIDATION,
140
+ # These kwargs will be passed to _generate_examples
141
+ gen_kwargs={
142
+ "filepath": os.path.join(data_dir, "instances_val2023r.jsonl"),
143
+ "split": "val_real",
144
+ },
145
+ ),
146
+ # datasets.SplitGenerator(
147
+ # name=datasets.Split.TEST,
148
+ # # These kwargs will be passed to _generate_examples
149
+ # gen_kwargs={
150
+ # "filepath": os.path.join(data_dir, "test.jsonl"),
151
+ # "split": "test"
152
+ # },
153
+ # ),
154
+ ]
155
+
156
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
157
+ def _generate_examples(self, filepath, split):
158
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
159
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
160
+ with open(filepath, encoding="utf-8") as f:
161
+ for key, row in enumerate(f):
162
+ data = json.loads(row)
163
+ yield key, {
164
+ "image_id": data["image_id"],
165
+ "file_name": data["file_name"],
166
+ "width": data["width"],
167
+ "height": data["height"],
168
+ "objects": {
169
+ "id": data["objects"]["id"],
170
+ "area": data["objects"]["area"],
171
+ "bbox": data["objects"]["bbox"],
172
+ "category": data["objects"]["category"],
173
+ },
174
+ }