【问题标题】:AttributeError: 'Tensor' object has no attribute 'to_sparse'AttributeError:“张量”对象没有属性“to_sparse”
【发布时间】:2020-10-22 11:27:48
【问题描述】:

我正在尝试使用 this Plant Leaves dataset(包含 .JPG、.PNG 和 .JPEG 文件的超过 35k 张图像)和 tensorflow 版本 1.14 使用 this tutorial
我遵循了类似的步骤,除了;跳过“使用 keras.preprocessing 加载”部分。我直接跳到“使用 tf.data 加载”部分。但是当我运行 sn-p 时,它向我抛出了这个错误:

File "D:\Softwares\Anaconda\lib\site-packages\tensorflow\python\ops\ragged\ragged_string_ops.py", line 640, in strings_split_v1
    return ragged_result.to_sparse()

AttributeError: 'Tensor' object has no attribute 'to_sparse'

完全错误:
我的代码 sn-p 是:

dir_root=pathlib.Path("D:/Projects/IIIT/LeafID/Dataset/PlantVillage")
list_ds=tf.data.Dataset.list_files(str(dir_root/"*/*"))

def getLabel(fpath):
    parts = tf.strings.split(fpath, os.path.sep)
    return parts[-2] == clnames

def decodeimg(img):
    img=tf.image.decode_jpeg(img,channels=3)
    img=tf.image.convert_image_dtype(img,tf.float32)
    return tf.image.resize(img,[64,64])

def process_path(fpath):
    label=getLabel(fpath)
    img=tf.io.read_file(fpath)
    img=decodeimg(img)
    return img, label

label_ds=list_ds.map(process_path,num_parallel_calls=AUTOTUNE)

这几乎类似于代码here,除了变量。 我不明白这里有什么问题? 图像转换为张量的过程是否有问题?因为当我打开 ragged_string_ops.py 时,它会显示如下内容:

if result_type == "SparseTensor":
      return ragged_result.to_sparse()

T.I.A.

【问题讨论】:

    标签: python image tensorflow loaddata


    【解决方案1】:

    我在该教程中遇到了类似的问题,发现这个问题表明它是某些 tensorflow 版本中 strings.split 的错误(我在 tf 1.14 中看到了这个问题,而 github 问题中的 OP 在 1.15 中看到了它):https://github.com/tensorflow/tensorflow/issues/33623

    从链接的问题 cmets,看起来有两种可能的解决方案 (1) 在字符串周围添加括号,例如c = tf.strings.split(['a b']) 要么 (2) 添加resultType='RaggedTensor',例如tf.strings.split('a b',result_type='RaggedTensor') 返回一个张量(尽管这看起来像是有问题的行为,可能会在更高版本的 tf 中得到纠正)。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 2017-12-06
      • 2018-07-02
      • 2020-06-26
      • 2018-06-20
      相关资源
      最近更新 更多