我在初始基础上用 VGG16 网络构建了一个 Sequential 模型,例如:
from keras.applications import VGG16
conv_base = VGG16(weights='imagenet',
# do not include the top, fully-connected Dense layers
include_top=False,
input_shape=(150, 150, 3))
from keras import models
from keras import layers
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
# the 3 corresponds to the three output classes
model.add(layers.Dense(3, activation='sigmoid'))
我的模型如下所示:
model.summary()
Layer (type) Output Shape Param # ================================================================= vgg16 (Model) (None, 4, 4, 512) 14714688 _________________________________________________________________ flatten_1 (Flatten) (None, 8192) 0 _________________________________________________________________ dense_7 (Dense) (None, 256) 2097408 _________________________________________________________________ dense_8 (Dense) (None, 3) 771 ================================================================= Total params: 16,812,867 Trainable params: 16,812,867 Non-trainable params: 0 _________________________________________________________________
现在,我想获取与我的网络的 vgg16 模型部分关联的层名称。即类似的东西:
layer_name = 'block3_conv1'
filter_index = 0
layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
但是,由于 vgg16 卷积显示为模型并且它的层没有被暴露,我得到了错误:
ValueError:没有这样的层:block3_conv1
我该怎么做呢?