2017-05-05 199 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

相關問題