【问题标题】:"AttributeError: Layer cnn_model has no inbound nodes" when making a model from a subclassed model for Keras GradCam tutorial从 Keras GradCam 教程的子类模型制作模型时出现“AttributeError:层 cnn_model 没有入站节点”
【发布时间】:2021-05-19 14:55:37
【问题描述】:

我正在尝试使用我自己的模型来关注 GradCam Tutorial。这是它的架构:

import tensorflow as tf
from tensorflow import keras as K
import numpy as np

class CNNModel(K.Model):
    def __init__(self):
        super(CNNModel, self).__init__()
        self.base = K.applications.EfficientNetB1(input_shape=(224, 224, 12),
                                                  include_top=False,
                                                  weights=None)

        self.pool = K.layers.GlobalAveragePooling2D()
        self.drop1 = K.layers.Dropout(0.25)
        self.dense1 = K.layers.Dense(16, activation='relu')
        self.drop2 = K.layers.Dropout(0.25)
        self.out = K.layers.Dense(1, activation='sigmoid')

    def call(self, x, training=None, **kwargs):
        x = self.base(x)
        x = self.pool(x)
        x = self.drop1(x)
        x = self.dense1(x)
        x = self.drop2(x)
        x = self.out(x)
        return x

model = CNNModel()
model.build(input_shape=(None, 224, 224, 12))

我需要得到最后一个卷积层,所以我从基础 (EfficientNet) 模型中得到一个:

last_conv_layer_name = list(filter(lambda x: isinstance(x, tf.keras.layers.Conv2D), model.base.layers))[-1].name

然后我尝试在此基础上制作一个 2 输出模型,就像在教程中一样。

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

我明白了:

AttributeError: Layer cnn_model 没有入站节点

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    我遇到了关于子类 API 模型的类似问题,并进一步尝试通过将其合并到功能 API 中来在 grad-cam 中使用它。后来,当时对我有用的事情是为 grad-cam 单独构建一个子类模型,并在 __init__ 中构建所需的输出模型。

    class CNNModel(K.Model):
        def __init__(self):
            super(CNNModel, self).__init__()
            self.base = K.applications.EfficientNetB1(input_shape=(224, 224, 12),
                                                      include_top=False,
                                                      weights=None)
            # desired model 
            self.base = K.Model(
                    [self.base.inputs], 
                    [self.base.get_layer('top_conv').output, self.base.output]
                )
    
            self.pool = K.layers.GlobalAveragePooling2D()
            self.drop1 = K.layers.Dropout(0.25)
            self.dense1 = K.layers.Dense(16, activation='relu')
            self.drop2 = K.layers.Dropout(0.25)
            self.out = K.layers.Dense(1, activation='sigmoid')
    
        def call(self, x, training=None, **kwargs):
            x = self.base(x)
            top_conv = x[0]
            x = x[1] 
            x = self.pool(x)
            x = self.drop1(x)
            x = self.dense1(x)
            x = self.drop2(x)
            x = self.out(x)
            return top_conv, x
    
    model = CNNModel()
    model.build(input_shape=(None, 224, 224, 12))
    

    传递一些数据进行检查。

    img_array = np.random.rand(1, 224, 224, 12).astype(np.float32)
    (convOutputs, predictions) = model(img_array)
    print(convOutputs.shape, predictions.shape)
    (1, 7, 7, 1280) (1, 1)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2021-09-16
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多