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
def get_flops(model):
if isinstance(model,(keras.engine.functional.Functional,keras.engine.training.Model)):
run_meta=tf.compat.v1.RunMetadata()
opts=tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()
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]
real_model=tf.function(model).get_concrete_function(inputs)
frozen_func,_=convert_variables_to_constants_v2_as_graph(real_model)
flops=tf.compat.v1.profiler.profile(graph=frozen_func.graph,run_meta=run_meta,cmd="scope",options=opts)
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 BotMay 20 at 20:13