0
点赞
收藏
分享

微信扫一扫

openvino系列 3. 缓存模型(仅适用于GPU)

洛茄 2022-04-27 阅读 41

openvino系列 3. 修改模型的输入尺寸

环境描述:

  • 本案例运行环境:Win10
  • IDE:VSCode
  • openvino版本:2022.1
  • 代码链接,1-openvino-basicworkflow文件夹

文章目录


缓存模型

对于某些设备,例如 GPU,加载模型可能需要一些时间。 模型缓存通过将模型缓存在缓存目录中来解决此问题。 如果设置了ie.compile_model(model=net, device_name=device_name, config=config_dict),将使用缓存。 此选项检查缓存中是否存在模型。 如果缓存里面有模型,那么系统会从缓存中加载这个模型。 如果没有,系统则定期加载模型,并将其存储在缓存中,以便在下次加载模型时,将从缓存中加载模型。

在下面的简单案例中,我们创建了一个 model_cache 目录作为 model 的子目录,模型将在其中缓存指定设备。 模型将加载到 GPU。运行此单元一次后,模型将被缓存,因此此单元的后续运行将从缓存中加载模型。
需要注意,模型缓存功能不能在CPU设备上适用。

import time
from pathlib import Path
from openvino.runtime import Core, PartialShape

ie = Core()
device_name = "GPU"  # Model Caching is not available for CPU
if device_name in ie.available_devices and device_name != "CPU":
    cache_path = Path("model/model_cache")
    cache_path.mkdir(exist_ok=True)
    # Enable caching for Inference Engine. To disable caching set enable_caching = False
    enable_caching = True
    config_dict = {"CACHE_DIR": str(cache_path)} if enable_caching else {}
    classification_model_xml = "model/classification.xml"
    model = ie.read_model(model=classification_model_xml)

    start_time = time.perf_counter()
    compiled_model = ie.compile_model(model=model, device_name=device_name, config=config_dict)
    end_time = time.perf_counter()
    print(f"Loading the network to the {device_name} device took {end_time-start_time:.2f} seconds.")
else:
    print("Model caching is not available on CPU devices.")
举报

相关推荐

0 条评论