pytorch中使用TensorBoard学习笔记

TensorBoard

TensorBoard使用可视化工具能够很好的帮助我们查看训练模型的结果,因此,常常被大家作为可视化工具的首选。
pytorch中使用TensorBoard学习笔记_第1张图片pytorch中使用TensorBoard学习笔记_第2张图片

1. 启动模块/载入模块

from torch.utils.tensorboard import SummaryWriter
# default `log_dir` is "runs" - we'll be more specific here
writer = SummaryWriter('log_dir=runs/logs')

2. 写入模型

将一个batch_size图像写入tensorboard

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# create grid of images
img_grid = torchvision.utils.make_grid(images)

# show images
matplotlib_imshow(img_grid, one_channel=True)

# write to tensorboard
writer.add_image('four_fashion_mnist_images', img_grid)

运行:

tensorboard --logdir=runs

打开链接 http://localhost:6006
问题解决:解决tensorboard : 无法将“tensorboard”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
pytorch中使用TensorBoard学习笔记_第3张图片


model模型写入

init_img = torch.zeros((1, 3, 224, 224), device=device)
writer.add_graph(model, init_img)
writer.close()

Graphs展示
pytorch中使用TensorBoard学习笔记_第4张图片
通过add_embedding将高维数据可视化到低维数据中

# helper function
def select_n_random(data, labels, n=100):
    '''
    Selects n random datapoints and their corresponding labels from a dataset
    '''
    assert len(data) == len(labels)

    perm = torch.randperm(len(data))
    return data[perm][:n], labels[perm][:n]

# select random images and their target indices
images, labels = select_n_random(trainset.data, trainset.targets)

# get the class labels for each image
class_labels = [classes[lab] for lab in labels]

# log embeddings
features = images.view(-1, 28 * 28)
writer.add_embedding(features,
                    metadata=class_labels,
                    label_img=images.unsqueeze(1))
writer.close()

pytorch中使用TensorBoard学习笔记_第5张图片add_scalar将所需要的数据保存在文件里面供可视化使用

tags = ["train_loss", "accuracy", "learning_rate"]
writer.add_scalar(tags[0], mean_loss, epoch)
writer.add_scalar(tags[1], acc, epoch)
writer.add_scalar(tags[2], optimizer.param_groups[0]["lr"], epoch)

add_figure图像载入

def plot_classes_preds(net, images, labels):
    preds, probs = images_to_probs(net, images)
    # plot the images in the batch, along with predicted and true labels
    fig = plt.figure(figsize=(12, 48))
    for idx in np.arange(4):
        ax = fig.add_subplot(1, 4, idx+1, xticks=[], yticks=[])
        matplotlib_imshow(images[idx], one_channel=True)
        ax.set_title("{0}, {1:.1f}%\n(label: {2})".format(
            classes[preds[idx]],
            probs[idx] * 100.0,
            classes[labels[idx]]),
                    color=("green" if preds[idx]==labels[idx].item() else "red"))
    return fig
    
fig = plot_class_preds(net=model,
                               images_dir="./plot_img",
                               transform=data_transform["val"],
                               num_plot=5,
                               device=device)

writer.add_figure("predictions vs. actuals",
                                 figure=fig,
                                 global_step=epoch)  # 图像名称,fig,步长

pytorch中使用TensorBoard学习笔记_第6张图片
add_pr_curve载入预测曲线

def add_pr_curve_tensorboard(class_index, test_probs, test_label, global_step=0):
    '''
    Takes in a "class_index" from 0 to 9 and plots the corresponding
    precision-recall curve
    '''
    tensorboard_truth = test_label == class_index
    tensorboard_probs = test_probs[:, class_index]

    writer.add_pr_curve(classes[class_index],
                        tensorboard_truth,
                        tensorboard_probs,
                        global_step=global_step)
    writer.close()

总结

tensorboard使用起来确实很方便,便于可视化,有问题讨论区一起讨论哈

参考链接

你可能感兴趣的:(pytorch,python,深度学习)