forked from MaoYuwei/batch_shuffling
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5class_with_imbalance_v3.py
330 lines (257 loc) · 9.9 KB
/
5class_with_imbalance_v3.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# same class in each batch
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.optim as optim
import random
import numpy as np
from collections import Counter
from Tkinter import _flatten
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
import matplotlib.pyplot as plt
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Sequential(
torch.nn.Conv2d(3, 16, 3, padding=1), # 3*32*32 -> 16*32*32
torch.nn.ReLU(),
)
self.conv2 = nn.Sequential(
torch.nn.Conv2d(16, 32, 3, padding=1), # 16*32*32 -> 32*32*32
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, 2) # 32*32*32-> 32*16*16
)
self.conv3 = nn.Sequential(
torch.nn.Conv2d(32, 64, 3, padding=1), # 32*16*16 -> 64*16*16
torch.nn.ReLU(),
)
# self.conv4 = nn.Sequential(
# torch.nn.Conv2d(64, 128, 3, padding=1), # 64*16*16 -> 128*16*16
# torch.nn.ReLU(),
# torch.nn.MaxPool2d(2, 2) # 128*16*16 -> 128*8*8
# )
# self.conv5 = nn.Sequential(
# torch.nn.Conv2d(128, 256, 3, padding=1), # 128*8*8 -> 256*8*8
# torch.nn.ReLU(),
# torch.nn.MaxPool2d(2, 2) # 256*8*8 -> 256*4*4
# )
#
# self.gap = nn.AvgPool2d(4, 4)
# self.fc = torch.nn.Linear(256, 10)
self.fc1 = torch.nn.Linear(64*16*16, 1024)
self.fc2 = torch.nn.Linear(1024, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
# x = self.conv4(x)
# x = self.conv5(x)
# x = self.gap(x)
x = x.view(-1, 64*16*16)
x = self.fc1(x)
x = self.fc2(x)
return x
def run(k, trainset, testloader, epochs, batch_size):
seed = 34
# print("seed: ", seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
model = Net()
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001)
train_loss_list = []
test_loss_list = []
best_r = 0
best_epoch = 0
best_model = None
for epoch in range(epochs): # loop over the dataset multiple times
shuffle_seed = k*N+epoch
seeds.append(shuffle_seed)
# print('shuffle_seed: ', shuffle_seed)
np.random.seed(shuffle_seed) # Numpy module.
random.seed(shuffle_seed) # Python random module.
indices = list(i for i in range(27500))
random.shuffle(indices)
indices = CreateIndices(indices, trainset, batch_size)
new_trainset = torch.utils.data.Subset(trainset, indices)
trainloader = torch.utils.data.DataLoader(new_trainset,
batch_size=batch_size,
shuffle=False,
num_workers=0)
running_loss = 0.0
# train
model.train()
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() * labels.size()[0]
train_loss = running_loss / 27500
train_loss_list.append(train_loss)
# test
model.eval()
correct = 0
total = 0
running_loss = 0.0
with torch.no_grad():
for i, data in enumerate(testloader, 0):
# get the inputs
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
running_loss += loss.item() * labels.size()[0]
# get correct
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
test_loss = running_loss / 5500
test_loss_list.append(test_loss)
# print('epoch: {}, train loss: {}, test loss: {}'.format(epoch, train_loss, test_loss))
# print('Accuracy of the network on the 10000 test images: %.5f %%' % (
# 100 * correct / float(total)))
result = 100 * correct / float(total)
if result>best_r:
best_r =result
best_epoch = epoch
best_model = model
return best_r, best_epoch, train_loss_list, test_loss_list
def CreateIndices(init_indices, trainset, batch_size):
datasets = []
labelsets = []
for data, label in trainset:
datasets.append(data)
labelsets.append(label)
label_indices = [[] for _ in range(10)]
for i in range(27500):
indice = init_indices[i]
label_indices[labelsets[indice]].append(indice)
label_list = list(range(10))
random.shuffle(label_list)
# print(label_list)
first_half = []
second_half = []
for i in range(5):
first_half.append(label_indices[label_list[i]])
second_half.append(label_indices[label_list[5+i]])
indices = []
indices_tmp = list(map(list, zip(*first_half)))
indices_tmp = list(_flatten(indices_tmp))
indices.extend(indices_tmp)
indices_tmp = list(map(list, zip(*second_half)))
indices_tmp = list(_flatten(indices_tmp))
indices.extend(indices_tmp)
# print(len(indices))
label_indices = [x[500:] for x in label_indices[5:]]
# print(len(label_indices))
# print(len(label_indices[0]))
# print(len(label_indices[-1]))
indices_tmp = list(map(list, zip(*label_indices)))
indices_tmp = list(_flatten(indices_tmp))
indices.extend(indices_tmp)
batch_ind = range(53)
random.shuffle(batch_ind)
final_indices = []
for i in batch_ind:
tmp = indices[i*batch_size: (i+1)*batch_size]
final_indices.extend(tmp)
final_indices.extend(init_indices[27136:])
# indices = final_indices
# print(len(indices))
# new_trainset = torch.utils.data.Subset(trainset, indices)
#
# trainloader = torch.utils.data.DataLoader(
# new_trainset, batch_size=batch_size)
#
# # batch_lists = [[] for _ in range(49)]
# for step, (data, label) in enumerate(trainloader):
# print("Step: ", step)
# label = label.numpy()
# print(Counter(label))
return final_indices
def construct_imbalance_data(trainset):
print(len(trainset))
minority = []
for i in range(5):
data = [x for x in trainset if (x[-1] == i)]
minority.extend(data[:int(len(trainset)/100)])
print(len(minority))
majority = [x for x in trainset if (x[-1] > 4)]
print(len(majority))
trainset = minority + majority
print(len(trainset))
return trainset
if __name__ == '__main__':
# load datasets
batch_size_list = [512]
classes_num = 5
epochs = 100
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data',
train=True,
download=True,
transform=transform)
testset = torchvision.datasets.CIFAR10(root='./data',
train=False, download=True,
transform=transform)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
trainset = construct_imbalance_data(trainset)
testset = construct_imbalance_data(testset)
for batch_size in batch_size_list:
testloader = torch.utils.data.DataLoader(testset,
batch_size=batch_size, shuffle=False,
num_workers=2)
N = 20 # running times
seeds = []
results = []
train_loss_ret = []
test_loss_ret = []
for k in range(N):
best_r, best_epoch, train_loss_list, test_loss_list = run(k, trainset, testloader, epochs, batch_size)
print('running time: {}, best results: {}, best epoch: {}'.format(k, best_r, best_epoch))
results.append(best_r)
# ax1 = plt.subplot(range(epochs), loss_list, '-')
ax1 = plt.subplot(2, 1, 1)
ax2 = plt.subplot(2, 1, 2)
plt.sca(ax1)
plt.plot(range(epochs), train_loss_list, '-')
plt.sca(ax2)
plt.plot(range(epochs), test_loss_list, '-')
train_loss_ret.append(train_loss_list)
test_loss_ret.append(test_loss_list)
plt.xlabel('epochs')
plt.ylabel('loss')
plt.title('model name=imbalance_test')
plt.show()
print('batch_size: ', batch_size)
# print("seeds: ", seeds)
print("results: ", results)
print("mean: ", np.mean(results))
print("variance: ", np.var(results))
print('std: ', np.std(results))
print(train_loss_ret)
print(test_loss_ret)
test_loss_ret = np.array(test_loss_ret)
test_min = test_loss_ret.min(axis=0)
test_max = test_loss_ret.max(axis=0)
plt.plot(range(epochs), test_min, '-')
plt.plot(range(epochs), test_max, '-')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.title('model name=imbalance_v_test')
plt.show()