1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Tensorflow keras generator yielded an element that did not match the expected...

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 08:52.

  1. Stack

    Stack Membro Participativo

    I am trying to make an image classification network with tensorflow keras. I have a directory with subdirectories label_0 and label_1

    def load_data(image_dir, img_size=(128, 128), batch_size=32):
    """
    Load the dataset using ImageDataGenerator from the directories.
    """
    # Create an ImageDataGenerator for rescaling the images and data augmentation
    train_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)

    # Load training data from directories
    train_generator = train_datagen.flow_from_directory(
    image_dir,
    target_size=img_size,
    batch_size=batch_size,
    class_mode='binary'
    )

    # Load validation data from directories
    validation_generator = train_datagen.flow_from_directory(
    image_dir,
    target_size=img_size,
    batch_size=batch_size,
    class_mode='binary'
    )

    return train_generator, validation_generator


    and I create a model with

    def build_cnn(input_shape=(128, 128, 3)):
    """
    Build a simple CNN model.
    """
    model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
    ])

    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    return model


    Now, training seems to work fine:

    train_generator, validation_generator = CNN.load_data(image_dir)
    # Build the model
    model = CNN.build_cnn()
    model.summary()
    # Train the model
    history = model.fit(
    train_generator,
    epochs=10,
    validation_data=validation_generator
    )


    but when I try to load untrained data in another subdirectory to_test:

    def load_unlabeled_data(to_test_dir, img_size=(128, 128)):
    """
    Load the unlabeled images for prediction.
    """
    test_datagen = ImageDataGenerator(rescale=1./255)

    test_generator = test_datagen.flow_from_directory(
    to_test_dir,
    target_size=img_size,
    batch_size=1,
    class_mode=None, # No labels for test data
    shuffle=False
    )

    return test_generator


    and calling it

    test_generator = CNN.load_unlabeled_data(to_test_dir)

    predictions = model.predict(test_generator)


    I get the error


    TypeError: generator yielded an element that did not match the expected structure. The expected structure was (tf.float32,), but the yielded element was [[[[1. 1. 1.] [1. 1. 1.] [1. 1. 1.] ...
    [1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]

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

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

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

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

    ...

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

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

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

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

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

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

    I dont even know where to start and I havent found any similar error, or at least nothing what seems similar to my case. What can I do?

    Continue reading...

Compartilhe esta Página