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
|
img_path = "images/5e233a14c9334a84a879795ee679d2c1.jpg" img_path_abs = "E:\coding\torch_test\images\5e233a14c9334a84a879795ee679d2c1.jpg"
|
2022.3.27
PyCharm控制台python shell与IPython shell的切换
详见https://www.cnblogs.com/miaoning/p/11069224.html
但是要注意需要在conda的环境中安装IPython
Anaconda Prompt 切换工作路径
首先切到C盘根目录下,然后直接输入对应盘符号即可
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")
for i in range(100): writer.add_scalar("y=2x",2*i,i)
writer.close()
|
效果图
2022.3.28
基本结构
关注官方文档,函数的输入输出,使用
等方式尝试了解数据类型。
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
img_path = "images/5e233a14c9334a84a879795ee679d2c1.jpg"
img = Image.open(img_path)
writer = SummaryWriter("cslogs")
tensor_ToTensor = transforms.ToTensor() img_tensor = tensor_ToTensor(img) writer.add_image("ToTensor", img_tensor)
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)
tensor_resize = transforms.Resize([512, 512])
img_resize = tensor_ToTensor(tensor_resize(img)) writer.add_image("Resize", img_resize, 0)
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)
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() ])
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()
|