Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering two statistical functions with name 'FusedBatchNormV3,flops'! (Previous registration was in register /usr/local/lib/python3.10/dist-packages/tensorflow/python/framework/registry.py:65)" #19

Open
rsarka34 opened this issue Oct 13, 2023 · 1 comment

Comments

@rsarka34
Copy link

KeyError Traceback (most recent call last)
in <cell line: 4>()
2 from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
3
----> 4 from keras_flops import get_flops
5
6 # build model

4 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/framework/registry.py in register(self, candidate, name)
55 if name in self._registry:
56 frame = self._registry[name][_LOCATION_TAG]
---> 57 raise KeyError(
58 "Registering two %s with name '%s'! "
59 "(Previous registration was in %s %s:%d)" %

KeyError: "Registering two statistical functions with name 'FusedBatchNormV3,flops'! (Previous registration was in register /usr/local/lib/python3.10/dist-packages/tensorflow/python/framework/registry.py:65)"

@m-shahpouri
Copy link

m-shahpouri commented Dec 5, 2023

Hello, my friend. According to this answer #17 (comment) you have to go to

/home/name/miniconda3/envs/tf-2.13/lib/python3.9/site-packages/keras_flops/flops_registory.py

directory and comment or remove following lines:

@ops.RegisterStatistics("FusedBatchNormV3", "flops")
@ops.RegisterStatistics("Max", "flops")
@ops.RegisterStatistics("AddV2", "flops")

I hope it helps you.

Maybe better solution

However, I think this code is easier to use instead of editing a package file:

import tensorflow as tf
from tensorflow.python.profiler.model_analyzer import profile
from tensorflow.python.profiler.option_builder import ProfileOptionBuilder

def get_flops(model):
  forward_pass = tf.function(model.call, input_signature=[tf.TensorSpec(shape=(1,) + model.input_shape[1:])])
  graph_info = profile(forward_pass.get_concrete_function().graph, options=ProfileOptionBuilder.float_operation())
  flops = graph_info.total_float_ops
  return flops

HEIGHT = 224
WIDTH = 224
CHANNELS = 3

model = tf.keras.Sequential([
    tf.keras.Input(shape=(HEIGHT, WIDTH, CHANNELS), name='model_input'),
    tf.keras.layers.Conv2D(filters=3, kernel_size=(1, 1), padding='same', groups=3, activation='linear', name='DepthwiseConv2D_1'),
    tf.keras.layers.Dense(units=1, activation='relu', name='PointwiseConv2D_1')
    ])

# model.compile(optimizer='adam', loss='bce', metrics=['accuracy'])

flops = get_flops(model)
macs = flops / 2
print(f"MACs: {macs:,}")
print(f"FLOPs: {flops:,}")
MACs: 250,880
FLOPs: 501,760

Source: tensorflow/tensorflow#32809 (comment)

Just keep in mind that the source computes MACs tensorflow/tensorflow#32809 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants