pytorch study notes - tensorboard use

The use of tensorboard (1)

The use of the .add_scalar() method in the SummaryWriter class (you can hold down Ctrl and click add_scalar to view the function of this method).

First install tensorboard with pip, and execute the following command to draw an image with y=2x.

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("logs")

for i in range(100):
    writer.add_scalar("y = 2x", 2 * i, i)

writer.close()

Note that it needs to be closed with writer.close() at the end of the code, otherwise the picture cannot be displayed by the following method.

Here's how to open the drawn image, open the python terminal.

If the terminal is not Pytorch, but PS (as shown above), go to settings and change the path to cmd!

Use the following command in the terminal, if you find that it doesn't work, you need to install tensorflow. logdir is the name of the folder where the event is located.

tensorboard --logdir=logs

It is very fast to install with Douban mirror source:

pip install tensorflow -i https://pypi.douban.com/simple

Open tensorboard again using the command above.

6006 is the port name. If you use the same port, there may be problems. You can use the following command to actively set the port name.

tensorboard --logdir=logs --port=6008

Click on the URL and the image pops up.

The use of tensorboard (2)

The use of the .add_image() method in the SummaryWriter class (you can hold down Ctrl and click add_image to view the function of this method).

Since the image types it requires are the types of the white lines above, and the image type created by the following method is JPEGimagefile does not meet the requirements, so use numpy.array() to convert the PIL image.

image_path = "data/train/ants_image/0013035.jpg"
from PIL import Image
img = Image.open(image_path)
print(type(img))

# output <class 'PIL.JpegImagePlugin.JpegImageFile'>

import numpy as np
img_array = np.array(img)
print(type(img_array))

# output <class 'numpy.ndarray'>

You can see that the image type has met the requirements.

Since the default image dimension is: math:`(3, H, W)`, that is, the number of channels is in the front, and then the height and width of the image, so according to the prompt inside the function, add dataformats = "HWC ".

from torch.utils.tensorboard import SummaryWriter
from PIL import Image
import numpy as np

writer = SummaryWriter("logs")
image_path = "data/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)

# The parameters of this method can refer to the help documentation, "1" can be understood as the number of training steps
writer.add_image("test", img_array, 1, dataformats="HWC")
writer.close()

# <class 'numpy.ndarray'>
# (512, 768, 3)

Dimensions can also be transformed directly using np.transpose().

from torch.utils.tensorboard import SummaryWriter
from PIL import Image
import numpy as np

writer = SummaryWriter("logs")
image_path = "data/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)

# Use np.transpose() to transform the dimension, you do not need to add dataformats="HWC" to the following parameters
# The second parameter of the transpose() function is where the index value is changed
img_array = np.transpose(img_array, (2, 0, 1))    

print(img_array.shape)
writer.add_image("test", img_array, 1)
writer.close()

# <class 'numpy.ndarray'>
# (512, 768, 3)
# (3,512, 768)

At this time, run it again in the terminal, refresh and view the picture on tensorboard.

The part of reading the image in the above code can use OpenCV to read the image, and the image read by opencv is of type numpy.array. The opencv-python library needs to be installed first.

from torch.utils.tensorboard import SummaryWriter
import cv2

writer = SummaryWriter("logs")
image_path = "data/train/ants_image/6743948_2b8c096dda.jpg"

img_cv = cv2.imread(image_path)
print(type(img_cv))

writer.add_image("ants", img_cv, 1, dataformats="HWC")

writer.close()

At the beginning, there was a situation where cv2 could not display the callable functions.

By searching for information, it is found that cv2.pyd in the anaconda\envs\pytorch\Lib\site-packages\cv2 folder needs to be copied to the site-packages folder, and the problem is solved.

Tags: Pytorch Deep Learning

Posted by pvraja on Fri, 20 Jan 2023 10:18:36 +0300