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

Hello, I’m trying to load cifar dataset

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
from tensorflow.keras import datasets, layers
from glob import glob
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_paths = glob('dataset/cifar/train/*.png')
test_paths = glob('dataset/cifar/train/*.png')

def get_label_name (path):
    lbl_name = path.split('_')[-1].replace('.png','')
    return lbl_name

classes = np.unique([get_label_name (path) for path in train_paths])

def onehot_encoding(label_name):
    onehot_encoding = tf.cast(classes == label_name, tf.uint8)
    return onehot_encoding

def read_dataset(path):
    gfile = tf.io.read_file(path)
    image = tf.io.decode_image(gfile)

class_name = get_label_name(path)
label = onehot_encoding(class_name)
return image, label

train_dataset = tf.data.Dataset.from_tensor_slices(train_paths)
train_dataset = train_dataset.map(read_dataset)
train_dataset = train_dataset.batch(batch_size=32)
train_dataset = train_dataset.shuffle(buffer_size=len(train_paths))
train_dataset = train_dataset.repeat()

However, error message ’ AttributeError: ‘Tensor’ object has no attribute ‘split’’ comes out.
Is there any way to solve this problem?
I am looking forward to see an help. Thanks in advance.
Kind regards,
Yoon Ho

Issue with this line class_name = get_label_name(path), you are trying to get the class_name but its giving Tensor value. It should be list of classes.

Find the below working code,

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘2’
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
from tensorflow.keras import datasets, layers
from glob import glob
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_paths = glob(‘dataset/cifar/train/.png’)
test_paths = glob('dataset/cifar/train/
.png’)

def get_label_name (path):
lbl_name = path.split(’/’)[-1].replace(’.png’,’’)
return lbl_name

classes = np.unique([get_label_name (path) for path in train_paths])
classes

def onehot_encoding(label_name):
onehot_encoding = tf.cast(classes == label_name, tf.uint8)
return onehot_encoding

def read_dataset(path):
gfile = tf.io.read_file(path)
image = tf.io.decode_image(gfile)

    class_name = [get_label_name (path) for path in train_paths]
    label = onehot_encoding(class_name)
    return image, label

train_dataset = tf.data.Dataset.from_tensor_slices(train_paths)
train_dataset = train_dataset.map(read_dataset)
train_dataset = train_dataset.batch(batch_size=32)
train_dataset = train_dataset.shuffle(buffer_size=len(train_paths))
train_dataset = train_dataset.repeat()

2 Likes