最近在研究模型的計算量,發現Pytorch有庫可以直接計算模型的計算量,所以需要一個一個Keras和Tensorflow可以用的,直接把Model接入到函數中,print一下就可以計算出FLOPs
FLOPS:注意全大寫,是floating point operations per second的縮寫,意指每秒浮點運算次數,理解為計算速度。是一個衡量硬件性能的指標。
FLOPs:注意s小寫,是floating point operations的縮寫(s表復數),意指浮點運算數,理解為計算量。可以用來衡量算法/模型的復雜度。
對於計算量主要有Madds和MFlops兩個概念。shufflenet的論文用的是Flops,Mobilenet用的是Madds,Flops應該是Madds的兩倍,具體可參考
https://blog.csdn.net/shwan_ma/article/details/84924142
https://www.zhihu.com/question/65305385/answer/451060549
計算函數如下:
import tensorflow as tf import keras.backend as K def get_flops(model): run_meta = tf.RunMetadata() opts = tf.profiler.ProfileOptionBuilder.float_operation() # We use the Keras session graph in the call to the profiler. flops = tf.profiler.profile(graph=K.get_session().graph, run_meta=run_meta, cmd='op', options=opts) return flops.total_float_ops # Prints the "flops" of the model. # .... Define your model here .... print(get_flops(model))
貼一個Mask_RCNN的計算結果
Profile: node name | # float_ops Mul 98.06m float_ops (100.00%, 44.68%) Sum 57.48m float_ops (55.32%, 26.19%) Square 45.05m float_ops (29.14%, 20.52%) AddN 9.37m float_ops (8.61%, 4.27%) Sub 3.13m float_ops (4.34%, 1.43%) AssignSub 3.13m float_ops (2.91%, 1.43%) Add 3.12m float_ops (1.49%, 1.42%) Rsqrt 61.25k float_ops (0.06%, 0.03%) Maximum 30.78k float_ops (0.03%, 0.01%) RealDiv 24.98k float_ops (0.02%, 0.01%) RsqrtGrad 16.38k float_ops (0.01%, 0.01%) GreaterEqual 4.10k float_ops (0.00%, 0.00%) Neg 108 float_ops (0.00%, 0.00%) AssignAdd 17 float_ops (0.00%, 0.00%) Equal 10 float_ops (0.00%, 0.00%) Log 6 float_ops (0.00%, 0.00%) Greater 3 float_ops (0.00%, 0.00%) Pow 3 float_ops (0.00%, 0.00%) Less 2 float_ops (0.00%, 0.00%) ======================End of Report========================== flops = 219.492156MFlops