Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow Serving returns 400 Bad Request error

I have 2 models that I am experimenting with, the inputs and outputs are the same for both of them. One is a CNN and the other has only Dense layers. I have saved both models in the same way and Im trying to get predictions from them using TF Serving Rest API.

CNN Model:

inputs = tf.keras.layers.Input(shape=(50,3))
x = tf.keras.layers.Conv1D(filters=12, kernel_size=2, strides=1, padding='valid', activation='relu')(inputs)
x = tf.keras.layers.MaxPooling1D()(x)
x = tf.keras.layers.Conv1D(filters=12, kernel_size=2, strides=1, padding='valid', activation='relu')(x)
x = tf.keras.layers.MaxPooling1D()(x)
x = tf.keras.layers.Conv1D(filters=12, kernel_size=2, strides=1, padding='valid', activation='relu')(x)
x = tf.keras.layers.Flatten()(x)
predictions = tf.keras.layers.Dense(3, activation='softmax')(x)

Dense Model:

inputs = tf.keras.layers.Input(shape=(50,3))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(32, activation='relu')(x)
x = tf.keras.layers.Flatten()(x)
predictions = tf.keras.layers.Dense(3, activation='softmax')(x)

When trying to send a prediction request to the CNN model with TF Serving, I get the correct response, but the same request being sent to the Dense model returns a 400 Bad Request error.

reading = np.random.uniform(0, 1, (50, 3))
r = requests.post('http://localhost:8501/v1/models/my_model:predict', data=json.dumps({'instances': reading.tolist()}))

CNN Model returns correctly:

{"predictions": [[1.78464702e-20, 4.44278494e-33, 4.32330665e-07]]}

Dense Model returns:

{"error": "indices[0] = 2 is not in [0, 2)\\n\\t [[{{node model/dense/Tensordot/GatherV2_1}}]]"}

Any ideas whats happening here?

like image 696
m.umar Avatar asked Aug 02 '26 03:08

m.umar


1 Answers

Looking closely, the Dense model expected input as (-1,50,3).

A simple np.reshape(-1,50,3) solved the problem for me.

like image 74
m.umar Avatar answered Aug 03 '26 15:08

m.umar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!