2016-02-04 97 views
5

我正在嘗試使用TensorFlow讀取圖像分類問題的某些圖像輸入。TensorFlow將圖像張量調整爲動態圖形

當然,我正在用tf.image.decode_jpeg(...)這樣做。我的圖像大小不一,因此我無法爲圖像張量指定固定的形狀。

但我需要根據實際尺寸縮放圖像。具體來說,我想將短邊縮放到一個固定值,並且以保持長寬比的方式縮放較長邊。

我可以通過shape = tf.shape(image)獲得某個圖像的實際形狀。我也能做到計算爲新的長邊像

shape = tf.shape(image) 
height = shape[0] 
width = shape[1] 
new_shorter_edge = 400 
if height <= width: 
    new_height = new_shorter_edge 
    new_width = ((width/height) * new_shorter_edge) 
else: 
    new_width = new_shorter_edge 
    new_height = ((height/width) * new_shorter_edge) 

我現在的問題是我無法通過new_heightnew_widthtf.image.resize_images(...),因爲他們中的一個是張量和resize_images預計整數作爲高度和寬度的投入。

有沒有辦法「拉出」張量的整數或是否有任何其他方式與TensorFlow做我的任務?

在此先感謝。


編輯

因爲我也有some other issuestf.image.resize_images,這裏是爲我工作的代碼:

shape = tf.shape(image) 
height = shape[0] 
width = shape[1] 
new_shorter_edge = tf.constant(400, dtype=tf.int32) 

height_smaller_than_width = tf.less_equal(height, width) 
new_height_and_width = tf.cond(
    height_smaller_than_width, 
    lambda: (new_shorter_edge, _compute_longer_edge(height, width, new_shorter_edge)), 
    lambda: (_compute_longer_edge(width, height, new_shorter_edge), new_shorter_edge) 
) 

image = tf.expand_dims(image, 0) 
image = tf.image.resize_bilinear(image, tf.pack(new_height_and_width)) 
image = tf.squeeze(image, [0]) 

回答

4

要做到這一點是使用(目前實驗的方法,但在下一個版本中可用)tf.cond() *運營商。該運算符能夠測試在運行時計算的值,並根據該值執行兩個分支之一。

shape = tf.shape(image) 
height = shape[0] 
width = shape[1] 
new_shorter_edge = 400 
height_smaller_than_width = tf.less_equal(height, width) 

new_shorter_edge = tf.constant(400) 
new_height, new_width = tf.cond(
    height_smaller_than_width, 
    lambda: new_shorter_edge, (width/height) * new_shorter_edge, 
    lambda: new_shorter_edge, (height/width) * new_shorter_edge) 

現在你有new_heightnew_widthTensor值,將採取在運行時適當的值。


*  要訪問運營商在目前發佈的版本,你需要導入以下:

from tensorflow.python.ops import control_flow_ops 

...然後使用control_flow_ops.cond()代替tf.cond()

+0

這是非常好的,很酷的功能。但是我的問題依然存在。 'tf.image.resize_images(...)'接受'int32'作爲第二個和第三個參數。這就是'new_height'和'new_width'的值應該去的地方。 在我對TensorFlow的理解中,對'eval()'的調用不起作用,因爲這隻在運行時進行評估。是否有任何命令告訴TensorFlow「在圖建設時間」拉出張量的第一個(也是唯一的)整數? – mackcmillion

+0

調用'tf.image.resize_images(image,new_height,new_width)'總是拋出TypeError:期望的int32,得到包含'_Message'類型的張量的列表。' – mackcmillion

+0

啊,這對我來說似乎是一個錯誤。我提交了[問題](https://github.com/tensorflow/tensorflow/issues/1001),並會盡快修復。 – mrry