2017-01-29 129 views
8

我試圖將不同大小的圖像保存到tf記錄中。我發現即使圖像尺寸不同,我仍然可以使用FixedLenFeature加載它們。Tensorflow VarLenFeature vs FixedLenFeature

通過檢查FixedLenFeatureVarLenFeature上的文檔,我發現差異似乎是VarLenFeauture返回一個稀疏張量。

任何人都可以說明一些應該使用的情況FixedLenFeatureVarLenFeature

+0

你用什麼功能保存圖像數據? tf.train.BytesList? – Pietrko

+0

是的,我正在使用tf.train.Byteslist – ZijunLost

回答

10

您可以加載圖像,可能是因爲您使用功能類型tf.train.BytesList()保存了圖像,整個圖像數據是列表中的一個大字節值。

如果我是對的,您正在使用tf.decode_raw從TFRecord加載的圖像中獲取數據。

關於用例: 我不是專家,但我使用VarLenFeature來保存數據集以進行對象檢測任務。 每個圖像有不同數量的邊界框(等於圖像中的對象)。 我使用objects_number功能來追蹤對象(和bbox)的數量。 每個邊界框是4浮子的座標列表

我使用以下代碼來加載:

features = tf.parse_single_example(
    serialized_example, 
    features={ 
     # We know the length of both fields. If not the 
     # tf.VarLenFeature could be used 
     'height': tf.FixedLenFeature([], tf.int64), 
     'width': tf.FixedLenFeature([], tf.int64), 
     'depth': tf.FixedLenFeature([], tf.int64), 
     # Label part 
     'objects_number': tf.FixedLenFeature([], tf.int64), 
     'bboxes': tf.VarLenFeature(tf.float32), 
     'labels': tf.VarLenFeature(tf.int64), 
     # Dense data 
     'image_raw': tf.FixedLenFeature([],tf.string) 

    }) 

# Get metadata 
objects_number = tf.cast(features['objects_number'], tf.int32) 
height = tf.cast(features['height'], tf.int32) 
width = tf.cast(features['width'], tf.int32) 
depth = tf.cast(features['depth'], tf.int32) 

# Actual data 
image_shape = tf.parallel_stack([height, width, depth]) 
bboxes_shape = tf.parallel_stack([objects_number, 4]) 

# BBOX data is actually dense convert it to dense tensor 
bboxes = tf.sparse_tensor_to_dense(features['bboxes'], default_value=0) 

# Since information about shape is lost reshape it 
bboxes = tf.reshape(bboxes, bboxes_shape) 
image = tf.decode_raw(features['image_raw'], tf.uint8) 
image = tf.reshape(image, image_shape) 

注意,「image_raw」的長度是固定功能(有一個元素)和保持類型的值「字節」,但是「字節類型」的值本身可以具有可變大小(它是一串字節,並且可以有許多符號)。 所以「image_raw」是一個帶有「bytes」類型的ONE元素的列表,它可以是超大的。

進一步闡述它是如何工作: 特點是值列表,這些值都具有特定的「類型」。

數據類型爲功能是數據類型爲張量的子集,則有:

  • 的Int64(在內存64位空間)
  • 字節(只要你想佔用的字節數在內存中)
  • 浮動(佔用內存32-​​64位IDK的多少)

您可以檢查here張量數據類型爲s

所以,你可以存儲可變長度數據沒有VarLenFeatures所有(實際上還有你這樣做),但首先你需要把它轉換成字節/串功能,然後對其進行解碼。 這是最常見的方法。

+0

嗨,謝謝,但我仍然對最後一句話感到困惑:'注意圖像是固定長度的類型爲「tf.string」的特徵,其中字符串本身可以具有可變大小「。你能詳細說明一下嗎? – ZijunLost

+0

當然,更新了答案。 – Pietrko

+0

這對你有用嗎? – Pietrko