-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewBN.py
218 lines (179 loc) · 6.51 KB
/
newBN.py
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
# -*- coding: utf-8 -*-
import os
import torch
import torch.nn as nn
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torch.nn.functional as F
import torch.optim as optim
from matplotlib.pyplot import figure
from radam import *
import time
from pytorch_four_things_BN import *
import matplotlib.pyplot as plt
root = './data'
if not os.path.exists(root):
os.mkdir(root)
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (1.0,))
])
train_set = dset.MNIST(root=root, train=True, transform=transforms.ToTensor(), download=True)
test_set = dset.MNIST(root=root, train=False, transform=transforms.ToTensor(), download=True)
batch_size = 128
total_epoch = 50
learning_rate = 1e-3
use_cuda = torch.cuda.is_available()
train_loader = torch.utils.data.DataLoader(
dataset=train_set,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(
dataset=test_set,
batch_size=batch_size,
shuffle=False)
"""## Model class
Simple classifier composed with 2 convolution layer and 1 linear layer
"""
class p1CNN(nn.Module):
def __init__(self):
super(p1CNN, self).__init__()
self.c1 = nn.Conv2d(1, 16, kernel_size=3, padding=1)
self.c2 = nn.Conv2d(16, 1, kernel_size=3, padding=1)
self.mp = nn.MaxPool2d(kernel_size=2)
self.li = nn.Linear(7*7, 10)
def forward(self, x, batch_norm_type=None, is_training=True):
x = self.mp(F.relu(self.c1(x)))
if batch_norm_type == 1:
# Simple Batch Normalization (batch size =128)
x = normalization_layer(x, channels_per_group=1, examples_per_group=128, is_training=is_training)
elif batch_norm_type == 2:
# Ghost Batch Normalization (ghost batch size = 16)
x = normalization_layer(x, channels_per_group=1, examples_per_group=16, is_training=is_training)
elif batch_norm_type == 3:
# Group Normalization
x = normalization_layer(x, channel_groups=32, examples_per_group=1, is_training=is_training)
elif batch_norm_type == 4:
# Batch/Group Normalization Generalization
x = normalization_layer(x, channel_groups=32, examples_per_group=2, is_training=is_training)
x = self.mp(F.relu(self.c2(x)))
x = x.view(-1, 7*7)
x = self.li(x)
return x
def weights_init(m):
classname = m.__class__.__name__
for p in model.parameters():
p.data.fill_(1)
"""## Model Assign"""
model1 = p1CNN().cuda()
model2 = p1CNN().cuda()
model3 = p1CNN().cuda()
model4 = p1CNN().cuda()
model5 = p1CNN().cuda()
tmp = p1CNN().cuda()
tmp.load_state_dict(model1.state_dict())
model2.load_state_dict(tmp.state_dict())
model3.load_state_dict(tmp.state_dict())
model4.load_state_dict(tmp.state_dict())
model5.load_state_dict(tmp.state_dict())
model_list = [model1, model2, model3, model4, model5]
optimizer1 = RAdam(model1.parameters(), lr=0.01)
optimizer2 = RAdam(model2.parameters(), lr=0.01)
optimizer3 = RAdam(model3.parameters(), lr=0.01)
optimizer4 = RAdam(model4.parameters(), lr=0.01)
optimizer5 = RAdam(model5.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()
optim_list = [optimizer1, optimizer2, optimizer3, optimizer4, optimizer5]
"""## Optimizer Assign
## Train 5 models
"""
# Train
train_loss_list, test_acc_list = [], []
for i in range(5):
tmp1, tmp2 = [], []
for j in range(total_epoch):
tmp1.append(0)
tmp2.append(0)
train_loss_list.append(tmp1)
test_acc_list.append(tmp2)
for epoch in range(total_epoch):
# trainning
for i, (model, optim) in enumerate(zip(model_list, optim_list)):
model.train()
if epoch == 0 and i == 0:
start = time.time()
total_loss = 0
total_batch = 0
for batch_idx, (x, target) in enumerate(train_loader):
if use_cuda:
x, target = x.cuda(), target.cuda()
model.train()
optim.zero_grad()
out = model(x, i)
loss = criterion(out, target)
total_loss += loss.item()
loss.backward()
optim.step()
total_batch += 1
train_loss_list[i][epoch] = total_loss / total_batch
print ('==>>> epoch: {}, batch index: {}, train loss: {:.6f}, {}'
.format(epoch, batch_idx+1, total_loss / total_batch, total_batch))
# testing
total_loss = 0
total_batch = 0
correct_cnt = 0
total_cnt = 0
for batch_idx, (x, target) in enumerate(test_loader):
model.eval()
if use_cuda:
x, target = x.cuda(), target.cuda()
out = model(x, i)
loss = criterion(out, target)
_, pred_label = torch.max(out.data, 1)
total_cnt += x.data.size()[0]
correct_cnt += (pred_label == target.data).sum().item()
total_loss += loss.item()
total_batch += 1
test_acc_list[i][epoch] = correct_cnt / total_cnt
print ('==>>> epoch: {}, batch index: {}, test loss: {:.6f}, acc: {:.3f}'
.format(epoch, batch_idx+1, total_loss / total_batch, correct_cnt * 1.0 / total_cnt))
if epoch == 0 and i == 0:
end = time.time()
estimate = end - start
total = estimate * 9 * total_epoch
print(f'{total // 60} minute and {total % 60} sec left')
"""## Plot result, using subplot"""
figure(num=None, figsize=(16, 7), dpi=80, facecolor='w', edgecolor='k')
plt.subplot(121)
for line in train_loss_list:
plt.plot(line)
plt.legend(labels=('Plain', 'BN', 'GBN', 'GN', 'B/GNG'))
plt.xlabel('epoch')
plt.ylabel('Loss')
plt.grid()
plt.subplot(122)
for line in test_acc_list:
plt.plot(line)
plt.legend(labels=('Plain', 'BN', 'GBN', 'GN', 'B/GNG'))
plt.xlabel('epoch')
plt.ylabel('Acc')
plt.grid()
plt.suptitle('Comparison for No BN, 4 kinds of BNs')
plt.show()
figure(num=None, figsize=(16, 7*4), dpi=80, facecolor='w', edgecolor='k')
for i in range(5):
plt.subplot2grid((5, 2), (i, 0))
plt.plot(train_loss_list[i])
plt.legend(labels=(str(i)))
plt.xlabel('epoch')
plt.ylabel('Loss')
plt.grid()
for i in range(5):
plt.subplot2grid((5, 2), (i, 1))
plt.plot(test_acc_list[i])
plt.legend(labels=(str(i)))
plt.xlabel('epoch')
plt.ylabel('Loss')
plt.grid()
plt.suptitle('Comparison for No BN, 4 kinds of BNs')
plt.show()