Reshape the input for BatchDataset trained model

Issue

I trained my tensorflow model on images after convert it to BatchDataset

  IMG_size = 224
  INPUT_SHAPE = [None, IMG_size, IMG_size, 3] # 4D input

  model.fit(x=train_data,
            epochs=EPOCHES,
            validation_data=test_data,
            validation_freq=1, # check validation metrics every epoch
            callbacks=[tensorboard, early_stopping])


     model.compile(
      loss=tf.keras.losses.CategoricalCrossentropy(), 
      optimizer=tf.keras.optimizers.Adam(), 
      metrics=["accuracy"] 
    )


  model.build(INPUT_SHAPE)

the train_data type is:
tensorflow.python.data.ops.dataset_ops.BatchDataset.

I want to run my model on a single numpy array or tensor constant, but it will be 3D input matrix not 4D as the input TensorShape([224, 224, 3]); how can i reshape it?

Solution

You can expand the dimensions of your image matrix by using this code:

newImage = tf.expand_dims(Original_Image, axis = 0)

then pass it to the predict function, it will work fine.

Answered By – Yusuf Taha

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.