深度学习-Pytorch

深度学习-Pytorch

CGC Lv4

此为本人学习深度学习及Pytorch基础知识时所作,可能存在部分错误之处,敬请谅解。

TensorBoard

代码

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

writer = SummaryWriter("logs") # 创建实例,将事件文件储存到logs文件夹下


img_path = "./training_dataset/train/ants_image/0013035.jpg"
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

writer.add_image("test", img, 1, dataformats = "HWC") # 图像工具


for i in range(100):
writer.add_scalar("y = 3x", 3*i, i) # 画函数工具

writer.close()

其中,对writer.add_image("test", img, 1, dataformats = "HWC")
1为步数,dataformats为格式

Terminal

1
2
tensorboard --logdir=logs
# 其中logdir=事件文件所在文件夹名

输出内容为TensorBoard 2.10.0 at http://localhost:6006/ (Press CTRL+C to quit)
表示在端口6006训练\

可用tensorboard --logdir=logs --port=6007指定端口

TransForms

代码

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
from torchvision import transforms
img_path = "./training_dataset/train/ants_image/0013035.jpg"
img = Image.open(img_path)

# Totensor
trans_totensor = transforms.ToTensor() # 创建对应工具
tensor_img = trans_totensor(img) # 转化为tensor数据类型
writer.add_image("ToTensor", tensor_img)

# Normalize
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(tensor_img) # 归一化
writer.add_image("Normalize", img_norm)

# Resize
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img)
img_resize = trans_totensor(img_resize) # 改变大小
writer.add_image("Resize", img_resize, 0)

# 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("Resize2", img_resize_2, 0)

# RandomCrop
trans_random = transforms.RandomCrop(512) # 括号内用于指定大小,可分别指定长宽
trans_conpose_2 = transforms.Compose([trans_random, trans_totensor])
for i in range(10):
img_crop = trans_conpose_2(img)
writer.add_image("RandomCrop", img_crop, i) # 随机裁剪


writer.close()

torchvision

官方数据集下载

1
2
train_data = torchvision.datasets.CIFAR10(root = "./dataset_test", train = True, transform = dataset_transform, download = True)
test_set = torchvision.datasets.CIFAR10(root = "./dataset_test", train = False, transform = dataset_transform, download = True)

root为本地下载存放地址
train为True为下载训练集 为False为下载测试集
transform为图片转化的工具 如dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
download为是否下载该数据集

数据集内容 例

1
img, target = test_set[0]

对应输出为
(<PIL.Image.Image image mode=RGB size=32x32 at 0x106B06F4E50>, 3)
(此处前部分为不经过transform进行变换的默认PIL格式照片)
前部分为图片,后部分为该图片对应的对象在classes(test_set.classes)中的下标
若为

1
test_set.classes[target]

则输出对应为cat

dataloader

数据加载器

1
2
test_loader = DataLoader(dataset = test_data, batch_size = 4, shuffle = True, num_workers = 0, drop_last = False)

dataset为指定数据集(测试集)
batch_size为指定每次从dataset中去除n个数据进行打包
shuffle若为True则每次训练会打乱顺序 一般为True
num_work为主进程数量
drop_last为保存最后一次取的数据

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 准备测试集
test_data = torchvision.datasets.CIFAR10(root = "./dataset_test", train = False, transform = torchvision.transforms.ToTensor())

test_loader = DataLoader(dataset = test_data, batch_size = 4, shuffle = True, num_workers = 0, drop_last = False)

# 测试集中第一张照片及target
img, target = test_data[0]

# 得到总的imgs与targets
writer = SummaryWriter("dataloader_logs")
step = 0
for data in test_loader:
imgs, targets = data
writer.add_images("test_data", imgs, step)
step += 1

writer.close()

convolution functional

卷积层 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import torch
import torch.nn.functional as F # 卷积函数

input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]])

kernel = torch.tensor([[1, 2, 1],
[0, 1, 0],
[2, 1, 0]])

input = torch.reshape(input,(1, 1, 5, 5)) # 更改尺寸为(1, 1, 5, 5)
kernel = torch.reshape(kernel,(1, 1, 3, 3)) # 更改尺寸为(1, 1, 3, 3)

output = F.conv2d(input, kernel, stride = 1, padding = 0)
print(output)

对于torch.reshape(input,(1, 1, 5, 5))
shape为四通道
第一位为channel通道数量
第二位为输入channel/group(batch_size)
三四位分别为高和宽
及有dilation 空洞卷积

F.conv2d意为二维卷积
stride为卷积核移动步数,padding为填充情况

conv2d小历程

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
import torch
import torchvision
from torch.utils.data import DataLoader
from torch import nn
from torch.nn import Conv2d
from torch.utils.tensorboard import SummaryWriter
import cv2

dataset = torchvision.datasets.CIFAR10("./conv2d_dataset", train = False, transform = torchvision.transforms.ToTensor(),download = True)

dataloader = DataLoader(dataset, batch_size = 64)

class Fun(nn.Module):
def __init__(self):
super(Fun, self).__init__()
self.conv1 = Conv2d(in_channels = 3, out_channels = 6, kernel_size = 3, stride = 1, padding = 1)

def forward(self, x):
x = self.conv1(x)
return x

test = Fun()

writer = SummaryWriter("con2d_logs")
step = 0

for data in dataloader:
imgs, targets = data
output = test(imgs)

writer.add_images("input", imgs, step)

output = torch.reshape(output, (-1, 3, 32, 32))
writer.add_images("output",output, step)

step += 1

将数据集中的图片进行卷积操作,即卷积层

池化层

最大池化

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
import torch
from torch import nn
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("maxpool_data", train = False, download = True, transform = torchvision.transforms.ToTensor())

dataLoader = DataLoader(dataset, batch_size= 64)


class Test(nn.Module):
def __init__(self):
super(Test, self).__init__()
self.maxpool1 = nn.MaxPool2d(kernel_size = 3, ceil_mode = True)

def forward(self,input):
output = self.maxpool1(input)
return output

test = Test()

step = 0

writer = SummaryWriter("maxpool_logs")
for data in dataLoader:
imgs, targets = data
writer.add_images("input", imgs, step)

output = test(imgs)
writer.add_images("output", output, step)
step += 1

其中self.maxpool1 = nn.MaxPool2d()(kernel_size = 3, ceil_mode = True)中的ceil_mode 为补全,为True即当kernel加载到不满九个数据的位置时仍然计

池化可以缩减目标空间 增加运行速度

非线性激活

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
import torch
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter


dataset = torchvision.datasets.CIFAR10("maxpool_data", train = False, download = True, transform = torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size = 64)

class test(torch.nn.Module):
def __init__(self):
super(test, self).__init__()
self.relu1 = torch.nn.ReLU()
self.sigmoid1 = torch.nn.Sigmoid()

def forward(self, input):
output = self.relu1(input)
return output

test = test()
writer = SummaryWriter("relu_logs")
step = 0

for data in dataloader:
imgs, targets = data
writer.add_images("input", imgs, step)
output = test(imgs)
writer.add_images("output", output, step)
step += 1

writer.close()

线性层

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
from modulefinder import Module
import torchvision
from torch.utils.data import DataLoader
import torch

dataset = torchvision.datasets.CIFAR10("maxpool_data", train = False, download = True, transform = torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size = 64)

class test(torch.nn.Module):
def __init__(self):
super(test, self).__init__()
self.linear1 = torch.nn.Linear(196608, 10)

def forward(self, input):
output = self.linear1(input)
return output

test = test()

for data in dataloader:
imgs, targets = data
print(imgs.shape)
# output = torch.reshape(imgs, (1, 1, 1, -1))
output = torch.flatten(imgs) # 将矩阵展开成一行
print(output.shape)
output = test(output)
print(output.shape)

也作为全连接层?

Sequential及网络搭建小历程

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
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch
from torch.utils.tensorboard import SummaryWriter



class test(torch.nn.Module):
def __init__(self):
super(test, self).__init__()
# self.conv1 = torch.nn.Conv2d(3, 32, 5, padding = 2)
# self.maxpool1 = torch.nn.MaxPool2d(2)
# self.conv2 = torch.nn.Conv2d(32, 32, 5, padding = 2)
# self.maxpool2 = torch.nn.MaxPool2d(2)
# self.conv3 = torch.nn.Conv2d(32, 64, 5, padding = 2)
# self.maxpool3 = torch.nn.MaxPool2d(2)
# self.flatten = torch.nn.Flatten()
# self.linear1 = torch.nn.Linear(1024, 64)
# self.linear2 = torch.nn.Linear(64, 10)

self.model1 = torch.nn.Sequential(
torch.nn.Conv2d(3, 32, 5, padding = 2),
torch.nn.MaxPool2d(2),
torch.nn.Conv2d(32, 32, 5, padding = 2),
torch.nn.MaxPool2d(2),
torch.nn.Conv2d(32, 64, 5, padding = 2),
torch.nn.MaxPool2d(2),
torch.nn.Flatten(),
torch.nn.Linear(1024, 64),
torch.nn.Linear(64, 10)
)

def forward(self, x):
# x = self.conv1(x)
# x = self.maxpool1(x)
# x = self.conv2(x)
# x = self.maxpool2(x)
# x = self.conv3(x)
# x = self.maxpool3(x)
# x = self.flatten(x)
# x = self.linear1(x)
# x = self.linear2(x)

x = self.model1(x)

return x

test = test()
print(test)
input = torch.ones((64, 3, 32, 32))
output = test(input)
print(output.shape)

write = SummaryWriter("seq_logs")
write.add_graph(test, input) # 输出运行的树图
write.close()

损失函数

1
2
3
4
5
6
7
8
inputs = torch.tensor([1, 2, 3], dtype = torch.float32)
targets = torch.tensor([1, 2, 5], dtype = torch.float32)

inputs = torch.reshape(input, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 3, 1))

loss = torch.nn.L1loss(reduction = 'sum')
result = loss(inputs, targets)
  • Title: 深度学习-Pytorch
  • Author: CGC
  • Created at: 2023-03-14 21:29:53
  • Updated at: 2023-07-09 20:06:24
  • Link: https://redefine.ohevan.com/2023/03/14/深度学习-Pytorch/
  • License: This work is licensed under CC BY-NC-SA 4.0.