如何在Keras中计算Mobilenet FLOP [英] how to calculate a Mobilenet FLOPs in Keras

查看:1445
本文介绍了如何在Keras中计算Mobilenet FLOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

run_meta = tf.RunMetadata()
enter codwith tf.Session(graph=tf.Graph()) as sess:
K.set_session(sess)


with tf.device('/cpu:0'):
    base_model = MobileNet(alpha=1, weights=None, input_tensor=tf.placeholder('float32', shape=(1,224,224,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))

当我运行上面的代码时,我得到了下面的结果

When I run above code, I got a below result

1,137,481,704 --- 4,253,864

这与本文所述的触发器不同.

This is different from the flops described in the paper.

移动网络: https://arxiv.org/pdf/1704.04861.pdf

ShuffleNet: https://arxiv.org/pdf/1707.01083.pdf

ShuffleNet: https://arxiv.org/pdf/1707.01083.pdf

如何计算论文中所述的确切触发器?

How to calculate exact flops described in the paper?

推荐答案

tl; dr您实际上得到了正确的答案!您只是在比较翻倍数和翻倍数(从本文中得出),因此需要除以二.

如果您使用的是Keras,那么您列出的代码会稍微使事情复杂化...

If you're using Keras, then the code you listed is slightly over-complicating things...

model是任何已编译的Keras模型.我们可以使用以下代码获得模型的触发器.

Let model be any compiled Keras model. We can arrive at the flops of the model with the following code.

import tensorflow as tf
import keras.backend as K


def get_flops():
    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 ....
# You need to have compiled your model before calling this.
print(get_flops())

但是,当我在计算机上查看自己的示例(不是Mobilenet)时,打印出的 total_float_ops为2115 ,当我我只是打印了flops变量:

However, when I look at my own example (not Mobilenet) that I did on my computer, the printed out total_float_ops was 2115 and I had the following results when I simply printed the flops variable:

[...]
Mul                      1.06k float_ops (100.00%, 49.98%)
Add                      1.06k float_ops (50.02%, 49.93%)
Sub                          2 float_ops (0.09%, 0.09%)

很明显,total_float_ops属性考虑了乘法,加法和减法.

It's pretty clear that the total_float_ops property takes into consideration multiplication, addition and subtraction.

然后,我回头看一下MobileNets的示例,简要浏览一下论文,我发现MobileNet的实现是基于参数数量的默认Keras实现:

I then looked back at the MobileNets example, looking through the paper briefly, I found the implementation of MobileNet that is the default Keras implementation based on the number of parameters:

表中的第一个模型与您得到的结果(4,253,864)相匹配,并且多加"结果大约是您得到的flops结果的一半.因此,您有正确的答案,只是您误以为是Mult-Adds(又名乘累加或MAC)的翻牌.

The first model in the table matches the result you have (4,253,864) and the Mult-Adds are approximately half of the flops result that you have. Therefore you have the correct answer, it's just you were mistaking flops for Mult-Adds (aka multiply accumulates or MACs).

如果要计算MAC的数量,只需要将以上代码的结果除以2.

If you want to compute the number of MACs you simply have to divide the result from the above code by two.

这篇关于如何在Keras中计算Mobilenet FLOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆