0
# Model Export
1
2
ModelScope's export framework provides capabilities for converting and deploying models to different formats and target platforms. The framework supports various export formats including ONNX, TensorRT, and framework-specific formats.
3
4
## Capabilities
5
6
### Base Exporter Class
7
8
Abstract base class for all model exporters.
9
10
```python { .api }
11
class Exporter:
12
"""
13
Base class for model exporters.
14
"""
15
16
def __init__(self, **kwargs):
17
"""
18
Initialize exporter with configuration parameters.
19
20
Parameters:
21
- **kwargs: Exporter-specific configuration options
22
"""
23
24
def export(
25
self,
26
model,
27
output_dir: str,
28
input_shape: tuple = None,
29
**kwargs
30
):
31
"""
32
Export model to target format.
33
34
Parameters:
35
- model: Model instance to export
36
- output_dir: Directory to save exported model
37
- input_shape: Expected input shape for the model
38
- **kwargs: Additional export parameters
39
"""
40
```
41
42
### Framework-Specific Exporters
43
44
Exporters for different deep learning frameworks.
45
46
```python { .api }
47
class TorchModelExporter(Exporter):
48
"""
49
PyTorch model exporter supporting various output formats.
50
"""
51
52
def __init__(self, **kwargs):
53
"""Initialize PyTorch model exporter."""
54
55
def export(
56
self,
57
model,
58
output_dir: str,
59
input_shape: tuple = None,
60
export_format: str = 'onnx',
61
opset_version: int = 11,
62
**kwargs
63
):
64
"""
65
Export PyTorch model.
66
67
Parameters:
68
- model: PyTorch model instance
69
- output_dir: Output directory for exported model
70
- input_shape: Model input shape
71
- export_format: Target format ('onnx', 'torchscript', 'tensorrt')
72
- opset_version: ONNX opset version
73
"""
74
75
class TfModelExporter(Exporter):
76
"""
77
TensorFlow model exporter for various deployment formats.
78
"""
79
80
def __init__(self, **kwargs):
81
"""Initialize TensorFlow model exporter."""
82
83
def export(
84
self,
85
model,
86
output_dir: str,
87
export_format: str = 'savedmodel',
88
**kwargs
89
):
90
"""
91
Export TensorFlow model.
92
93
Parameters:
94
- model: TensorFlow model instance
95
- output_dir: Output directory for exported model
96
- export_format: Target format ('savedmodel', 'tflite', 'tfjs')
97
"""
98
```
99
100
### Exporter Builder
101
102
Factory function for creating exporters from configuration.
103
104
```python { .api }
105
def build_exporter(cfg: dict, default_args: dict = None):
106
"""
107
Build exporter from configuration dictionary.
108
109
Parameters:
110
- cfg: Exporter configuration dictionary
111
- default_args: Default arguments to merge
112
113
Returns:
114
Exporter instance
115
"""
116
```
117
118
## Domain-Specific Exporters
119
120
### Computer Vision Exporters
121
122
```python { .api }
123
class CartoonTranslationExporter(Exporter):
124
"""
125
Specialized exporter for cartoon translation models.
126
"""
127
128
def __init__(self, **kwargs):
129
"""Initialize cartoon translation exporter."""
130
131
class FaceDetectionSCRFDExporter(Exporter):
132
"""
133
Exporter for SCRFD face detection models.
134
"""
135
136
def __init__(self, **kwargs):
137
"""Initialize SCRFD face detection exporter."""
138
```
139
140
### Multi-Modal Exporters
141
142
```python { .api }
143
class StableDiffusionExporter(Exporter):
144
"""
145
Exporter for Stable Diffusion models with optimization for inference.
146
"""
147
148
def __init__(self, **kwargs):
149
"""Initialize Stable Diffusion exporter."""
150
```
151
152
### Natural Language Processing Exporters
153
154
```python { .api }
155
class CsanmtForTranslationExporter(Exporter):
156
"""
157
Exporter for CSANMT translation models.
158
"""
159
160
def __init__(self, **kwargs):
161
"""Initialize CSANMT translation exporter."""
162
163
class SbertForSequenceClassificationExporter(Exporter):
164
"""
165
Exporter for Sentence-BERT sequence classification models.
166
"""
167
168
def __init__(self, **kwargs):
169
"""Initialize SBERT sequence classification exporter."""
170
171
class SbertForZeroShotClassificationExporter(Exporter):
172
"""
173
Exporter for Sentence-BERT zero-shot classification models.
174
"""
175
176
def __init__(self, **kwargs):
177
"""Initialize SBERT zero-shot classification exporter."""
178
```
179
180
## Usage Examples
181
182
### Basic Model Export
183
184
```python
185
from modelscope import Model, TorchModelExporter
186
187
# Load PyTorch model
188
model = Model.from_pretrained('damo/cv_resnet50_image-classification_imagenet')
189
190
# Create exporter
191
exporter = TorchModelExporter()
192
193
# Export to ONNX format
194
exporter.export(
195
model=model,
196
output_dir='./exported_model',
197
input_shape=(1, 3, 224, 224),
198
export_format='onnx',
199
opset_version=11
200
)
201
202
print("Model exported to ONNX format")
203
```
204
205
### Export with Different Formats
206
207
```python
208
from modelscope import Model, TorchModelExporter
209
210
model = Model.from_pretrained('model_name')
211
exporter = TorchModelExporter()
212
213
# Export to ONNX
214
exporter.export(
215
model=model,
216
output_dir='./onnx_export',
217
export_format='onnx',
218
input_shape=(1, 3, 224, 224)
219
)
220
221
# Export to TorchScript
222
exporter.export(
223
model=model,
224
output_dir='./torchscript_export',
225
export_format='torchscript',
226
input_shape=(1, 3, 224, 224)
227
)
228
229
# Export to TensorRT
230
exporter.export(
231
model=model,
232
output_dir='./tensorrt_export',
233
export_format='tensorrt',
234
input_shape=(1, 3, 224, 224),
235
precision='fp16' # Half precision for faster inference
236
)
237
```
238
239
### TensorFlow Model Export
240
241
```python
242
from modelscope import Model, TfModelExporter
243
244
# Load TensorFlow model
245
tf_model = Model.from_pretrained('tensorflow_model_name')
246
247
# Create TensorFlow exporter
248
tf_exporter = TfModelExporter()
249
250
# Export to SavedModel format
251
tf_exporter.export(
252
model=tf_model,
253
output_dir='./tf_savedmodel',
254
export_format='savedmodel'
255
)
256
257
# Export to TensorFlow Lite
258
tf_exporter.export(
259
model=tf_model,
260
output_dir='./tf_lite',
261
export_format='tflite',
262
optimize=True, # Enable optimizations
263
quantize=True # Enable quantization
264
)
265
266
# Export to TensorFlow.js
267
tf_exporter.export(
268
model=tf_model,
269
output_dir='./tfjs',
270
export_format='tfjs'
271
)
272
```
273
274
### Domain-Specific Export
275
276
```python
277
from modelscope import Model
278
from modelscope.exporters import FaceDetectionSCRFDExporter, StableDiffusionExporter
279
280
# Face detection model export
281
face_model = Model.from_pretrained('damo/cv_ddsar_face-detection_iclr23-damofd')
282
face_exporter = FaceDetectionSCRFDExporter()
283
284
face_exporter.export(
285
model=face_model,
286
output_dir='./face_detection_export',
287
input_shape=(1, 3, 640, 640),
288
confidence_threshold=0.5,
289
nms_threshold=0.4
290
)
291
292
# Stable Diffusion model export
293
sd_model = Model.from_pretrained('stable_diffusion_model')
294
sd_exporter = StableDiffusionExporter()
295
296
sd_exporter.export(
297
model=sd_model,
298
output_dir='./stable_diffusion_export',
299
optimize_for_inference=True,
300
enable_memory_efficient_attention=True
301
)
302
```
303
304
### Export Configuration
305
306
```python
307
from modelscope import build_exporter
308
309
# Define export configuration
310
export_config = {
311
'type': 'TorchModelExporter',
312
'export_format': 'onnx',
313
'opset_version': 12,
314
'dynamic_axes': {
315
'input': {0: 'batch_size'},
316
'output': {0: 'batch_size'}
317
},
318
'optimize': True
319
}
320
321
# Build exporter from configuration
322
exporter = build_exporter(export_config)
323
324
# Export model
325
exporter.export(
326
model=model,
327
output_dir='./configured_export',
328
input_shape=(1, 3, 224, 224)
329
)
330
```
331
332
### Batch Export for Multiple Models
333
334
```python
335
from modelscope import Model, TorchModelExporter
336
337
models_to_export = [
338
'damo/cv_resnet50_image-classification_imagenet',
339
'damo/nlp_structbert_sentence-similarity_chinese',
340
'damo/cv_yolox-s_detection_coco'
341
]
342
343
exporter = TorchModelExporter()
344
345
for model_name in models_to_export:
346
print(f"Exporting {model_name}...")
347
348
# Load model
349
model = Model.from_pretrained(model_name)
350
351
# Determine input shape based on model type
352
if 'cv' in model_name:
353
input_shape = (1, 3, 224, 224)
354
else:
355
input_shape = (1, 512) # For NLP models
356
357
# Export model
358
output_dir = f"./exports/{model_name.replace('/', '_')}"
359
exporter.export(
360
model=model,
361
output_dir=output_dir,
362
input_shape=input_shape,
363
export_format='onnx'
364
)
365
366
print(f"Exported to {output_dir}")
367
```
368
369
### Custom Export with Preprocessing
370
371
```python
372
from modelscope import Model, TorchModelExporter
373
import torch
374
375
class ModelWithPreprocessing(torch.nn.Module):
376
def __init__(self, base_model, preprocessor):
377
super().__init__()
378
self.base_model = base_model
379
self.preprocessor = preprocessor
380
381
def forward(self, x):
382
# Apply preprocessing
383
x = self.preprocessor(x)
384
# Run through base model
385
return self.base_model(x)
386
387
# Load base model and preprocessor
388
base_model = Model.from_pretrained('model_name')
389
preprocessor = torch.nn.Sequential(
390
torch.nn.functional.normalize,
391
# Add other preprocessing steps
392
)
393
394
# Combine model with preprocessing
395
combined_model = ModelWithPreprocessing(base_model, preprocessor)
396
397
# Export combined model
398
exporter = TorchModelExporter()
399
exporter.export(
400
model=combined_model,
401
output_dir='./combined_export',
402
input_shape=(1, 3, 224, 224),
403
export_format='onnx'
404
)
405
```
406
407
### Export Validation and Testing
408
409
```python
410
from modelscope import Model, TorchModelExporter
411
import numpy as np
412
import onnxruntime as ort
413
414
# Export model
415
model = Model.from_pretrained('model_name')
416
exporter = TorchModelExporter()
417
418
output_dir = './validation_export'
419
exporter.export(
420
model=model,
421
output_dir=output_dir,
422
input_shape=(1, 3, 224, 224),
423
export_format='onnx'
424
)
425
426
# Validate exported model
427
def validate_export(original_model, onnx_path, test_input):
428
# Get original model output
429
original_model.eval()
430
with torch.no_grad():
431
original_output = original_model(test_input)
432
433
# Get ONNX model output
434
ort_session = ort.InferenceSession(onnx_path)
435
onnx_output = ort_session.run(
436
None,
437
{'input': test_input.numpy()}
438
)[0]
439
440
# Compare outputs
441
diff = np.abs(original_output.numpy() - onnx_output)
442
max_diff = np.max(diff)
443
444
print(f"Maximum difference: {max_diff}")
445
return max_diff < 1e-5 # Tolerance for numerical differences
446
447
# Create test input
448
test_input = torch.randn(1, 3, 224, 224)
449
450
# Validate
451
is_valid = validate_export(
452
model,
453
f'{output_dir}/model.onnx',
454
test_input
455
)
456
457
print(f"Export validation: {'PASSED' if is_valid else 'FAILED'}")
458
```
459
460
### Production Deployment Export
461
462
```python
463
from modelscope import Model, TorchModelExporter
464
465
# Load production model
466
production_model = Model.from_pretrained('production_model_name')
467
468
# Configure for production deployment
469
exporter = TorchModelExporter()
470
471
# Export optimized for production
472
exporter.export(
473
model=production_model,
474
output_dir='./production_export',
475
input_shape=(1, 3, 224, 224),
476
export_format='onnx',
477
opset_version=12,
478
optimize=True,
479
dynamic_axes={
480
'input': {0: 'batch_size'},
481
'output': {0: 'batch_size'}
482
},
483
# Production-specific optimizations
484
graph_optimization_level='all',
485
enable_memory_pattern=True,
486
enable_cpu_mem_arena=True
487
)
488
489
print("Model exported and optimized for production deployment")
490
```