小土堆视频笔记

小土堆pytorch学习记录

1
2
3
4
5
6
7
8
9
10
11
过程记录
1.cmd中nvidia-smi 可以查看最高支持的cuda版本
2.创建与删除虚拟环境
conda create -n myenv python=3.8
conda remove -n myenv --all
查看虚拟环境的路径
conda info --envs
conda env list
3.在torch官网中,最好使用pip install ,不用conda install下载torch
4.在虚拟环境中 需要使用conda install nb_conda 下载jupyter 相关依赖
这样就可以在jupyter中使用虚拟环境(应该和ipython相关)

1. torchvision.datasets以及DataLoader的使用

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

test_data = torchvision.datasets.CIFAR10(root='dataset1',train=False,transform=torchvision.transforms.ToTensor())
data_loader = DataLoader(test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=False)

writer = SummaryWriter("logs")

for epoch in range(2):
step = 0
for data in data_loader:
img, label = data
writer.add_images("dataloader {}".format(epoch), img, step)
step = step + 1

writer.close()

#在terminal中输入tensorboard --logdir=logs

2. 文件的创建以及写入

1
2
with open(os.path.join(root_path,out_path,"{}.txt".format(name)), 'w') as f:
f.write(content)

3.PIL Image的转换

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
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
from torchvision import transforms


1.将Image转换为np.array, shape=H*W*C
writer = SummaryWriter("logs")
root_path = "dataset/train/ants_image/0013035.jpg"
image = Image.open(root_path)
image.show() #查看图片
image = np.array(image)
writer.add_image("test",image,1,dataformats='HWC')

2.将Image转换为tensor 不需要转换通道,tensor格式就是c*h*w
trans = transforms.ToTensor()
image = trans(image)
writer.add_image("test",image,1)

3.先resize然后转换为tensor
tran_resize2 = transforms.Resize(512)
tran_tensor = transforms.ToTensor()
tran_compose = transforms.Compose([tran_resize2,tran_tensor])
img_compose = tran_compose(image)

4.自定义dataset

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
from torch.utils.data import Dataset
from PIL import Image
import os

class mydata(Dataset):

def __init__(self, root_path, label_path):
self.root_path = root_path
self.label_path = label_path
self.img_path = os.path.join(root_path, label_path)
self.img_list = os.listdir(self.img_path)

def __getitem__(self, idx):
img_name = self.img_list[idx]
img = Image.open(os.path.join(self.img_path, img_name))
label = self.label_path
return img, label

def __len__(self):
return len(self.img_list)

root_path = "dataset/train"
ants_label_path = "ants"
bees_label_path = "bees"
ants_datalist = mydata(root_path, ants_label_path)
bees_datalist = mydata(root_path, bees_label_path)
train_datalist = ants_datalist + bees_datalist

5.改变tensor维度

1
2
3
4
5
6
7
8
9
10
11
12
13
(64*1*28*28)
import torch
from torch import nn
a = torch.randn(64,1,28,28)

a=a.view(-1,28*28)

a=torch.reshape(a,(-1,28*28))

flatten = nn.Flatten()
a=flatten(a)

a = a.reshape(a.shape[0],-1)

6.model的保存与加载

1
2
3
4
5
6
7
8
9
10
非预训练的vgg16模型
vgg16 = torchvision.models.vgg16(weights=None)
两种保存方式
1.torch.save(vgg16, "vgg16.pth")
2.torch.save(vgg16.state_dict(), 'vgg16.pth')

对应的两种加载方式
1.vgg16=torch.load('vgg16.pth')
2.vgg16 = torchvision.models.vgg16(weights=None)
vgg16.load_state_dict(torch.load('vgg16.pth'))

7.总的模型

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import torch
import torchvision

#获取数据
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

train_data = torchvision.datasets.CIFAR10('data',train=True,download=True,transform=torchvision.transforms.ToTensor())
test_data = torchvision.datasets.CIFAR10('data',train=True,download=True,transform=torchvision.transforms.ToTensor())

train_loader = DataLoader(train_data,batch_size=64,shuffle=True)
test_loader = DataLoader(test_data, batch_size=64, shuffle=True)

#数据长度
train_size = len(train_data)
test_size = len(test_data)


#加载模型
class Tudui(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, padding=2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, padding=2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, padding=2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(1024, 64),
nn.Linear(64, 10)
)
def forward(self, x):
output = self.model(x)
return output

tudui = Tudui()
tudui = tudui.cuda()

#损失函数
loss_func = nn.CrossEntropyLoss()
loss_func = loss_func.cuda()

#优化器
optimizer = torch.optim.SGD(tudui.parameters(),lr=0.01)

#
epoch = 20
train_cnt = 0
test_cnt = 0

#
writer = SummaryWriter("nn_model")

for i in range(epoch):
train_loss = 0
print("---------第{}次迭代开始了------------".format(i+1))
for data in train_loader:
imgs, labels = data
imgs = imgs.cuda()
labels = labels.cuda()
output = tudui(imgs)
loss = loss_func(output, labels)

optimizer.zero_grad()
loss.backward()
optimizer.step()

train_loss += loss.item()
train_cnt += 1
if train_cnt % 100 == 0:
print("第{}次训练的损失是{}".format(train_cnt,loss.item()))
writer.add_scalar('train_loss',loss.item(), train_cnt)

test_loss = 0
acc = 0
with torch.no_grad():
for data in test_loader:
imgs, labels = data
imgs = imgs.cuda()
labels = labels.cuda()
output = tudui(imgs)
loss = loss_func(output, labels)
acc += (output.argmax(1) == labels).sum()
test_loss += loss


print("第{}次迭代后的损失为{}".format(i,test_loss))
print("第{}次迭代后的acc为{}".format(i,acc/test_size))
writer.add_scalar("test_loss",loss,i)

torch.save(tudui, 'vgg_{}.pth'.format(i))

writer.close()

8.其余的一些函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.numpy转换为tensor
a = np.array([[1,2],[3,4]])
b=torch.from_numpy(a)
pirnt(b)

2.其余类型转换为tensor
list = [1,2,3]
tensor = torch.tensor(list)
tensor = torch.tensor([[1,2],[3,4]],dtype=torch.float)


3.改变模型的某一层
vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)
vgg16_true.classifier.add_module("last",nn.Linear(1000,10))
print(vgg16_true)