'How can I save the output of a convolution layer with and without a dilation rate as images

I want to save a image file to see about difference using convolution layer with dilation rate and without that.

Of course I can search images about that, but I want to see difference of my dataset.

Is there a special function? or Can I make code with opencv like a keras layers?

I am using python 3.8 and tf 2.4.0



Solution 1:[1]

You can use matplotlib and a custom Callback to save the feature maps of a CNN layer after every epoch. Here is a working example:

import tensorflow as tf
import pathlib
import matplotlib.pyplot as plt

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

batch_size = 32

normalized_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(180, 180),
  batch_size=batch_size)

model = tf.keras.Sequential([
  tf.keras.layers.Rescaling(1./255, input_shape=(180, 180, 3)),
  tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu', dilation_rate=(3, 3)),
  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.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(5)
])

class CustomCallback(tf.keras.callbacks.Callback):
   def __init__(self, data, sample_size):
        self.data = data
        self.sample_size = sample_size
   def on_epoch_end(self, epoch, logs=None):
        images, _ = next(iter(self.data.take(self.sample_size)))
        image = tf.expand_dims(images[0], axis=0)
        output = first_cnn_layer(image)
        plt.imsave('image{}.png'.format(epoch), images[0].numpy()/255)
        ix = 1
        for _ in range(4):
          for _ in range(4):
            ax = plt.subplot(4, 4, ix)
            ax.set_xticks([])
            ax.set_yticks([])
            plt.imshow(output[0, :, :, ix-1], cmap='gray')
            ix += 1
        plt.savefig('filters{}.png'.format(epoch))



first_cnn_layer = model.layers[1]
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(normalized_ds, epochs=2, callbacks=[CustomCallback(normalized_ds, 1)])

With dilation_rate=(1, 1) at epoch 1:

Original image

                  enter image description here

Feature maps

enter image description here

With dilation_rate=(3, 3) at epoch 1:

Original image

                  enter image description here

Feature maps

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1