2017-05-05 200 views
2

我需要调整一些3D数据的大小,例如用于2d数据的tf.image.resize_images方法。在张量流中调整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]) 

果然:

Original

到:

resized

我相信最近的邻居不应该有楼梯 - 套管效应(我故意删除了颜色)。

哈斯答案工作正常,但是我想知道我的最新错误是否有人可以破解它。

+0

什么是您的3D数据格式? –

+0

这是一个尺寸[batch_size,width,height,depth,1]的张量,类型为float32,值为1的5维可能在某个点上变为3 –

+0

我指的是您使用的是哪种3D数据?深度图,音量,点云,... –

回答

2

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

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; resized_along_width是最终调整大小的张量。在这里,我们要调整三维图像的尺寸为(50,60,70)

+0

@GregCawthorne出于好奇,我的解决方案不适合你吗? – Savvy

+0

我还没有跑这个,但我想调整大小的所有维度。要做第三次调用resize_by_axis就足够了吗? –

+0

Wontonimo的答案是100%tensorflow,所以我想它会更快。我将在稍后报告。 –

2

张量已经是4D,1D分配给'batch_size',另一个3D分配给宽度,高度,深度。如果您正在寻找处理3D图像,让他们分批在此配置

[batch_size, width, height, depth, 1]

然后使用挤压功能,删除不必要的最终尺寸,像这样:

tf.squeeze(yourData, [4])

这将输出张量或形状

[batch_size, width, height, depth]

这是tensorflow将优雅地使用的。

此外

如果你有尺寸得心应手,希望使用tensorflow的重塑能力,而不是你可以像这样:

reshapedData = tf.reshape(yourData, [batch_size, width, height, depth])

就个人而言,我会使用挤压向下一位程序员声明,你的代码只打算去掉大小为1的维度,而重塑可以让我更多,并且会让下一个开发者不得不尝试找出你为什么要重新生成平。

更新包括不断变化的第四维

您想有时使用尺寸 [batch_size, width, height, depth, 1] 有时使用 [batch_size, width, height, depth, n]

没问题。这是相同的解决方案,但现在你不能使用挤压,而是只是留下了重塑像这样:

reshapedData = tf.reshape(yourData, [batch_size, width, height, depth*n])

怎么会这样的工作?我们假设深度是图像帧的数量,n是颜色深度(RGB可能为3)。重塑将一个接一个堆叠彩色框架。毫无疑问,张量流在输入后立即有一个卷积层。卷积层将像您的单色帧一样容易地处理您的彩色帧(尽管具有更多的计算能力和参数)。

和另外缩放

好,这里是如何缩放图像,使用tf.image。resize_images调整像这样后:

reshapedData = tf.image.resize_images(tf.reshape(yourData, [batch_size, width, height, depth*n]) , new_size)

其中大小是如果[new_height,new_width],或在您的情况[宽度*标度,高度*刻度]

new_size = tf.constant([ width * scale , height * scale ])

二维张量

,然后回到原来的

如果人后L您希望它同样可以在塑造形象的这种调整大小:[batch_size, width, height, depth, n]那么简单的使用这个代码

tf.reshape(yourData, [batch_size, width*scale, height*scale, depth,n])

最后除了添加地址缩放深度也

这里是我的解决办法:

我们要重塑这个矩阵,并扩大它类似于一个三维矩阵如何在numpy的扩大这样

a = np.array([[1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27],[1, 2,3, 4, 5, 6, 7, 8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]]) 
print a.reshape([2,3,3,3]) 
a.reshape([54,1]).dot(np.ones([1,8])).reshape([2,3,3,3,2,2,2]).transpose([0,1,6,2,5,3,4]).reshape([2,6,6,6]) 
print a 

这里是tensorflow代码

isolate = tf.transpose(yourdata,[0,4,1,2,3]) # [batch_size,n,width,height,depth] 
flatten_it_all = tf.reshape([batch_size * n * width * height * depth , 1]) # flatten it 

expanded_it = flatten_it_all * tf.ones([1,8]) 
prepare_for_transpose = tf.reshape(expanded_it , [batch_size*n,width,height,depth,2,2,2]) 

transpose_to_align_neighbors = tf.transpose(prepare_for_transpose, [0,1,6,2,5,3,4]) 
expand_it_all = tf.reshape(transpose_to_align_neighbors , [batch_size,n,width*2,height*2,depth*2]) 

#### - removing this section because the requirements changed 
# do a conv layer here to 'blend' neighbor values like: 
# averager = tf.ones([2,2,2,1,1]) * 1./8. 
# tf.nn.conf3d(expand_it_all , averager , padding="SAME") 
# for n = 1. for n = 3, I'll leave it to you. 

# then finally reorder and you are done 
reorder_dimensions = tf.transpose(expand_it_all,[0,2,3,4,1]) # [batch_size,width*2,height*2,depth*2,n] 
+0

我已经更新了这个问题,以便更好地表明我希望输出尺寸符合'提高分辨率' –

+0

此解决方案通过压缩一个通道尺寸来处理1通道数据。但可能不适合3通道数据。 – hars

+1

增加了3通道数据的解决方案。谢谢。 – Wontonimo

相关问题