Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从张量字符串加载图像_Python_Tensorflow - Fatal编程技术网

Python 从张量字符串加载图像

Python 从张量字符串加载图像,python,tensorflow,Python,Tensorflow,我已经创建了一个字符串的Tensorflow数据集(其中每个字符串都是dicom图像的路径),我想将预处理函数映射到该数据集。预处理功能应使用pydicom包从dicom文件加载像素阵列。但是当我试图映射一个函数时,虽然我得到了一个属性错误。如何使用下面的函数从张量中读取字符串值?我正在使用Tensorflow 2.0.0和pydicom 1.3.0 AttributeError: in converted code: <ipython-input-12-eff65198c202

我已经创建了一个字符串的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'

pydicom.dcmread(img_路径)
中,
img_路径
是tf.string张量。我认为pydicom不支持读取张量对象

我找到了一个在tensorflow中提供DICOM操作的解决方案。以下代码改编自tf2.0,并在其上进行了测试

def加载和预处理图像(img路径):
_字节=tf.io.read\u文件(img\u路径)
dicom_数据=解码dicom_图像(_字节,dtype=tf.float32)
返回dicom_数据
dicom_文件_列表=['path/to/dicom']
#创建数据集(指向dicom路径的字符串列表)
image\u train\u ds=tf.data.Dataset.from\u tensor\u切片(dicom\u文件列表)
image\u train\u ds=image\u train\u ds.map(加载和预处理图像)

现在有一个Tensorflow io包允许这样做。这相当简单

  • 安装软件包:
  • 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 paths
    image_train_ds = image_train_ds.map(load_and_preprocess_image)
    
    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)