簡體   English   中英

轉換 tf.data.dataset

[英]Transforming a tf.data.dataset

假設我有一個 32*32*3 類型圖像的數據集作為源數據:

<DatasetV1Adapter shapes: {coarse_label: (), image: (32, 32, 3), label: ()}, types: {coarse_label: tf.int64, image: tf.uint8, label: tf.int64}>

序列化數據后,我得到:

<MapDataset shapes: {depth: (), height: (), image_raw: (), label: (), width: ()}, types: {depth: tf.int64, height: tf.int64, image_raw: tf.string, label: tf.int64, width: tf.int64}>

我可以使用這段代碼訪問每個元素:

for i in parsed_image_dataset.take(1):
  j=i['image_raw']
array_shape = e1['image'].numpy().shape
print(np.frombuffer(j.numpy(), dtype = 'uint8').reshape(array_shape))

其中e1是在原始數據get_next使用get_next生成的。 get_next ,正如預期的那樣,打印打印出與預序列化相同的圖像。但是,我可以以某種方式將序列化數據集立即轉換為原始uint8數據集,而不是逐個元素執行此操作嗎?

您可以按照以下步驟在 uint8 中獲取圖像。

創建序列化數據。

list_ds = tf.data.Dataset.list_files("img_dir_path/*")

創建一個函數,它將 file_path 作為參數並以 uint8 格式返回圖像。

def process_img(file_path):
  img = tf.io.read_file(file_path)

  img = tf.image.decode_jpeg(img, channels=3)
  return img

使用 map 函數將上述函數應用於 list_ds 對象中的所有項。

processed_images = list_ds.map(process_img)

processing_images將包含給定圖像目錄的 uint8 格式的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM