为了账号安全,请及时绑定邮箱和手机立即绑定

从张量字符串加载图像

从张量字符串加载图像

一只斗牛犬 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 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号