SaulLu commited on
Commit
f8377d4
·
1 Parent(s): 702b2d6

add loading script

Browse files
Files changed (1) hide show
  1. Standford-Cars.py +312 -0
Standford-Cars.py ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Datasets Authors.
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
+ """Stanford cars dataset loading script."""
15
+
16
+
17
+ import csv
18
+ import json
19
+ import os
20
+ from pathlib import Path
21
+ import datasets
22
+ import scipy.io as sio
23
+
24
+ _CITATION = """\
25
+ @inproceedings{KrauseStarkDengFei-Fei_3DRR2013,
26
+ title = {3D Object Representations for Fine-Grained Categorization},
27
+ booktitle = {4th International IEEE Workshop on 3D Representation and Recognition (3dRR-13)},
28
+ year = {2013},
29
+ address = {Sydney, Australia},
30
+ author = {Jonathan Krause and Michael Stark and Jia Deng and Li Fei-Fei}
31
+ }"""
32
+
33
+ _DESCRIPTION = (
34
+ "The Cars dataset contains 16,185 images of 196 classes of cars. The data is split into 8,144 "
35
+ "training images and 8,041 testing images, where each class has been split roughly in a 50-50 "
36
+ "split. Classes are typically at the level of Make, Model, Year, e.g. 2012 Tesla Model S or "
37
+ "2012 BMW M3 coupe."
38
+ )
39
+
40
+ _HOMEPAGE = "http://ai.stanford.edu/~jkrause/cars/car_dataset.html"
41
+
42
+ _LICENSE = ""
43
+
44
+ _URLS = {
45
+ "car_devkit": "https://ai.stanford.edu/~jkrause/cars/car_devkit.tgz",
46
+ "cars_train": "https://ai.stanford.edu/~jkrause/car196/cars_train.tgz",
47
+ "cars_test": "https://ai.stanford.edu/~jkrause/car196/cars_test.tgz",
48
+ "cars_test_annos_withlabels": "https://ai.stanford.edu/~jkrause/car196/cars_test_annos_withlabels.mat",
49
+ }
50
+
51
+
52
+ _NAMES = [
53
+ 'AM General Hummer SUV 2000',
54
+ 'Acura RL Sedan 2012',
55
+ 'Acura TL Sedan 2012',
56
+ 'Acura TL Type-S 2008',
57
+ 'Acura TSX Sedan 2012',
58
+ 'Acura Integra Type R 2001',
59
+ 'Acura ZDX Hatchback 2012',
60
+ 'Aston Martin V8 Vantage Convertible 2012',
61
+ 'Aston Martin V8 Vantage Coupe 2012',
62
+ 'Aston Martin Virage Convertible 2012',
63
+ 'Aston Martin Virage Coupe 2012',
64
+ 'Audi RS 4 Convertible 2008',
65
+ 'Audi A5 Coupe 2012',
66
+ 'Audi TTS Coupe 2012',
67
+ 'Audi R8 Coupe 2012',
68
+ 'Audi V8 Sedan 1994',
69
+ 'Audi 100 Sedan 1994',
70
+ 'Audi 100 Wagon 1994',
71
+ 'Audi TT Hatchback 2011',
72
+ 'Audi S6 Sedan 2011',
73
+ 'Audi S5 Convertible 2012',
74
+ 'Audi S5 Coupe 2012',
75
+ 'Audi S4 Sedan 2012',
76
+ 'Audi S4 Sedan 2007',
77
+ 'Audi TT RS Coupe 2012',
78
+ 'BMW ActiveHybrid 5 Sedan 2012',
79
+ 'BMW 1 Series Convertible 2012',
80
+ 'BMW 1 Series Coupe 2012',
81
+ 'BMW 3 Series Sedan 2012',
82
+ 'BMW 3 Series Wagon 2012',
83
+ 'BMW 6 Series Convertible 2007',
84
+ 'BMW X5 SUV 2007',
85
+ 'BMW X6 SUV 2012',
86
+ 'BMW M3 Coupe 2012',
87
+ 'BMW M5 Sedan 2010',
88
+ 'BMW M6 Convertible 2010',
89
+ 'BMW X3 SUV 2012',
90
+ 'BMW Z4 Convertible 2012',
91
+ 'Bentley Continental Supersports Conv. Convertible 2012',
92
+ 'Bentley Arnage Sedan 2009',
93
+ 'Bentley Mulsanne Sedan 2011',
94
+ 'Bentley Continental GT Coupe 2012',
95
+ 'Bentley Continental GT Coupe 2007',
96
+ 'Bentley Continental Flying Spur Sedan 2007',
97
+ 'Bugatti Veyron 16.4 Convertible 2009',
98
+ 'Bugatti Veyron 16.4 Coupe 2009',
99
+ 'Buick Regal GS 2012',
100
+ 'Buick Rainier SUV 2007',
101
+ 'Buick Verano Sedan 2012',
102
+ 'Buick Enclave SUV 2012',
103
+ 'Cadillac CTS-V Sedan 2012',
104
+ 'Cadillac SRX SUV 2012',
105
+ 'Cadillac Escalade EXT Crew Cab 2007',
106
+ 'Chevrolet Silverado 1500 Hybrid Crew Cab 2012',
107
+ 'Chevrolet Corvette Convertible 2012',
108
+ 'Chevrolet Corvette ZR1 2012',
109
+ 'Chevrolet Corvette Ron Fellows Edition Z06 2007',
110
+ 'Chevrolet Traverse SUV 2012',
111
+ 'Chevrolet Camaro Convertible 2012',
112
+ 'Chevrolet HHR SS 2010',
113
+ 'Chevrolet Impala Sedan 2007',
114
+ 'Chevrolet Tahoe Hybrid SUV 2012',
115
+ 'Chevrolet Sonic Sedan 2012',
116
+ 'Chevrolet Express Cargo Van 2007',
117
+ 'Chevrolet Avalanche Crew Cab 2012',
118
+ 'Chevrolet Cobalt SS 2010',
119
+ 'Chevrolet Malibu Hybrid Sedan 2010',
120
+ 'Chevrolet TrailBlazer SS 2009',
121
+ 'Chevrolet Silverado 2500HD Regular Cab 2012',
122
+ 'Chevrolet Silverado 1500 Classic Extended Cab 2007',
123
+ 'Chevrolet Express Van 2007',
124
+ 'Chevrolet Monte Carlo Coupe 2007',
125
+ 'Chevrolet Malibu Sedan 2007',
126
+ 'Chevrolet Silverado 1500 Extended Cab 2012',
127
+ 'Chevrolet Silverado 1500 Regular Cab 2012',
128
+ 'Chrysler Aspen SUV 2009',
129
+ 'Chrysler Sebring Convertible 2010',
130
+ 'Chrysler Town and Country Minivan 2012',
131
+ 'Chrysler 300 SRT-8 2010',
132
+ 'Chrysler Crossfire Convertible 2008',
133
+ 'Chrysler PT Cruiser Convertible 2008',
134
+ 'Daewoo Nubira Wagon 2002',
135
+ 'Dodge Caliber Wagon 2012',
136
+ 'Dodge Caliber Wagon 2007',
137
+ 'Dodge Caravan Minivan 1997',
138
+ 'Dodge Ram Pickup 3500 Crew Cab 2010',
139
+ 'Dodge Ram Pickup 3500 Quad Cab 2009',
140
+ 'Dodge Sprinter Cargo Van 2009',
141
+ 'Dodge Journey SUV 2012',
142
+ 'Dodge Dakota Crew Cab 2010',
143
+ 'Dodge Dakota Club Cab 2007',
144
+ 'Dodge Magnum Wagon 2008',
145
+ 'Dodge Challenger SRT8 2011',
146
+ 'Dodge Durango SUV 2012',
147
+ 'Dodge Durango SUV 2007',
148
+ 'Dodge Charger Sedan 2012',
149
+ 'Dodge Charger SRT-8 2009',
150
+ 'Eagle Talon Hatchback 1998',
151
+ 'FIAT 500 Abarth 2012',
152
+ 'FIAT 500 Convertible 2012',
153
+ 'Ferrari FF Coupe 2012',
154
+ 'Ferrari California Convertible 2012',
155
+ 'Ferrari 458 Italia Convertible 2012',
156
+ 'Ferrari 458 Italia Coupe 2012',
157
+ 'Fisker Karma Sedan 2012',
158
+ 'Ford F-450 Super Duty Crew Cab 2012',
159
+ 'Ford Mustang Convertible 2007',
160
+ 'Ford Freestar Minivan 2007',
161
+ 'Ford Expedition EL SUV 2009',
162
+ 'Ford Edge SUV 2012',
163
+ 'Ford Ranger SuperCab 2011',
164
+ 'Ford GT Coupe 2006',
165
+ 'Ford F-150 Regular Cab 2012',
166
+ 'Ford F-150 Regular Cab 2007',
167
+ 'Ford Focus Sedan 2007',
168
+ 'Ford E-Series Wagon Van 2012',
169
+ 'Ford Fiesta Sedan 2012',
170
+ 'GMC Terrain SUV 2012',
171
+ 'GMC Savana Van 2012',
172
+ 'GMC Yukon Hybrid SUV 2012',
173
+ 'GMC Acadia SUV 2012',
174
+ 'GMC Canyon Extended Cab 2012',
175
+ 'Geo Metro Convertible 1993',
176
+ 'HUMMER H3T Crew Cab 2010',
177
+ 'HUMMER H2 SUT Crew Cab 2009',
178
+ 'Honda Odyssey Minivan 2012',
179
+ 'Honda Odyssey Minivan 2007',
180
+ 'Honda Accord Coupe 2012',
181
+ 'Honda Accord Sedan 2012',
182
+ 'Hyundai Veloster Hatchback 2012',
183
+ 'Hyundai Santa Fe SUV 2012',
184
+ 'Hyundai Tucson SUV 2012',
185
+ 'Hyundai Veracruz SUV 2012',
186
+ 'Hyundai Sonata Hybrid Sedan 2012',
187
+ 'Hyundai Elantra Sedan 2007',
188
+ 'Hyundai Accent Sedan 2012',
189
+ 'Hyundai Genesis Sedan 2012',
190
+ 'Hyundai Sonata Sedan 2012',
191
+ 'Hyundai Elantra Touring Hatchback 2012',
192
+ 'Hyundai Azera Sedan 2012',
193
+ 'Infiniti G Coupe IPL 2012',
194
+ 'Infiniti QX56 SUV 2011',
195
+ 'Isuzu Ascender SUV 2008',
196
+ 'Jaguar XK XKR 2012',
197
+ 'Jeep Patriot SUV 2012',
198
+ 'Jeep Wrangler SUV 2012',
199
+ 'Jeep Liberty SUV 2012',
200
+ 'Jeep Grand Cherokee SUV 2012',
201
+ 'Jeep Compass SUV 2012',
202
+ 'Lamborghini Reventon Coupe 2008',
203
+ 'Lamborghini Aventador Coupe 2012',
204
+ 'Lamborghini Gallardo LP 570-4 Superleggera 2012',
205
+ 'Lamborghini Diablo Coupe 2001',
206
+ 'Land Rover Range Rover SUV 2012',
207
+ 'Land Rover LR2 SUV 2012',
208
+ 'Lincoln Town Car Sedan 2011',
209
+ 'MINI Cooper Roadster Convertible 2012',
210
+ 'Maybach Landaulet Convertible 2012',
211
+ 'Mazda Tribute SUV 2011',
212
+ 'McLaren MP4-12C Coupe 2012',
213
+ 'Mercedes-Benz 300-Class Convertible 1993',
214
+ 'Mercedes-Benz C-Class Sedan 2012',
215
+ 'Mercedes-Benz SL-Class Coupe 2009',
216
+ 'Mercedes-Benz E-Class Sedan 2012',
217
+ 'Mercedes-Benz S-Class Sedan 2012',
218
+ 'Mercedes-Benz Sprinter Van 2012',
219
+ 'Mitsubishi Lancer Sedan 2012',
220
+ 'Nissan Leaf Hatchback 2012',
221
+ 'Nissan NV Passenger Van 2012',
222
+ 'Nissan Juke Hatchback 2012',
223
+ 'Nissan 240SX Coupe 1998',
224
+ 'Plymouth Neon Coupe 1999',
225
+ 'Porsche Panamera Sedan 2012',
226
+ 'Ram C/V Cargo Van Minivan 2012',
227
+ 'Rolls-Royce Phantom Drophead Coupe Convertible 2012',
228
+ 'Rolls-Royce Ghost Sedan 2012',
229
+ 'Rolls-Royce Phantom Sedan 2012',
230
+ 'Scion xD Hatchback 2012',
231
+ 'Spyker C8 Convertible 2009',
232
+ 'Spyker C8 Coupe 2009',
233
+ 'Suzuki Aerio Sedan 2007',
234
+ 'Suzuki Kizashi Sedan 2012',
235
+ 'Suzuki SX4 Hatchback 2012',
236
+ 'Suzuki SX4 Sedan 2012',
237
+ 'Tesla Model S Sedan 2012',
238
+ 'Toyota Sequoia SUV 2012',
239
+ 'Toyota Camry Sedan 2012',
240
+ 'Toyota Corolla Sedan 2012',
241
+ 'Toyota 4Runner SUV 2012',
242
+ 'Volkswagen Golf Hatchback 2012',
243
+ 'Volkswagen Golf Hatchback 1991',
244
+ 'Volkswagen Beetle Hatchback 2012',
245
+ 'Volvo C30 Hatchback 2012',
246
+ 'Volvo 240 Sedan 1993',
247
+ 'Volvo XC90 SUV 2007',
248
+ 'smart fortwo Convertible 2012',
249
+ ]
250
+
251
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
252
+ class NewDataset(datasets.GeneratorBasedBuilder):
253
+ """TODO: Short description of my dataset."""
254
+
255
+ VERSION = datasets.Version("1.0.0")
256
+
257
+ def _info(self):
258
+ features = datasets.Features(
259
+ {
260
+ "image": datasets.Image(),
261
+ "label": datasets.features.ClassLabel(names=_NAMES),
262
+ 'bbox': datasets.features.Array2D(
263
+ shape=(1, 4), dtype="int64"
264
+ ),
265
+ }
266
+ )
267
+ return datasets.DatasetInfo(
268
+ description=_DESCRIPTION,
269
+ features=features,
270
+ homepage=_HOMEPAGE,
271
+ license=_LICENSE,
272
+ citation=_CITATION,
273
+ )
274
+
275
+ def _split_generators(self, dl_manager):
276
+ data_dir = dl_manager.download_and_extract(_URLS)
277
+
278
+ return [
279
+ datasets.SplitGenerator(
280
+ name=datasets.Split.TRAIN,
281
+ gen_kwargs={
282
+ "annotations_mat_path": Path(data_dir["car_devkit"]) / "devkit" / "cars_train_annos.mat" ,
283
+ "images_base_path": Path(data_dir["cars_train"]) / "cars_train" ,
284
+ "split": "train",
285
+ },
286
+ ),
287
+ datasets.SplitGenerator(
288
+ name=datasets.Split.TEST,
289
+ gen_kwargs={
290
+ "annotations_mat_path": Path(data_dir["cars_test_annos_withlabels"]) ,
291
+ "images_base_path": Path(data_dir["cars_test"]) / "cars_test" ,
292
+ "split": "test",
293
+ },
294
+ ),
295
+ ]
296
+
297
+ def _generate_examples(self, annotations_mat_path, images_base_path, split):
298
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
299
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
300
+ for annotation in sio.loadmat(annotations_mat_path)["annotations"][0]:
301
+ image_name = str(annotation[5].item())
302
+ image_path = images_base_path / image_name
303
+ label = _NAMES[annotation[4].item() - 1]
304
+ ymin = annotation[1].item()
305
+ xmin = annotation[0].item()
306
+ ymax = annotation[3].item()
307
+ xmax = annotation[2].item()
308
+ yield f"{label}/{image_name}", {
309
+ "image": str(image_path),
310
+ "label": label,
311
+ "bbox": [[ymin, ymax, xmin, xmax]],
312
+ }