code-loader 0.2.87__tar.gz → 0.2.89__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {code_loader-0.2.87 → code_loader-0.2.89}/PKG-INFO +1 -1
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/contract/datasetclasses.py +6 -2
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/contract/responsedataclasses.py +2 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/leap_binder/leapbinder.py +9 -12
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/leap_loader_parallelized_base.py +2 -2
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/leaploader.py +65 -19
- code_loader-0.2.89/code_loader/visualizer_calculator_parallelized.py +63 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/pyproject.toml +1 -1
- {code_loader-0.2.87 → code_loader-0.2.89}/LICENSE +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/README.md +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/contract/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/contract/enums.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/contract/exceptions.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/contract/visualizer_classes.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/utils.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/decoder.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/enums.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/grid.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/loss.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/pytorch_utils.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/utils.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/instancesegmentation/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/instancesegmentation/utils.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/leap_binder/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/metric_calculator_parallelized.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/metrics/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/metrics/default_metrics.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/samples_generator_parallelized.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/utils.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/visualizers/__init__.py +0 -0
- {code_loader-0.2.87 → code_loader-0.2.89}/code_loader/visualizers/default_visualizers.py +0 -0
@@ -21,9 +21,13 @@ SectionCallableInterface = Callable[[int, PreprocessResponse], npt.NDArray[np.fl
|
|
21
21
|
|
22
22
|
MetadataSectionCallableInterface = Union[
|
23
23
|
Callable[[int, PreprocessResponse], int],
|
24
|
+
Callable[[int, PreprocessResponse], Dict[str, int]],
|
24
25
|
Callable[[int, PreprocessResponse], str],
|
26
|
+
Callable[[int, PreprocessResponse], Dict[str, str]],
|
25
27
|
Callable[[int, PreprocessResponse], bool],
|
26
|
-
Callable[[int, PreprocessResponse],
|
28
|
+
Callable[[int, PreprocessResponse], Dict[str, bool]],
|
29
|
+
Callable[[int, PreprocessResponse], float],
|
30
|
+
Callable[[int, PreprocessResponse], Dict[str, float]]
|
27
31
|
]
|
28
32
|
|
29
33
|
|
@@ -66,6 +70,7 @@ class ConfusionMatrixElement:
|
|
66
70
|
ConfusionMatrixCallableInterface = Callable[[tf.Tensor, tf.Tensor], List[List[ConfusionMatrixElement]]]
|
67
71
|
|
68
72
|
CustomCallableInterfaceMultiArgs = Callable[..., tf.Tensor]
|
73
|
+
CustomMultipleReturnCallableInterfaceMultiArgs = Callable[..., Dict[str, tf.Tensor]]
|
69
74
|
ConfusionMatrixCallableInterfaceMultiArgs = Callable[..., List[List[ConfusionMatrixElement]]]
|
70
75
|
MetricCallableReturnType = Union[tf.Tensor, List[List[ConfusionMatrixElement]]]
|
71
76
|
|
@@ -113,7 +118,6 @@ class GroundTruthHandler(DatasetBaseHandler):
|
|
113
118
|
class MetadataHandler:
|
114
119
|
name: str
|
115
120
|
function: MetadataSectionCallableInterface
|
116
|
-
type: DatasetMetadataType
|
117
121
|
|
118
122
|
|
119
123
|
@dataclass
|
@@ -10,8 +10,9 @@ from code_loader.contract.datasetclasses import SectionCallableInterface, InputH
|
|
10
10
|
GroundTruthHandler, MetadataHandler, DatasetIntegrationSetup, VisualizerHandler, PreprocessResponse, \
|
11
11
|
PreprocessHandler, VisualizerCallableInterface, CustomLossHandler, CustomCallableInterface, PredictionTypeHandler, \
|
12
12
|
MetadataSectionCallableInterface, UnlabeledDataPreprocessHandler, CustomLayerHandler, MetricHandler, \
|
13
|
-
|
14
|
-
|
13
|
+
CustomCallableInterfaceMultiArgs, ConfusionMatrixCallableInterfaceMultiArgs, \
|
14
|
+
CustomMultipleReturnCallableInterfaceMultiArgs
|
15
|
+
from code_loader.contract.enums import LeapDataType
|
15
16
|
from code_loader.metrics.default_metrics import metrics_names_to_functions
|
16
17
|
from code_loader.utils import to_numpy_return_wrapper
|
17
18
|
from code_loader.visualizers.default_visualizers import DefaultVisualizer, \
|
@@ -86,18 +87,15 @@ class LeapBinder:
|
|
86
87
|
|
87
88
|
@typechecked
|
88
89
|
def add_custom_metric(self,
|
89
|
-
function: Union[CustomCallableInterfaceMultiArgs,
|
90
|
+
function: Union[CustomCallableInterfaceMultiArgs,
|
91
|
+
CustomMultipleReturnCallableInterfaceMultiArgs,
|
92
|
+
ConfusionMatrixCallableInterfaceMultiArgs],
|
90
93
|
name: str) -> None:
|
91
94
|
arg_names = inspect.getfullargspec(function)[0]
|
92
95
|
self.setup_container.metrics.append(MetricHandler(name, function, arg_names))
|
93
96
|
|
94
97
|
@typechecked
|
95
|
-
def add_prediction(self, name: str, labels: List[str]
|
96
|
-
custom_metrics: Optional[
|
97
|
-
List[Union[CustomCallableInterface, ConfusionMatrixCallableInterface]]] = None) -> None:
|
98
|
-
if metrics or custom_metrics:
|
99
|
-
raise DeprecationWarning("Adding metrics on 'leap_binder.add_prediction' method is deprecated."
|
100
|
-
"Please update the leap script and use metric block instead.")
|
98
|
+
def add_prediction(self, name: str, labels: List[str]) -> None:
|
101
99
|
self.setup_container.prediction_types.append(PredictionTypeHandler(name, labels))
|
102
100
|
|
103
101
|
@typechecked
|
@@ -108,9 +106,8 @@ class LeapBinder:
|
|
108
106
|
self._encoder_names.append(name)
|
109
107
|
|
110
108
|
@typechecked
|
111
|
-
def set_metadata(self, function: MetadataSectionCallableInterface,
|
112
|
-
|
113
|
-
self.setup_container.metadata.append(MetadataHandler(name, function, metadata_type))
|
109
|
+
def set_metadata(self, function: MetadataSectionCallableInterface, name: str) -> None:
|
110
|
+
self.setup_container.metadata.append(MetadataHandler(name, function))
|
114
111
|
|
115
112
|
@typechecked
|
116
113
|
def set_custom_layer(self, custom_layer: Type[tf.keras.layers.Layer], name: str) -> None:
|
@@ -32,13 +32,13 @@ class LeapLoaderParallelizedBase(ABC):
|
|
32
32
|
self._generate_inputs_thread: Optional[Thread] = None
|
33
33
|
self._should_stop_thread = False
|
34
34
|
|
35
|
-
def
|
35
|
+
def _calculate_n_workers_bpyproject.tomly_hardware(self) -> int:
|
36
36
|
p = psutil.Process(self.processes[0].pid)
|
37
37
|
memory_usage_in_bytes = p.memory_info().rss
|
38
38
|
total_memory_in_bytes = psutil.virtual_memory().total
|
39
39
|
|
40
40
|
n_workers = min(int(multiprocessing.cpu_count()),
|
41
|
-
int(total_memory_in_bytes * 0.
|
41
|
+
int(total_memory_in_bytes * 0.5 / memory_usage_in_bytes))
|
42
42
|
n_workers = max(n_workers, 1)
|
43
43
|
return n_workers
|
44
44
|
|
@@ -22,7 +22,7 @@ except Exception as e:
|
|
22
22
|
from code_loader.contract.datasetclasses import DatasetSample, DatasetBaseHandler, InputHandler, \
|
23
23
|
GroundTruthHandler, PreprocessResponse, VisualizerHandler, VisualizerCallableReturnType, CustomLossHandler, \
|
24
24
|
PredictionTypeHandler, MetadataHandler, CustomLayerHandler, MetricHandler
|
25
|
-
from code_loader.contract.enums import DataStateEnum, TestingSectionEnum, DataStateType
|
25
|
+
from code_loader.contract.enums import DataStateEnum, TestingSectionEnum, DataStateType, DatasetMetadataType
|
26
26
|
from code_loader.contract.exceptions import DatasetScriptException
|
27
27
|
from code_loader.contract.responsedataclasses import DatasetIntegParseResult, DatasetTestResultPayload, \
|
28
28
|
DatasetPreprocess, DatasetSetup, DatasetInputInstance, DatasetOutputInstance, DatasetMetadataInstance, \
|
@@ -44,6 +44,12 @@ class LeapLoader:
|
|
44
44
|
torch.cuda.is_available = lambda: False
|
45
45
|
|
46
46
|
self.evaluate_module()
|
47
|
+
except TypeError as e:
|
48
|
+
import traceback
|
49
|
+
if "leap_binder.set_metadata(" in traceback.format_exc(5):
|
50
|
+
raise DeprecationWarning(
|
51
|
+
"Please remove the metadata_type on leap_binder.set_metadata in your dataset script")
|
52
|
+
raise DatasetScriptException(getattr(e, 'message', repr(e))) from e
|
47
53
|
except Exception as e:
|
48
54
|
raise DatasetScriptException(getattr(e, 'message', repr(e))) from e
|
49
55
|
|
@@ -133,7 +139,7 @@ class LeapLoader:
|
|
133
139
|
handlers_test_payloads = self._check_handlers()
|
134
140
|
test_payloads.extend(handlers_test_payloads)
|
135
141
|
is_valid = all([payload.is_passed for payload in test_payloads])
|
136
|
-
setup_response = self.get_dataset_setup_response()
|
142
|
+
setup_response = self.get_dataset_setup_response(handlers_test_payloads)
|
137
143
|
except DatasetScriptException as e:
|
138
144
|
line_number = get_root_exception_line_number()
|
139
145
|
general_error = f"Something went wrong, {repr(e.__cause__)} line number: {line_number}"
|
@@ -180,26 +186,42 @@ class LeapLoader:
|
|
180
186
|
idx = 0
|
181
187
|
dataset_base_handlers: List[Union[DatasetBaseHandler, MetadataHandler]] = self._get_all_dataset_base_handlers()
|
182
188
|
for dataset_base_handler in dataset_base_handlers:
|
183
|
-
test_result = DatasetTestResultPayload(dataset_base_handler.name)
|
189
|
+
test_result = [DatasetTestResultPayload(dataset_base_handler.name)]
|
184
190
|
for state, preprocess_response in preprocess_result.items():
|
185
191
|
if state == DataStateEnum.unlabeled and isinstance(dataset_base_handler, GroundTruthHandler):
|
186
192
|
continue
|
187
193
|
state_name = state.name
|
188
194
|
try:
|
189
195
|
raw_result = dataset_base_handler.function(idx, preprocess_response)
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
+
handler_type = 'metadata' if isinstance(dataset_base_handler, MetadataHandler) else None
|
197
|
+
if isinstance(dataset_base_handler, MetadataHandler) and isinstance(raw_result, dict):
|
198
|
+
metadata_test_result_payloads = [
|
199
|
+
DatasetTestResultPayload(f'{dataset_base_handler.name}_{single_metadata_name}')
|
200
|
+
for single_metadata_name, single_metadata_result in raw_result.items()
|
201
|
+
]
|
202
|
+
for i, (single_metadata_name, single_metadata_result) in enumerate(raw_result.items()):
|
203
|
+
metadata_test_result = metadata_test_result_payloads[i]
|
204
|
+
result_shape = get_shape(single_metadata_result)
|
205
|
+
metadata_test_result.shape = result_shape
|
206
|
+
metadata_test_result.raw_result = single_metadata_result
|
207
|
+
metadata_test_result.handler_type = handler_type
|
208
|
+
test_result = metadata_test_result_payloads
|
209
|
+
else:
|
210
|
+
result_shape = get_shape(raw_result)
|
211
|
+
test_result[0].shape = result_shape
|
212
|
+
test_result[0].raw_result = raw_result
|
213
|
+
test_result[0].handler_type = handler_type
|
214
|
+
|
215
|
+
# setting shape in setup for all encoders
|
216
|
+
if isinstance(dataset_base_handler, (InputHandler, GroundTruthHandler)):
|
217
|
+
dataset_base_handler.shape = result_shape
|
196
218
|
|
197
219
|
except Exception as e:
|
198
220
|
line_number = get_root_exception_line_number()
|
199
|
-
test_result.display[state_name] = f"{repr(e)} line number: {line_number}"
|
200
|
-
test_result.is_passed = False
|
221
|
+
test_result[0].display[state_name] = f"{repr(e)} line number: {line_number}"
|
222
|
+
test_result[0].is_passed = False
|
201
223
|
|
202
|
-
result_payloads.
|
224
|
+
result_payloads.extend(test_result)
|
203
225
|
|
204
226
|
return result_payloads
|
205
227
|
|
@@ -224,7 +246,7 @@ class LeapLoader:
|
|
224
246
|
return heatmap_function(**input_tensors_by_arg_name)
|
225
247
|
|
226
248
|
@staticmethod
|
227
|
-
def get_dataset_setup_response() -> DatasetSetup:
|
249
|
+
def get_dataset_setup_response(handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
|
228
250
|
setup = global_leap_binder.setup_container
|
229
251
|
assert setup.preprocess is not None
|
230
252
|
|
@@ -251,8 +273,26 @@ class LeapLoader:
|
|
251
273
|
ground_truths.append(
|
252
274
|
DatasetOutputInstance(name=gt.name, shape=gt.shape))
|
253
275
|
|
254
|
-
|
255
|
-
|
276
|
+
metadata_instances = []
|
277
|
+
for handler_test_payload in handlers_test_payloads:
|
278
|
+
if handler_test_payload.handler_type != 'metadata':
|
279
|
+
continue
|
280
|
+
if hasattr(handler_test_payload.raw_result, 'tolist'):
|
281
|
+
handler_test_payload.raw_result = handler_test_payload.raw_result.tolist()
|
282
|
+
metadata_type = type(handler_test_payload.raw_result)
|
283
|
+
if metadata_type == int:
|
284
|
+
metadata_type = float
|
285
|
+
if metadata_type == str:
|
286
|
+
dataset_metadata_type = DatasetMetadataType.string
|
287
|
+
elif metadata_type == bool:
|
288
|
+
dataset_metadata_type = DatasetMetadataType.boolean
|
289
|
+
elif metadata_type == float:
|
290
|
+
dataset_metadata_type = DatasetMetadataType.float
|
291
|
+
else:
|
292
|
+
raise Exception(f"Unsupported return type of metadata {handler_test_payload.name}."
|
293
|
+
f"The return type should be one of [int, float, str, bool]. Got {metadata_type}")
|
294
|
+
metadata_instances.append(DatasetMetadataInstance(name=handler_test_payload.name,
|
295
|
+
type=dataset_metadata_type))
|
256
296
|
|
257
297
|
visualizers = [
|
258
298
|
VisualizerInstance(visualizer_handler.name, visualizer_handler.type, visualizer_handler.arg_names)
|
@@ -271,8 +311,8 @@ class LeapLoader:
|
|
271
311
|
metric_inst = MetricInstance(metric.name, metric.arg_names)
|
272
312
|
metrics.append(metric_inst)
|
273
313
|
|
274
|
-
return DatasetSetup(preprocess=dataset_preprocess, inputs=inputs, outputs=ground_truths,
|
275
|
-
visualizers=visualizers, prediction_types=prediction_types,
|
314
|
+
return DatasetSetup(preprocess=dataset_preprocess, inputs=inputs, outputs=ground_truths,
|
315
|
+
metadata=metadata_instances, visualizers=visualizers, prediction_types=prediction_types,
|
276
316
|
custom_losses=custom_losses, metrics=metrics)
|
277
317
|
|
278
318
|
@staticmethod
|
@@ -287,6 +327,7 @@ class LeapLoader:
|
|
287
327
|
|
288
328
|
@lru_cache()
|
289
329
|
def _preprocess_result(self) -> Dict[DataStateEnum, PreprocessResponse]:
|
330
|
+
self.exec_script()
|
290
331
|
preprocess = global_leap_binder.setup_container.preprocess
|
291
332
|
# TODO: add caching of subset result
|
292
333
|
assert preprocess is not None
|
@@ -325,7 +366,12 @@ class LeapLoader:
|
|
325
366
|
preprocess_state = preprocess_result[state]
|
326
367
|
for handler in global_leap_binder.setup_container.metadata:
|
327
368
|
handler_result = handler.function(idx, preprocess_state)
|
328
|
-
|
329
|
-
|
369
|
+
if isinstance(handler_result, dict):
|
370
|
+
for single_metadata_name, single_metadata_result in handler_result.items():
|
371
|
+
handler_name = f'{handler.name}_{single_metadata_name}'
|
372
|
+
result_agg[handler_name] = single_metadata_result
|
373
|
+
else:
|
374
|
+
handler_name = handler.name
|
375
|
+
result_agg[handler_name] = handler_result
|
330
376
|
|
331
377
|
return result_agg
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# mypy: ignore-errors
|
2
|
+
from typing import Optional, List, Tuple, Dict
|
3
|
+
from multiprocessing import Process, Queue
|
4
|
+
|
5
|
+
import numpy as np
|
6
|
+
|
7
|
+
from code_loader.leap_loader_parallelized_base import LeapLoaderParallelizedBase
|
8
|
+
from dataclasses import dataclass
|
9
|
+
import tensorflow as tf
|
10
|
+
from code_loader.leaploader import LeapLoader
|
11
|
+
|
12
|
+
|
13
|
+
@dataclass
|
14
|
+
class VisualizerSerializableError:
|
15
|
+
visualizer_id: str
|
16
|
+
visualizer_name: str
|
17
|
+
index_in_batch: int
|
18
|
+
exception_as_str: str
|
19
|
+
|
20
|
+
|
21
|
+
class VisualizerCalculatorParallelized(LeapLoaderParallelizedBase):
|
22
|
+
def __init__(self, code_path: str, code_entry_name: str, n_workers: Optional[int] = 2,
|
23
|
+
max_samples_in_queue: int = 128) -> None:
|
24
|
+
super().__init__(code_path, code_entry_name, n_workers, max_samples_in_queue, "spawn")
|
25
|
+
|
26
|
+
@staticmethod
|
27
|
+
def _process_func(code_path: str, code_entry_name: str,
|
28
|
+
visualizers_to_process: Queue, ready_visualizations: Queue) -> None:
|
29
|
+
import os
|
30
|
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
31
|
+
|
32
|
+
leap_loader = LeapLoader(code_path, code_entry_name)
|
33
|
+
|
34
|
+
# running preprocessing to sync preprocessing in main thread (can be valuable when preprocess is filling a
|
35
|
+
# global param that visualizer is using)
|
36
|
+
leap_loader._preprocess_result()
|
37
|
+
leap_loader._preprocess_result.cache_clear()
|
38
|
+
|
39
|
+
while True:
|
40
|
+
index_in_batch, visualizer_id, visualizer_name, input_arg_name_to_tensor = \
|
41
|
+
visualizers_to_process.get(block=True)
|
42
|
+
try:
|
43
|
+
with tf.device('/cpu:0'):
|
44
|
+
visualizer_result = \
|
45
|
+
leap_loader.visualizer_by_name()[visualizer_name].function(**input_arg_name_to_tensor)
|
46
|
+
except Exception as e:
|
47
|
+
ready_visualizations.put(VisualizerSerializableError(
|
48
|
+
visualizer_id, visualizer_name, index_in_batch, str(e)))
|
49
|
+
continue
|
50
|
+
|
51
|
+
ready_visualizations.put((index_in_batch, visualizer_id, visualizer_result))
|
52
|
+
|
53
|
+
def _create_and_start_process(self) -> Process:
|
54
|
+
process = self.multiprocessing_context.Process(
|
55
|
+
target=VisualizerCalculatorParallelized._process_func,
|
56
|
+
args=(self.code_path, self.code_entry_name, self._inputs_waiting_to_be_process,
|
57
|
+
self._ready_processed_results))
|
58
|
+
process.daemon = True
|
59
|
+
process.start()
|
60
|
+
return process
|
61
|
+
|
62
|
+
def calculate_visualizers(self, input_arg_name_to_tensor_list: List[Tuple[int, str, str, Dict[str, np.array]]]):
|
63
|
+
return self.start_process_inputs(input_arg_name_to_tensor_list)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/detection/yolo/pytorch_utils.py
RENAMED
File without changes
|
File without changes
|
{code_loader-0.2.87 → code_loader-0.2.89}/code_loader/helpers/instancesegmentation/__init__.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|