从嵌套网络获取中间层输出-Keras

时间:2020-06-12 01:01:12

标签: tensorflow keras

我有一个采用VGG16编码器体系结构的U-net网络,具有预训练的imagenet权重。由于我的输入图像是灰度图像,因此在将输入发送到U-net模型之前,我添加了一个深度为3的卷积层。

现在,我正在尝试获取U-net网络中中间层的输出。我创建了一个中间模型,其输出是我感兴趣的图层的输出。这是我的代码:

base_model = sm.Unet('vgg16', encoder_weights='imagenet', classes=1, activation='sigmoid')
inp = Input(shape=(448, 224, 1))
l1 = Conv2D(3, (1,1))(inp)
out = base_model(l1)
model = Model(inp, out)
model.summary()

intermediate_layer_model = Model(inputs=model.layers[0].input,                                   
                               outputs=model.get_layer('model_1').get_layer('center_block2_relu').output)

以下是输出:

Layer (type)                 Output Shape              Param #
=================================================================
input_2 (InputLayer)         (None, 448, 224, 1)       0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 448, 224, 3)       6
_________________________________________________________________
model_1 (Model)              multiple                  23752273
=================================================================
Total params: 23,752,279
Trainable params: 23,748,247
Non-trainable params: 4,032
_________________________________________________________________

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(?, ?, ?, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

在我看来,具有输入层(input_1)的U-net模型存在问题,并且在构建middle_layer_model时未提供此信息。但是,我希望中间模型仅将灰度图像作为输入,而不需要额外的3通道输入。

任何帮助将不胜感激。

0 个答案:

没有答案