我試圖將不同大小的圖像保存到tf記錄中。我發現即使圖像尺寸不同,我仍然可以使用FixedLenFeature
加載它們。Tensorflow VarLenFeature vs FixedLenFeature
通過檢查FixedLenFeature
和VarLenFeature
上的文檔,我發現差異似乎是VarLenFeauture
返回一個稀疏張量。
任何人都可以說明一些應該使用的情況FixedLenFeature
或VarLenFeature
?
我試圖將不同大小的圖像保存到tf記錄中。我發現即使圖像尺寸不同,我仍然可以使用FixedLenFeature
加載它們。Tensorflow VarLenFeature vs FixedLenFeature
通過檢查FixedLenFeature
和VarLenFeature
上的文檔,我發現差異似乎是VarLenFeauture
返回一個稀疏張量。
任何人都可以說明一些應該使用的情況FixedLenFeature
或VarLenFeature
?
您可以加載圖像,可能是因爲您使用功能類型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元素的列表,它可以是超大的。
進一步闡述它是如何工作: 特點是值列表,這些值都具有特定的「類型」。
數據類型爲功能是數據類型爲張量的子集,則有:
您可以檢查here張量數據類型爲s。
所以,你可以存儲可變長度數據沒有VarLenFeatures
所有(實際上還有你這樣做),但首先你需要把它轉換成字節/串功能,然後對其進行解碼。 這是最常見的方法。
你用什麼功能保存圖像數據? tf.train.BytesList? – Pietrko
是的,我正在使用tf.train.Byteslist – ZijunLost