TensorFlow.js 调整 3D 张量大小

标签 tensorflow image-processing tensorflow.js

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

最佳答案

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

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

reshape 的一个应用是从初始数据集创建批处理

如果输入和输出大小不匹配,可以使用 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]

的图像
// 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 张量大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53368649/

相关文章:

javascript - Tensorflow JS tfjs |无法使用 tf.loadLayersModel 加载模型

python - 将 retrain.py 的输出转换为 tensorflow.js

python - Ubuntu 18.04 LTS 安装 cocoapi

python - 将图像 ( png ) 转换为矩阵,然后转换为一维数组

java - 将 float[][] 转换为灰度图像

java - 检测两个图像之间的变化(检测新对象的出现)

javascript - tensorflow JS : TypeError: Cannot read property 'fetch' of undefined

tensorflow - tflearn 和 tf.contrib.learn 之间有什么关系?

tensorflow - 如何创建与FSNS数据集格式相同的数据集?

tensorflow - Keras 导入错误 Tensorflow 后端