OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Error from Keras: ValueError: Must provide at least one structure

  • Thread starter Thread starter MiXen
  • Start date Start date
M

MiXen

Guest
I spotted weird problem with my script to train CNN model on Keras library. When I'm trying to learn simple CNN model during ending first epoch Keras throwing an error saying:

File "File.py", line 86, in generateModelForData(train_dataset, validation_dataset) File "File.py", line 44, in generateModelForData model.fit(train_dataset, batch_size=1, epochs=1000, verbose=1, validation_data=validation_dataset, callbacks=[my_callbacks]) File ".local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler raise e.with_traceback(filtered_tb) from None File ".local/lib/python3.10/site-packages/keras/src/utils/tree.py", line 236, in map_structure raise ValueError("Must provide at least one structure") ValueError: Must provide at least one structure

My model looks like that:


Code:
    model = Sequential()
    model.add(Conv2D(8, (3, 3), activation='relu', input_shape=[32, 128, 3]))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(2, 2))
    model.add(Conv2D(8, (3, 3), activation="relu"))
    model.add(MaxPooling2D(2, 2))
    model.add(Flatten())
    model.add(Dense(250, activation="relu"))
    model.add(BatchNormalization())
    model.add(Dense(120, activation="relu"))
    model.add(Dense(50, activation="relu"))
    model.add(Dense(1, activation="sigmoid", name="output_layer"))
    optimizer = keras.optimizers.Adam(learning_rate=1e-4)
    #
    checkpoint_filepath = '/tmp/checkpoint'
    model_checkpoint_callback = ModelCheckpoint(
        filepath=checkpoint_filepath,
        save_weights_only=True,
        monitor='val_accuracy',
        mode='max',
        save_best_only=True)
    #
    model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', Precision(), Recall(), AUC()])
    my_callbacks = [EarlyStopping(monitor='accuracy', patience=10, mode='max'), model_checkpoint_callback]
    #
    model.summary()
    model.fit(train_dataset, batch_size=1, epochs=1000, verbose=1, validation_data=validation_dataset, callbacks=[my_callbacks])

Datasets is generated in that way:


Code:
image_generator = ImageDataGenerator(rescale=1/255)
train_dataset = image_generator.flow_from_directory(batch_size=5,
                                                 directory='new_heatmaps/train',
                                                 shuffle=True,
                                                 target_size=(32, 128),
                                                 subset="training",
                                                 class_mode='binary')

validation_dataset = image_generator.flow_from_directory(batch_size=5,
                                                 directory='new_heatmaps/test',
                                                 shuffle=True,
                                                 target_size=(32, 128),
                                                 subset="validation",
                                                 class_mode='binary')

What could be the cause of this structure error? When Keras throw some kind of error?
<p>I spotted weird problem with my script to train CNN model on Keras library. When I'm trying to learn simple CNN model during ending first epoch Keras throwing an error saying:</p>
<blockquote>
<p>File "File.py", line 86, in
generateModelForData(train_dataset, validation_dataset)
File "File.py", line 44, in generateModelForData
model.fit(train_dataset, batch_size=1, epochs=1000, verbose=1, validation_data=validation_dataset, callbacks=[my_callbacks])
File ".local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
raise e.with_traceback(filtered_tb) from None
File ".local/lib/python3.10/site-packages/keras/src/utils/tree.py", line 236, in map_structure
raise ValueError("Must provide at least one structure")
ValueError: Must provide at least one structure</p>
</blockquote>
<p>My model looks like that:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code> model = Sequential()
model.add(Conv2D(8, (3, 3), activation='relu', input_shape=[32, 128, 3]))
model.add(BatchNormalization())
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(8, (3, 3), activation="relu"))
model.add(MaxPooling2D(2, 2))
model.add(Flatten())
model.add(Dense(250, activation="relu"))
model.add(BatchNormalization())
model.add(Dense(120, activation="relu"))
model.add(Dense(50, activation="relu"))
model.add(Dense(1, activation="sigmoid", name="output_layer"))
optimizer = keras.optimizers.Adam(learning_rate=1e-4)
#
checkpoint_filepath = '/tmp/checkpoint'
model_checkpoint_callback = ModelCheckpoint(
filepath=checkpoint_filepath,
save_weights_only=True,
monitor='val_accuracy',
mode='max',
save_best_only=True)
#
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', Precision(), Recall(), AUC()])
my_callbacks = [EarlyStopping(monitor='accuracy', patience=10, mode='max'), model_checkpoint_callback]
#
model.summary()
model.fit(train_dataset, batch_size=1, epochs=1000, verbose=1, validation_data=validation_dataset, callbacks=[my_callbacks])</code></pre>
</div>
</div>
</p>
<p>Datasets is generated in that way:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>image_generator = ImageDataGenerator(rescale=1/255)
train_dataset = image_generator.flow_from_directory(batch_size=5,
directory='new_heatmaps/train',
shuffle=True,
target_size=(32, 128),
subset="training",
class_mode='binary')

validation_dataset = image_generator.flow_from_directory(batch_size=5,
directory='new_heatmaps/test',
shuffle=True,
target_size=(32, 128),
subset="validation",
class_mode='binary')</code></pre>
</div>
</div>
</p>
<p>What could be the cause of this structure error? When Keras throw some kind of error?</p>
 

Latest posts

J
Replies
0
Views
1
jbowerbir
J
V
Replies
0
Views
1
Vinicius Martin
V
Top