“属性错误:层 cnn_model 没有入站节点";从 Keras GradCam 教程的子类模型制作模型时 [英] "AttributeError: Layer cnn_model has no inbound nodes" when making a model from a subclassed model for Keras GradCam tutorial

查看:20
本文介绍了“属性错误:层 cnn_model 没有入站节点";从 Keras GradCam 教程的子类模型制作模型时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我自己的模型遵循此 GradCam 教程.这是它的架构:

I'm trying to follow this GradCam Tutorial with my own model. Here is its architecture:

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) 模型中得到一个:

I need to get the last convolutional layer, so I'm getting the one from the base (EfficientNet) model:

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

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

Then I'm trying to make a 2 output model based on that, just like in the tutorial.

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

我得到:

AttributeError: Layer cnn_model 没有入站节点

AttributeError: Layer cnn_model has no inbound nodes

推荐答案

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

I faced a similar issue regarding the subclassed API model and further trying to use it in grad-cam by incorporating it into functional API. Later, the thing that worked for me that time was to build a subclassed model separately for grad-cam either and build desired output model in __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))

传递一些数据进行检查.

Passing some data to check.

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)

这篇关于“属性错误:层 cnn_model 没有入站节点";从 Keras GradCam 教程的子类模型制作模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆