【问题标题】:issue decoding an image using jpeg_js使用 jpeg_js 解码图像的问题
【发布时间】:2020-06-02 09:15:12
【问题描述】:

我的环境:

ubuntu 18.04
rtx 2080ti
cuda 10.1
node v12.16.3 
tfjs 1.7.4

保存的模型是efficientdet-d0,
推理步骤在inference step

js解析图片数据,我把img.png转换成img.jpg,save_model的结果和saved_model result一样
将 saved_model 转换为 tfjs_graph_model 的命令是

tensorflowjs_converter --input_format=tf_saved_model /tmp/saved_model ~/DATA/http_models/specDetection/

我的测试代码是

var tfc = require("@tensorflow/tfjs-converter");
var tf  = require("@tensorflow/tfjs-core");
var jpeg_js = require("jpeg-js");
var fs = require("fs");


async function loadModel() {
    var modelUrl = "http://localhost:8000/model.json"
    var model = await tfc.loadGraphModel(modelUrl);
    return model;
}

async function detect() {
    var model = await loadModel();
    var img = fs.readFileSync("~/SRC/automl_test/efficientdet/img.jpg");
    const input = jpeg_js.decode(img,{useTArray:true,formatAsRGBA:false});

    const batched = tf.tidy(() => {
        const img = tf.browser.fromPixels(input);
        // Reshape to a single-element batch so we can pass it to executeAsync.
        return img.expandDims(0);
    });

    const result = await model.executeAsync({'image_arrays:0':batched},['detections:0']);
    console.log(result);
}

detect();

当使用我的测试代码检测 img.jpg 中的对象时,没有检测到 --- 结果的大小为 0
我该怎么做才能解决这个问题?
感谢您的任何提示

编辑:
代码1:

 var img = fs.readFileSync("~/DATA/http_models/specDetection/test.jpg");

    var dataJpegJs = jpeg_js.decode(img,{useTArray:true,formatAsRGBA:false})
    var batched = tf.browser.fromPixels({data:dataJpegJs.data, width: dataJpegJs.width, height:dataJpegJs.height},3);
    batched = batched.slice([0,0,0],[-1,-1,3]);
    var result = await model.executeAsync({'image_arrays:0':batched.expandDims(0)},['detections:0']);
    result = tf.slice(result,[0,0,1],[1,-1,4]);

代码2:

var img = fs.readFileSync("~/DATA/http_models/specDetection/test.jpg");

    var dataJpegJs = jpeg_js.decode(img,{useTArray:true,formatAsRGBA:true})
    var batched = tf.browser.fromPixels({data:dataJpegJs.data, width: dataJpegJs.width, height:dataJpegJs.height},4);
    batched = batched.slice([0,0,0],[-1,-1,3]);
    var result = await model.executeAsync({'image_arrays:0':batched.expandDims(0)},['detections:0']);
    result = tf.slice(result,[0,0,1],[1,-1,4]);

代码 1 得到了错误的结果,而代码 2 得到了正确的结果。 代码 2 使用 formatAsRGBA:true 解码 jpg,并在 tf.browser.fromPixels 中设置 numChannels=4。 jpeg-js 必须将 jpg 解码为 RGBA 才能正常工作。 我认为这是 jpeg-js 的错误。或者我不熟悉 jpg 编码?

【问题讨论】:

  • 你确定你在这两种情况下都做同样的预处理吗?
  • 感谢您的回复。我使用@edkeveked 代码,它可以工作

标签: tensorflow object-detection tensorflow.js


【解决方案1】:

张量没有很好地生成。 fromPixels 主要用于从 htmlImageElement 获取张量。打印张量的摘要并将其与为 python 生成的摘要进行比较就足以说​​明这一点。

jpeg-js 有问题吗?

首先我们需要知道 imageData 是如何工作的。图像数据像素是 4 个数值 R、G、B、A。当使用 jpeg_js.decode 解码的数据作为 3 通道 (formatAsRGBA:false) 的 tf.browser.fromPixel 的参数时,它被视为图像数据.让我们考虑数据[a, b, c, d, e, f] = jpeg_js.decode("path", {formatAsRGBA:false}) 和从中创建的张量 t t = tf.browser.fromPixels({data, width: 2, height: 1})。它是如何解释的? tf.browser.fromPixels,将创建一个高度为 1 和宽度为 2 的 ImageDate。因此,imageData 的大小将是 1 * 2 * 4(而不是 1 * 2 * 3)并且其所有值都设置为 0。然后它将解码的数据复制到 imageData。所以imageData = [a, b, c, d, e, f, 0, 0]。 结果,切片 (t.slice([0, 0, 0], [-1, -1, 3]) 将是[a, b, c, e, f, 0]jpeg_jstf.browser.fromPixels 都不是问题所在。这就是 imageData 的工作原理

可以做什么?

  • 保留解码图像的alpha通道formatAsRGBA:true
  • 不使用 tf.browser.fromPixels,直接使用 tf.tensor 创建张量
const img = tf.tensor(input.data, [input.height, input.width, 3])

另一种选择是使用tensorflow-node。而tf.node.decodeImage 可以从张量中解码图像。

const img = fs.readFileSync("path/of/image");
const tensor = tf.node.decodeImage(img)
// use the tensor for prediction

不像jpeg-js只适用于jpeg编码格式的图片,它可以解码更广泛的图片

【讨论】:

  • 非常感谢您的回复。这是 fromPixels 的错误。我使用你的代码,它可以工作。我的环境不是节点,我会找到另一种将图像解码为张量的方法。再次感谢您的回复。
  • 你的环境是什么?
  • 我的环境是微信小程序。抱歉我的错误,这实际上是 jpeg-js 的一个错误
  • 无论环境如何,它看起来都像是 nodejs 的超集。我不认为这是jpeg-js 的错误。您只是以错误的方式使用tf.browser.fromPixels。使用jpeg-js查看我的更新答案
  • 我将差异解码选项与 jpeg-js 进行了比较。请参阅我有问题的更新。我认为这是 jpeg-js 的错误。还是我不熟悉 jpg 编码?
猜你喜欢
  • 2022-06-15
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
相关资源
最近更新 更多