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

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

從張量字符串加載圖像

從張量字符串加載圖像

一只斗牛犬 2022-06-22 19:01:18
我創建了一個字符串的 Tensorflow 數據集(其中每個字符串都是 dicom 圖像的路徑),我想將預處理函數映射到數據集。預處理函數應該使用pydicom包從 dicom 文件加載像素數組。但是當我嘗試映射一個函數時,雖然我得到一個屬性錯誤。如何使用如下函數從張量中讀取字符串值?我正在使用 Tensorflow 2.0.0 和 pydicom 1.3.0。AttributeError: in converted code:    <ipython-input-12-eff65198c202>:12 load_and_preprocess_image  *        dicom_data = pydicom.dcmread(img_path)    /opt/conda/lib/python3.6/site-packages/pydicom/filereader.py:849 dcmread  *        dataset = read_partial(fp, stop_when, defer_size=defer_size,    /opt/conda/lib/python3.6/site-packages/pydicom/filereader.py:651 read_partial  *        preamble = read_preamble(fileobj, force)    /opt/conda/lib/python3.6/site-packages/pydicom/filereader.py:589 read_preamble  *        preamble = fp.read(128)    AttributeError: 'Tensor' object has no attribute 'read'這是我創建數據集并將預處理函數映射到它的代碼。def load_and_preprocess_image(img_path):    """ Load image, resize, and normalize the image"""    dicom_data = pydicom.dcmread(img_path))    image = tf.convert_to_tensor(dicom_data.pixel_array, dtype=tf.float32)    return image# Create dataset (list of strings that lead to dicom paths)image_train_ds = tf.data.Dataset.from_tensor_slices(dicom_files_list)# Map a preprocessing function to list of dicom pathsimage_train_ds = image_train_ds.map(load_and_preprocess_image)
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

有一個 Tensorflow-io 包現在允許這樣做。它相當簡單。


安裝包:

pip install -q tensorflow-io

import tensorflow_io as tfio


def load_and_preprocess_image(img_path):

    _bytes = tf.io.read_file(img_path)

    dicom_data = tfio.image.decode_dicom_image(_bytes, dtype=tf.float32)

    return dicom_data


dicom_files_list = ['path/to/dicom']


# Create dataset (list of strings that lead to dicom paths)

image_train_ds = tf.data.Dataset.from_tensor_slices(dicom_files_list)

image_train_ds = image_train_ds.map(load_and_preprocess_image)


查看完整回答
反對 回復 2022-06-22
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

在 中pydicom.dcmread(img_path),img_path是 tf.string 張量。我不認為pydicom支持讀取張量對象。


我找到了一種解決方法,它是在 tensorflow 中提供 DICOM 操作的 gradient_decode_dicom 。以下代碼改編自此 colab,并在 tf2.0 上進行了測試。


def load_and_preprocess_image(img_path):

    _bytes = tf.io.read_file(img_path)

    dicom_data = decode_dicom_image(_bytes, dtype=tf.float32)

    return dicom_data


dicom_files_list = ['path/to/dicom']


# Create dataset (list of strings that lead to dicom paths)

image_train_ds = tf.data.Dataset.from_tensor_slices(dicom_files_list)

image_train_ds = image_train_ds.map(load_and_preprocess_image)


查看完整回答
反對 回復 2022-06-22
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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