How To Calculate A Net's Flops In CNN

 if you use Keras and TensorFlow as Backend then you can try the following example. It calculates the FLOPs for the MobileNet.

import tensorflow as tfimport keras.backend as Kfrom keras.applications.mobilenet import MobileNetrun_meta = tf.RunMetadata()with tf.Session(graph=tf.Graph()) as sess:    K.set_session(sess)    net = MobileNet(alpha=.75, input_tensor=tf.placeholder('float32', shape=(1,32,32,3)))    opts = tf.profiler.ProfileOptionBuilder.float_operation()        flops = tf.profiler.profile(sess.graph, run_meta=run_meta, cmd='op', options=opts)    opts = tf.profiler.ProfileOptionBuilder.trainable_variables_parameter()        params = tf.profiler.profile(sess.graph, run_meta=run_meta, cmd='op', options=opts)    print("{:,} --- {:,}".format(flops.total_float_ops, params.total_parameters))