-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
295 lines (225 loc) · 9.6 KB
/
utils.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
import torch
from torchvision.models.resnet import ResNet, Bottleneck, BasicBlock
from torchvision import models
from torchvision.datasets.folder import ImageFolder
import numpy as np
from sklearn.metrics import auc, roc_auc_score, average_precision_score
from sklearn.metrics import precision_recall_curve
import gflags
import sys
import torch.nn as nn
from torch.utils.data.sampler import Sampler
import torch.distributed as dist
# Flags = gflags.FLAGS
def resnet18():
model = ResNet(BasicBlock, [2, 2, 2, 2], num_classes=1)
return model
# def resnet18():
# # model = ResNet(BasicBlock, [2, 2, 2, 2], num_classes=200)
# net = models.resnet18(True)
# #Finetune Final few layers to adjust for tiny imagenet input
# net.avgpool = nn.AdaptiveAvgPool2d(1)
# net.fc.out_features = 1
# return net
def resnet34():
model = ResNet(BasicBlock, [3, 4, 6, 3], num_classes=1)
return model
def resnet50():
model = ResNet(Bottleneck, [3, 4, 6, 3], num_classes=1)
return model
class MnistModel(nn.Module):
def __init__(self):
super(MnistModel, self).__init__()
self.conv1 = nn.Conv2d(1, 20, kernel_size=5)
self.conv2 = nn.Conv2d(20, 50, kernel_size=5)
self.fc1 = nn.Linear(800, 500)
self.fc2 = nn.Linear(500, 1)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(2)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, (nn.Conv2d)):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 1 / m.bias.numel())
if isinstance(m, (nn.Linear)):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 1 / m.bias.numel())
def forward(self, x):
x = self.maxpool(self.relu(self.conv1(x)))
x = self.maxpool(self.relu(self.conv2(x)))
x = x.view(-1, 800)
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
class FMnistModel(nn.Module):
def __init__(self):
super(FMnistModel, self).__init__()
print("Fashion Mnist")
self.conv1 = nn.Conv2d(1, 5, kernel_size=3)
self.conv2 = nn.Conv2d(5, 10, kernel_size=3)
self.fc1 = nn.Linear(250, 100)
self.fc2 = nn.Linear(100, 1)
self.tanh = nn.Tanh()
self.maxpool = nn.MaxPool2d(2)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, (nn.Conv2d)):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 1 / m.bias.numel())
if isinstance(m, (nn.Linear)):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 1 / m.bias.numel())
def forward(self, x):
x = self.maxpool(self.tanh(self.conv1(x)))
x = self.maxpool(self.tanh(self.conv2(x)))
x = x.view(-1, 250)
x = self.tanh(self.fc1(x))
x = self.fc2(x)
return x
class CIFARModel(nn.Module):
def __init__(self):
super(CIFARModel, self).__init__()
# convolutional layer
self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
# max pooling layer
self.pool = nn.MaxPool2d(2, 2)
self.relu = nn.ReLU(inplace=True)
# fully connected layers
self.fc1 = nn.Linear(64 * 4 * 4, 512)
self.fc2 = nn.Linear(512, 64)
self.fc3 = nn.Linear(64, 1)
def forward(self, x):
# add sequence of convolutional and max pooling layers
x = self.pool(self.relu(self.conv1(x)))
x = self.pool(self.relu(self.conv2(x)))
x = self.pool(self.relu(self.conv3(x)))
# flattening
x = x.view(-1, 64 * 4 * 4)
# fully connected layers
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
class MLP(nn.Module):
def __init__(self, input):
super(MLP, self).__init__()
self.fc1 = nn.Linear(input, 28)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Linear(28, 1)
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
class AUPRCSampler(Sampler):
def __init__(self, labels, batchSize, posNum=1):
# positive class: minority class
# negative class: majority class
self.labels = labels
self.posNum = posNum
self.batchSize = batchSize
# print("AUPRCSampler", self.posNum, self.batchSize)
self.clsLabelList = np.unique(labels)
self.dataDict = {}
for label in self.clsLabelList:
self.dataDict[str(label)] = []
for i in range(len(self.labels)):
self.dataDict[str(self.labels[i])].append(i)
self.ret = []
def __iter__(self):
minority_data_list = self.dataDict[str(1)]
majority_data_list = self.dataDict[str(0)]
# print("AUPRCSampler222", len(minority_data_list), len(majority_data_list))
# print(len(minority_data_list), len(majority_data_list))
np.random.shuffle(minority_data_list)
np.random.shuffle(majority_data_list)
# In every iteration : sample 1(posNum) positive sample(s), and sample batchSize - 1(posNum) negative samples
if len(minority_data_list) // self.posNum > len(majority_data_list)//(self.batchSize - self.posNum): # At this case, we go over the all positive samples in every epoch.
# extend the length of majority_data_list from len(majority_data_list) to len(minority_data_list)* (batchSize-posNum)
majority_data_list.extend(np.random.choice(majority_data_list, len(minority_data_list) // self.posNum * (self.batchSize - self.posNum) - len(majority_data_list), replace=True).tolist())
elif len(minority_data_list) // self.posNum < len(majority_data_list)//(self.batchSize - self.posNum): # At this case, we go over the all negative samples in every epoch.
# extend the length of minority_data_list from len(minority_data_list) to len(majority_data_list)//(batchSize-posNum) + 1s
minority_data_list.extend(np.random.choice(minority_data_list, len(majority_data_list) // (self.batchSize - self.posNum)*self.posNum - len(minority_data_list), replace=True).tolist())
# print("AUPRCSampler333", len(minority_data_list), len(majority_data_list))
self.ret = []
for i in range(len(minority_data_list) // self.posNum):
self.ret.extend(minority_data_list[i*self.posNum:(i+1)*self.posNum])
startIndex = i *(self.batchSize - self.posNum)
endIndex = (i+1)*(self.batchSize - self.posNum)
self.ret.extend(majority_data_list[startIndex:endIndex])
return iter(self.ret)
def __len__ (self):
return len(self.ret)
def ave_prc(targets, preds):
return average_precision_score(targets, preds)
def test_classification(model, test_loader, device):
model.eval()
preds = torch.Tensor([]).to(device)
targets = torch.Tensor([]).to(device)
# if Flags.datasets == 'tiny':
for (inputs, target) in test_loader:
inputs = inputs.to(device)
target = target.to(device).float()
if torch.max(target) > 1:
print("ERRRORR----- test_classification ----")
# else:
# target[target <= 4] = 0
# target[target > 4] = 1
with torch.no_grad():
out = model(inputs)
if out.shape[1] == 1:
pred = torch.sigmoid(out) ### prediction real number between (0,1)
else:
print("ERROR !!!")
preds = torch.cat([preds, pred], dim=0)
targets = torch.cat([targets, target], dim=0)
return preds, targets
# return ave_prc(targets.cpu().detach().numpy(), preds.cpu().detach().numpy()),
def evalutation(model, test_set, device):
model.eval()
#### testing #######
test_pred = []
test_true = []
with torch.no_grad():
for batch_idx, (data, target) in enumerate(test_set):
data = data.to(device)
# target = target.to(device)
y_pred = torch.sigmoid(model(data))
test_pred.append(y_pred.cpu().detach().numpy())
test_true.append(target.numpy())
test_true = np.concatenate(test_true)
test_pred = np.concatenate(test_pred)
print("============")
print(ave_prc(test_true, test_pred))
precision, recall, _ = precision_recall_curve(test_true, test_pred)
# print("######## PRC ##########")
# for i in range(len(precision)):
# print(precision[i], recall[i])
an_array = np.concatenate((np.expand_dims(precision, axis = 1), np.expand_dims(recall, axis = 1)), axis=1)
mat = np.matrix(an_array)
return mat
def save_pr(preds, targets):
#### save to .txt ###
precision, recall, _ = precision_recall_curve(targets.cpu().detach().numpy(), preds.cpu().detach().numpy())
an_array = np.concatenate((np.expand_dims(precision, axis = 1),
np.expand_dims(recall, axis = 1)), axis=1)
mat = np.matrix(an_array)
filename = "PR/PR_" + Flags.datasets + '_' + Flags.method + ".txt"
with open(filename, "w") as f:
for line in mat:
np.savetxt(f, line, fmt='%.2f')
def ring_reduce(rank, left, right, msg, device):
msg = msg.contiguous().cpu()
lef_buff = msg.clone()
rig_buff = msg.clone()
####### simple ####
req0 = dist.isend(msg, dst=right)
dist.recv(lef_buff, src=left)
req0.wait()
req0 = dist.isend(msg, dst=left)
dist.recv(rig_buff, src=right)
req0.wait()
return (lef_buff + msg + rig_buff).to(device) / 3