Tensorboard图孤立层

问题描述 投票:1回答:1

[建立包括转移学习的模型(来自VGG-16)。我遇到这种奇怪的行为。 Tensorboard图显示了在分离点上方不是新模型而是新模型一部分的层,它们只是悬垂在那儿。

enter image description here


[进一步调查时,model.summary()不会显示这些图层,model.get_layer("block4_conv1")也无法找到它们,而keras tf.keras.utils.plot_model也不会显示它们。但是如果它们不属于图形,那么张量板将如何知道它们?

为了建立新模型,我使用了推荐的方法。

模型第一阶段:

    vgg_input_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_tensor=x)
    final_vgg_kayer = vgg_input_model.get_layer("block3_pool")
    input_model = tf.keras.Model(inputs=vgg_input_model.inputs, outputs=final_vgg_kayer.output)
    input_model.trainable = True

    x = tf.keras.layers.Conv2D(512, 1, padding="same", activation='relu', name="stage0_final_conv1")(input_model.output)
    x = tf.keras.layers.Conv2D(512, 1, padding="same", activation='relu', name="stage0_final_conv2")(x)
    x = tf.keras.layers.Conv2D(256, 1, padding="same", activation='relu', name="stage0_final_conv3")(x)
    x = tf.keras.layers.Conv2D(128, 1, padding="same", activation='relu', name="stage0_final_conv4")(x)

TF:2.1 (nightly-2.x)
PY:3.5
Tensorboard: 2.1.0a20191124
tensorflow tensorboard
1个回答
0
投票

[尝试了多种方法后,我得出结论,建议的方法是错误的。进行model_b=tf.keras.Model(inputs=model_a.inputs,outputs=model_a.get_layet("some_layer").output)会导致model_a的图层悬空。在两者之间使用tf.keras.backend.clear_session()可能会清除keras图,但随后将tensorboard的图留空。

我发现的最好的解决方案是所需模型的config + weights逐层复制。并在新模型中重建连接。这样,在两个模型之间的Keras图中就没有任何关系。(对于像VGG这样的顺序模型来说很简单,但对于像ResNet这样的东西可能会更困难]]


示例代码:

tf.keras.backend.clear_session()

input_shape = (368, 368, 3)  #only the input shape is shared between the models
#transfer learning model definition
input_layer_vgg = tf.keras.layers.Input(shape=input_shape)
vgg_input_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_layer_vgg)
name_last_layer = "block3_pool" #the last layer to copy

tf.keras.backend.clear_session() #clean the graph from the transfer learning model

input_layer = tf.keras.layers.Input(shape=input_shape) #define the input layer for the first model
x=input_layer
for layer in vgg_input_model.layers[1:]:    #copy over layers, without the other input layer
    config=layer.get_config()  #get config
    weights=layer.get_weights() #get weights
    #print(config)
    copy_layer=type(layer).from_config(config) #create the new layer from config
    x=copy_layer(x) #connect to previous layers,
                    #required for the proper sizing of the layer,
                    #set_weights will not work without it
    copy_layer.set_weights(weights)    

    if layer.name == name_last_layer:
        break
del vgg_input_model

input_model=tf.keras.Model(inputs=input_layer,outputs=x) #create the new model,
                                                        #if needed x can be used further doen the line
© www.soinside.com 2019 - 2024. All rights reserved.