首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >图像调整方法(resizeBilinear和resizeNearestNeighbor)在Tensorflow.js中不返回正确的结果。

图像调整方法(resizeBilinear和resizeNearestNeighbor)在Tensorflow.js中不返回正确的结果。
EN

Stack Overflow用户
提问于 2020-09-17 11:33:33
回答 1查看 1.5K关注 0票数 3

问题

  1. resizeBilinear(*1)和resizeNearestNeighbor(*2)有什么区别?特别是,resizeNearestNeighbor没有返回正确的图像(一半图像是黑色的)。(图像的一半是黑色的。)
  2. ResizeBilinear不返回分割的正确结果。(图像的一半是灰色的。)为什么会这样呢?这与resizeNearestNeighbor的结果非常相似。

背景

我想开发一个在Tensorflow.js中进行分段的应用程序。幸运的是,我在Python中找到了一些用于分段的示例代码。(*3)我相信我可以通过将获得的Keras模型转换为TFJS模型,在Tensorflow.js中进行分段。我试图调整Tensorflow.js中的图像大小,但是我无法得到正确的结果。有人有什么好主意吗?

这是我写的源代码。(我不习惯写JavaScript。)

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
<head></head>
<meta charset="utf-8"/>
<body>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<canvas id="canvas1" width="256" height="256" style="border: 2px solid;"></canvas>
<canvas id='canvas2' width="128" height="128" style="border: 2px solid;"></canvas>
<canvas id='canvas3' width="128" height="128" style="border: 2px solid;"></canvas>
<canvas id='canvas4' width="128" height="128" style="border: 2px solid;"></canvas>
<canvas id='canvas5' width="128" height="128" style="border: 2px solid;"></canvas>
<script>

var inputImg = new Image();inputImg.src = "../00_resources/woman172.jpg";
const canvas1 = document.getElementById('canvas1');
const canvas2 = document.getElementById('canvas2');
const canvas3 = document.getElementById('canvas3');
const canvas4 = document.getElementById('canvas4');
const canvas5 = document.getElementById('canvas5');

const SIZE = 128;
const COL_CHANNEL = 3;
const BIN_CHANNEL = 1;

inputImg.onload = () => {
  const inCtx = canvas1.getContext('2d');
  inCtx.drawImage(inputImg, 0, 0, canvas1.width, canvas1.height);
};


(async() => {

    // load model
    let model = await tf.loadGraphModel('/tfjsdoc/Portrait-Segmentation/model/deconv_bnoptimized_munet_to_tfjs_graph_model/model.json');

    // image1 (original image)
    let img1 = tf.browser.fromPixels(canvas1, COL_CHANNEL);
    tf.browser.toPixels(img1, canvas2);// OK

    // image2 (resized image - using 'resizeNearestNeighbor')
    let img2 = img1.resizeNearestNeighbor([SIZE, SIZE]).toFloat().div(tf.scalar(255));
    tf.browser.toPixels(img2, canvas3);// NG

    // image3 (resized image - using 'resizeBilinear')
    let img3 = img1.resizeBilinear([SIZE, SIZE]).toFloat().div(tf.scalar(255));
    tf.browser.toPixels(img3, canvas4);// OK?

    // predict (image3 is used to make predictions, but the same problem occurs as in image2)
    let pred = await model.predict(tf.reshape(img3, [-1,SIZE,SIZE,COL_CHANNEL])).data();
    tf.browser.toPixels(tf.tensor(pred, [SIZE,SIZE, BIN_CHANNEL]), canvas5);// NG

})();

</script>
</body>
</html>

捕获执行结果

从左到右。

  • 原始图像
  • 原始图像是在画布上拍摄和绘制的。
  • 使用resizeNearestNeighbor调整原始图像的大小并在画布上绘制(图像的一半是黑色的)
  • 使用resizeBilinear调整原始图像的大小,并在画布上绘制(看上去正确)
  • 用resizeBilinear对原始图像进行分割,并在画布上绘制(一半图像是灰色的)

参考资料

(*1) https://js.tensorflow.org/api/latest/#image.resizeBilinear

(*2) https://js.tensorflow.org/api/latest/#image.resizeNearestNeighbor

(*3) https://github.com/anilsathyan7/Portrait-Segmentation

EN

回答 1

Stack Overflow用户

发布于 2020-10-01 22:11:08

根据我的经验,两者似乎都完成了输入图像的相似大小调整。ResizeBilinear对图像执行双线性插值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63937147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档