Keras|Tensorflow 计算模型的FLOPs


最近在研究模型的计算量,发现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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM