Skip to content

Commit 8c863bf

Browse files
authored
(core) redistribute optional imports around modelExporters
1 parent fe8c990 commit 8c863bf

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

pysipfenn/core/modelExporters.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@ class ONNXExporter:
2424
"""
2525

2626
def __init__(self, calculator: Calculator):
27-
"""Initialize the ``ONNXExporter`` using a calculator object."""
28-
try:
29-
from onnxconverter_common import float16
30-
from onnxsim import simplify
31-
except ModuleNotFoundError as e:
32-
print('\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install pySIPFENN in '
33-
'`dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)')
34-
27+
"""Initialize the ``ONNXExporter`` using a calculator object."""
3528
self.simplifiedDict = {model: False for model in calculator.loadedModels.keys()}
3629
self.fp16Dict = {model: False for model in calculator.loadedModels.keys()}
3730
self.calculator = calculator
@@ -86,6 +79,11 @@ def simplify(self, model: str) -> None:
8679
Returns:
8780
None
8881
"""
82+
try:
83+
from onnxsim import simplify
84+
except ModuleNotFoundError as e:
85+
print('\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install pySIPFENN in '
86+
'`dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)')
8987
print(f'Simplifying {model}')
9088
assert model in self.calculator.loadedModels, f'{model} not loaded in calculator. Nothing to simplify.'
9189
loadedModel = self.calculator.loadedModels[model]
@@ -110,6 +108,11 @@ def toFP16(self, model: str) -> None:
110108
Returns:
111109
None
112110
"""
111+
try:
112+
from onnxconverter_common import float16
113+
except ModuleNotFoundError as e:
114+
print('\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install pySIPFENN in '
115+
'`dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)')
113116
print(f'Converting {model} to FP16')
114117
assert model in self.calculator.loadedModels, f'{model} not loaded in calculator. Nothing to convert to FP16.'
115118
loadedModel = self.calculator.loadedModels[model]
@@ -239,12 +242,6 @@ class CoreMLExporter:
239242
"""
240243

241244
def __init__(self, calculator: Calculator):
242-
try:
243-
import coremltools as ct
244-
except ModuleNotFoundError as e:
245-
print('\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install pySIPFENN in '
246-
'`dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)')
247-
248245
self.calculator = calculator
249246
assert len(self.calculator.loadedModels)>0, 'No models loaded in calculator. Nothing to export.'
250247
print(f'Initialized CoreMLExporter with models: {list(self.calculator.loadedModels.keys())}')
@@ -265,6 +262,12 @@ def export(self, model: str, append: str = '') -> None:
265262
Returns:
266263
None
267264
"""
265+
try:
266+
import coremltools as ct
267+
except ModuleNotFoundError as e:
268+
print('\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install pySIPFENN in '
269+
'`dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)')
270+
268271
print(f'Exporting {model} to CoreML')
269272
assert model in self.calculator.loadedModels, f'{model} not loaded in calculator. Nothing to export.'
270273
loadedModel = self.calculator.loadedModels[model]

0 commit comments

Comments
 (0)