TensorFlow.js调整3D Tensor的大小 [英] TensorFlow.js Resize 3D Tensor

查看:145
本文介绍了TensorFlow.js调整3D Tensor的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D张量,其尺寸如下:宽度x高度x深度.我需要将可变大小的卷调整为特定的形状,例如256 x 256 x256.不幸的是,在TensorFlow.js中,它们具有用于调整大小的方法集,例如 tf.image.resizeBilinear & tf.image.resizeNearestNeighbor 仅适用于2D图像.是否有一种变通方法来使这些方法在3D空间中工作?

I have a 3D tensor with the the following dimensions : Width x Height x Depth. I need to resize variable sized volumes to a specific shape say 256 x 256 x 256. Unfortunately, in TensorFlow.js the set of methods they have for resizing such as tf.image.resizeBilinear & tf.image.resizeNearestNeighbor only work for 2D images. Is there a workaround to get these methods to work in 3D space?

推荐答案

要调整张量的大小,可以使用 tf.reshape (如果输入大小与输出大小匹配)

To resize a tensor, one can use tf.reshape if the input size matches the output size

const x = tf.tensor(Array.from({length :64}, (_, i) => i), [4, 4]);
x.reshape([1, 16])

重塑的一种应用是从初始数据集中创建批处理时

One application of reshape is when creating batches from an initial dataset

如果输入和输出大小不匹配,则可以使用 tf.切片

If the input and the output size does not match, one can use tf.slice

const x = tf.tensor(Array.from({length :64}, (_, i) => i), [4, 4, 4]);
x.slice([1, 1, 1], [2, 2, 2]) // we are taking the 8 values at the center of the cube

后者可以用于裁剪形状为[ height, width, channels]

The latter can be used to crop an image with the shape [ height, width, channels]

// t is a tensor
// edge is the size of an edge of the cube
const cropImage = (t, edge) => {
 shape = t.shape;
 startCoord = shape.map(i => (i - edge) / 2)
 return t.slice(startCoord, [edge, edge, edge])
 // to keep the number of channels 
 return t.slice([...startCoord.slice(0, shape.length - 1), 0], [edge, edge, channels])
 }

这篇关于TensorFlow.js调整3D Tensor的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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