在Tensorflow中调整tf.image.resize_images等3D数据的大小

Gre*_*rne 5 3d gpu resize scale tensorflow

我需要调整一些3D数据的大小,例如tf.image.resize_images2d数据的方法。

我当时以为可以尝试tf.image.resize_images在循环和交换轴上运行它,但是我认为必须有一种更简单的方法。简单的最近邻居应该可以。

有任何想法吗?这不是理想的,但是我可以解决数据仅为0或1的情况,并使用类似以下内容:

tf.where(boolMap, tf.fill(data_im*2, 0), tf.fill(data_im*2), 1)
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何得到boolMap。使用tf.while_loop来遍历所有值是否会大大降低性能?我觉得除非有某种自动循环并行化,否则它会在GPU上实现。

数据是大小的张量 [batch_size, width, height, depth, 1]

提前致谢。

注意输出尺寸应为:

[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])
Run Code Online (Sandbox Code Playgroud)

变成:

原版的

变成:

调整大小

我相信最近的邻居不应该具有阶梯效果(我有意去除了颜色)。

Hars的回答正确无误,但我想知道如果有人能破解我的问题了。

小智 4

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

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)
Run Code Online (Sandbox Code Playgroud)

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