Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Pytorch use Tensorboard to observe data

Shulou Source: shulou.com Published: 2022-06-02 05:18:28 09月14日 Update

This article mainly introduces Pytorch how to use Tensorboard to observe data, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1.Tensorboard

There are two common methods:

One is add_scalar () showing the curve.

One is the add_image () display image.

Install Tensorboard first

Enter commands in your compilation environment (conda activate XXX)

Pip install tensorboard1. Enter the code using add_scalar ()

From torch.utils.tensorboard import SummaryWriter # switch package writer = SummaryWriter ('logs') # here you create a logs file with the curve generated by your add_scalar, # where the first quantity of writer.add_scalar () is the name of the curve # the second quantity is the ordinate scalar_value, and the third quantity is the Abscissa global_step (also understood as loss worthy step size) for i in range (100): writer.add_scalar ("quadratic", I * * 2, I) writer.close ()

Open this Tensorboard file

Tensorboard-- logdir=logs#1. The file name of this logdir must be the same as the file name created before, otherwise it is easy to report an error, No dashboards are active for the current data set.#2. The command entered by this tensorboard must be in the upper layer of the logs file, otherwise it is also easy to report an error, No dashboards are active for the current data set.

The result is:

two。 Enter the code using add_image ()

Note: functions in add_image () generally have three quantities:

The first is the name of the image, the second is the image (must be tensor or numpy.ndarray), and the third is the step size (which image can be understood as the training or testing phase)

Where the shape of the image must be CHW, but the shape of the image read by opencv is HWC

So you have to use the dataformats conversion to convert the image's shape to HWC as follows

The following code tests two images (one from aligned and the other from original) to simulate which image the training or test phase program runs on

From torch.utils.tensorboard import SummaryWriterimport cv2writer = SummaryWriter ('logs') aligned_img_path = "D:\\ data\\ basic\\ Image\\ aligned\\ test_0001_aligned.jpg" original_img_path = "D:\ data\\ basic\\ Image\\ original\\ test_0001.jpg" aligned_img = cv2.imread (aligned_img_path) original_img = cv2.imread (original_img_path) print (type (aligned_img)) # numpyprint (aligned_img) .shape) # writer.add_image ("img" Aligned_img, 1, dataformats='HWC') # this figure has already used writer.add_image ("img", original_img, 2, dataformats='HWC') in my first test of add_image () # this is my second test writer.close ()

Achieve the result:

IMAGES appears in tensorboard, and step1 is the graph of aligned, while step2 is the graph of original.

Thank you for reading this article carefully. I hope the article "how to use Pytorch to observe data with Tensorboard" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Image file test article input three code curve data observation name command file name result stage step size training consistency two value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia MariaDB Huawei OPPO Reno