小土堆pytorch学习笔记002

目录

1、TensorBoard的使用

(1)显示坐标:

 (2)显示图片:

2、Transform的使用

3、常见的Transforms

(1)#ToTensor()

(2)# Normalize()

(3)#Resize()

(4)#Compose()

4、总结:


1、TensorBoard的使用

(1)显示坐标:

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

writer = SummaryWriter("logs")   # 写入的位置  log_dir = 'logs
# writer.add_image()
"""
 y = x
 
"""
for i in range(100):
    writer.add_scalar("y=x", i, i)

writer.close()

运行:

小土堆pytorch学习笔记002_第1张图片 浏览器运行结果:

小土堆pytorch学习笔记002_第2张图片

# 运行
>tensorboard --logdir=logs        #会打开http://localhost:6006/

>tensorboard --logdir=logs --port=6007   (#增加端口设置)

 (2)显示图片:

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

writer = SummaryWriter("logs")   # 写入的位置  log_dir = 'logs

"""
参数:
    def add_image(
        self, tag, img_tensor, global_step=None, walltime=None, dataformats="CHW"
    ):
    其中:
     img_tensor (torch.Tensor, numpy.ndarray, or string/blobname): Image data
"""
image_path = "./hymenoptera_data/train/ants/0013035.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
print(type(img_array))
print(img_array.shape)   # (512, 768, 3)   H高度,W宽度,C通道数

writer.add_image("test", img_array, 5, dataformats="HWC")  # 从PIL到numpy,需要在add_image()中指定shape中每一个数字/维表示的含义。


"""
 y = x
 
 参数:
 def add_scalar(
        self,  
        tag,   # 标签
        scalar_value,   # y轴
        global_step=None,   # x轴
        walltime=None,
        new_style=False,
        double_precision=False,
    ):
"""
for i in range(100):
    writer.add_scalar("y=2x", 3*i, i)

writer.close()

结果展示:

小土堆pytorch学习笔记002_第3张图片

2、Transform的使用

(1)transforms该如何使用(python)

from PIL import Image
from torchvision import transforms

# python的用法  ==>tensor
"""
通过 transforms.ToTensors去解决两个问题:
    1、transforms该如何使用(python)
    2、为什么我们需要Tensor数据类型
"""

img_path = "./dataset/Snipaste_01.jpg"
img = Image.open(img_path)
print(img)

# 1、transforms该如何使用(python)
tensor_trans = transforms.ToTensor
tensor_img = tensor_trans().__call__(img)  # 将img转换成 tensor_img的图片

print(tensor_img)

运行结果:

小土堆pytorch学习笔记002_第4张图片
(2)为什么我们需要Tensor数据类型  (在之后的神经网络会用到,前向传播、反向传播等)

from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms


# python的用法  ==>tensor
"""
通过 transforms.ToTensors去解决两个问题:
    1、transforms该如何使用(python)
    2、为什么我们需要Tensor数据类型
"""

img_path = "./dataset/Snipaste_01.jpg"
img = Image.open(img_path)
print(img)

# 1、transforms该如何使用(python)
tensor_trans = transforms.ToTensor
tensor_img = tensor_trans().__call__(img)  # 将img转换成 tensor_img的图片
print(tensor_img)


writer = SummaryWriter("logs_")
writer.add_image("Tensor_img", tensor_img)
writer.close()


运行结果截图:

小土堆pytorch学习笔记002_第5张图片

3、常见的Transforms

小土堆pytorch学习笔记002_第6张图片

(1)#ToTensor()

(2)# Normalize()

from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = "./dataset/Snipaste_01.jpg"
img = Image.open(img_path)
print(img)


writer = SummaryWriter("logs__")

# ToTensor()
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# Normalize
"""
output[channel] = (input[channel] - mean[channel]) / std[channel]
"""
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([6,3,5],[3,2,1])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm)


writer.close()

Noumalize() 和ToTensor()运行结果:

小土堆pytorch学习笔记002_第7张图片

(3)#Resize()

(4)#Compose()

from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = "./dataset/Snipaste_01.jpg"
img = Image.open(img_path)
print(img)


writer = SummaryWriter("logs__")

# ToTensor()
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# Normalize
"""
output[channel] = (input[channel] - mean[channel]) / std[channel]
"""
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([6,3,5],[3,2,1])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm)


# Resize()
print(img.size)
trans_resize = transforms.Resize((512, 512))
# img PIL -> resize ->img_resize  PIL
img_resize = trans_resize(img)
print(img_resize)
# img_resize PIL -> totensor -> img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize)


#Compose   --resize -2
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize_compose", img_resize_2, 1)



writer.close()

4、总结:

①关注输入和输出类型。

②多看官方文档。

③关注方法需要哪些参数

④查看数据类型 type( xxx)

你可能感兴趣的:(深度学习,pytorch,学习,笔记)