tensorflow 2.0 學習 (九) tensorboard可視化功能認識


代碼如下:

# encoding :utf-8

import io  # 文件數據流
import datetime
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
# 導入常見網絡層, sequential容器, 優化器, 損失函數
from tensorflow.keras import layers, Sequential, optimizers, losses, metrics
import os # 運維模塊, 調用系統命令
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 只顯示warring和error


def preprocess(x, y):
    x = tf.cast(x, dtype=tf.float32) / 255.
    y = tf.cast(y, dtype=tf.int32)
    return x, y


def plot_to_image(figure):
    buf = io.BytesIO()  # 在內存中存儲畫
    plt.savefig(buf, format='png')
    plt.close(figure)
    buf.seek(0)
    # 傳化為TF 圖
    image = tf.image.decode_png(buf.getvalue(), channels=4)
    image = tf.expand_dims(image, 0)
    return image


def image_grid(images):
    # 返回一個5x5的mnist圖像
    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


batchsz = 128
path = r'G:\2019\python\mnist.npz'
(x, y), (x_val, y_val) = tf.keras.datasets.mnist.load_data(path)
print('datasets:', x.shape, y.shape, x.min(), x.max())

db = tf.data.Dataset.from_tensor_slices((x,y))
db = db.map(preprocess).shuffle(60000).batch(batchsz).repeat(10)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz, drop_remainder=True)

network = Sequential([
    layers.Dense(256, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(64, activation='relu'),
    layers.Dense(32, activation='relu'),
    layers.Dense(10)
])

network.build(input_shape=(None, 28*28))
network.summary()
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)  # 創建監控類,監控數據寫入到log_dir目錄

sample_img = next(iter(db))[0]
sample_img = sample_img[0]  # 第一張圖
sample_img = tf.reshape(sample_img, [1, 28, 28, 1])
with summary_writer.as_default():  # 寫入環境
    tf.summary.image("Training sample:", sample_img, step=0)

for step, (x, y) in enumerate(db):    # 遍歷切分好的數據step:0->599
    with tf.GradientTape() as tape:
        x = tf.reshape(x, (-1, 28*28))
        out = network(x)
        y = tf.one_hot(y, depth=10)
        loss = tf.reduce_mean(tf.losses.categorical_crossentropy(y, out, from_logits=True))

    grads = tape.gradient(loss, network.trainable_variables)
    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)  # 將loss寫入到train-loss中

    if step % 500 == 0:
        total, total_correct = 0., 0

        for _, (m, n) in enumerate(ds_val):
            m = tf.reshape(m, (-1, 28*28))
            out = network(m)
            pred = tf.argmax(out, axis=1)
            pred = tf.cast(pred, dtype=tf.int32)
            correct = tf.equal(pred, n)
            total_correct += tf.reduce_sum(tf.cast(correct, dtype=tf.int32)).numpy()
            total += m.shape[0]

        print(step, 'Evaluate Acc:', total_correct / total)

        val_images = m[:25]
        val_images = tf.reshape(val_images, [-1, 28, 28, 1])
        with summary_writer.as_default():
            tf.summary.scalar('test-acc', float(total_correct / total), step=step)  # 寫入測試准確率
            tf.summary.image("val-onebyone-images:", val_images, max_outputs=25, step=step)  # 可視化測試用圖片,25張
            val_images = tf.reshape(val_images, [-1, 28, 28])
            figure = image_grid(val_images)
            tf.summary.image('val-images:', plot_to_image(figure), step=step)

后台cmd下,輸入:tensorboard --logdir "C:\Users\Z He\PycharmProjects\he-learn\logs";

復制鏈接,在edge中打開,如下:

loss率

准確率:

圖像:

 

可視化確實有助於認識學習的效果,今后盡可能用上可視化。

下次更新,擬合與過擬合中的關於月牙形圖像處理的例子。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM