Home > Blockchain >  TensorFlow 2.9.1 model.predict error when using array of Tensors as input
TensorFlow 2.9.1 model.predict error when using array of Tensors as input

Time:08-16

From according to Keras Sequential Model.predict() documentation, the model can use a variety of input forms, including:

A TensorFlow tensor, or a list of tensors (in case the model has multiple inputs).

I want to test model by using image sets from image_dataset_from_directory.

Test dataset_Code

However I get the following error:

ValueError: Layer "model" expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 224, 224, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=int32>]

The shapes are as follows:

  • image_batch <KerasTensor: shape=(7, 224, 224, 3) dtype=float32
  • label_batch tf.Tensor([0 0 1 1 2 2 2], shape=(7,), dtype=int32)

How can I solve this ValueError ?

Thank you

CodePudding user response:

just do this


preds=new_model.predict(Test_dataset)
for i, p in enumerate(preds):
    file=os.path.basename(Test_dataset.filenames[i])
    index=np.argmax(p)
    pred_klass=class_names[index]
    print(f'{file:^30s}{pred_class:^6s}')

this will predict all the test files

  • Related