0

When run code, I get error:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (63,) + inhomogeneous part.

train = img_Datagen.flow_from_directory(train_dir,
                                       batch_size = 16,
                                       class_mode = 'binary',
                                       target_size = (IMG_HEIGHT, IMG_WIDTH)
                                       )

validation = val_Datagen.flow_from_directory(val_dir,
                                              batch_size = 2,
                                              class_mode = 'binary',
                                              target_size = (IMG_HEIGHT, IMG_WIDTH)
                                            )
history = model.fit(train, epochs = 2, 
                    validation_data = validation,
                    steps_per_epoch = 63,
                    callbacks = [lr],
                    batch_size = 16)
# save best checkpoint as hdf5
model.save('/tmp/best_model3_1.hdf5')

# load the best model
best_model3_1 = tf.keras.models.load_model('/tmp/best_model3_1.hdf5')

# calculate test accuracy using the best model
loss, acc = best_model3_1.evaluate_generator(test)

print("\nTest Loss: {:.2f}".format(loss))
print("Test Accuracy: {:.2f}".format(acc))
print(len(actual_labels))

63
print(len(predicted_labels))

63
actual_labels, predicted_labels = [], []
for _ in range(len(test)):
    test_images, test_labels = next(test)
    actual_labels.append(test_labels)
    predicted_labels.append(best_model3_1.predict(test_images).ravel())

actual_labels = np.array(actual_labels, dtype = float).ravel()
predicted_labels = np.array(predicted_labels, dtype = float).ravel()

Error:
      5     predicted_labels.append(best_model3_1.predict(test_images).ravel())
      6 
----> 7 actual_labels = np.array(actual_labels, dtype = float).ravel()
      8 predicted_labels = np.array(predicted_labels, dtype = float).ravel()

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (63,) + inhomogeneous part.

Why do I get this error message? ValueError: setting an array element with a sequence.

I shoud be fix ????

Thank you.

Roseanne
  • 11
  • 2
  • Hi @Roseanne, this error occurs when the sequences in the `train` are not of the same length. Ensure that the sequences are of same length and kindly refer to [thread1](https://stackoverflow.com/a/72476646/14290697) and [thread2](https://stackoverflow.com/questions/67183501/setting-an-array-element-with-a-sequence-requested-array-has-an-inhomogeneous-sh). Thank you! –  Nov 07 '22 at 14:17

0 Answers0