简体   繁体   中英

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

I was trying out this tutorial using this Plant Leaves dataset (with over 35k images consisting.JPG, .PNG as well as.JPEG files) with tensorflow version 1.14
And I followed similar steps except; skipping "Load using keras.preprocessing" part. I directly jumped over to "Load using tf.data" part. But when I ran the snippet it threw me this error:

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'

Complete error:
在此处输入图像描述 My code snippet is:

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)

which is almost similar to the code here , except the variables. I couldn't understand what's the problem here? Is there something wrong with the process of images getting converted to tensor? Because when I open ragged_string_ops.py, it shows me something like this:

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

From the linked issue comments, it looks like two possible solutions are (1) adding brackets around your string, eg c = tf.strings.split(['a b']) or (2) adding resultType='RaggedTensor', eg tf.strings.split('a b',result_type='RaggedTensor') to return a tensor (although this looks like it's buggy behavior, and may be corrected in later versions of tf).

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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