移植预先训练的keras模型并在IPU上运行



我正在尝试将两个预先训练的keras模型移植到IPU机器中。我设法使用IPUstrategy.scope加载并运行了它们,但我不知道我的做法是否正确。我有我的预训练模型在.h5文件格式。我这样加载它们:

def first_model():
model = tf.keras.models.load_model("./model1.h5")
return model 

在搜索了你的ipu.keras.models.py文件后,我找不到任何加载方法来加载我的预训练模型,这就是我使用tf.keras.models.load_model((.的原因

然后我用这个代码运行:

cfg=ipu.utils.create_ipu_config()
cfg=ipu.utils.auto_select_ipus(cfg, 1)
ipu.utils.configure_ipu_system(cfg)
ipu.utils.move_variable_initialization_to_cpu()
strategy = ipu.ipu_strategy.IPUStrategy()
with strategy.scope():
model = first_model()
print('compile attemptn')
model.compile("sgd", "categorical_crossentropy", metrics=["accuracy"])
print('compilation completedn')

print('running attemptn')
res = model.predict(input_img)[0]
print('run completedn')

你可以在这里看到输出:链接

因此,我很难理解这个系统是如何以及是否正常工作的。基本上,model.compile不会编译我的模型,但当我使用model.predict时,系统首先编译,然后运行。为什么会发生这种情况?有没有其他方法可以在IPU芯片上运行预先训练的keras模型?

我的另一个问题是,是否可以在ipu.keras.model中加载预先训练的keras模型,然后使用model.fit/evaluate对其进行进一步训练和评估,然后保存以备将来使用?

最后一个问题是关于图的编译部分。有没有一种方法可以避免每次在不同的策略.scope((中使用model.product((时重新编译图形?

我使用tensorflow2.1.2轮式

感谢您抽出时间

为了添加一些上下文,Graphcore TensorFlow轮子包括一个用于IPU的Keras端口,可用作tensorflow.python.ipu.keras。您可以通过此链接访问IPU Keras的API文档。该模块包含针对TensorFlow Keras类ModelSequential的IPU特定优化替换,以及更高性能的多IPU类,如PipelineModelPipelineSequential

根据您的具体问题,当您提到目前没有IPU特定的方法来加载预先训练的Keras模型时,您是对的。我鼓励您,因为您似乎可以访问IPU,联系Graphcore支持。在执行此操作时,请附上经过预训练的Keras型号model1.h5和代码的独立复制器。

将主题切换到重新编译问题:使用可执行缓存可以防止重新编译,您可以使用环境变量TF_POPLAR_FLAGS='--executable_cache_path=./cache'进行设置。我还建议查看以下资源:

  • 本教程收集了有关重新编译的一些注意事项,以及在IPU上使用TensorFlow2时如何避免重新编译
  • Graphcore TensorFlow文档解释了如何在IPU上使用预编译模式

最新更新