tensorflow - TensorFlow.js 调整 3D 张量大小
问题描述
我有一个具有以下尺寸的 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])
}
推荐阅读
- java - 静态成员的方法是否被认为是静态的?
- kubernetes - 使用 Kubernetes 注册 GOCD 代理
- ios - 检索 RLMArray 中存在值的 RLMObject
在 iOS 中 - c# - 如何在c#或jquery中将双引号替换为单引号?
- java - Hibernate - 自定义查询未通过 ManyToOne 单向关系的 child 参数找到实体
- java - 如何在jsp页面中将字符串显示为xml
- hadoop - 使用 ElasticSearch 作为永久存储
- ios - 在 IOS 中使用 AVFoundation 捕获相机图像和实时过滤器
- angularjs - ng-repeat 在 angularjs 中具有数组的对象上
- ios - 实现从 iOS7 到 iOS 11 的推送通知。* 我必须处理所有三种情况吗?