Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate a net's FLOPs in CNN [closed]

Tags:

I want to design a convolutional neural network which occupy GPU resource no more than Alexnet.I want to use FLOPs to measure it but I don't know how to calculate it.Is there any tools to do it,please?

like image 939
StalkerMuse Avatar asked Apr 19 '17 08:04

StalkerMuse


People also ask

How does CNN calculate number of FLOPs?

Calculating the FLOPs in a Model ➡️ For a refresher on CNNs, you can check this cheatsheet. To calculate the FLOPs in a model, here are the rules: Convolutions - FLOPs = 2x Number of Kernel x Kernel Shape x Output Shape. Fully Connected Layers - FLOPs = 2x Input Size x Output Size.

How do you calculate FLOPs for a convolution layer?

So when we calculate FLOPs, we only need to multiply the size of the feature map on the basis of the parameters, that is, for a certain convolutional layer, the number of FLOPs is: [(K_h * K_w )* C_{in} + 1][(H_{out}W_{out})* C_{out} ] = [(K_h * K_w * C_{in}) * C_{out} + C_{out}][H_{out}W_{out}]= num_{parameter}*size_{ ...

How are model FLOPs calculated?

It is usually calculated using the number of multiply-add operations that a model performs. Multiply-add operations, as the name suggests, are operations involving multiplication and addition of 2 or more variables. For example, the expression, a * b + c * d, has 2 flops while a * b + c * d + e * f + f * h has 4 flops.

How do you calculate trainable parameters?

To calculate the learnable parameters here, all we have to do is just multiply the by the shape of width m, height n, previous layer's filters d and account for all such filters k in the current layer. Don't forget the bias term for each of the filter.


2 Answers

For online tool see http://dgschwend.github.io/netscope/#/editor . For alexnet see http://dgschwend.github.io/netscope/#/preset/alexnet . This supports most wide known layers. For custom layers you will have to calculate yourself.

like image 86
lnman Avatar answered Sep 25 '22 11:09

lnman


For future visitors, 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 tf
import keras.backend as K
from keras.applications.mobilenet import MobileNet

run_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))
like image 26
Tobias Scheck Avatar answered Sep 22 '22 11:09

Tobias Scheck