Opencv读取图片,获得numpy型数据类型
复制图片的相对路径
目前这种type不适用,考虑用numpy类型
安装opencv,在pytorch环境下
pip install opencv-python
导入numpy
import numpy as np
将PIL类型的img转换为 NumPy 数组
img_array=np.array(img)
HWC三通道
H:高度 W:宽度 C:通道
from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Imagewriter = SummaryWriter("logs") 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)writer.add_image("test",img_array,1,dataformats='HWC')# for i in range(100): # writer.add_scalar("y=2x",3*i,i)writer.close()
从PIL到numpy,需要在add_image()中指定shape中每一个数字/维表示的含义
终端运行
tensorboard --logdir=logs --port=6007
点击蓝色链接
点击“IMAGES”
来到
修改一下
使用另一张图片的路径,运行
from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Imagewriter = SummaryWriter("logs") 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)# writer.add_image("test",img_array,1,dataformats='HWC') writer.add_image("test",img_array,2,dataformats='HWC')# for i in range(100): # writer.add_scalar("y=2x",3*i,i)writer.close()
回到网站,进行刷新
刷新后
拖动滑轮进行图片查看
拖到左边后,可以看到之前的图片
更换标签
from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Imagewriter = SummaryWriter("logs") # image_path="dataset/train/ants_image/0013035.jpg" image_path="dataset/train/ants_image/5650366_e22b7e1065.jpg" img_PIL=Image.open(image_path) img_array=np.array(img_PIL) print(type(img_array)) print(img_array.shape)# writer.add_image("test",img_array,1,dataformats='HWC') # writer.add_image("test",img_array,2,dataformats='HWC') writer.add_image("train",img_array,1,dataformats='HWC')# for i in range(100): # writer.add_scalar("y=2x",3*i,i)writer.close()
运行后来到网站查看
参考
【PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】】 https://www.bilibili.com/video/BV1hE411t7RN/?p=9&share_source=copy_web&vd_source=be33b1553b08cc7b94afdd6c8a50dc5a