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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
| # 필요한 라이브러리 임포트
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import visdom
import torchvision.models.resnet as resnet
# Visdom 초기화
vis = visdom.Visdom()
vis.close(env="main")
# Value Tracker 함수 정의
def value_tracker(value_plot, value, num):
'''num, loss_value are Tensors'''
vis.line(X=num,
Y=value,
win=value_plot,
update='append')
# 장치 설정
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print (f"device is {device}")
# 랜덤 시드 설정
torch.manual_seed(777)
if device == 'cuda':
torch.cuda.manual_seed_all(777)
# 데이터셋 전처리 및 정규화
transform = transforms.Compose([
transforms.ToTensor()
])
# CIFAR10 데이터셋 로드 및 평균/표준편차 계산
trainset = torchvision.datasets.CIFAR10(root='./cifar10', train=True, download=True, transform=transform)
train_data_mean = trainset.data.mean(axis=(0, 1, 2)) / 255
train_data_std = trainset.data.std(axis=(0, 1, 2)) / 255
# 데이터셋 변환 설정
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize(train_data_mean, train_data_std)
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(train_data_mean, train_data_std)
])
# 데이터셋 준비
trainset = torchvision.datasets.CIFAR10(root='./cifar10', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True, num_workers=0)
testset = torchvision.datasets.CIFAR10(root='./cifar10', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=256, shuffle=False, num_workers=0)
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# ResNet 모델 정의
conv1x1 = resnet.conv1x1
Bottleneck = resnet.Bottleneck
BasicBlock = resnet.BasicBlock
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000, zero_init_residual=False):
super(ResNet, self).__init__()
self.inplanes = 16
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.relu = nn.ReLU(inplace=True)
self.layer1 = self._make_layer(block, 16, layers[0], stride=1)
self.layer2 = self._make_layer(block, 32, layers[1], stride=1)
self.layer3 = self._make_layer(block, 64, layers[2], stride=2)
self.layer4 = self._make_layer(block, 128, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(128 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
nn.BatchNorm2d(planes * block.expansion),
)
layers = [block(self.inplanes, planes, stride, downsample)]
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
# ResNet50 모델 생성
resnet50 = ResNet(Bottleneck, [3, 4, 6, 3], 10, True).to(device)
# 학습 준비
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(resnet50.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
lr_sche = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.5)
# Loss 및 Accuracy 플롯 초기화
loss_plt = vis.line(Y=torch.Tensor(1).zero_(), opts=dict(title='Loss Tracker', legend=['Loss'], showlegend=True))
acc_plt = vis.line(Y=torch.Tensor(1).zero_(), opts=dict(title='Accuracy', legend=['Accuracy'], showlegend=True))
# Accuracy 체크 함수 정의
def acc_check(net, test_set, epoch, save=1):
correct = 0
total = 0
with torch.no_grad():
for data in test_set:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
acc = 100 * correct / total
print(f'Accuracy of the network on the 10000 test images: {acc:.2f}%')
if save:
torch.save(net.state_dict(), f"./model/model_epoch_{epoch}_acc_{int(acc)}.pth")
return acc
# 모델 학습
epochs = 150
for epoch in range(epochs):
running_loss = 0.0
# lr_sche.step()
for i, data in enumerate(trainloader, 0):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = resnet50(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 30 == 29:
value_tracker(loss_plt, torch.Tensor([running_loss / 30]), torch.Tensor([i + epoch * len(trainloader)]))
print(f'[{epoch + 1}, {i + 1}] loss: {running_loss / 30:.3f}')
running_loss = 0.0
lr_sche.step()
acc = acc_check(resnet50, testloader, epoch, save=1)
value_tracker(acc_plt, torch.Tensor([acc]), torch.Tensor([epoch]))
print('Finished Training')
# 최종 모델 평가
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = resnet50(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 10000 test images: {100 * correct / total:.2f}%')
# [1, 30] loss: 2.039
# [1, 60] loss: 1.885
# [1, 90] loss: 1.775
# [1, 120] loss: 1.753
# [1, 150] loss: 1.670
# [1, 180] loss: 1.609
# Accuracy of the network on the 10000 test images: 40.86%
# [2, 30] loss: 1.527
# [2, 60] loss: 1.472
# [2, 90] loss: 1.439
# [2, 120] loss: 1.368
# [2, 150] loss: 1.341
# [2, 180] loss: 1.268
# Accuracy of the network on the 10000 test images: 47.97%
# ...
# [28, 30] loss: 0.153
# [28, 60] loss: 0.154
# [28, 90] loss: 0.159
# [28, 120] loss: 0.160
# [28, 150] loss: 0.176
# [28, 180] loss: 0.178
# Accuracy of the network on the 10000 test images: 85.07%
# [29, 30] loss: 0.180
# [29, 60] loss: 0.149
# [29, 90] loss: 0.153
# [29, 120] loss: 0.154
# [29, 150] loss: 0.169
# [29, 180] loss: 0.167
# Accuracy of the network on the 10000 test images: 84.50%
# ...
# [39, 30] loss: 0.051
# [39, 60] loss: 0.056
# [39, 90] loss: 0.048
# [39, 120] loss: 0.057
# [39, 150] loss: 0.057
# [39, 180] loss: 0.064
# Accuracy of the network on the 10000 test images: 85.56%
# [40, 30] loss: 0.066
# [40, 60] loss: 0.058
# [40, 90] loss: 0.054
# [40, 120] loss: 0.065
# [40, 150] loss: 0.063
# [40, 180] loss: 0.064
# Accuracy of the network on the 10000 test images: 85.76%
|