leap-model-parser 0.1.225.dev2__py3-none-any.whl → 0.1.226__py3-none-any.whl
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.
- leap_model_parser/contract/graph.py +1 -1
- leap_model_parser/keras_json_model_import.py +1 -1
- leap_model_parser/model_parser.py +45 -9
- {leap_model_parser-0.1.225.dev2.dist-info → leap_model_parser-0.1.226.dist-info}/METADATA +2 -2
- {leap_model_parser-0.1.225.dev2.dist-info → leap_model_parser-0.1.226.dist-info}/RECORD +8 -8
- {leap_model_parser-0.1.225.dev2.dist-info → leap_model_parser-0.1.226.dist-info}/LICENSE +0 -0
- {leap_model_parser-0.1.225.dev2.dist-info → leap_model_parser-0.1.226.dist-info}/WHEEL +0 -0
- {leap_model_parser-0.1.225.dev2.dist-info → leap_model_parser-0.1.226.dist-info}/entry_points.txt +0 -0
|
@@ -47,7 +47,7 @@ class Node:
|
|
|
47
47
|
outputs: Dict[str, ConnectionOutput] = field(default_factory=dict)
|
|
48
48
|
pruning_plan_id: Optional[str] = None
|
|
49
49
|
wrapper: Optional[WrapperData] = None
|
|
50
|
-
shape: Optional[List[
|
|
50
|
+
shape: Optional[List[str]] = None
|
|
51
51
|
|
|
52
52
|
def __key(self):
|
|
53
53
|
return (self.id, self.name)
|
|
@@ -205,7 +205,7 @@ class KerasJsonModelImport:
|
|
|
205
205
|
'serialized_call_kwargs': serialized_call_kwargs})
|
|
206
206
|
|
|
207
207
|
def generate_regular_node(self, layer: Dict[str, Any], layer_metadata: Dict[str, Any], node_key: str,
|
|
208
|
-
layer_name_to_inbound_nodes: Dict[str, List[
|
|
208
|
+
layer_name_to_inbound_nodes: Dict[str, List[keras_node]]):
|
|
209
209
|
data = layer['config']
|
|
210
210
|
if layer['class_name'] in ('TFOpLambda', 'SlicingOpLambda') or layer['class_name'] in self.custom_layers:
|
|
211
211
|
call_args = layer_name_to_inbound_nodes[layer['config']
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
# mypy: ignore-errors
|
|
2
|
+
|
|
1
3
|
import glob
|
|
2
4
|
import json
|
|
3
5
|
import ntpath
|
|
4
|
-
import pickle
|
|
5
6
|
import tarfile
|
|
6
7
|
import tempfile
|
|
7
8
|
from importlib.util import find_spec
|
|
8
9
|
from pathlib import Path
|
|
9
10
|
|
|
10
|
-
|
|
11
11
|
import tensorflow as tf # type: ignore
|
|
12
12
|
from code_loader.contract.mapping import NodeConnection, NodeMapping # type: ignore
|
|
13
13
|
from keras import Model # type: ignore
|
|
@@ -34,6 +34,7 @@ if spec is not None:
|
|
|
34
34
|
|
|
35
35
|
onnx_imported = True
|
|
36
36
|
|
|
37
|
+
|
|
37
38
|
class ModelParser:
|
|
38
39
|
def __init__(self, should_transform_inputs_and_outputs=False,
|
|
39
40
|
custom_layers=None,
|
|
@@ -54,12 +55,14 @@ class ModelParser:
|
|
|
54
55
|
ImportModelTypeEnum.PB_TF2.value: self.convert_pb_model,
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
@staticmethod
|
|
59
|
+
def _add_output_node_shape_to_model_schema(model_schema: Dict, keras_model: Model):
|
|
58
60
|
for i, layer in enumerate(keras_model.layers):
|
|
59
61
|
model_schema['config']['layers'][i]['output_shape'] = list(layer.output_shape)
|
|
60
62
|
|
|
61
63
|
def get_keras_model_and_model_graph(
|
|
62
|
-
self, model_path: Path, model_type: ImportModelTypeEnum) -> Tuple[
|
|
64
|
+
self, model_path: Path, model_type: ImportModelTypeEnum) -> Tuple[
|
|
65
|
+
Dict[str, Node], List[InputInfo], Optional[Model], Optional[str]]:
|
|
63
66
|
model_to_keras_converter: Optional[Callable[[str], Tuple[Dict[str, Node], Model, Optional[str]]]] = \
|
|
64
67
|
self._model_types_converter.get(model_type.value)
|
|
65
68
|
if model_to_keras_converter is None:
|
|
@@ -90,7 +93,6 @@ class ModelParser:
|
|
|
90
93
|
|
|
91
94
|
return graph, connected_inputs, keras_model_with_weights, error_info
|
|
92
95
|
|
|
93
|
-
|
|
94
96
|
def _get_k_model_from_pb_path(self, file_path: str):
|
|
95
97
|
tar_file = tarfile.open(file_path)
|
|
96
98
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
@@ -105,7 +107,8 @@ class ModelParser:
|
|
|
105
107
|
k_model = self._load_keras_model_with_custom_layers(pb_folder_path)
|
|
106
108
|
return k_model
|
|
107
109
|
|
|
108
|
-
def generate_model_graph(self, model_path: Path, model_type: ImportModelTypeEnum) -> Tuple[
|
|
110
|
+
def generate_model_graph(self, model_path: Path, model_type: ImportModelTypeEnum) -> Tuple[
|
|
111
|
+
Dict[str, Node], List[InputInfo]]:
|
|
109
112
|
model_graph, connected_inputs, _, error_info = self.get_keras_model_and_model_graph(
|
|
110
113
|
model_path, model_type)
|
|
111
114
|
return model_graph, connected_inputs
|
|
@@ -130,23 +133,56 @@ class ModelParser:
|
|
|
130
133
|
node.name for node in onnx_model.graph.initializer]
|
|
131
134
|
input_names = list(set(input_all) - set(input_initializer))
|
|
132
135
|
converted_response: ConvertedResponse = onnx_to_keras(onnx_model, input_names=input_names,
|
|
133
|
-
|
|
136
|
+
name_policy='attach_weights_name',
|
|
137
|
+
allow_partial_compilation=False)
|
|
134
138
|
return self.convert_to_keras_model(converted_response.converted_model, converted_response.error_info)
|
|
135
139
|
|
|
136
140
|
def _load_keras_model_with_custom_layers(self, file_path: str):
|
|
137
141
|
custom_objects = {}
|
|
138
142
|
if self.custom_layers is not None:
|
|
139
143
|
custom_objects = self.custom_layers
|
|
140
|
-
|
|
144
|
+
|
|
145
|
+
try:
|
|
146
|
+
return load_model(file_path, custom_objects=custom_objects, compile=False)
|
|
147
|
+
except OSError as e:
|
|
148
|
+
if 'signature' in str(e):
|
|
149
|
+
raise Exception('Unable to open model file. The model might be corrupted or not a valid.')
|
|
150
|
+
else:
|
|
151
|
+
raise e
|
|
152
|
+
|
|
141
153
|
|
|
142
154
|
def convert_h5_model(self, file_path: str) -> Tuple[Dict[str, Node], Model, Optional[str]]:
|
|
143
155
|
imported_model = self._load_keras_model_with_custom_layers(file_path)
|
|
144
156
|
return self.convert_to_keras_model(imported_model)
|
|
145
157
|
|
|
146
|
-
def convert_to_keras_model(self, k_model, error_info: Optional[str] = None) -> Tuple[
|
|
158
|
+
def convert_to_keras_model(self, k_model, error_info: Optional[str] = None) -> Tuple[
|
|
159
|
+
Dict[str, Node], Model, Optional[str]]:
|
|
147
160
|
converted_k_model = convert_channels_first_to_last(
|
|
148
161
|
k_model, self._should_transform_inputs_and_outputs, self.custom_layers)
|
|
149
162
|
|
|
163
|
+
from keras.saving.legacy.saved_model import json_utils # type: ignore
|
|
164
|
+
import numpy as np
|
|
165
|
+
|
|
166
|
+
_orig_get_json_type = json_utils.get_json_type
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _patched_get_json_type(obj): # type: ignore
|
|
170
|
+
# Handle numpy dtype explicitly
|
|
171
|
+
if isinstance(obj, np.dtype):
|
|
172
|
+
return obj.name # e.g. "int64"
|
|
173
|
+
# Make sure common numpy scalars/containers are handled robustly
|
|
174
|
+
if isinstance(obj, np.integer):
|
|
175
|
+
return int(obj)
|
|
176
|
+
if isinstance(obj, np.floating):
|
|
177
|
+
return float(obj)
|
|
178
|
+
if isinstance(obj, np.bool_):
|
|
179
|
+
return bool(obj)
|
|
180
|
+
if isinstance(obj, np.ndarray):
|
|
181
|
+
return obj.tolist()
|
|
182
|
+
return _orig_get_json_type(obj)
|
|
183
|
+
|
|
184
|
+
json_utils.get_json_type = _patched_get_json_type
|
|
185
|
+
|
|
150
186
|
model_schema = json.loads(converted_k_model.to_json())
|
|
151
187
|
|
|
152
188
|
return model_schema, converted_k_model, error_info
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: leap-model-parser
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.226
|
|
4
4
|
Summary:
|
|
5
5
|
Home-page: https://github.com/tensorleap/leap-model-parser
|
|
6
6
|
License: MIT
|
|
@@ -17,7 +17,7 @@ Requires-Dist: keras-data-format-converter (==0.1.22)
|
|
|
17
17
|
Requires-Dist: leap-model-rebuilder (==0.1.7)
|
|
18
18
|
Requires-Dist: numpy (>=1.22.3,<2.0.0)
|
|
19
19
|
Requires-Dist: onnx (==1.13.0)
|
|
20
|
-
Requires-Dist: onnx2kerastl (==0.0.
|
|
20
|
+
Requires-Dist: onnx2kerastl (==0.0.176)
|
|
21
21
|
Requires-Dist: tensorflow (==2.12.0) ; platform_machine == "x86_64"
|
|
22
22
|
Requires-Dist: tensorflow-io-gcs-filesystem (==0.34.0)
|
|
23
23
|
Requires-Dist: tensorflow-macos (==2.12.0) ; platform_machine == "arm64"
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
|
|
2
2
|
leap_model_parser/__init__.py,sha256=OAU7rFHAVVWUM-cDtQ4Ohum567KN8M-YTkHZp5KiYbo,132
|
|
3
3
|
leap_model_parser/contract/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
leap_model_parser/contract/graph.py,sha256=
|
|
4
|
+
leap_model_parser/contract/graph.py,sha256=lbs_IUnlkvoVjiARBMJee-aNbqeNXNPsUeOEwQliKgU,1170
|
|
5
5
|
leap_model_parser/contract/importmodelresponse.py,sha256=GlvnKS8xrebU2Sj0dxqtEhAOOo3DlOtT7AMJ2BlzH9E,145
|
|
6
6
|
leap_model_parser/contract/nodedata.py,sha256=1_ML0nzp3QUZ0_9mGSLhfO4_hqjYMwi0DWLwymUnWEs,43326
|
|
7
7
|
leap_model_parser/contract/ui_components.json,sha256=0lsxwOLElW1E-imCcdh3zKPWgzFuQ_bApG6aHvYfTvo,410591
|
|
8
|
-
leap_model_parser/keras_json_model_import.py,sha256=
|
|
8
|
+
leap_model_parser/keras_json_model_import.py,sha256=h9-eb41W67goutRV5c3VkStxvntgb6EmPFdPeshhtsY,17058
|
|
9
9
|
leap_model_parser/leap_graph_editor.py,sha256=0SDHNVkMq94PXWaTqZzMlzJMRLWebOj4uyz64c_KCTQ,13559
|
|
10
|
-
leap_model_parser/model_parser.py,sha256=
|
|
10
|
+
leap_model_parser/model_parser.py,sha256=W8FGKCBOBClIl0sdBlTdoSRT8sIdVrlbO8veNMouJ7o,8169
|
|
11
11
|
leap_model_parser/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
12
|
leap_model_parser/utils/layerpedia/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
13
|
leap_model_parser/utils/layerpedia/layerpedia.py,sha256=1syubfXBTB630TVkgcQ-Ge7Qe9Zbr6EtZRreuqCJnQ8,9292
|
|
@@ -18,8 +18,8 @@ leap_model_parser/utils/uicomponents/generatenodedata.py,sha256=LRaPlO5jJ9pUtkvL
|
|
|
18
18
|
leap_model_parser/utils/uicomponents/tensorflowinscpection.py,sha256=ym613z9iQKPDBpr0RYD35bTABdm1L-Ez86G47BYT7qw,6775
|
|
19
19
|
leap_model_parser/utils/uicomponents/ui_components.json,sha256=0lsxwOLElW1E-imCcdh3zKPWgzFuQ_bApG6aHvYfTvo,410591
|
|
20
20
|
leap_model_parser/utils/uicomponents/ui_components_config.yaml,sha256=cRH8T-c3TAL0nfefRvt9pFsjbTWNEg38NRyHR7RpJsk,19534
|
|
21
|
-
leap_model_parser-0.1.
|
|
22
|
-
leap_model_parser-0.1.
|
|
23
|
-
leap_model_parser-0.1.
|
|
24
|
-
leap_model_parser-0.1.
|
|
25
|
-
leap_model_parser-0.1.
|
|
21
|
+
leap_model_parser-0.1.226.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
|
|
22
|
+
leap_model_parser-0.1.226.dist-info/METADATA,sha256=GtWZ8_i_RR1EKOGGcdMfvk-H8mMGtB0hMJTLppRTF44,1104
|
|
23
|
+
leap_model_parser-0.1.226.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
24
|
+
leap_model_parser-0.1.226.dist-info/entry_points.txt,sha256=ZvV6EuQt1uAqwapNg5Lo2qjJM9ZG5g2wfzZoLh_Ztyk,77
|
|
25
|
+
leap_model_parser-0.1.226.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
{leap_model_parser-0.1.225.dev2.dist-info → leap_model_parser-0.1.226.dist-info}/entry_points.txt
RENAMED
|
File without changes
|