-
Notifications
You must be signed in to change notification settings - Fork 0
/
colab.pynb
132 lines (107 loc) · 3.88 KB
/
colab.pynb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import torch
from torchvision import datasets, transforms
from torch.utils.data import dataloader
import torch.nn as nn
import torch
import torch.nn.functional as F
import torch.nn as nn
# 定义残差块
class ResBlk(nn.Module):
def __init__(self, ch_in, ch_out, stride):
super(ResBlk, self).__init__()
self.conv1 = nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(ch_out)
self.conv2 = nn.Conv2d(ch_out, ch_out, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(ch_out)
self.extra = nn.Sequential(
nn.Conv2d(ch_in, ch_out, kernel_size=1, stride=stride),
nn.BatchNorm2d(ch_out)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out = self.extra(x) + out
out = F.relu(out)
return out
# 定义ResNet18网络结构
class ResNet18(nn.Module):
def __init__(self):
super(ResNet18, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=3, padding=0),
nn.BatchNorm2d(64)
)
self.blk1 = ResBlk(64, 64, stride=2)
self.blk2 = ResBlk(64, 128, stride=2)
self.blk3 = ResBlk(128, 256, stride=2)
self.blk4 = ResBlk(256, 512, stride=2)
self.outlayer = nn.Linear(512*1*1, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.blk1(x)
x = self.blk2(x)
x = self.blk3(x)
x = self.blk4(x)
x = F.adaptive_avg_pool2d(x, [1, 1])
x = x.view(x.size(0), -1)
x = self.outlayer(x)
return x
batch_sz = 128
learn_rate = 1e-3
train_num = 1000
# 导入训练集数据
train_data = dataloader.DataLoader(
datasets.CIFAR10(root='data/', train=True, transform=transforms.Compose([
transforms.Resize((32, 32)), # 重新设置图片大小
transforms.ToTensor(), # 将图片转化为tensor
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]) # 进行归一化
]), download=True), shuffle=True, batch_size=batch_sz
)
# 导入测试集数据
train_test = dataloader.DataLoader(
datasets.CIFAR10(root='data/', train=False, transform=transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
]), download=True), shuffle=True, batch_size=batch_sz
)
device = torch.device('cuda') # 设置在GPU上进行训练
# 定义模型
model = ResNet18().to(device)
# 定义损失函数和优化方式
criteon = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learn_rate)
# 训练模型
for epoch in range(10):
model.train()
for batch_idx, (x, label) in enumerate(train_data):
x = x.to(device)
label = label.to(device)
logits = model(x) # 经过模型得到的数据
loss = criteon(logits, label)
#print('logits:', logits[0])
#print('label:', label[0])
# 反向传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx == len(train_data) - 1:
print(epoch, 'loss:', loss.item())
# 进行测试
model.eval()
with torch.no_grad():
total_correct = 0
total_num = 0
for x, label in train_test:
x = x.to(device)
label = label.to(device)
logits = model(x)
pred = logits.argmax(dim=1)
correct = torch.eq(pred, label).float().sum().item()
total_correct += correct
total_num += x.size(0)
acc = total_correct / total_num
print('完成第{}轮!'.format(epoch+1))
print(epoch, 'test acc测试准确率:', acc)