Tensorboard可视化

在项目文件夹下创建logs文件夹
Tensorboard可视化_第1张图片
tensorboard --logdir logs 命令启动
此命令指定监听的文件夹
Tensorboard可视化_第2张图片
进入页面后
这是之前跑的数据
Tensorboard可视化_第3张图片
Tensorboard可视化_第4张图片

设置自动刷新页面的时间
Tensorboard可视化_第5张图片

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)

# 上面的代码指定将数据往哪个文件夹下写,并指定本次数据的文件名,此处以时间命名

对图片进行合并操作

def plot_to_image(figure):
    """Converts the matplotlib plot specified by 'figure' to a PNG image and
    returns 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)
    # Convert PNG buffer to TF image
    image = tf.image.decode_png(buf.getvalue(), channels=4)
    # Add the batch dimension
    image = tf.expand_dims(image, 0)
    return image


def image_grid(images):
    """Return a 5x5 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(images.numpy().shape[0]):
        # 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

将数据保存到指定文件中

# 取25张图片
 val_images = next(db_test_iter)[0][:25]
 val_images = tf.reshape(val_images, [-1, 28, 28, 1])
 with summary_writer.as_default():
 	 # (图表名称, 数据(y坐标), x坐标)
     tf.summary.scalar('test-acc', float(acc), step=epoch)
     tf.summary.image("val-onebyone-images:", val_images, max_outputs=25, step=epoch)

     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)

完整代码:

import datetime
import tensorflow as tf
from tensorflow import keras
from matplotlib import pyplot as plt
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
import os
import io

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


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):
    """Converts the matplotlib plot specified by 'figure' to a PNG image and
    returns 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)
    # Convert PNG buffer to TF image
    image = tf.image.decode_png(buf.getvalue(), channels=4)
    # Add the batch dimension
    image = tf.expand_dims(image, 0)
    return image


def image_grid(images):
    """Return a 5x5 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(images.numpy().shape[0]):
        # 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.fashion_mnist.load_data()
print(x.shape, y.shape)

# plt.figure(figsize=(20, 8), dpi=80)
#
# plt.imshow(x[0])
#
# plt.show()

BATCH_SIZE = 128

# [[x1, y1], [x2, y2]....]
db = tf.data.Dataset.from_tensor_slices((x, y))
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))

db = db.map(preprocess).shuffle(10000).batch(BATCH_SIZE)

db_test = db_test.map(preprocess).batch(BATCH_SIZE)
# train时,迭代的是原生db
db_iter = iter(db)
print(next(db_iter)[0].shape)

# Sequential 接收一个列表
model = Sequential([
    layers.Dense(256, activation=tf.nn.relu),  # [b, 784]@[784, 256] + [256] => [b, 256]
    layers.Dense(128, activation=tf.nn.relu),  # [b, 256]@[256, 128] + [128] => [b, 128]
    layers.Dense(64, activation=tf.nn.relu),  # [b, 128]@[128, 64] + [64] => [b, 64]
    layers.Dense(32, activation=tf.nn.relu),  # [b, 64]@[64, 32] + [32] => [b, 32]
    layers.Dense(10)  # logits  [b, 32]@[32, 10] + [10]=> [b, 10]
])

model.build([None, 28 * 28])
model.summary()  # 打印网络信息

optimizers = optimizers.Adam(learning_rate=1e-3)

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)
sample_img = next(iter(db))[0]
# get first image instance
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)

db_test_iter = iter(db_test)

# print(next(db_test_iter)[0].shape)

def main():

    for epoch in range(1, 20):
        for step, (x, y) in enumerate(db):
            # [b, 28, 28] => [b, 784]
            x = tf.reshape(x, [-1, 28 * 28])

            with tf.GradientTape() as tape:

                logits = model(x)
                y = tf.one_hot(y, depth=10)
                prob = tf.nn.softmax(logits)

                # MSE => [b] 需要mean成标量才能运算
                loss_mse = tf.reduce_mean(tf.losses.MSE(y, prob))
                # cross_entryopy注意from_logits设置为True
                loss_cros = tf.reduce_mean(tf.losses.categorical_crossentropy(y, logits, from_logits=True))

            # 求gradient
            grads = tape.gradient(loss_cros, model.trainable_variables)
            # 此处需要zip,zip后[(w1_grad, w1),(w2_grad, w2),(w3_grad, w3)..]
            optimizers.apply_gradients(zip(grads, model.trainable_variables))

            if step % 100 == 0:
                print("epoch: %s,  step: %s,  loss: %s" % (epoch, step, loss_cros.numpy()))
                with summary_writer.as_default():
                    tf.summary.scalar('train-loss', float(loss_cros), step=step)

                    # test
        # test集y不用作one_hot
        corr_num = 0
        total_num = 0
        for (x, y) in db_test:
            # x:[b, 28, 28] => [b, 784]
            x = tf.reshape(x, [-1, 28*28])

            logits = model(x)

            prob = tf.nn.softmax(logits)
            # prob:int64 [b, 10]
            # 注意此处是在第二个维度求最大值
            prob = tf.argmax(prob, axis=1)
            prob = tf.cast(prob, dtype=tf.int32)

            res = tf.cast(tf.equal(y, prob), dtype=tf.int32)
            # reduce_sum求出来的时scalar
            corr_num += int(tf.reduce_sum(res))
            # shape返回的时列表[b, ..]
            total_num += x.shape[0]

        acc = corr_num / total_num
        print("test acc = ", acc)


        print(x.shape)
        # 取25张图片
        val_images = next(db_test_iter)[0][:25]
        val_images = tf.reshape(val_images, [-1, 28, 28, 1])
        with summary_writer.as_default():
            tf.summary.scalar('test-acc', float(acc), step=epoch)
            tf.summary.image("val-onebyone-images:", val_images, max_outputs=25, step=epoch)

            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)


if __name__ == '__main__':
    main()

你可能感兴趣的:(深度学习笔记,Tensorflow,python,tensorflow,可视化,深度学习)