-
Notifications
You must be signed in to change notification settings - Fork 0
/
infocoreml
354 lines (314 loc) · 15.6 KB
/
infocoreml
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# extracts all coreml model's informations
import coremltools as ct
import os
# Function to get the model path interactively
def get_model_path():
model_path = input("Enter the path to your Core ML model (.mlmodel or .mlpackage file) or press Enter to exit: ").strip()
if model_path:
return model_path
else:
return None
# Load the Core ML model
def load_model(model_path):
if os.path.isdir(model_path):
# Assuming it's an .mlpackage directory
model = ct.models.MLModel(model_path)
else:
# Assuming it's an .mlmodel file
model = ct.models.MLModel(model_path)
return model
# Extract model metadata
def extract_metadata(model):
metadata = model.get_spec().description.metadata
print("Model Metadata:")
print(f" Author: {metadata.author.strip()}")
print(f" Short description: {metadata.shortDescription.strip()}")
print(f" License: {metadata.license.strip()}")
print(f" Version: {metadata.versionString.strip()}")
# Extract input and output descriptions
def extract_io_description(model):
input_desc = model.get_spec().description.input
output_desc = model.get_spec().description.output
print("\nInput Descriptions:")
for input in input_desc:
shape = []
if input.type.WhichOneof('Type') == 'multiArrayType':
shape = input.type.multiArrayType.shape
elif input.type.WhichOneof('Type') == 'imageType':
shape = [input.type.imageType.width, input.type.imageType.height, input.type.imageType.colorSpace]
print(f" Name: {input.name.strip()}, Type: {input.type.WhichOneof('Type')}, Shape: {shape}")
print("\nOutput Descriptions:")
for output in output_desc:
shape = []
if output.type.WhichOneof('Type') == 'multiArrayType':
shape = output.type.multiArrayType.shape
elif output.type.WhichOneof('Type') == 'imageType':
shape = [output.type.imageType.width, output.type.imageType.height, output.type.imageType.colorSpace]
print(f" Name: {output.name.strip()}, Type: {output.type.WhichOneof('Type')}, Shape: {shape}")
# Extract neural network layer information
def extract_layers(model):
spec = model.get_spec()
if spec.WhichOneof('Type') == 'neuralNetwork':
nn_spec = spec.neuralNetwork
print("\nNeural Network Layers:")
for layer in nn_spec.layers:
try:
print(f"Layer Name: {layer.name.strip()}")
if layer.HasField('convolution'):
conv = layer.convolution
print(f" Type: Convolution")
print(f" Kernel Channels: {conv.kernelChannels}")
print(f" Output Channels: {conv.outputChannels}")
print(f" Kernel Size: {conv.kernelSize}")
print(f" Stride: {conv.stride}")
print(f" Weights: {len(conv.weights.floatValue)} values")
if conv.hasBias:
print(f" Bias: {conv.bias.floatValue}")
elif layer.HasField('innerProduct'):
ip = layer.innerProduct
print(f" Type: Fully Connected")
print(f" Input Channels: {ip.inputChannels}")
print(f" Output Channels: {ip.outputChannels}")
print(f" Weights: {len(ip.weights.floatValue)} values")
if ip.hasBias:
print(f" Bias: {ip.bias.floatValue}")
elif layer.HasField('activation'):
activation = layer.activation
print(f" Type: Activation")
print(f" Activation Type: {activation.WhichOneof('NonlinearityType')}")
elif layer.HasField('batchnorm'):
bn = layer.batchnorm
print(f" Type: Batch Normalization")
print(f" Gamma: {bn.gamma.floatValue}")
print(f" Beta: {bn.beta.floatValue}")
print(f" Mean: {bn.mean.floatValue}")
print(f" Variance: {bn.variance.floatValue}")
elif layer.HasField('pooling'):
pool = layer.pooling
print(f" Type: Pooling")
print(f" Pooling Type: {pool.type}")
print(f" Kernel Size: {pool.kernelSize}")
print(f" Stride: {pool.stride}")
# Added try-except for padding to avoid missing attribute errors
try:
print(f" Padding: {pool.padding}")
except AttributeError:
print(f" Padding: Not available")
elif layer.HasField('softmax'):
sm = layer.softmax
print(f" Type: Softmax")
print(f" Axis: {sm.axis}")
elif layer.HasField('reshape'):
reshape = layer.reshape
print(f" Type: Reshape")
print(f" Target Shape: {reshape.targetShape}")
except Exception as e:
print(f" Error processing layer {layer.name.strip()}: {e}")
# Extract preprocessing and postprocessing steps
def extract_preprocessing_postprocessing(model):
spec = model.get_spec()
if spec.HasField('pipeline'):
pipeline = spec.pipeline
print("\nPipeline Models:")
for model in pipeline.models:
print(f" Model Type: {model.WhichOneof('Type')}")
if spec.HasField('neuralNetwork'):
nn_spec = spec.neuralNetwork
if nn_spec.preprocessing:
print("\nPreprocessing Steps:")
for step in nn_spec.preprocessing:
print(f" Preprocessing Type: {step.WhichOneof('preprocessor')}")
if step.HasField('scaler'):
scaler = step.scaler
print(f" Scaler Mean: {scaler.channelScale}")
print(f" Scaler Bias: {scaler.channelBias}")
if nn_spec.postprocessing:
print("\nPostprocessing Steps:")
for step in nn_spec.postprocessing:
print(f" Postprocessing Type: {step.WhichOneof('postprocessor')}")
if step.HasField('scaler'):
scaler = step.scaler
print(f" Scaler Mean: {scaler.channelScale}")
print(f" Scaler Bias: {scaler.channelBias}")
# Extract pipeline models (if applicable)
def extract_pipeline_models(model):
spec = model.get_spec()
if spec.HasField('pipeline'):
pipeline = spec.pipeline
print("\nPipeline Models:")
for submodel in pipeline.models:
print(f" Sub-model Type: {submodel.WhichOneof('Type')}")
# Extract training information (if available)
def extract_training_information(model):
spec = model.get_spec()
if spec.HasField('neuralNetwork'):
nn_spec = spec.neuralNetwork
if nn_spec.HasField('trainingInput'):
training = nn_spec.trainingInput
print("\nTraining Information:")
print(f" Optimizer: {training.optimizer}")
print(f" Learning Rate: {training.learningRate}")
print(f" Epochs: {training.epochs}")
# Extract custom layers (if applicable)
def extract_custom_layers(model):
spec = model.get_spec()
if spec.HasField('neuralNetwork'):
nn_spec = spec.neuralNetwork
for layer in nn_spec.layers:
if layer.HasField('custom'):
custom = layer.custom
print(f"\nCustom Layer: {custom.className}")
# Extract feature descriptions
def extract_feature_descriptions(model):
input_desc = model.get_spec().description.input
output_desc = model.get_spec().description.output
print("\nInput Feature Descriptions:")
for input in input_desc:
print(f" Name: {input.name.strip()}, Type: {input.type.WhichOneof('Type')}")
if input.type.WhichOneof('Type') == 'multiArrayType':
print(f" Shape: {input.type.multiArrayType.shape}")
print("\nOutput Feature Descriptions:")
for output in output_desc:
print(f" Name: {output.name.strip()}, Type: {output.type.WhichOneof('Type')}")
if output.type.WhichOneof('Type') == 'multiArrayType':
print(f" Shape: {output.type.multiArrayType.shape}")
# Extract feature constraints (if applicable)
def extract_feature_constraints(model):
input_desc = model.get_spec().description.input
output_desc = model.get_spec().description.output
print("\nInput Feature Constraints:")
for input in input_desc:
constraints = input.type.multiArrayType
if constraints.HasField('shape'):
print(f" Name: {input.name.strip()}, Shape: {constraints.shape}")
print("\nOutput Feature Constraints:")
for output in output_desc:
constraints = output.type.multiArrayType
if constraints.HasField('shape'):
print(f" Name: {output.name.strip()}, Shape: {constraints.shape}")
# Extract model type-specific information
def extract_model_type_specific_info(model):
spec = model.get_spec()
model_type = spec.WhichOneof('Type')
print(f"\nModel Type: {model_type}")
# Add more details for specific model types if needed
# Extract quantization details (if applicable)
def extract_quantization_details(model):
spec = model.get_spec()
if spec.HasField('quantization'):
quantization = spec.quantization
print("\nQuantization Details:")
print(f" Quantization Scheme: {quantization.scheme}")
print(f" Parameters: {quantization.parameters}")
# Extract specification version
def extract_specification_version(model):
spec = model.get_spec()
print(f"\nSpecification Version: {spec.specificationVersion}")
# Extract protobuf messages
def extract_protobuf_messages(model):
spec = model.get_spec()
print(f"\nProtobuf Messages: {spec}")
# Extract weights and biases for all layers
def extract_weights_biases(model):
spec = model.get_spec()
if spec.WhichOneof('Type') == 'neuralNetwork':
nn_spec = spec.neuralNetwork
print("\nWeights and Biases:")
for layer in nn_spec.layers:
try:
if layer.HasField('convolution'):
conv = layer.convolution
print(f"Layer: {layer.name.strip()}")
print(f" Weights: {conv.weights.floatValue}")
if conv.hasBias:
print(f" Bias: {conv.bias.floatValue}")
elif layer.HasField('innerProduct'):
ip = layer.innerProduct
print(f"Layer: {layer.name.strip()}")
print(f" Weights: {ip.weights.floatValue}")
if ip.hasBias:
print(f" Bias: {ip.bias.floatValue}")
except Exception as e:
print(f" Error processing layer {layer.name.strip()}: {e}")
def main():
while True:
model_path = get_model_path()
if not model_path:
print("Exiting the script.")
break
try:
model = load_model(model_path)
if input('Get Model Metadata? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_metadata(model)
except Exception as e:
print(f" Error extracting model metadata: {e}")
if input('Get Input and Output Descriptions? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_io_description(model)
except Exception as e:
print(f" Error extracting input and output descriptions: {e}")
if input('Get Layer Information? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_layers(model)
except Exception as e:
print(f" Error extracting layer information: {e}")
if input('Get Preprocessing and Postprocessing Steps? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_preprocessing_postprocessing(model)
except Exception as e:
print(f" Error extracting preprocessing and postprocessing steps: {e}")
if input('Get Pipeline Models? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_pipeline_models(model)
except Exception as e:
print(f" Error extracting pipeline models: {e}")
if input('Get Training Information? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_training_information(model)
except Exception as e:
print(f" Error extracting training information: {e}")
if input('Get Custom Layers? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_custom_layers(model)
except Exception as e:
print(f" Error extracting custom layers: {e}")
if input('Get Feature Descriptions? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_feature_descriptions(model)
except Exception as e:
print(f" Error extracting feature descriptions: {e}")
if input('Get Feature Constraints? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_feature_constraints(model)
except Exception as e:
print(f" Error extracting feature constraints: {e}")
if input('Get Model Type Specific Information? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_model_type_specific_info(model)
except Exception as e:
print(f" Error extracting model type specific information: {e}")
if input('Get Quantization Details? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_quantization_details(model)
except Exception as e:
print(f" Error extracting quantization details: {e}")
if input('Get Specification Version? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_specification_version(model)
except Exception as e:
print(f" Error extracting specification version: {e}")
if input('Get Protobuf Messages? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_protobuf_messages(model)
except Exception as e:
print(f" Error extracting protobuf messages: {e}")
if input('Get Weights and Biases for All Layers? Enter "y" or press "Enter" to get, enter "n" to dismiss: ').strip().lower() != 'n':
try:
extract_weights_biases(model)
except Exception as e:
print(f" Error extracting weights and biases: {e}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()