tf-explain working with tensorboard

1,048次阅读
没有评论
tf-explain working with tensorboard

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.

Installation:

pip install tf-explain
pip install tensorflow==2.1.0

Usage of their API’s:

1.) Build a tf.keras modelimg_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)

3.) Instantiate callbacks:

Now, let’s instantiate various callbacks, that will be provided to the model. These callbacks will be for numbers 0 as well as 4. In some of them, you can see the name of the layer provided, which can be seen as a convolutional layer in the model above.callbacks = [tf_explain.callbacks.GradCAMCallback(validation_class_zero, class_index=0, layer_name=”layer2″),
tf_explain.callbacks.GradCAMCallback(validation_class_four, class_index=4, layer_name=”layer2″),tf_explain.callbacks.ActivationsVisualizationCallback(validation_class_zero, layers_name=[“layer2”]),
tf_explain.callbacks.ActivationsVisualizationCallback(validation_class_four, layers_name=[“layer2”]),tf_explain.callbacks.SmoothGradCallback(validation_class_zero, class_index=0, num_samples=15, noise=1.0),
tf_explain.callbacks.SmoothGradCallback(validation_class_four, class_index=4, num_samples=15, noise=1.0),tf_explain.callbacks.IntegratedGradientsCallback(validation_class_zero, class_index=0, n_steps=10),
tf_explain.callbacks.IntegratedGradientsCallback(validation_class_four, class_index=4, n_steps=10),tf_explain.callbacks.VanillaGradientsCallback(validation_class_zero, class_index=0),
tf_explain.callbacks.VanillaGradientsCallback(validation_class_four, class_index=4),tf_explain.callbacks.GradientsInputsCallback(validation_class_zero, class_index=0),
tf_explain.callbacks.GradientsInputsCallback(validation_class_four, class_index=4)
]

4.) Loading tensorboard:%reload_ext tensorboard
%tensorboard — logdir logs

5.) Training of the model:opt1 = tf.keras.optimizers.Adam(learning_rate=0.001)model.compile(optimizer=opt1, loss=”categorical_crossentropy”, metrics=[“accuracy”])
model.fit(train_images, train_labels, epochs=20, batch_size=32, callbacks=callbacks)

6.) Results:

Results that I obtained were loss: 0.4919 — accuracy: 0.8458. These were obtained after just 4–5 epochs. I tried with various optimizers and the maximum time were taken by SGD and AdaGrad for about 15 epochs.

7.) Tensorboard results:

Activation Visualisations of images numbered 0 and 4:

tf-explain working with tensorboard

GradCAM implementation of images numbered 0 and 4:

tf-explain working with tensorboard

Gradient Inputs of images numbered 0 and 4:

tf-explain working with tensorboard

Vanilla Gradients of images numbered 0 and 4:

tf-explain working with tensorboard

Integrate Gradients of images numbered 0 and 4:

tf-explain working with tensorboard

Smooth Gradients of images numbered 0 and 4:

tf-explain working with tensorboard

This is all from my side. You can reach me via:

Email : ashishgusain12345@gmail.com

Githubhttps://github.com/AshishGusain17

LinkedIn : https://www.linkedin.com/in/ashish-gusain-257b841a2/

Credits : https://tf-explain.readthedocs.io/en/latest/

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 12 月
 123
45678910
11121314151617
18192021222324
25262728293031
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开 Jay 2026-01-08 20:18:...
2025最大AI赢家的凡尔赛年度总结,哈萨比斯Jeff Dean联手执笔

2025最大AI赢家的凡尔赛年度总结,哈萨比斯Jeff Dean联手执笔

2025最大AI赢家的凡尔赛年度总结,哈萨比斯Jeff Dean联手执笔 鹭羽 2025-12-24 09:1...
AI Coding新王登场!MiniMax M2.1拿下多语言编程SOTA

AI Coding新王登场!MiniMax M2.1拿下多语言编程SOTA

AI C++oding新王登场!MiniMax M2.1拿下多语言编程SOTA 克雷西 2025-12-24 ...
智能体落地元年,Agent Infra是关键一环|对话腾讯云&Dify

智能体落地元年,Agent Infra是关键一环|对话腾讯云&Dify

智能体落地元年,Agent Infra是关键一环|对话腾讯云&Dify 鹭羽 2025-12-23 1...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
易烊千玺的华为绿手机,真的AI了

易烊千玺的华为绿手机,真的AI了

Failed to fetch content Read More 
AI狼人杀大决战!GPT、Qwen、DeepSeek大乱斗,人类高玩汗流浃背

AI狼人杀大决战!GPT、Qwen、DeepSeek大乱斗,人类高玩汗流浃背

AI狼人杀大决战!GPT、Qwen、DeepSeek大乱斗,人类高玩汗流浃背 鹭羽 2025-12-23 14...
长城首个VLA车型发布,魏建军回应「赌上姓氏造车」

长城首个VLA车型发布,魏建军回应「赌上姓氏造车」

长城首个VLA车型发布,魏建军回应「赌上姓氏造车」 贾浩楠 2025-12-23 13:57:25 来源:量子...