在张量流中调整 3D 数据的大小,如 tf.image.resize_images

2024-03-09

我需要调整一些 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 的答案工作正常,但是我想知道我的答案有什么问题,如果有人可以破解它。


我的方法是沿两个轴调整图像大小,在下面粘贴的代码中,我沿深度然后宽度重新采样

def resize_by_axis(image, dim_1, dim_2, ax, is_grayscale):

    resized_list = []


    if is_grayscale:
        unstack_img_depth_list = [tf.expand_dims(x,2) for x in tf.unstack(image, axis = ax)]
        for i in unstack_img_depth_list:
            resized_list.append(tf.image.resize_images(i, [dim_1, dim_2],method=0))
        stack_img = tf.squeeze(tf.stack(resized_list, axis=ax))
        print(stack_img.get_shape())

    else:
        unstack_img_depth_list = tf.unstack(image, axis = ax)
        for i in unstack_img_depth_list:
            resized_list.append(tf.image.resize_images(i, [dim_1, dim_2],method=0))
        stack_img = tf.stack(resized_list, axis=ax)

    return stack_img

resized_along_depth = resize_by_axis(x,50,60,2, True)
resized_along_width = resize_by_axis(resized_along_depth,50,70,1,True)

其中 x 是灰度或 RGB 的 3 维张量; resized_along_width 是最终调整大小的张量。这里我们想要将 3D 图像的尺寸调整为 (50,60,70)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在张量流中调整 3D 数据的大小,如 tf.image.resize_images 的相关文章

随机推荐