微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

tf.dataSync不会以可读的形式从BlazeFaceModel返回张量

如何解决tf.dataSync不会以可读的形式从BlazeFaceModel返回张量

在使用tensorflow.js将面孔发送到另一个模型之前,我正在使用BlazefaceModel来检测面孔

当我使用自定义模型并尝试获取张量输出时,我使用了下面的代码,它可用于返回张量。

    const returnTensors = true;
    const faces = await blazeModel.estimateFaces(tensor,returnTensors);
    if (faces !== null) {
      // Download the tensors to view the shape
      const face = faces.dataSync();
      face.forEach((pred,i) => {
        console.log(`x: ${i},pred: ${pred}`);
      });
    }

但是将其应用于BlazefaceModel的张量输出时会引发以下错误

faces.dataSync is not a function. (In 'faces.dataSync()','faces.dataSync' is undefined)

console.log(面孔)的输出

Array [
  Object {
    "bottomright": Tensor {
      "dataId": Object {},"dtype": "float32","id": 60793,"isdisposedInternal": false,"kept": false,"rankType": "1","scopeId": 116528,"shape": Array [
        2,],"size": 2,"strides": Array [],},"landmarks": Tensor {
      "dataId": Object {},"id": 60795,"rankType": "2","scopeId": 116532,"shape": Array [
        6,2,"size": 12,"strides": Array [
        2,"probability": Tensor {
      "dataId": Object {},"id": 60785,"scopeId": 116495,"shape": Array [
        1,"size": 1,"topLeft": Tensor {
      "dataId": Object {},"id": 60792,"scopeId": 116526,]

解决方法

faces不是张量。它是带有键值(其中值为张量)的json数组。如果您想一次获取数组中的所有张量,则可以使用Object.values(faces[0])

tensors = Object.values(faces[0]) // array of tensor
tensors.map(t => t.dataSync()) // download the value of the tensor to a js array

// alternatively they can all be converted to a big tensor before using only once dataSync()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。