【问题标题】:tensorflow model gives "graph disconnected" errortensorflow 模型给出“图形断开连接”错误
【发布时间】:2022-01-18 15:21:43
【问题描述】:

我正在尝试/摆弄/学习一些小的 ML 问题。

我有一个基于预训练卷积基础的加载模型,其中包含一些自训练的密集层(有关模型详细信息,请参见下文)。

我想尝试在模型上应用一些可视化,例如激活和 Grad CAM 可视化 (https://www.statworx.com/de/blog/erklaerbbarkeit-von-deep-learning-modellen-mit-grad-cam/)。但我做不到。

我试图创建一个基于我的新模型(如文章中所示)

grad_model = tf.keras.models.Model(model.inputs,
                                   [model.get_layer('vgg16').output,
                                    model.output])

但这已经失败并出现错误:

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

我不明白这是什么意思。该模型确实有效(我可以对其进行评估并进行预测)。 如果我从输出列表中省略 model.get_layer('vgg16').output,调用不会失败,但当然,这是可视化所必需的。

我做错了什么?

在我从头开始构建和训练的模型中,我能够创建一个以激活作为输出的类似模型,但在这里我得到了这些错误。

我的模特的详细信息

使用以下代码创建模型,然后训练并保存。

from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers


conv_base  = keras.applications.vgg16.VGG16(
    weights="vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5",
    include_top=False)
conv_base.trainable = False
data_augmentation = keras.Sequential(
    [
        layers.experimental.preprocessing.RandomFlip("horizontal"),
        layers.experimental.preprocessing.RandomRotation(0.1),
        layers.experimental.preprocessing.RandomZoom(0.2),
    ]
)

inputs = keras.Input(shape=(180, 180, 3))
x = data_augmentation(inputs)
x = conv_base(x)
x = layers.Flatten()(x)
x = layers.Dense(256)(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs, outputs)
model.compile(loss="binary_crossentropy",
              optimizer="rmsprop",
              metrics=["accuracy"])

稍后加载:

model = keras.models.load_model("myModel.keras")
print(model.summary())
print(model.get_layer('sequential').summary())
print(model.get_layer('vgg16').summary())


输出:

Model: "functional_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_6 (InputLayer)         [(None, 180, 180, 3)]     0         
_________________________________________________________________
sequential (Sequential)      (None, 180, 180, 3)       0         
_________________________________________________________________
vgg16 (Functional)           (None, None, None, 512)   14714688  
_________________________________________________________________
flatten_1 (Flatten)          (None, 12800)             0         
_________________________________________________________________
dense_2 (Dense)              (None, 256)               3277056   
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 257       
=================================================================
Total params: 17,992,001
Trainable params: 10,356,737
Non-trainable params: 7,635,264
_________________________________________________________________
None
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
random_flip (RandomFlip)     (None, 180, 180, 3)       0         
_________________________________________________________________
random_rotation (RandomRotat (None, 180, 180, 3)       0         
_________________________________________________________________
random_zoom (RandomZoom)     (None, 180, 180, 3)       0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
None
Model: "vgg16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         [(None, None, None, 3)]   0         
_________________________________________________________________
block1_conv1 (Conv2D)        multiple                  1792      
_________________________________________________________________
block1_conv2 (Conv2D)        multiple                  36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   multiple                  0         
_________________________________________________________________
block2_conv1 (Conv2D)        multiple                  73856     
_________________________________________________________________
block2_conv2 (Conv2D)        multiple                  147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   multiple                  0         
_________________________________________________________________
block3_conv1 (Conv2D)        multiple                  295168    
_________________________________________________________________
block3_conv2 (Conv2D)        multiple                  590080    
_________________________________________________________________
block3_conv3 (Conv2D)        multiple                  590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   multiple                  0         
_________________________________________________________________
block4_conv1 (Conv2D)        multiple                  1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        multiple                  2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        multiple                  2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   multiple                  0         
_________________________________________________________________
block5_conv1 (Conv2D)        multiple                  2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        multiple                  2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        multiple                  2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   multiple                  0         
=================================================================
Total params: 14,714,688
Trainable params: 7,079,424
Non-trainable params: 7,635,264

【问题讨论】:

    标签: python-3.x tensorflow keras


    【解决方案1】:

    您可以通过以下方式实现您想要的。首先,按如下方式定义您的模型:

    inputs = tf.keras.Input(shape=(180, 180, 3))
    x = data_augmentation(inputs, training=True)
    x = keras.applications.VGG16(input_tensor=x,
                                   include_top=False,
                                   weights=None)
    x.trainable = False
    x = layers.Flatten()(x.output)
    x = layers.Dense(256)(x)
    x = layers.Dropout(0.5)(x)
    x = layers.Dense(1, activation='sigmoid')(x)
    model = keras.Model(inputs, x)
    
    for i, layer in enumerate(model.layers):
        print(i, layer.name, layer.output_shape, layer.trainable)
    
    ...
    17 block5_conv2 (None, 11, 11, 512) False
    18 block5_conv3 (None, 11, 11, 512) False
    19 block5_pool (None, 5, 5, 512) False
    20 flatten_2 (None, 12800) True
    21 dense_4 (None, 256) True
    22 dropout_2 (None, 256) True
    23 dense_5 (None, 1) True
    

    现在,构建具有所需输出层的 grad-cam 模型,如下所示:

    grad_model = keras.models.Model(
        [model.inputs], 
        [model.get_layer('block5_pool').output, 
         model.output]
    )
    

    测试

    image = np.random.rand(1, 180, 180, 3).astype(np.float32) 
    
    with tf.GradientTape() as tape:
        convOutputs, predictions = grad_model(tf.cast(image, tf.float32))
        loss = predictions[:, tf.argmax(predictions[0])]
    
    grads = tape.gradient(loss, convOutputs)
    print(grads) 
    
    tf.Tensor(
    [[[[ 9.8454033e-04  3.6991197e-03 ... -1.2012678e-02
        -1.7934230e-03  2.2925171e-03]
       [ 1.6165405e-03 -1.9513096e-03 ... -2.5789393e-03
         1.2443252e-03 -1.3931725e-03]
       [-2.0554627e-04  1.2232144e-03 ...  5.2324748e-03
         3.1955825e-04  3.4566019e-03]
       [ 2.3650150e-03 -2.5699558e-03 ... -2.4103196e-03
         5.8940407e-03  5.3285398e-03]
    
    ...
    

    【讨论】:

    • 您能否解释一下,我的模型发生了什么以及它有何不同以及有何不同?我的意思是,我可以训练和使用它...
    • 是否有可能让它工作并且仍然在子模型或子层中拥有预训练的部分(或者无论如何命名)
    • 在您的模型中,vgg 在某种程度上充当单层。也许这就是您断开图表的原因。同样,即使您将输入张量传递给 vgg,您也可以构建一个 grad-cam 模型,但您可能会再次面临来自 tape.gradient 的 None 输出,因为来自输入层的特征(在您的情况下这是vgg
    猜你喜欢
    • 2021-07-09
    • 2023-03-27
    • 2022-11-20
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多