'Builing Custom Predict Function for Model in Keras

I see Keras allows writing custom layers and training routines. But, for building a custom model in keras, we can build a class that will extend keras.Model class. We can customize the training step with gradient tape, but how about customizing the prediction function that is usually called model.predict?

class MiniInception(tf.keras.Model):
    def __init__(self, num_classes=10):
        super(MiniInception, self).__init__()

    def call(self, input_tensor, training=False, **kwargs):
        # forward pass
        pass

    def build_graph(self, raw_shape): 
        pass

I would like to call our custom predict MiniInception.custom_predict() not MiniInception.predict()? Can we do that please in keras?

Edit:

class MiniInception(tf.keras.Model):
    def __init__(self, num_classes=10):
        super(MiniInception, self).__init__()

    def call(self, input_tensor, training=False, **kwargs):
        # forward pass
        pass

    def build_graph(self, raw_shape): 
        pass

    def custom_predict(self, whatever_inputs_you_want, **kwargs):
        #do whatever you like
 
        predict_results = self.predict(..., **kwargs)

        #do more

        return your_results

Thanks.



Solution 1:[1]

Just create a

def custom_predict(self, whatever_inputs_you_want, **kwargs):
    #do whatever you like
 
    predict_results = self.predict(..., **kwargs)

    #do more

    return your_results

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Möller