tf-explain is a pip installable library which is completely built over tensorflow 2.0 and is used with tf.keras. It helps in better understanding of our model that is currently training. In tf.keras we use all of it’s apis in the callbacks that is provided to the model while training.
Definitely, tf-explain is not the official product of tensorflow but it is completely build over tensorflow 2.0. One of it’s main advantage is the usage of tensorboard that provides us information related to the images with a better view and clear graphics.
The entire code is present at https://github.com/AshishGusain17/Grad-CAM-implementation/blob/master/tf_explain_methods.ipynb . The implementation of various methods over the images can be seen below with graphics.
pip install tf-explain
pip install tensorflow==2.1.0
1.) Build a tf.keras model
img_input = tf.keras.Input((28,28,1))
x = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation=”relu” , name=”layer1")(img_input)
x = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation=”relu”, name=”layer2")(x)
x = tf.keras.layers.MaxPool2D(pool_size=(2, 2))(x)
x = tf.keras.layers.Dropout(0.25)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(128, activation=”relu”)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(10, activation=”softmax”)(x)
model = tf.keras.Model(img_input, x)
model.summary()
2.) Create the validation dataset for any particular label that will be given as input to the API’s. We have used mnist dataset with 60,000 training images and 10,000 test images. It has 10 classes with images of numbers ranging from 0–9. Let’s create the tuples for labels 0 and 4 as validation_class_zero and validation_class_four as below.
# Here, we choose 5 elements with one hot encoded label “0” == [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
validation_class_zero = (
np.array(
[
el
for el, label in zip(test_images, test_labels)
if np.all(np.argmax(label) == 0)
][0:5]
),None)
# Here, we choose 5 elements with one hot encoded label “4” == [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
validation_class_four = (
np.array(
[
el
for el, label in zip(test_images, test_labels)
if np.all(np.argmax(label) == 4)
][0:5]
),None)
#tensorboard #keras #google-colab #tensorflow #grad-cam