Categories
Misc

iterate through image_dataset_from_directory keras

How do I iterate through a custom dataset using image_dataset_from_directory? I’m trying to print the labels one by one, but it’s loading the images in batches. Is there any way the data can be loaded in without batches? My labels are one-hot encoded.

CODE:

ds_train = tf.keras.preprocessing.image_dataset_from_directory(
‘dataset’,
labels=’inferred’,
label_mode = “categorical”,
class_names=classes,
color_mode=’grayscale’,
image_size=(28,28),
shuffle=True,
seed=123,
validation_split=0.3,
subset=”training”
)
# load test data
ds_test = tf.keras.preprocessing.image_dataset_from_directory(
‘dataset’,
labels=’inferred’,
label_mode = “categorical”,
class_names=classes,
color_mode=’grayscale’,
image_size=(28,28),
shuffle=True,
seed=123,
validation_split=0.3,
subset=”validation”
)
for x,y in ds_train:
print(y)

OUTPUT:

tf.Tensor(

[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]

[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]

[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]

[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]

[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 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 0. 0.]

[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]

[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]], shape=(24, 10), dtype=float32)

submitted by /u/berimbolo21
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *