Как заморозить граф Keras со слоями BatchNorm - PullRequest
1 голос
/ 25 марта 2019

Я пытаюсь загрузить замороженный график Keras со слоями Batchnorm, но получаю ошибку:

Message: TensorFlow.TFException : Input 0 of node
DenseNet/DenseBlock/ConvBlock/dense_0_0_bn/cond/ReadVariableOp/Switch was
passed float from DenseNet/DenseBlock/ConvBlock/dense_0_0_bn/gamma:0
incompatible with expected resource.

Обычно решение этой проблемы заключается в следующем: keras.backend.set_learning_phase (0) , однако при загрузке графика в другом API (например, TensorflowSharp / TfLite) это не вариант (так как насколько я могу судить).

Вот как я сейчас сохраняю график:

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):

    from tensorflow.python.framework.graph_util import convert_variables_to_constants
    import tensorflow as tf

    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
        output_names += [v.op.name for v in tf.global_variables()]
        # Graph -> GraphDef ProtoBuf
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""

        for node in input_graph_def.node:
            if node.op == 'RefSwitch':
                for index in range(len(node.input)):
                    if 'moving_' in node.input[index]:
                        node.input[index] = node.input[index] + '/read'
            elif node.op == 'AssignSub':
                node.op = 'Sub'
                if 'use_locking' in node.attr: del node.attr['use_locking']

        frozen_graph = convert_variables_to_constants(session, input_graph_def,
                                                      output_names, freeze_var_names)
        return frozen_graph

Можно ли каким-либо образом программно удалить слои Batchnorm перед сохранением, чтобы я мог загрузить модель в среде вне Keras?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...