-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiftyone_dataset_creator.py
More file actions
299 lines (230 loc) · 12.4 KB
/
fiftyone_dataset_creator.py
File metadata and controls
299 lines (230 loc) · 12.4 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# This file is to contain the core dataset creation code using the FiftyOne package
# to use images from Google.
# It should expose simple methods or an object with methods that can be called
# from the adapter.
import os
from typing import List
from datetime import datetime, timedelta
import json
import shutil
# Import CodeProject.AI SDK
from codeproject_ai_sdk import RequestData, ModuleRunner, JSON, get_folder_size
from utils import Actions, ActionStates, ProgressHandler, InitializationError
class YoloV5DatasetCreator:
def __init__(self, runner:ModuleRunner, module_path:str, fiftyone_dirname:str, zoo_dirname:str,
datasets_dirname:str, server_root_path:str):
self.runner = runner
self.module_path = module_path
self.fiftyone_dirname = fiftyone_dirname
self.zoo_dirname = zoo_dirname
self.datasets_dirname = datasets_dirname
self.server_root_path = server_root_path
self.progress = ProgressHandler()
self.action_state = ActionStates.Idle
self.action_message = ""
self.cancel_requested = False
self.init_fiftyone()
def init_fiftyone(self):
# This module is reloaded by spawn.py inside numpy. There's some
# processing we need to do to import fiftyone, so let's do this only
# when we're actually running the code, not each time we import this
# module
# We still need to import modules so we have access to the namespace,
# but once a module has been imported within a module, it's just accessed
# via a lookup, and doesn't actually go through all the init code.
# Keep things neat, and also attempt to mitigate permission issues with the
# fiftyone mongodb by having it all sit under the current module's folder
fiftyone_path = os.path.normpath(os.path.join(self.module_path, self.fiftyone_dirname))
os.environ["FIFTYONE_DATABASE_DIR"] = fiftyone_path
# try:
# if not os.path.exists(fiftyone_path):
# os.mkdir(fiftyone_path)
# except Exception as pathdir_ex:
# message = F"Unable to create fiftyone folder: {pathdir_ex}"
# We'll import and fail quickly if needed
try:
import fiftyone.zoo as foz
except Exception as zoo_ex:
# Clear the problem for next time
shutil.rmtree(fiftyone_path)
message = F"Unable to import and initialise the fiftyone.zoo package: {zoo_ex}"
print(message)
raise InitializationError(message)
try:
import fiftyone as fo
except Exception as ex:
if 'fiftyone.core.service.DatabaseService failed to bind to port' in str(ex):
message = "Failed to connect to mongoDB server. Possibly it was left in a bad state"
else:
message = F"Unable to import and initialise the fiftyone package:{ex}"
raise InitializationError(message)
import fiftyone.utils.openimages as fouo
# configure FiftyOne
fo.config.default_ml_backend = "torch"
fo.config.dataset_zoo_dir = os.path.join(self.module_path, self.zoo_dirname)
fo.config.show_progress_bars = False
fo.config.do_not_track = True
self.available_classes = fouo.get_classes()
self.available_classes_lower = None
print("*** FiftyOne imported successfully")
def create_dataset(self, dataset_name:str, classes:List[str], num_images:int) -> bool:
try:
self.cancel_requested = False
self.action_state = ActionStates.Initializing
self.progress.value = 0
# Already imported, so these won't do any database setup (hopefully),
# but we need to 'import' again to get access to the namespace
import fiftyone as fo
import fiftyone.zoo as foz
import fiftyone.utils.openimages as fouo
self.action_state = ActionStates.Running
download_splits = ['train', 'validation', 'test']
export_splits = ['train', 'val', 'test']
# Export the Dataset
export_dir = f'{self.datasets_dirname}/{dataset_name}'
if os.path.exists(export_dir):
shutil.rmtree(export_dir)
label_types = ["detections"]
# This will throw on invalid class name.
try:
normalized_classes = self.normalize_classlist(classes)
except Exception as ex:
self.action_state = ActionStates.Failed
self.action_message = str(ex)
return False
num_classes = len(normalized_classes)
# 1 init, 5 for each class/split (4 loading, 1 exporting). 'units' are arbitrary here
self.progress.max = 1 + num_classes * 5 * len(export_splits)
self.action_message = "Acquiring training data"
if fo.dataset_exists(dataset_name):
fo.delete_dataset(dataset_name)
self.progress.value += 1 # basic init done
if self.cancel_requested:
self.action_state = ActionStates.Cancelled
self.action_message = "Create Dataset Cancelled"
return False
if fo.dataset_exists(dataset_name):
fo.delete_dataset(dataset_name)
class_index = 1
for current_class in normalized_classes:
for split in download_splits:
self.action_message = f"{class_index}/{num_classes}: Loading {split} split for '{current_class}' from Open Images"
# this results in a 60, 20, 20 split for train, validation, test
num_samples = num_images if split == 'train' else num_images // 3
dataset = foz.load_zoo_dataset('open-images-v7',
splits=split,
label_types=label_types,
classes = current_class,
#only_matching = True,
max_samples=num_samples,
#seed=42,
shuffle=True,
dataset_name=dataset_name)
self.progress.value += 4 # This is a really long step, so boost it
if self.cancel_requested:
self.action_state = ActionStates.Cancelled
self.action_message = "Create Dataset Cancelled"
return False
self.action_message = f"Export {split} split for '{current_class}' to '{export_dir}'"
dataset.export(export_dir = export_dir,
dataset_type= fo.types.YOLOv5Dataset,
label_field = 'ground_truth',
split = 'val' if split == 'validation' else split,
classes = normalized_classes)
fo.delete_dataset(dataset_name);
self.progress.value += 1 # +1 for each export, 3 in total
if self.cancel_requested:
self.action_state = ActionStates.Cancelled
self.action_message = "Create Dataset Cancelled"
return False
class_index += 1
self.action_state = ActionStates.Completed
self.action_message = "Dataset successfully created"
# Here would be the place to write a marker or info file that would
# indicate that the dataset is complete
dataset_info = {
"name" : dataset_name,
"classes" : normalized_classes,
"num_images" : num_images,
"created" : datetime.now().isoformat()
}
info_filename = self.get_dataset_info_filename(dataset_name)
with open(info_filename, 'w') as f:
f.write(json.dumps(dataset_info))
return True
finally:
self.cancel_requested = False
def cancel(self):
cancelable_states = [ActionStates.Running, ActionStates.Initializing]
if self.action_state in cancelable_states:
self.cancel_requested = True
self.action_state = ActionStates.Cancelling
self.action_message = "Cancelling..."
def get_state(self) -> (ActionStates, str):
return self.action_state, self.action_message
def list_classes(self) -> JSON:
return {
"success": True,
"classes": self.available_classes
}
def get_dataset_info(self, dataset_name:str) -> any:
""" Returns an object representing the current state of the model """
# Already imported, so these won't do any database setup (hopefully),
# but we need to 'import' again to get access to the namespace
import fiftyone as fo
import fiftyone.zoo as foz
import fiftyone.utils.openimages as fouo
if not dataset_name:
return { "success": False, "error": "Dataset name not specified." }
dataset_path = os.path.join(self.module_path, self.datasets_dirname, dataset_name)
if os.path.exists(dataset_path):
dataset_size = get_folder_size(dataset_path)
else:
dataset_size = 0
# after the dataset has been created, the info file will be present.
dataset_info_filename = self.get_dataset_info_filename(dataset_name)
dataset_created = os.path.exists(dataset_info_filename)
# Trim the root from this path. This may cause gnashing of teeth to those
# who want the full path, but we're going to have people posting screen
# shots of their window and so we have to remove the sensitive info
rootPrefix = "" # "<app>";
display_dataset_path = dataset_path or ""
if display_dataset_path.startswith(self.server_root_path):
display_dataset_path = rootPrefix + display_dataset_path[len(self.server_root_path):]
return {
"success": True,
"dataset_name": dataset_name,
"dataset_dir": dataset_path ,
"dataset_created": dataset_created,
"dataset_size_mb": round(dataset_size / (1024 * 1000), 1),
"display_dataset_path": display_dataset_path,
}
def get_dataset_info_filename(self, dataset_name: str) -> str:
return os.path.join(self.datasets_dirname, dataset_name, "info.json")
def normalize_classlist(self, classes : List[str]) -> List[str]:
"""
This method converts a list of classes to the normalized values used by
Open Images. Class names are case sensitive. If a class can not be found,
then an Exception is Raised to quickly abort the operation and report
the error to the user so that they can correct the mistake.
"""
if not classes:
raise Exception(f"The list of class names is empty.")
# create the lookup if required.
if not self.available_classes:
# Already imported, so these won't do any database setup (hopefully),
# but we need to 'import' again to get access to the namespace
import fiftyone.utils.openimages as fouo
self.available_classes = fouo.get_classes()
if not self.available_classes_lower:
self.available_classes_lower = [class_name.lower() for class_name in self.available_classes]
# TODO: Rework this to use a dictionary keyed by class.lower()
classes_lower = [class_name.lower() for class_name in classes]
found_classes = []
for class_lower in classes_lower:
try:
idx = self.available_classes_lower.index(class_lower)
found_classes.append(self.available_classes[idx])
except ValueError:
raise Exception(f"Cannot find class {class_lower} in available classes.")
return found_classes