将具有Azure自定义视觉训练的模型与tensorflow.js一起使用 [英] Use Azure custom-vision trained model with tensorflow.js

查看:191
本文介绍了将具有Azure自定义视觉训练的模型与tensorflow.js一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Azure Custom Vision训练了一个模型,并下载了Android的TensorFlow文件 (请参阅: https://docs.microsoft.com/zh-CN/azure/cognitive-services/custom-vision-service/export-your-model ).如何与 tensorflow.js 一起使用?

I've trained a model with Azure Custom Vision and downloaded the TensorFlow files for Android (see: https://docs.microsoft.com/en-au/azure/cognitive-services/custom-vision-service/export-your-model). How can I use this with tensorflow.js?

我需要一个模型(pb文件)和权重(json文件).但是,Azure给了我一个.pb和一个带有标签的文本文件.

I need a model (pb file) and weights (json file). However Azure gives me a .pb and a textfile with tags.

从我的研究中,我还了解到也存在不同的pb文件,但是我找不到Azure Custom Vision导出的类型.

From my research I also understand that there are also different pb files, but I can't find which type Azure Custom Vision exports.

我找到了 tfjs转换器.这是为了将TensorFlow SavedModel(Azure的* .pb文件是SavedModel?)或Keras模型转换为Web友好格式.但是,我需要填写"output_node_names"(如何获取这些?).我也不确定100%我的Android pb文件是否等于"tf_saved_model".

I found the tfjs converter. This is to convert a TensorFlow SavedModel (is the *.pb file from Azure a SavedModel?) or Keras model to a web-friendly format. However I need to fill in "output_node_names" (how do I get these?). I'm also not 100% sure if my pb file for Android is equal to a "tf_saved_model".

我希望有人给你一个小窍门或一个起点.

I hope someone has a tip or a starting point.

推荐答案

只模仿我说的话此处以节省您的点击次数.我希望直接导出到tfjs的选项很快就可以使用.

Just parroting what I said here to save you a click. I do hope that the option to export directly to tfjs is available soon.

这些是我为使导出的TensorFlow模型对我有用的步骤:

These are the steps I did to get an exported TensorFlow model working for me:

  1. 用Pad替换PadV2操作.这个python函数应该做到这一点. input_filepath是.pb模型文件的路径,而output_filepath是将要创建的更新的.pb文件的完整路径.
  1. Replace PadV2 operations with Pad. This python function should do it. input_filepath is the path to the .pb model file and output_filepath is the full path of the updated .pb file that will be created.

import tensorflow as tf
def ReplacePadV2(input_filepath, output_filepath):
    graph_def = tf.GraphDef()
    with open(input_filepath, 'rb') as f:
        graph_def.ParseFromString(f.read())

    for node in graph_def.node:
        if node.op == 'PadV2':
            node.op = 'Pad'
            del node.input[-1]
            print("Replaced PadV2 node: {}".format(node.name))

    with open(output_filepath, 'wb') as f:
        f.write(graph_def.SerializeToString())

  1. 安装tensorflowjs 0.8.6 或更早版本.在更高版本中,不推荐使用的转换.
  2. 调用转换器时,将--input_format设置为tf_frozen_model并将output_node_names设置为model_outputs.这是我使用的命令.
  1. Install tensorflowjs 0.8.6 or earlier. Converting frozen models is deprecated in later versions.
  2. When calling the convertor, set --input_format as tf_frozen_model and set output_node_names as model_outputs. This is the command I used.

tensorflowjs_converter --input_format=tf_frozen_model --output_json=true --output_node_names='model_outputs' --saved_model_tags=serve  path\to\modified\model.pb  folder\to\save\converted\output

理想情况下,tf.loadGraphModel('path/to/converted/model.json')现在应该可以工作了(已针对tfjs 1.0.0及更高版本进行了测试).

Ideally, tf.loadGraphModel('path/to/converted/model.json') should now work (tested for tfjs 1.0.0 and above).

这篇关于将具有Azure自定义视觉训练的模型与tensorflow.js一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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