SoFunction
Updated on 2024-11-17

The use of TensorBoard and torchsummary in Pytorch.

Neural Network Visualization Tools

TensorBoard is a powerful visualization tool with two methods of calling it in pytorch:

tensorboardX import SummaryWriter

This method was written by a god on the internet when tensorboard was not officially supported.

import SummaryWriter

This method was officially added in a later update

1.1 Calling methods

1.1.1 Creating the interface SummaryWriter

Function: Create interface

Call method:

writer = SummaryWriter("runs")

Parameters:

log_dir: event file output folder

comment: folder suffix when log_dir is not specified

filename_suffix: event file filename suffix

1.1.2 Record scalars add_scalars()

Function: record scalars add_scalars()

Call method:

writer.add_scalars("name",{"dic":val},epoch)

Parameters:

tag: the tag name of the image

scalar_step: the scalar to be recorded

global_step: number of rounds

1.1.3 Statistical Histogram add_histogram()

Functions: statistical histograms and multi-quartile line graphs

Call method:

writer.add_histogram("weight",,epoch)

Parameters:

tag: the tag name of the image

values: data to be histogrammed

global_step: number of rounds

bins: values like 'tensorflow', 'auto', 'fd', etc.

1.1.4 Batch display image add_image()

Function: Batch display image

Call method:

writer.add_image(“Cifar10”, img_batch, epoch,'CHW')

Parameters:

tag: the tag name of the image

img_tensor: image data, note size

global_step: number of rounds

dataformats: data forms, CHW,HWC,HW

1.1.5 Viewing the model graph add_graph()

Function: View Model Drawing

Call method:

writer.add_graph(model=net,input_to_model=(1,3, 224, 224).to(device))

Parameters:

model: model, must be

input_to_model: data to be output to the model

verbose: whether or not to print information about the structure of the calculation diagram

Remember to write ()

2. View network layer shape, parameters torchsummary

Function: View network layer shape, parameters

Call method:

from torchsummary import summary
summary(net, input_size=(3, 224, 224))

Parameters:

model: pytorch model

input_size: model input size

batch_size:batch size

device:“cuda” or “cpu”

3. Start tensorboard

Open a terminal with cmd in the file path and type

tensorboard --logdir="./runs"

Runs is the filename where I saved the file, open the following link

在这里插入图片描述

Addendum: pytorch call to tensorboard method attempts

The tensorboard provides a good interface for monitoring training losses and can help us better tune our parameters. The following section describes how to call tensorboard in pytorch.

in the first place

Installing tensorboard, tensorflow, and tensorboardX

secondly

Import SummaryWriter at the beginning of the file.

from tensorboardX import SummaryWriter

tertiary

Same as tensorflow's tensorboard, tensorboardX provides multiple recording methods such as scalar, image, and so on.

writer = SummaryWriter('path')

If you don't add path, it is named after the time by default.

fourth

Adding Monitor Variables

writer.add_scalar('Train/Acc', Acc, iter)

fifthly

Open tensorboard

tensorboard --logdir 'path'

sixthly

Open port 6006 in your browser

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.