Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 可视化VGG16的特征图_Python_Tensorflow_Keras - Fatal编程技术网

Python 可视化VGG16的特征图

Python 可视化VGG16的特征图,python,tensorflow,keras,Python,Tensorflow,Keras,我正在使用tf2.3.0和python3.6 下面是我加载模型的方式 from __future__ import absolute_import, division, print_function import tensorflow as tf import tensorflow.keras as keras import tensorflow.keras.layers as layers import numpy as np from tensorflow.keras.application

我正在使用tf2.3.0和python3.6

下面是我加载模型的方式

from __future__ import absolute_import, division, print_function
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
import numpy as np
from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications import resnet
import matplotlib.pyplot as plt

def create_vgg():
    vgg16 = VGG16(include_top=False, weights='imagenet',input_shape=(224,224,3))
    inputs = keras.Input(shape=(224,224,3))
    bn = layers.BatchNormalization()(inputs)
    x = vgg16(bn)
    x = layers.Conv2D(256,kernel_size=(1,1),activation='relu',name='conv2D',kernel_regularizer=keras.regularizers.l2(0.001))(x)
    flat_out = layers.Flatten()(x)
    h1 = layers.Dense(1024,activation='relu',name='fc1',kernel_regularizer=keras.regularizers.l2(0.001))(flat_out)
    h2 = layers.Dropout(0.5)(h1)
    h3 = layers.Dense(32,activation='relu',name='fc2',kernel_regularizer=keras.regularizers.l2(0.001))(h2)
    h4 = layers.Dropout(0.5)(h3)
    new_out = layers.Dense(1,activation='sigmoid',name='prediction')(h4)
    return keras.Model(inputs,new_out)

vgg = create_vgg()
vgg.load_weights('F:\\logs\\best_weight.h5')
vgg.compile(optimizer=keras.optimizers.Adam(5e-6),
            loss="binary_crossentropy", metrics=['accuracy'])

我想可视化前16层的所有特征图:

output_layers = [layer.output for layer in vgg.layers[:16]]
activation_model = keras.models.Model(inputs=vgg.input,outputs=output_layers)
我在执行最后一行时出错:

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


如何修复此问题

这可能是由于加载的权重大小不匹配造成的,或者加载的权重可能缺少一些有关权重的数据最终模型中有两个输入层。您需要去掉预训练vgg网络附带的输入层,并用自己的输入层替换它。