简体   繁体   English

如何将batch_normalization从python转换为c#

[英]How to convert batch_normalization from python to c#

In the last few months, I worked through Python and Tensorflow, building a neural network.在过去的几个月里,我通过 Python 和 Tensorflow 构建了一个神经网络。 The network performs pretty well on a large amount of data (the precision of my prediction is 85% after training on a data-set of 120000 records).该网络在大量数据上表现非常好(在对 120000 条记录的数据集进行训练后,我的预测精度为 85%)。

My neural network makes use of batch-normalization, learning-rate decay, dropout.. It uses an Adam-Optimizer to minimize the loss.我的神经网络利用批量归一化、学习率衰减、辍学......它使用 Adam-Optimizer 来最小化损失。 After training I store my model by a saver to store the mean/variance-variables of batch_normalization:训练后,我通过保存程序存储我的模型以存储 batch_normalization 的均值/方差变量:

    saver = tf.compat.v1.train.Saver(tf.global_variables())
    saver.save(sess, "sessionSave")

After searching for a proper way to convert this model to c#, I found a Tensorflow- implementation for .NET (SciSharp).在寻找将这个模型转换为 c# 的正确方法后,我找到了一个 .NET (SciSharp) 的 Tensorflow 实现。 But I cannot find an implementation of batch_normalization.但是我找不到 batch_normalization 的实现。 In this specific case it would be the following python code call which I need to convert:在这种特定情况下,我需要转换以下 python 代码调用:

    Z_BN = tf.contrib.layers.batch_norm(Z, is_training=train,updates_collections=ops.GraphKeys.UPDATE_OPS, scope="scope"+str(i), reuse=True)

If there is a way to convert this call, I would need to solve the problem of the saved mean/variance-variables differently as well.如果有办法转换这个调用,我需要以不同的方式解决保存的均值/方差变量的问题。 I think, I am not able to implement batch_normalization myself.我认为,我自己无法实现 batch_normalization。 Is there someone who can provide an implementation for this requirement?有人可以为此要求提供实现吗?

If you only need your model to work in .NET for inference, it should be sufficient to convert it to some widely used format such as ONNX, which you can later use with ML.NET: https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/object-detection-onnx如果您只需要您的模型在 .NET 中工作以进行推理,那么将其转换为一些广泛使用的格式(例如 ONNX)就足够了,您以后可以将其与 ML.NET 一起使用: https ://docs.microsoft.com/ en-us/dotnet/machine-learning/tutorials/object-detection-onnx

You may also try to use the model directly via ML.NET, but your mileage may vary: https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/image-classification您也可以尝试通过 ML.NET 直接使用该模型,但您的里程可能会有所不同: https ://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/image-classification

If neither of these approaches work for you, or you need to fine-tune/train the model from C# too, you can try a commercial solution: Gradient , which exposes the entire TensorFlow 1.15 APIs to .NET.如果这两种方法都不适合您,或者您也需要从 C# 微调/训练模型,您可以尝试商业解决方案: Gradient ,它将整个 TensorFlow 1.15 API 公开给 .NET。 Though in that case you would have to use the supported BatchNormalization class from either tensorflow.keras.layers or tensorflow.layers (the later one is deprecated) instead of the one from contrib .尽管在这种情况下,您必须使用tensorflow.keras.layerstensorflow.layers (后者已弃用)中受支持的BatchNormalization类,而不是来自contrib The later is a wrapper anyway . 无论如何,后者是一个包装器

Disclaimer: I am the author of Gradient.免责声明:我是 Gradient 的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM