Data visualisation code with data augmentation layer

Following the code given here for Visualizing Intermediate Representations, I am running:

successive_outputs = [layer.output for layer in CNN_model.layers]
visualization_model = tf.keras.models.Model(inputs=CNN_model.inputs, outputs=successive_outputs)

but keep getting the error ‘ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 200, 200, 3), dtype=tf.float32, name=‘random_flip_input’)…’

The model contains some data augmentation layers which I think are causing the issue. Model is built with:

CNN_model = tf.keras.Sequential([
        tf.keras.layers.Rescaling(1. / 255),
        data_augmentation,

        # Add convolutions and max pooling
        tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])
# Compile the model
CNN_model.compile(loss='binary_crossentropy', optimizer=RMSprop(),
                  metrics=[tf.keras.metrics.BinaryAccuracy(), tf.keras.metrics.Precision(name='precision')])

# Train the model
epochs = 1  # how long to train for
CNN_model.fit(
        train_ds,
        validation_data=val_ds,
        epochs=epochs
    )

What is the appropriate way for me to ignore the augmentation layers when trying to visualise the feature maps?

Which course, week, and assignment are you doing? Or is it your personal project?

From Course 2 Week 1 but adapted for a personal project

Ohk. It is TF1 course 2.

I am sorry but I don’t have the answer to this question. Maybe duplicating the model but without augmentation. I am not sure.

I think I have fixed this by just changing the outputs to only include appropriate layers:

successive_outputs = [layer.output for layer in CNN_model.layers if
                          'conv' in layer.name or 'max_pooling' in layer.name]  # only extract features from conv/pool layers

That’s great…