tf-explain working with tensorboard

1,027次阅读
没有评论
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、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
反超Nano Banana!OpenAI旗舰图像生成模型上线

反超Nano Banana!OpenAI旗舰图像生成模型上线

反超Nano Banana!OpenAI旗舰图像生成模型上线 Jay 2025-12-17 10:25:43 ...
OpenAI突然开源新模型!99.9%的权重是0,新稀疏性方法代替MoE

OpenAI突然开源新模型!99.9%的权重是0,新稀疏性方法代替MoE

OpenAI突然开源新模型!99.9%的权重是0,新稀疏性方法代替MoE 闻乐 2025-12-14 14:2...
最新评论
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时代的范式思维转变 | MEET2026

交大高金朱宁:经济学家视角下AI时代的范式思维转变 | MEET2026

交大高金朱宁:经济学家视角下AI时代的范式思维转变 | MEET2026 西风 2025-12-13 12:5...
半世纪难题48小时破解!陶哲轩组队把AI数学玩成打怪游戏了

半世纪难题48小时破解!陶哲轩组队把AI数学玩成打怪游戏了

半世纪难题48小时破解!陶哲轩组队把AI数学玩成打怪游戏了 鹭羽 2025-12-13 22:43:25 来源...
美国视频生成老炮儿,入局世界模型

美国视频生成老炮儿,入局世界模型

美国视频生成老炮儿,入局世界模型 鹭羽 2025-12-13 22:41:00 来源:量子位 三连发:真实场景...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
为Token付费是一件很愚蠢的事情,用户应该为智能付费丨RockAI刘凡平@MEET2026

为Token付费是一件很愚蠢的事情,用户应该为智能付费丨RockAI刘凡平@MEET2026

为Token付费是一件很愚蠢的事情,用户应该为智能付费丨RockAI刘凡平@MEET2026 西风 2025-...