简体   繁体   English

在 NodeJS 上运行 Frozen Tensorflow 模型

[英]Running Frozen Tensorflow model on NodeJS

I'm new to tensorflowjs (and js in general), however I need to run a trained model on it.我是 tensorflowjs(以及一般的 js)的新手,但是我需要在它上面运行一个训练有素的模型。 Currently I converted the model to json format, but struggle to feed data to it:目前我将模型转换为 json 格式,但很难向它提供数据:

const tf = require('@tensorflow/tfjs')
const tfn = require('@tensorflow/tfjs-node-gpu')

async function start() { 
    const handler = tfn.io.fileSystem("./model/model.json"); 
    const model = await tf.loadGraphModel(handler); 
    let latents = tf.randomNormal([1,512], 'float32'); 
    let labels = tf.zeros([1, 0]); 
    model.predict([latents, labels]);
}
start();

But I receive an error saying The Conv2D op currently supports NHWC tensor format on the CPU. The op was given the format: NCHW但是我收到一条错误消息,说The Conv2D op currently supports NHWC tensor format on the CPU. The op was given the format: NCHW The Conv2D op currently supports NHWC tensor format on the CPU. The op was given the format: NCHW

So as I understood, it is a tfjs issue, so I tried to create a float32 array and pass it to model like this:所以据我所知,这是一个 tfjs 问题,所以我尝试创建一个 float32 数组并将其传递给模型,如下所示:

var f32array = new Float32Array(512);
model.predict([f32array, labels]);

But then I see an error saying the dtype of dict['Gs/latents_in'] provided in model.execute(dict) must be float32, but was undefined但是后来我看到一个错误,说the dtype of dict['Gs/latents_in'] provided in model.execute(dict) must be float32, but was undefined

With python, I'm running inference by using this code:使用 python,我使用以下代码运行推理:

graph = load_graph("dash/frozen_model.pb")

    x = graph.get_tensor_by_name('prefix/Gs/latents_in:0')
    x2 = graph.get_tensor_by_name('prefix/Gs/labels_in:0')
    y = graph.get_tensor_by_name('prefix/Gs/images_out:0')


    with tf.Session(graph=graph, config = config) as sess:
        while True:
            start_time = time.time()

            latents = np.random.randn(1, 512).astype(np.float32)
            labels = np.zeros([latents.shape[0], 0], np.float32)
            y_out = sess.run(y, feed_dict = { x: latents, x2: labels})

Would appreciate any help将不胜感激任何帮助

Passing the data as Float32Array will not work since model.predict expects either a tensor or an array of tensors.将数据作为Float32Array传递将不起作用,因为model.predict需要张量或张量数组。

As indicated by the error:如错误所示:

The Conv2D op currently supports NHWC tensor format on the CPU. Conv2D op 目前在 CPU 上支持 NHWC 张量格式。 The op was given the format: NCHW op 的格式为:NCHW

the conv2D as of the version 1.6 in js only supports the format NHWC. js 中1.6版的 conv2D 仅支持格式 NHWC。 The only thing you can do is to change the model in python in order to use only the NHWC format.您唯一能做的就是在 python 中更改模型,以便仅使用 NHWC 格式。

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

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