For some reason, I can’t find built-in solutions (not really?) in keras and tensorflow, while on the site https://keras.io/api/applications/ they provide Time (ms) per inference step (CPU), but for some reason they did not describe how they calculated or which function they used.


1 Answer 1

def get_flops(model):
    if isinstance(model,(keras.engine.functional.Functional,keras.engine.training.Model)):
        from tensorflow.python.framework.convert_to_constants import (convert_variables_to_constants_v2_as_graph)
        inputs=[tf.TensorSpec([1]+inp.shape[1:],inp.dtype) for inp in model.inputs]
        return flops.total_float_ops

from https://github.com/tokusumi/keras-flops/blob/master/keras_flops/flops_calculation.py

  • $\begingroup$ Thank you, maybe you also know how to calculate inference time (I want to test and compare h5 and tflite models)? $\endgroup$ May 18 at 13:04
  • $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    May 20 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.