TensorBoard可视化
利用TensorBoard对MNIST数据集在训练和测试时进行数据监听,将loss与预测值以图的形式显示出来。
首先在cmd进入你的tensorflow环境,通过目录进入你要监听的.py文件的主目录下,我的.py文件实在logs目录下,然后输入下面的代码开启TensorBoard,然后再网页输入下面的网址进入TensorBoard页面。运行py文件就可以进行数据的监听了。
监听画面如下:
调用plo_to_image接口,将数据集中的图像转换成png类型。
def plo_to_image(figure):
"""Converts the matplotlib plot specified by 'figure' to a PNG image and
return it. The supplied figure is closed and inaccessible after this call."""
#Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
#Closing the figure prevents it from being displayed directly inside the notebook
plt.close(figure)
buf.seek(0)
image = tf.image.decode_png(buf.getvalue(), channels=4)
image = tf.expand_dims(image, 0)
return image
调用image_grid接口,将数据集中的图片以25张图片合并在一张图片上。
def image_grid(images):
"""返回5 * 5的MNIST图像网格作为matplotlib图."""
#创建一个包含图的图形.
figure =plt.figure(figsize=(10, 10))
for i in range(25):
#开始下一个子图
plt.subplot(5, 5, i+1, title='name')
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(images[i], cmap=plt.cm.binary)
return figure
创建监听对象路径
#创建监听对象路径
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = 'logs/' + current_time
summary_writer = tf.summary.create_file_writer(log_dir)
将数据和图片传入TensorBoard中进行监听
with summary_writer.as_default():
#一张一张的显示
tf.summary.scalar('test-acc', float(total_correct/total), step=step)
tf.summary.image("test-onebyone-images:", test_images, max_outputs=25, step=step)
#将25张图片合成一张显示
test_images = tf.reshape(test_images,[-1, 28, 28])
figure = image_grid(test_images)
tf.summary.image('test-images:',plo_to_image(figure), step=step)
完整代码
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets,layers,optimizers,Sequential,metrics
from matplotlib import pyplot as plt
import io
import datetime
def preprocess(x,y):#数据预处理
x = tf.cast(x, dtype=tf.float32)/255.
y = tf.cast(y, dtype=tf.int32)
return x,y
def plo_to_image(figure):
"""Converts the matplotlib plot specified by 'figure' to a PNG image and
return it. The supplied figure is closed and inaccessible after this call."""
#Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
#Closing the figure prevents it from being displayed directly inside
#the notebook
plt.close(figure)
buf.seek(0)
image = tf.image.decode_png(buf.getvalue(), channels=4)
image = tf.expand_dims(image, 0)
return image
def image_grid(images):
"""Return a 5*5 grid of the MNIST images as a matplotlib figure."""
#Create a figure to contain the plot.
figure =plt.figure(figsize=(10, 10))
for i in range(25):
#Start next subplot
plt.subplot(5, 5, i+1, title='name')
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(images[i], cmap=plt.cm.binary)
return figure
#加载数据集
(x, y), (x_test, y_test) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape)#(60000, 28, 28) (60000,)
batchsz = 128
db = tf.data.Dataset.from_tensor_slices((x,y))
db = db.map(preprocess).shuffle(60000).batch(batchsz).repeat(10) #调用预处理函数,对每一个x,y进行处理,打散数据,截断
db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))
db_test = db_test.map(preprocess).batch(batchsz, drop_remainder=True)
#Squential是个容器,将创建的list传给Sequential,它就是一个网络层
network = Sequential([
layers.Dense(256, activation = tf.nn.relu), #[b,784]==>[b,256] 全连接层,线性激活函数
layers.Dense(128, activation = tf.nn.relu), #[b,256]==>[b,128]
layers.Dense(64, activation = tf.nn.relu), #[b,128]==>[b, 64]
layers.Dense(32, activation = tf.nn.relu), #[b, 64]==>[b, 32]
layers.Dense(10) #[b, 32]==>[b, 10]
])
network.build(input_shape=[None, 28*28])#喂一个输入,构建一个权值
network.summary()#调试,可以将网络结构打印出来
#优化器 : w = w - lr*grad
optimizer = optimizers.Adam(lr = 0.01)
#创建监听对象路径
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = 'logs/' + current_time
summary_writer = tf.summary.create_file_writer(log_dir)
#get x from (x, y)
smaple_img = next(iter(db))[0]
#get first image instance
smaple_img = smaple_img[0]
smaple_img = tf.reshape(smaple_img, [1, 28, 28, 1])
with summary_writer.as_default():
tf.summary.image("Training sample:", smaple_img, step=0)
for step,(x,y) in enumerate(db):
#求梯度
with tf.GradientTape() as tape:
# x:[b,28,28] ==> [b,784]
# y:[b]
x = tf.reshape(x, [-1, 28 * 28])
#[b,784] ==> [b,10]
out = network(x)#调用网络层,得到没有归一化的输出值logits
y_onehot = tf.one_hot(y, depth = 10) #将y转成one_hot
loss = tf.losses.categorical_crossentropy(y_onehot, out, from_logits=True) #交叉熵损失函数
loss = tf.reduce_mean(loss)
grads = tape.gradient(loss, network.trainable_variables) #model.trainable_variables返回【w,b】参数列表
optimizer.apply_gradients(zip(grads, network.trainable_variables)) #原地更新参数
if step%100 == 0:
print(step,'loss:',float(loss))
with summary_writer.as_default():
tf.summary.scalar('train-loss', float(loss), step=step)
#evaluate
if step % 500 ==0:
total, total_correct = 0, 0
for _,(x,y) in enumerate(db_test):
# x:[b,28,28] ==> [b,784]
# y:[b]
x = tf.reshape(x, [-1, 28 * 28])
# [b,784] ==> [b,10]
out = network(x) # 调用网络层,使用当前状态的【w,b】,得出测试图片的输出值
#[b,10] ==> [b] pred: int64 ==> int32
pred = tf.argmax(out, axis=1) #获取prob的最大得分的索引位置【0-9】
pred = tf.cast(pred, dtype=tf.int32)
#pred:[b]
#y:[b] 存储的时数字的值【0-9】
correct = tf.equal(pred,y) #比较pred与y,相等返回True,不等返回False
# bool tensor => int tensor => numpy
correct = tf.reduce_sum(tf.cast(correct, dtype=tf.int32)) # 将bool型转成int,求出b张图片中正确的个数
total_correct += int(correct) # 求出测试数据集中总的正确个数
total += x.shape[0] #计算测试集的图片个数
print(step, 'Test Acc:', total_correct/total)
#print(x.shape)
test_images = x[:25]
test_images = tf.reshape(test_images,[-1, 28, 28, 1])
with summary_writer.as_default():
#一张一张的显示
tf.summary.scalar('test-acc', float(total_correct/total), step=step)
tf.summary.image("test-onebyone-images:", test_images, max_outputs=25, step=step)
#将25张图片合成一张显示
test_images = tf.reshape(test_images,[-1, 28, 28])
figure = image_grid(test_images)
tf.summary.image('test-images:',plo_to_image(figure), step=step)