有没有找到张量质心的有效方法?我正在处理N个堆叠的体积(Nx64x64x64),并希望获得一个xx,y,z位置为每个64x64x64体积质心的Nx3张量
答案 0 :(得分:1)
根据公式,您只需要将每个坐标乘以相应的质量,将所有内容相加并除以总质量即可。
import tensorflow as tf
# Input volumes
volumes = tf.placeholder(tf.float32, [None, 64, 64, 64])
# Make array of coordinates (each row contains three coordinates)
ii, jj, kk = tf.meshgrid(tf.range(64), tf.range(64), tf.range(64), indexing='ij')
coords = tf.stack([tf.reshape(ii, (-1,)), tf.reshape(jj, (-1,)), tf.reshape(kk, (-1,))], axis=-1)
coords = tf.cast(coords, tf.float32)
# Rearrange input into one vector per volume
volumes_flat = tf.reshape(volumes, [-1, 64 * 64 * 64, 1])
# Compute total mass for each volume
total_mass = tf.reduce_sum(volumes_flat, axis=1)
# Compute centre of mass
centre_of_mass = tf.reduce_sum(volumes_flat * coords, axis=1) / total_mass
答案 1 :(得分:0)
这是numpy.apply_over_axes
的完美用例:
my_tensor = np.apply_over_axes( np.mean, my_tensor, (1,2,3))
这将返回一个形状为(N,1,1,1)
的数组,其平均值沿每个轴。