我需要调整一些 3D 数据的大小,例如tf.image.resize_images
二维数据的方法。
我想我可以尝试跑步tf.image.resize_images
在它上循环并交换轴,但我认为一定有一种更简单的方法。简单的最近邻应该没问题。
有任何想法吗?这并不理想,但我可以解决数据仅为 0 或 1 的情况,并使用如下内容:
tf.where(boolMap, tf.fill(data_im*2, 0), tf.fill(data_im*2), 1)
但我不知道如何获得boolMap
。会使用tf.while_loop
检查所有值会大大降低性能吗?我觉得它会在 GPU 上,除非有某种自动循环并行化。
数据是大小的张量[batch_size, width, height, depth, 1]
提前致谢。
N.B 输出尺寸应为:
[batch_size, width*scale, height*scale, depth*scale, 1]
我想出了这个:
def resize3D(self, input_layer, width_factor, height_factor, depth_factor):
shape = input_layer.shape
print(shape)
rsz1 = tf.image.resize_images(tf.reshape(input_layer, [shape[0], shape[1], shape[2], shape[3]*shape[4]]), [shape[1]*width_factor, shape[2]*height_factor])
rsz2 = tf.image.resize_images(tf.reshape(tf.transpose(tf.reshape(rsz1, [shape[0], shape[1]*width_factor, shape[2]*height_factor, shape[3], shape[4]]), [0, 3, 2, 1, 4]), [shape[0], shape[3], shape[2]*height_factor, shape[1]*width_factor*shape[4]]), [shape[3]*depth_factor, shape[2]*height_factor])
return tf.transpose(tf.reshape(rsz2, [shape[0], shape[3]*depth_factor, shape[2]*height_factor, shape[1]*width_factor, shape[4]]), [0, 3, 2, 1, 4])
结果是:
into:
我相信最近的邻居不应该有楼梯效应(我故意去掉了颜色)。
Hars 的答案工作正常,但是我想知道我的答案有什么问题,如果有人可以破解它。