graph disconnected error when trying visualizing and obtaining feature map nested model

Viewed 57

I'm trying to obtain feature maps of the model which include three nested models (functional model). when I create another model to do this task throw graph disconnected error. I used the below snippet. my purpose is to obtain a feature map in a nested model after feed input for prediction.

model = tf.keras.models.Model(inputs =my_model.input, outputs = my_model.layers[5].output)

My model architecture is below

from os import name
import NumPy as np
import tensorflow.keras as kr
import tensorflow as tf
from tensorflow_addons.optimizers import AdamW
import tensorflow.keras.backend as k
from tensorflow.keras import layers
from tensorflow.keras import activations
from tensorflow.keras import models
from tensorflow.keras.optimizers import Adam
from classification_models.keras import Classifiers
import torchvision.models as tmodels


# import from Classifier file in directory path


class DeepRhythmModel:

    def __init__(self,
                 motion_magnified_input_size=(1, 300)
                 , MMST_Map_size=(300, 25, 3)  # frames:temporal , block:spatial , rgb signal
                 , row_col_blocks_number=(5, 5)

                 , lr_rate=None
                 , batch_size=32
                 , num_classes=1
                 , weight_decay=None
                 , pretrained=False
                 , exist_meso=True
                 ):
        """
        initialize Model variable
        Args:
            input_size: (T,N,3)
            row_col_blocks_number
            ppg_map:
            motion_magnified_input:
            lr_rate:
            epochs:
        """
        self.weight_decay = weight_decay
        self.motion_magnified_input_size = motion_magnified_input_size
        self.MMST_Map_size = MMST_Map_size

        self.row_col_blocks_number = row_col_blocks_number
        self.lr_rate = lr_rate
        self.batch_size = batch_size
        # CREATE PRIOR SPATIAL ATTENTION LIST
        r, c = row_col_blocks_number
        self.pretrained = pretrained
        self.exist_meso = exist_meso
        self.num_classes = num_classes


    def build_model(self):

        #### INPUT ####
        prior_spatial_attention_input = layers.Input(
            shape=(1, self.row_col_blocks_number[0] * self.row_col_blocks_number[1]))
        MMST_Map_input = layers.Input(shape=self.MMST_Map_size)
        if self.exist_meso:
            frame_level_temporal_attention_input = layers.Input(shape=self.motion_magnified_input_size,
                                                                )
        block_level_temporal_attention_input = layers.Reshape(
            (self.MMST_Map_size[0], self.MMST_Map_size[1] * self.MMST_Map_size[2]))(MMST_Map_input)

        ### Block Level Temporal Attention Model

        blk_level_tmp_atn_inpt = self.block_level_temporal_attention_model()(block_level_temporal_attention_input)


        ### Adaptive Spatial Attention
        adp_sp_atn_out = self.adptive_spatial_attention()(MMST_Map_input)

        ## (tf + tb)
        if self.exist_meso:
            temporal = layers.Add()([blk_level_tmp_atn_inpt, frame_level_temporal_attention_input])
        else:
            temporal = blk_level_tmp_atn_inpt

        ## (sa + sp)
        spatial = layers.Add()([adp_sp_atn_out, prior_spatial_attention_input])

        # Attention map
        attention = layers.Dot(axes=(2, 1))([layers.Permute((2, 1))(temporal), spatial])

        gray_attention = layers.Reshape((attention.shape[1], attention.shape[2], 1))(attention)
        rgb_attention = tf.image.grayscale_to_rgb(gray_attention)

        # Multiple rgb attention & MMST_Map_input
        resnet_ipt = layers.Multiply()([rgb_attention, MMST_Map_input])
        final_output = self.resnet18()(resnet_ipt)

        if self.exist_meso:
            main_model = models.Model(
                inputs=[prior_spatial_attention_input, MMST_Map_input, frame_level_temporal_attention_input],
                outputs=[final_output], name='DeepRhythmModel')
        else:
            main_model = models.Model(inputs=[prior_spatial_attention_input, MMST_Map_input], outputs=[final_output],
                                      name='DeepRhythmModel')

        optimizer = AdamW(weight_decay = self.weight_decay, learning_rate = self.lr_rate)
        main_model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
        main_model.summary(expand_nested=True)

        return main_model

    # Create adaptive_spatial_attention module
    def adptive_spatial_attention(self):
        inpt = layers.Input(shape = self.MMST_Map_size)
        x = layers.Conv2D(64, (15, 1), activation='relu')(inpt)
        x = layers.BatchNormalization()(x)
        x = layers.MaxPooling2D(pool_size=(15, 1))(x)
        x = layers.Flatten()(x)
        x = layers.Dense(25, activation="tanh")(x)
        output = layers.Reshape((1, x.shape[1]))(x)
        return models.Model(inputs=inpt, outputs=output, name='adptive_spatial_attention')

    # BUILD BLOCK-LEVEL TEMPORAL ATTENTION MODEL
    def block_level_temporal_attention_model(self):

        inpt = layers.Input(shape=(self.MMST_Map_size[0], self.MMST_Map_size[1] * self.MMST_Map_size[2]))
        x = layers.LSTM(units = 300)(inpt)
        output = layers.Reshape((1, x.shape[1]))(x)
        return models.Model(inputs=inpt, outputs=output, name='block_level_temporal_attention_model')

    def resnet18(self):

        ResNet18, preprocess_input = Classifiers.get('resnet18')
        inpt = layers.Input(shape=self.MMST_Map_size)
        if self.pretrained:
            base_model = ResNet18(input_shape=(300, 25, 3), include_top=False)(inpt)
        else:
            base_model = ResNet18(input_shape=(300, 25, 3), include_top=False, weights=None)(inpt)
        x = layers.Flatten()(base_model)
        output = layers.Dense(1, activation='sigmoid')(x)
        return models.Model(inputs=inpt, outputs=output, name='resnet18')

To create the model this snippet used

DeepRhythmOBJ = DeepRhythmModel(lr_rate=lr_rate, batch_size = batch_size,weight_decay=weight_decay, pretrained=False)
new_model= DeepRhythmOBJ.build_model()
0 Answers
Related