打开anaconda prompt
1.下载tensorboard
(base) C:\Users\Jojo>conda activate pytorch
(pytorch) C:\Users\Jojo>pip install tensorboard
#查看tensorboard版本
(pytorch) C:\Users\Jojo>tensorboard --version
TensorFlow installation not found - running with reduced feature set.
2.10.1
补充:查看帮助文档的几种方式:
- 通过
jupyter notebook
from torch.utils.tensorboard import SummaryWriter
SummaryWriter??
- 通过
pycharm
按住ctrl
,鼠标移动到SummaryWriter
,会出现蓝色下划线,点击即可进入帮助文档
2.运行tensorboard
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
# writer.add_image()
# y=x
for i in range(100):
writer.add_scalar("y=x",i,i)
writer.close()
3.查看结果
- 打开
pycharm
中终端控制台 - 输入
tensorboard --logdir logs
点击蓝色链接,但是出现错误
- 修改端口号
tensorboard --logdir=logs --port=6007
点击蓝色链接
⚠️ log
的标题最好不要重复;- 当进行下一项任务时,先把之前已有的文件删除
安装opencv
直接在pycharm
的终端控制台中:
(pytorch) PS G:\python_project\learn_pytorch\learn_pytorch> pip install opencv-python
出现报错:
Failed to build opencv-python
ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects
解决办法:
- 查询
python
版本
(pytorch) C:\Users\Jojo>python --version
Python 3.6.15
- 查找和
python
版本对应的opencv
opencv
和python
相对应版本的查询链接:Links for opencv-python - 下载
opencv
pip install opencv-python==4.6.0.66
参考:
已解决python安装opencv报错ERROR: Could not build wheels for opencv-python
快捷键:
ctrl
+/
:注释alt
+7
:调出pycharm
中的结构
4.正式使用tensorboard
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
image_path = "dataset/val/ants/17081114_79b9a27724.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
##需要将图像格式转化为numpy
print(type(img_array))
print(img_array.shape)
writer.add_image("train",img_array,1, dataformats='HWC')
#图像的表示形式一般是长*宽*三个通道(RGB),但是默认的是CHW,所以需要改变参数
# y=2x
for i in range(100):
writer.add_scalar("y=2x",3*i,i)
writer.close()
可以不断的改变image_path
和train
这个title
,可视化训练的过程,可以用tensorboard --logdir=logs --port=6007
命令查看,改变图像的路径和Title
后,可以不断的刷新网页