pytorch入门

pytorch入门

视频链接

https://www.bilibili.com/video/BV1hE411t7RN?p=12&spm_id_from=pageDriver

常用快捷键

shift+enter 换行(可以在console中换行)

ctrl+p 看到当前函数需要哪些参数

基本说明

jupyter适合于查看帮助文档等

注意点

文件路径

windows下文件路径使用\\\\

尽量使用相对路径,绝对路径在windows下可能被当成转义符:

1
2
3
4
# absolute path = "E:\coding\torch_test\images\5e233a14c9334a84a879795ee679d2c1.jpg"
# relative path = "images/5e233a14c9334a84a879795ee679d2c1.jpg"
img_path = "images/5e233a14c9334a84a879795ee679d2c1.jpg"
img_path_abs = "E:\coding\torch_test\images\5e233a14c9334a84a879795ee679d2c1.jpg"

image-20220328105947199


2022.3.27

PyCharm控制台python shell与IPython shell的切换

详见https://www.cnblogs.com/miaoning/p/11069224.html

但是要注意需要在conda的环境中安装IPython

Anaconda Prompt 切换工作路径

首先切到C盘根目录下,然后直接输入对应盘符号即可

image-20220327100336654

Tensorboard

打开log文件并指定端口(此处为 http://localhost:6007/)

1
tensorboard --logdir=logs --port=6007

demo1:

1
2
3
4
5
6
7
8
9
10
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("logs")

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

writer.close()

效果图

image-20220327124109596


2022.3.28

Transforms

基本结构

image-20220328110649328

关注官方文档,函数的输入输出,使用

1
2
print
print(type())

等方式尝试了解数据类型。

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms

# absolute path = "E:\coding\torch_test\images\5e233a14c9334a84a879795ee679d2c1.jpg"
# relative path = "images/5e233a14c9334a84a879795ee679d2c1.jpg"
img_path = "images/5e233a14c9334a84a879795ee679d2c1.jpg"
# img_path_abs = "E:\coding\torch_test\images\5e233a14c9334a84a879795ee679d2c1.jpg"
img = Image.open(img_path)

writer = SummaryWriter("cslogs")

# ToTensor
tensor_ToTensor = transforms.ToTensor() # 创建ToTensor对象
img_tensor = tensor_ToTensor(img) # 这里其实调用了ToTensor的内置函数
writer.add_image("ToTensor", img_tensor)

# Normalize
tensor_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = tensor_norm(img_tensor)
writer.add_image("Normalize", img_norm)

# Resize
tensor_resize = transforms.Resize([512, 512])
# PIL -->Resize--> resize PIL -->ToTensor--> resize tensor
img_resize = tensor_ToTensor(tensor_resize(img))
writer.add_image("Resize", img_resize, 0)
# Compose style
tensor_resize_2 = transforms.Resize(512)
tensor_compose = transforms.Compose([tensor_resize_2, tensor_ToTensor])
img_resize_compose = tensor_compose(img)
writer.add_image("Resize", img_resize_compose, 1)

# RandomCrop
tensor_random = transforms.RandomCrop(512)
tensor_compose_2 = transforms.Compose([tensor_random, tensor_ToTensor])
for i in range(25):
img_crop = tensor_compose_2(img)
writer.add_image("RamdomCrop", img_crop, i)

writer.close()

Torchvision数据集使用

CIFAR10为例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import torchvision
from torch.utils.tensorboard import SummaryWriter

dataset_transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
# 一般download选项都设置为true
train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transform, download=True)
test_set = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=dataset_transform, download=True)

writer = SummaryWriter("P10")
for i in range(10):
img, tatget = train_set[i]
writer.add_image("Train Images", img, i)

writer.close()

pytorch入门
http://example.com/2022/02/27/pytorch-intro-1/
作者
Thunderbolt
发布于
2022年2月27日
许可协议