I am using map
function to apply preprocessing on dataset in order to read and extract labels from file paths using tf.data
but it returns the same label for all images
the file path is on the following shape where A is the label
/content/drive/MyDrive/prom02/dataset/train/A0_jpg.rf.292a080422ba984985192f413101af41.jpg
images_ds = tf.data.Dataset.list_files('/content/drive/MyDrive/prom02/dataset/train/*', shuffle=True)
images_ds = images_ds.shuffle(200)
train_size = int(image_count*0.8)
train_ds = images_ds.take(train_size)
test_ds = images_ds.skip(train_size)
len(train_ds),len(test_ds)
def hot_encode(label):
import string
alphabet = list(string.ascii_uppercase)
i=0
while i<26:
if label==alphabet[i]:
label=i
break
i+=1
label=tf.one_hot(label,26)
return label
def get_label(file_path):
import os
label=(str(file_path).split('/')[-1][0])
label= hot_encode(label)
return label
def scale(image,label):
return image/255, label
def process_image(file_path):
label = tf.cast(get_label(file_path),tf.float32)
img = tf.io.read_file(file_path) # load the raw data from the file as a string
img = tf.image.decode_jpeg(img)
img = tf.image.resize(img, [320, 320])
return tf.cast(img,tf.float32), label
train_ds = train_ds.map(process_image).map(scale).batch(32).cache().prefetch(tf.data.AUTOTUNE)
test_ds = test_ds.map(process_image).map(scale).batch(32).prefetch(tf.data.AUTOTUNE)
for img,label in train_ds.take(1):
print(label.numpy())
the result is always the encoded tensor of letter T as label while when i use
for img in images_ds.take(2):
print(get_label(img.numpy()))
it returns the true label