pyhton 计算flop_如何在Keras中计算Mobilenet FLOP

本文介绍如何使用TensorFlow和Keras计算模型的浮点运算次数(FLOPs)及参数量,并解释了FLOPs与论文中所提Mult-Adds的区别。通过一个MobileNet模型的例子,展示了实际操作步骤。
TensorFlow-v2.15

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

bd96500e110b49cbb3cd949968f18be7.png

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.

How to calculate exact flops described in the paper?

解决方案

tl;dr You've actually got the right answer! You are simply comparing flops with multiply accumulates (from the paper) and therefore need to divide by two.

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

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())

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%)

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

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:

8ec1f41346109b8342b080fb23e4c3d5.png

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).

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

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值