-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunction_CoReD.py
236 lines (220 loc) · 9.15 KB
/
Function_CoReD.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
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from torch.nn import functional as F
from Function_common import CustumDataset
def _GetIndex(data_1):
idx = -1
if data_1 > 0.5 and data_1 <= 0.6:
idx = 0
elif data_1 >0.6 and data_1 <= 0.7:
idx = 1
elif data_1 >0.7 and data_1 <= 0.8:
idx = 2
elif data_1 >0.8 and data_1 <= 0.9:
idx = 3
elif data_1 >0.9 and data_1 <= 1.0:
idx = 4
return idx
def _GetIndex_avgfeat(data_1):
idx = -1
if data_1 > 0.5 and data_1 <= 0.6:
idx = 0
return idx
def GetSplitLoaders_BinaryClasses(list_correct,dataset,train_aug=None,num_store_per=5,batch_size=128):
correct_loader=[[],[]]
for i in range(num_store_per):
list_temp = [list_correct[i][0],list_correct[i][1]]
for rf in range(len(list_temp)):
if not list_temp[rf] :
correct_loader[rf].append([])
continue
custum = CustumDataset(np.array(dataset.data[list_correct[i][rf]]),
np.array(dataset.target[list_correct[i][rf]]),
train_aug)
correct_loader[rf].append(DataLoader(custum,
batch_size=batch_size, shuffle=False, num_workers=4, pin_memory=True))
list_length_realfakeloader = [[len(j.dataset) if j else 0 for j in i] for i in correct_loader]
print("list_length_realfakeloader :", list_length_realfakeloader)
return correct_loader,np.array(list_length_realfakeloader)/len(dataset.target)
def GetSplitLoadersRealFake(list_correct,dataset,train_aug=None,num_store_per=5):
correct_loader=[[],[]]
for i in range(num_store_per):
list_temp = [list_correct[i][0],list_correct[i][1]]
for rf in range(len(list_temp)):
if not list_temp[rf] :
correct_loader[rf].append([])
continue
temp_dataset = copy.deepcopy(dataset)
temp_dataset.data = np.array(temp_dataset.data[list_correct[i][rf]])
temp_dataset.target = np.array(temp_dataset.target[list_correct[i][rf]])
custum = CustumDataset(temp_dataset.data,temp_dataset.target,train_aug)
correct_loader[rf].append(DataLoader(custum,
batch_size=200, shuffle=False, num_workers=4, pin_memory=True))
list_length_realfakeloader = [[len(j.dataset) if j else 0 for j in i] for i in correct_loader]
return correct_loader,np.array(list_length_realfakeloader)/len(dataset.target),save_ceckpoint_for_unlearning
def GetListTeacherFeatureFakeReal(model, loader,mode='Xception',showScatter = False, device='cuda'):
list_features = [[],[]]
maxpool = nn.MaxPool2d(4)
model.eval()
with torch.no_grad():
train_results, labels = [[],[]],[[],[]]
for i in range(len(loader)):
for j in range(len(loader[i])):
if not loader[i][j] :
train_results[i].append([])
list_features[i].append(torch.tensor(0))
continue
temp = None
for _,(img, label) in enumerate(loader[i][j]):
train_results[i].append(model(img.to(device)).cpu().detach().numpy())
labels[i].append(label)
if mode == 'Efficient':
test = model.extract_features(img.to(device))
else:
test = model.features(img.to(device))
if temp is not None:
temp = torch.cat((maxpool(test),temp))
else:
temp = maxpool(test)
temp = torch.mean(temp,dim=1)
temp = torch.mean(temp,dim=0)
list_features[i].append(temp.detach().cpu())
if showScatter:
train_results[i] = np.concatenate(train_results[j])
labels = np.concatenate(labels[j])
plt.figure(figsize=(5, 5), facecolor="azure")
for label in np.unique(labels[j]):
tmp = train_results[i][labels[j]==label]
plt.scatter(tmp[:, 0], tmp[:, 1], label=label)
else: continue
plt.legend()
plt.show()
return list_features
def func_correct(model, data_loader, device='cuda'):
list_correct = [[[],[]] for i in range(5)]
model.eval()
cnt=0
with torch.no_grad():
for i, (inputs, targets) in enumerate(data_loader):
_inputs = inputs.to(device)
_targets = targets.to(device)
outputs = model(_inputs)
temp = F.softmax(outputs,dim=1)
for l in range(len(_targets)):
idx = _GetIndex(temp[l][_targets[l]].data)
if idx >= 0:
if _targets[l]==0 :
list_correct[idx][0].append(cnt)
else : list_correct[idx][1].append(cnt)
cnt += 1
return list_correct
def func_correct_avgfeat(model, data_loader, device='cuda'):
list_correct = [[[],[]] for i in range(5)]
model.eval()
cnt=0
with torch.no_grad():
for i, (inputs, targets) in enumerate(data_loader):
_inputs = inputs.to(device)
_targets = targets.to(device)
outputs = model(_inputs)
temp = F.softmax(outputs,dim=1)
for l in range(len(_targets)):
idx = _GetIndex_avgfeat(temp[l][_targets[l]].data)
if idx >= 0:
if _targets[l]==0 :
list_correct[idx][0].append(cnt)
else : list_correct[idx][1].append(cnt)
cnt+=1
return list_correct
def GetRatioData(list_real_fake,correct_cnt):
if correct_cnt == 0 :return 0
list_length_realfakeloader = np.array([[len(j) if j else 0 for j in i] for i in list_real_fake])
return list_length_realfakeloader/correct_cnt
def correct_binary(model, inputs, targets, b_ratio_Data = False, device='cuda'):
list_correct = [[[], []] for i in range(5)]
model.eval()
cnt = 0
correct_cnt=0
ratio_data = None
with torch.no_grad():
_inputs = inputs.to(device)
_targets = targets.to(device)
outputs = model(_inputs)
temp = nn.Softmax(dim=1)(outputs)
temp = temp.cpu()
for l in range(len(_targets)):
idx = _GetIndex(temp[l][_targets[l]].data)
if idx >= 0:
correct_cnt+=1
if _targets[l] == 0:
list_correct[idx][0].append((cnt,_inputs[l]))
else:
list_correct[idx][1].append((cnt,_inputs[l]))
cnt += 1
if b_ratio_Data :
ratio_data = GetRatioData(list_correct,correct_cnt)
return list_correct, ratio_data
def correct_binary_avgfeat(model, inputs, targets, b_ratio_Data = False, device='cuda'):
list_correct = [[[], []] for i in range(5)]
model.eval()
cnt = 0
correct_cnt=0
ratio_data = None
with torch.no_grad():
_inputs = inputs.to(device)
_targets = targets.to(device)
outputs = model(_inputs)
temp = nn.Softmax(dim=1)(outputs)
for l in range(len(_targets)):
idx = _GetIndex_avgfeat(temp[l][_targets[l]].data)
if idx >= 0:
correct_cnt+=1
if _targets[l] == 0:
list_correct[idx][0].append((cnt,_inputs[l]))
else:
list_correct[idx][1].append((cnt,_inputs[l]))
cnt += 1
if b_ratio_Data :
ratio_data = GetRatioData(list_correct,correct_cnt)
return list_correct, ratio_data
def correct_2_avgfeat(model, inputs, targets, device='cuda'):
list_correct = [[[], []] for i in range(5)]
model.eval()
cnt = 0
_inputs = inputs.to(device)
_targets = targets.to(device)
with torch.no_grad():
for l in range(len(_targets)):
idx = 0
if idx >= 0:
correct_cnt+=1
if _targets[l] == 0:
list_correct[idx][0].append((cnt, _inputs[l]))
else:
list_correct[idx][1].append((cnt, _inputs[l]))
cnt += 1
return list_correct
def GetFeatureMaxpool(model,list_loader,mode='Xception',device='cuda'): #list_loader : consists of index,data
feat = None
maxpool = nn.MaxPool2d(4) #If using other networks, we can consider the number '4'
if not list_loader : return 0
for idx, img in list_loader:
img = torch.reshape(img,(1,3,128,128))
if mode == 'Efficient':
feat_std = model.extract_features(img.to(device))
else:
feat_std = model.features(img.to(device))
feat_std = feat_std.to(device)
if feat is not None:
feat = torch.cat((maxpool(feat_std),feat))
else:
feat = maxpool(feat_std)
if feat is None :
feat = torch.tensor(0)
else:
feat = torch.mean(feat, dim=1)
feat = torch.mean(feat, dim=0)
return feat.view(1,-1)