神经网络是否有通用格式

Is there a common format for neural networks

不同的团队使用不同的库来训练和 运行 神经网络(caffe、torch、theano...)。这使得共享变得困难:每个库都有自己的格式来存储网络,每次你想测试其他团队的工作时都必须安装一个新的库。

我正在寻找解决方案,让这不再那么乏味: - 是否有首选(共享?)格式来存储神经网络? - 是否有可以帮助处理不同类型网络/或将一种类型转换为另一种类型的服务或库?

谢谢!

Is there a preferred (shared?) format to store neural networks?

每个库/框架都有自己的序列化,例如Caffe 使用 Protocol Buffers, Torch has a built-in serialization scheme and Theano objects can be serialized with pickle.

在某些情况下,例如 OverFeat or darknet the weights and biases are stored on-disk in binary format via plain fwrite-s of the corresponding float(or double) contiguous arrays (see for more details). Note that this does not cover the architecture of the network / model which has to be known or represented separately (like declared explicitly at load time)。

还有:像 libccv stores the structure and the weights in a SQLite database 这样的库。

Is there a service or library that can help handle different types of networks / or transform one type into another?

我认为没有一个(元)库声称这样做。但它存在提供方便转换器的不同项目。

一些示例(非详尽无遗):

--

更新 (2017-09):两项值得注意的举措是:

(1) ONNX 格式(a.k.a。打开神经网络交换):

[...] a standard for representing deep learning models that enables models to be transferred between frameworks

查看这些 blog posts

(2)Apple引入的CoreML格式:

[...] a public file format (.mlmodel) for a broad set of ML methods [...] Models in this format can be directly integrated into apps through Xcode.

ONNX(开放式神经网络交换)

ONNX 是一个开源 AI 生态系统,它为神经网络提供通用格式

  • 它有助于一个深度学习模型转换为另一个。

    一般情况下,模型转换需要 weeks/months 没有 ONNX

    ONNX 提供更简单、更快速的转换过程:

    有关所有支持的转换,请参阅 here

  • 它使 部署 更容易,模型以更可取的方式存储: 在上图中,ONNX 充当 数据摄取层 ,将每个输入模型转换为相同的格式。否则,所有模型就像一堆拼图,彼此不匹配。


如何使用 ONNX - Keras 转换示例

假设您有 Keras 模型,并且您想将其转换为 ONNX:

model = load_model("keras_model.hdf5")  # h5 is also OK!

onnx_model = keras2onnx.convert_keras(model, model.name)

onnx_model_file = 'output_model.onnx'
onnx.save_model(onnx_model, onnx_model_file)

然后加载并 运行 保存的模型:

onnx_model = onnx.load_model('output_model.onnx')

content = onnx_model.SerializeToString()
sess = onnxruntime.InferenceSession(content)
x = x if isinstance(x, list) else [x]
feed = dict([(input.name, x[n]) for n, input in enumerate(sess.get_inputs())])

# Do inference
pred_onnx = sess.run(None, feed)

这个例子使用keras2onnx to convert the Keras model and onnxruntime推理

注意:还有很多 ONNX 格式的预训练模型。查看this


参考文献:
1. https://towardsdatascience.com/onnx-made-easy-957e60d16e94
2。 https://blog.codecentric.de/en/2019/08/portability-deep-learning-frameworks-onnx/
3。 http://on-demand.gputechconf.com/gtc/2018/presentation/s8818-onnx-interoperable-deep-learning-presented-by-facebook.pdf
4. https://devblogs.nvidia.com/tensorrt-3-faster-tensorflow-inference/