I wanted to add another example of data preprocessing while in inference:
def get_inference_model(model):
inputs = tf.keras.Input((543, 3), dtype=tf.float32, name=“inputs”)
vector = tf.image.resize(inputs, (CFG.sequence_length, 543))
vector = tf.where(tf.math.is_nan(vector), tf.zeros_like(vector), vector)
vector = tf.expand_dims(vector, axis=0)
vector = model(vector)
output = tf.keras.layers.Activation(activation="linear", name="outputs")(vector)
inference_model = tf.keras.Model(inputs=inputs, outputs=output)
inference_model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=["accuracy"])
return inference_model
This example presents a very interesting feature of tensorflow:
tf.where(…)
It allows you to add conditionals when doing preprocessing. Check this out HERE
Another powerful command you can find in tensorflow is ‘cond’. You can read about it HERE. With this command you can effectively create conditions to pre process your data in one way or another, supported by functions. And the nice thing about it is that it would all be packed in your inference model.