1

I am trying to find out the number of FLOPS my model uses using this code that I got online:

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

However, running this code gives me this error:

Traceback (most recent call last):
  File "/Users/Desktop/FYP/Code/Python/code/main.py", line 243, in <module>
    print(get_flops(model))
  File "/Users/Desktop/FYP/Code/Python/code/main.py", line 232, in get_flops
    run_meta = tf.RunMetadata()
AttributeError: module 'tensorflow' has no attribute 'RunMetadata'

How can I bypass this error? I have read online and the only help that I got is to update my tensorflow version. However, this is the most updated version.

Ruven Guna
  • 414
  • 7
  • 25

2 Answers2

1

AttributeError: module 'tensorflow' has no attribute 'RunMetadata'

You have received this error because the code which you have executed is compatible only to Tensorflow 1.x.

I have executed your code successfully without any changes in TF 1.x

%tensorflow_version 1.x
import tensorflow as tf
from keras import backend as K
from tensorflow.keras.models import Sequential 
from tensorflow.keras.layers import Dense 
print(tf.__version__)

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 ....
model = Sequential() 
model.add(Dense(8, activation = 'softmax')) 
print(get_flops(model))

Output:

Using TensorFlow backend.
1.15.2
0

In TF 2.x you have to use tf.compat.v1.RunMetadata instead of tf.RunMetadata

To work your code in TF 2.1.0, i have made all necessary changes that are compliant to TF 2.x

import tensorflow as tf
from tensorflow.keras.models import Sequential 
from tensorflow.keras.layers import Dense 
print(tf.__version__)
def get_flops(model):
    run_meta = tf.compat.v1.RunMetadata()
    opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()

    # We use the Keras session graph in the call to the profiler.
    flops = tf.compat.v1.profiler.profile(graph=tf.compat.v1.keras.backend.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 ....
model = Sequential() 
model.add(Dense(8, activation = 'softmax')) 
print(get_flops(model))

Output:

2.1.0
0

Please refer all compatible symbols for Tensorflow 2 here

0

replace run_meta with the following

run_meta= tf.compat.v1.RunMetadata()

check this link for more information: https://www.tensorflow.org/guide/migrate