繁体   English   中英

AttributeError:“张量”object 没有属性“to_sparse”

[英]AttributeError: 'Tensor' object has no attribute 'to_sparse'

我正在使用带有 tensorflow 版本 1.14 的植物叶子数据集(包含超过 35k 个图像,包括 .JPG、.PNG 和 .JPEG 文件)来尝试本教程
我遵循了类似的步骤,除了; 跳过“使用 keras.preprocessing 加载”部分。 我直接跳到“使用 tf.data 加载”部分。 但是当我运行代码片段时,它给了我这个错误:

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'

完全错误:
在此处输入图像描述 我的代码片段是:

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)

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

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

TIA

I ran into a similar issue working through that tutorial, and found this issue suggesting it's a bug with strings.split in certain tensorflow versions (I saw this issue in tf 1.14, and the OP in the github issue saw it in 1.15): https ://github.com/tensorflow/tensorflow/issues/33623

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

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM