亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Tensorflow 數據集管道中返回不同長度的數組

在 Tensorflow 數據集管道中返回不同長度的數組

慕虎7371278 2023-08-22 10:42:35
我正在 python 中使用 Tensorflow 進行對象檢測。我想使用張量流輸入管道來加載批量輸入數據。問題是圖像中的對象數量是可變的。想象一下我想做以下事情。注釋是圖像文件名及其包含的邊界框的數組。標簽被排除在外。每個邊界框由四個數字表示。import tensorflow as [email protected]()def prepare_sample(annotation):    annotation_parts = tf.strings.split(annotation, sep=' ')    image_file_name = annotation_parts[0]    image_file_path = tf.strings.join(["/images/", image_file_name])    depth_image = tf.io.read_file(image_file_path)    bboxes = tf.reshape(annotation_parts[1:], shape=[-1,4])    return depth_image, bboxesannotations = ['image1.png 1 2 3 4', 'image2.png 1 2 3 4 5 6 7 8']dataset = tf.data.Dataset.from_tensor_slices(annotations)dataset = dataset.shuffle(len(annotations))dataset = dataset.map(prepare_sample)dataset = dataset.batch(16)for image, bboxes in dataset:  pass在上面的示例中,image1 包含單個對象,而 image2 包含兩個對象。我收到以下錯誤:InvalidArgumentError:無法將張量添加到批次:元素數量不匹配。形狀為:[張量]:[1,4],[批次]:[2,4]這就說得通了。我正在尋找從映射函數返回不同長度數組的方法。我能做些什么?謝謝你!編輯:我想我找到了解決方案;我不再收到錯誤。我dataset.batch(16)改為dataset.padded_batch(16).
查看完整描述

1 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊


dataset.batch(16)更改為 后該錯誤將得到解決dataset.padded_batch(16)。


下面是相同的修改后的代碼。


import tensorflow as tf


@tf.function()

def prepare_sample(annotation):

? ? annotation_parts = tf.strings.split(annotation, sep=' ')

? ? image_file_name = annotation_parts[0]

? ? image_file_path = tf.strings.join(["/images/", image_file_name])

? ? depth_image = tf.io.read_file(image_file_path)

? ? bboxes = tf.reshape(annotation_parts[1:], shape=[-1,4])

? ? return depth_image, bboxes


annotations = ['image1.png 1 2 3 4', 'image2.png 1 2 3 4 5 6 7 8']

dataset = tf.data.Dataset.from_tensor_slices(annotations)

dataset = dataset.shuffle(len(annotations))

dataset = dataset.map(prepare_sample)

dataset = dataset.padded_batch(16)


for image, bboxes in dataset:

? pass



查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 4454 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號