如何从下面的架构中提取瓶颈层?

发布于 2025-01-16 05:44:45 字数 1691 浏览 0 评论 0原文

我创建了一个模型(如下)。训练后,我想从该模型的瓶颈层获取输出张量。 因此,我尝试创建提取层的模型并使用该模型进行预测。

from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation

nstrides = (1,1)
inputs = layers.Input(imshape)

conv01 = layers.Conv2D(32, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(inputs)
conv1 = layers.Conv2D(32, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv01)
pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)

.
.


#block4
conv04 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(pool3)
conv4 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv04)
pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

#bottlneck
conv05 = layers.Conv2D(512, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(pool4)
conv5 = layers.Conv2D(512, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv05)
upconv5 = layers.Conv2DTranspose(256, kernel_size=(2, 2), 
                       strides = (2,2))(conv5)

#upblock 1
conc6 = layers.concatenate([upconv5, conv4])
conv06 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conc6)
conv6 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv06)
up7 = layers.Conv2DTranspose(126, kernel_size=(2, 2), 
 strides = (2,2))(conv7)
.
.
.

#combine the model together
model = Model(inputs, outputs)

I have created an model (down below). And after training, I want to get the output tensor from the bottleneck layers of this model.
So I am trying to create a model of the extracted layers and use this model for predicting.

from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation

nstrides = (1,1)
inputs = layers.Input(imshape)

conv01 = layers.Conv2D(32, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(inputs)
conv1 = layers.Conv2D(32, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv01)
pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)

.
.


#block4
conv04 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(pool3)
conv4 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv04)
pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

#bottlneck
conv05 = layers.Conv2D(512, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(pool4)
conv5 = layers.Conv2D(512, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv05)
upconv5 = layers.Conv2DTranspose(256, kernel_size=(2, 2), 
                       strides = (2,2))(conv5)

#upblock 1
conc6 = layers.concatenate([upconv5, conv4])
conv06 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conc6)
conv6 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv06)
up7 = layers.Conv2DTranspose(126, kernel_size=(2, 2), 
 strides = (2,2))(conv7)
.
.
.

#combine the model together
model = Model(inputs, outputs)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2025-01-23 05:44:45

首先,为了找到所需的层(即新的输出张量),您可以首先执行

for i, layer in enumerate(model.layers):
    print(i, layer.name)
...
...
12 max_pooling2d_15
13 conv2d_65
14 conv2d_66
15 conv2d_transpose_12
16 concatenate_12
17 conv2d_67
...
...

此处,从 1315 的层索引来自于瓶颈层你的模型。如果你想从这个瓶颈层获取输出张量,你可以这样做:

new_model = Model(model.input, 
                model.get_layer(index=15).output)
# or, 
new_model = Model(model.input,
                model.get_layer(name='conv2d_transpose_12').output)

两者都是相同的,第一个是按index,第二个是按层name

First, in order to locate the desired layer which would be the new output tensor, you can first do

for i, layer in enumerate(model.layers):
    print(i, layer.name)
...
...
12 max_pooling2d_15
13 conv2d_65
14 conv2d_66
15 conv2d_transpose_12
16 concatenate_12
17 conv2d_67
...
...

Here, the layer index from 13 to 15 is from the bottleneck layer of your model. If you want to get the output tensor from this bottleneck layer, you can do:

new_model = Model(model.input, 
                model.get_layer(index=15).output)
# or, 
new_model = Model(model.input,
                model.get_layer(name='conv2d_transpose_12').output)

Both are the same, the first one is by index and the second one is by layer name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文