Tensorboard
Tensorboard is a built-in visualization tool of tensorflow. It visualizes the information of the log file output by tensorflow program, which makes the understanding, debugging and optimization of tensorflow program more simple and efficient. The visualization of tensorboard depends on the log file output by the tensorboard program, so the tensorboard and tensorflow programs run in different processes.
Open pychart and set the environment
Install tensorboard
Common functions
SummaryWriter()
SummaryWriter creates a tensorboard file, which is saved in the log_dir directory (the directory specified by yourself) writes files, which can be parsed by tensorboard
Parameters:
log_dir: save directory location. The default value is run/CURRENT_DATETIME_HOSTNAME, which changes after each run.
Using a hierarchical folder structure makes it easy to compare operations. For example, pass "runs / exp1", "runs / exp2" and so on for each new experiment to compare them.
flush_secs: indicates the time interval for writing the tensorboard file
The calling method is as follows:
writer = SummaryWriter(log_dir='logs',flush_secs=60)
writer.add_graph()
This function is used to create Graphs in tensorboard, where the network structure is stored. The common parameters are:
Model: pytorch model
input_to_model: input of pytorch model
writer.add_scalar()
This function is used to add loss to tensorboard. The common parameters are:
Tag: tag, as shown in the following figure_ loss
scalar_value: the value of the tag
global_step: the x-axis coordinate of the label
writer.add_scalar('Train_loss', loss, (epoch*epoch_size + iteration))
tensorboard --logdir=
After the logs file of tensorboard is generated, the file can be called on the command line and the website of tensorboard.
The specific code is as follows (the command will vary with different versions):
tensorboard --logdir=logs
tensorboard --logdir="logs"
tensorboard --logdir "log"
You can also specify the port name (one server may be trained by multiple people, and using one port at the same time may conflict):
tensorboard --logdir=logs --port=6007
After generating the tensorboard file, you can call the file on the command line and the tensorboard website.
The specific codes are as follows:
Click on the website in blue font:
If there is an error in this step, please refer to: No dashboards are active for the current data set Probable causes_ Don't finish the snow - CSDN blog
Code example:
""" SummaryWriter Create a tensorboard file, File saved in log_dir Directory write file, which can be tensorboard analysis Parameters: log_dir: Save directory location. The default value is run/CURRENT_DATETIME_HOSTNAME ,Changes after each run. Using a hierarchical folder structure makes it easy to compare operations. For example, pass for each new experiment“ runs / exp1"," runs / exp2"And so on in order to compare them. flush_secs: Indicates write tensorboard File interval """ from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter("logs") # Create a SummaryWriter() class instance and store the corresponding event file in the logs file # writer.add_image() """ add_scalar(): Add number to summary in Parameters: tag: Title of Icon scalar_value: The value to be saved corresponds to y axis global_step: What is the corresponding value when training to how many steps x axis """ for i in range(100): writer.add_scalar("y = x", i, i) # Add a scalar, that is, a number writer.close()
Install the open CV package: PIP install opencv Python
add_image()
Draw pictures, which can be used to check the input of the model, monitor the change of the feature map, or observe the weight.
Parameters:
-
tag: is the name of the saved graph
-
img_tensor: the type of picture is torch Tensor, numpy. Array, or string
-
golbal_step: which picture
-
dataformats = 'CHW', default CHW, tensor is CHW, numpy is HWC, C:channel H:high W:weight From PIL to numpy,
Need to add_image() specifies the meaning of each number / dimension in the shape
From PIL to numpy, you need to add_image() specifies the meaning of each number / dimension in the shape
from torch.utils.tensorboard import SummaryWriter from PIL import Image import numpy as np writer = SummaryWriter("logs") # Create a SummaryWriter() class instance and store the corresponding event file in the logs file image_path = "dataset/train/ants_image/0013035.jpg" img_PIL = Image.open(image_path) img_array = np.array(img_PIL) print(type(img_array)) print(img_array.shape) """ add_image(): Draw pictures, which can be used to check the input of the model and monitor feature map Change or observation weight. parameter: tag: Is the name of the saved graph img_tensor:Type of picture torch.Tensor, numpy.array, or string These three golbal_step:Which picture dataformats='CHW',default CHW,tensor yes CHW,numpy yes HWC,C:channel H:high W:weight. """ writer.add_image("test", img_array, 1, dataformats='HWC') # An error occurs if dataformats='HWC 'is not specified writer.close()
Execute the above statement and enter tensorboard --logdir=logs on the console
Enter the website and click Images: