首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keras函数替换中间层

Keras函数替换中间层
EN

Stack Overflow用户
提问于 2022-08-22 19:55:14
回答 1查看 92关注 0票数 0

我想在内置的keras模型中用BatchNorm层取代GroupNorm层,例如ResNet50。我正在尝试将节点的层重置为我的新层,但是当我查询model.summary()时,没有什么改变。

代码语言:javascript
复制
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras import layers

model = tf.keras.applications.resnet.ResNet50(include_top=False, weights=None)
channels = 3

for i,layer in enumerate(model.layers[:]):
    if 'bn' in layer.name:
        inbound_nodes = layer.inbound_nodes
        outbound_nodes = layer.outbound_nodes
        
        new_name = layer.name.replace('bn','gn')
        new_layer =  tfa.layers.GroupNormalization(channels)
        new_layer._name = new_name 
        
        for j in range(len(inbound_nodes)):
            inbound_nodes[j].layer = new_layer #set end of node to this layer
        
        for k in range(len(outbound_nodes)):
            new_layer.outbound_nodes.append(outbound_nodes[k])
        
        layer = new_layer
EN

回答 1

Stack Overflow用户

发布于 2022-08-23 09:22:13

我创建了以下代码,对此回答进行了一些更改,以便使if适用于您的情况:

代码语言:javascript
复制
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras import layers, Model 

model = tf.keras.applications.resnet.ResNet50(include_top=False, weights=None)
print(model.summary())
channels = 64

from keras.models import Model

def insert_layer_nonseq(model, layer_regex, insert_layer_factory,
                        insert_layer_name=None, position='after'):

    # Auxiliary dictionary to describe the network graph
    network_dict = {'input_layers_of': {}, 'new_output_tensor_of': {}}

    # Set the input layers of each layer
    for layer in model.layers:
        for node in layer._outbound_nodes:
            layer_name = node.outbound_layer.name
            if layer_name not in network_dict['input_layers_of']:
                network_dict['input_layers_of'].update(
                        {layer_name: [layer.name]})
            else:
                network_dict['input_layers_of'][layer_name].append(layer.name)

    # Set the output tensor of the input layer
    network_dict['new_output_tensor_of'].update(
            {model.layers[0].name: model.input})

    # Iterate over all layers after the input
    model_outputs = []
    for layer in model.layers[1:]:

        # Determine input tensors
        layer_input = [network_dict['new_output_tensor_of'][layer_aux] 
                for layer_aux in network_dict['input_layers_of'][layer.name]]
        if len(layer_input) == 1:
            layer_input = layer_input[0]

        # Insert layer if name matches
        if (layer.name).endswith(layer_regex):
            if position == 'replace':
                x = layer_input
            else:
                raise ValueError('position must be: replace')

            new_layer = insert_layer_factory()
            new_layer._name = '{}_{}'.format(layer.name, new_layer.name)
            x = new_layer(x)
            # print('New layer: {} Old layer: {} Type: {}'.format(new_layer.name, layer.name, position))
            
        else:
            x = layer(layer_input)

        # Set new output tensor (the original one, or the one of the inserted
        # layer)
        network_dict['new_output_tensor_of'].update({layer.name: x})

        # Save tensor in output list if it is output in initial model
        if layer_name in model.output_names:
            model_outputs.append(x)

    return Model(inputs=model.inputs, outputs=model_outputs)

def replace_layer():
  return tfa.layers.GroupNormalization(channels)

model = insert_layer_nonseq(model, 'bn', replace_layer, position="replace")

注意事项:我将您的channels变量从3更改为64,原因如下。

来自于参数的文档 of group

整数,用于组规范化的组数。可以在1,N的范围内,其中N是输入维数。输入维度必须可被组数整除。默认为32。

你应该选择最合适的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73450382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档