Keras部署以使用Tensorflow.js [英] Keras Deploy for Tensorflow.js Usage

查看:112
本文介绍了Keras部署以使用Tensorflow.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够为Tensorflow.js预测部署keras模型,但是 tf.js无法支持接受. Tf.js似乎接受JSON文件来加载(loadGraphModel()/loadLayersModel()),而不是keras SavedModel(.pb +/assets +/variables).

I need to be able to deploy a keras model for Tensorflow.js prediction, but the Firebase docs only seem to support a TFLite object, which tf.js cannot accept. Tf.js appears to accept JSON files for loading (loadGraphModel() / loadLayersModel() ), but not a keras SavedModel (.pb + /assets + /variables).

我如何实现这个目标?

Tensorflow.js部分的注释:有很多指向 loadFrozenModel()函数,该函数同时需要.pbweights_manifest.json.在我看来,在将其发送到GCloud之前,我必须以编程方式对其进行汇编,因为keras的SavedModel不包含两者(我包含.pb +/assets +/variables).

Note for the Tensorflow.js portion: There are a lot of pointers to the tfjs_converter, but the closest API function offered to what I'm looking for is the loadFrozenModel() function, which requires both a .pb and a weights_manifest.json. It seem to me like I'd have to programmatically assemble this before before sending it up to GCloud as a keras SavedModel doesn't contain both (mine contains .pb + /assets + /variables).

对于简单的部署功能而言,这似乎很乏味,而且我想我的问题只会出现在每种工具的常用用法上.

This seems tedious for a straightforward deployment feature, and I'd imagine my question only hits upon common usage of each tool.

我正在寻找的是从Keras =>的简单途径. Firebase/GCloud => Tensorflow.js.

推荐答案

所以我理解您的困惑,但您已经做好了一半准备. 因此,如果我理解正确,那么您的keras模型具有以下文件和文件夹:

So I understand your confusion but you have half part ready. So your keras model has the following files and folders if I understand correctly:

saved_model.pb
/assests
/variables

这足以将keras模型转换为tensorflow.js模型. 通过以下方式使用转换器脚本.确保您具有最新版本的tfjs.如果您没有最新版本,请尝试创建一个virtual environment并安装最新的tfjs,否则它将破坏您的tensorflow版本.

This is enough to convert the keras model to tensorflow.js model. Use the converter script in the following manner. Make sure you have the latest version of tfjs. If you do not have the latest version, try creating a virtual environment and install latest tfjs otherwise it will disrupt your tensorflow version.

import tensorflowjs as tfjs
import tensorflow as tf

model=tf.keras.models.load_model('path/to/keras/model')

tfjs.converters.save_keras_model(model, 'path/where/you/will/like/to/have/js/model/converted')

一旦转换了模型,您将收到js模型的以下文件.

Once you have converted the model you will receive following files for js model.

model.json
something.bin

您将必须使用网络服务器托管这些文件,并使其可用于loadLayersModel API,如下所示:

You will have to host those files using a webserver and just make it available for loadLayersModel API something like this:

const model = await tf.loadLayersModel(
     'location/of/model.json');

就是这样,您已经将模型从Keras转换为Tensorflowjs,并已在js中上传.

That is it and you have converted the model from Keras to Tensorflowjs and uploaded as well in js.

希望我的回答对您有所帮助.

I hope my answer helps you.

这篇关于Keras部署以使用Tensorflow.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆