Tensorflow是一款超重型进口车。我只想在需要时导入它。但是,我有这样一个模型加载函数:
from typing import Dict, Any
from keras.models import Model # Heavy import! Takes 2 seconds or so!
# Model loading is a heavy task. Only do it once and keep it in memory
model = None # type: Optional[Model]
def load_model(config: Dict[str, Any], shape) -> Model:
"""Load a model."""
if globals()['model'] is None:
globals()['model'] = create_model(wili.n_classes, shape)
print(globals()['model'].summary())
return globals()['model']
也许^{} 常量将帮助您:
例如:
TYPE_CHECKING
也可用于避免import cycles相关问题 更多 >
编程相关推荐