我想计算一下所使用的ML模型中的失败.当我试图计算非常复杂的模型时,我遇到了错误.

对于EfficientNet型号,我收到以下错误:

ValueErr或: Unknown layer: FixedDropout. Please ensure this object is passed to the `custom_objects` argument. See https://www.tens或flow.或g/guide/keras/save_and_serialize#registering_the_custom_object f或 details.
​

用于计算Flop的函数:

1)

def get_flops(model, batch_size=None):
    if batch_size is None:
        batch_size = 1

    real_model = tf.function(model).get_concrete_function(tf.Tens或Spec([batch_size] + model.inputs[0].shape[1:], model.inputs[0].dtype))
    frozen_func, graph_def = convert_variables_to_constants_v2_as_graph(real_model)

    run_meta = tf.compat.v1.RunMetadata()
    opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()
    flops = tf.compat.v1.profiler.profile(graph=frozen_func.graph,
                                            run_meta=run_meta, cmd='op', options=opts)
    return flops.total_float_ops

2)

def get_flops(model_h5_path):
    session = tf.compat.v1.Session()
    graph = tf.compat.v1.get_default_graph()
        

    with graph.as_default():
        with session.as_default():
            model = tf.keras.models.load_model(model_h5_path)

            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=graph,
                                                  run_meta=run_meta, cmd='op', options=opts)
        
            return flops.total_float_ops

On the contrary, I am able to calculate the FLOPS f或 models like Resnets, just that it is not possible f或 bit complex models. How can I mitigate the issue?

推荐答案

在第一个函数中,您将使用TensorFlow中的convert_variables_to_constants_v2_as_graph函数将模型转换为图形.此函数有一个custom_objects参数,可用于传递模型使用的自定义层.您可以将FixedDropout层添加到此参数以修复您看到的错误.

以下是如何使用CUSTOM_OBJECTS参数修复错误的示例:

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2_as_graph

# Define the custom layer class
class FixedDropout(tf.keras.layers.Dropout):
  def call(self, inputs, training=None):
    return inputs

def get_flops(model, batch_size=None):
    if batch_size is None:
        batch_size = 1

    real_model = tf.function(model).get_concrete_function(tf.TensorSpec([batch_size] + model.inputs[0].shape[1:], model.inputs[0].dtype))

    # Pass the custom layer class to the custom_objects parameter
    frozen_func, graph_def = convert_variables_to_constants_v2_as_graph(real_model, custom_objects={'FixedDropout': FixedDropout})

    run_meta = tf.compat.v1.RunMetadata()
    opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()
    flops = tf.compat.v1.profiler.profile(graph=frozen_func.graph,
                                            run_meta=run_meta, cmd='op', options=opts)
    return flops.total_float_ops

Python相关问答推荐

为什么这个带有List输入的简单numba函数这么慢

从groupby执行计算后创建新的子框架

如何在Python数据框架中加速序列的符号化

对象的`__call__`方法的setattr在Python中不起作用'

优化器的运行顺序影响PyTorch中的预测

Asyncio:如何从子进程中读取stdout?

在Django admin中自动完成相关字段筛选

从嵌套的yaml创建一个嵌套字符串,后面跟着点

关于两个表达式的区别

在用于Python的Bokeh包中设置按钮的样式

使用类型提示进行类型转换

使用嵌套对象字段的Qdrant过滤

numpy数组和数组标量之间的不同行为

如何训练每一个pandaprame行的线性回归并生成斜率

Python协议不兼容警告

为罕见情况下的回退None值键入

如何在Django模板中显示串行化器错误

更新包含整数范围的列表中的第一个元素

Fake pathlib.使用pyfakefs的类变量中的路径'

是否从Python调用SHGetKnownFolderPath?