-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
350 lines (303 loc) · 11.9 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import torch
import torch.nn as nn
import numpy as np
from scipy.spatial.distance import cdist
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
from PIL import Image
from torch.utils.data import Dataset
import torch.nn.functional as F
from sklearn.metrics import confusion_matrix
import os.path as osp
def Entropy(input_):
bs = input_.size(0)
entropy = -input_ * torch.log(input_ + 1e-5)
entropy = torch.sum(entropy, dim=1)
return entropy
def discrepancy(out1, out2):
out2_t = out2.clone()
out2_t = out2_t.detach()
out1_t = out1.clone()
out1_t = out1_t.detach()
#return (F.kl_div(F.log_softmax(out1), out2_t) + F.kl_div(F.log_softmax(out2), out1_t)) / 2
#return F.kl_div(F.log_softmax(out1), out2_t, reduction='none')
return (F.kl_div(F.log_softmax(out1), out2_t, reduction='none')
+F.kl_div(F.log_softmax(out2), out1_t, reduction='none')) / 2
#return F.kl_div(F.log_softmax(out1), out2_t, reduction='batchmean')
class CrossEntropyLabelSmooth(nn.Module):
def __init__(self,
num_classes,
epsilon=0.1,
use_gpu=True,
size_average=True):
super(CrossEntropyLabelSmooth, self).__init__()
self.num_classes = num_classes
self.epsilon = epsilon
self.use_gpu = use_gpu
self.size_average = size_average
self.logsoftmax = nn.LogSoftmax(dim=1)
def forward(self, inputs, targets):
log_probs = self.logsoftmax(inputs)
targets = torch.zeros(log_probs.size()).scatter_(
1,
targets.unsqueeze(1).cpu(), 1)
if self.use_gpu: targets = targets.cuda()
targets = (1 -
self.epsilon) * targets + self.epsilon / self.num_classes
if self.size_average:
loss = (-targets * log_probs).mean(0).sum()
else:
loss = (-targets * log_probs).sum(1)
return loss
def cal_acc(loader, netF, netB, netC):
start_test = True
with torch.no_grad():
iter_test = iter(loader)
for i in range(len(loader)):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
outputs = netC(netB(netF(inputs)))
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
_, predict = torch.max(all_output, 1)
accuracy = torch.sum(
torch.squeeze(predict).float() == all_label).item() / float(
all_label.size()[0])
mean_ent = torch.mean(Entropy(
nn.Softmax(dim=1)(all_output))).cpu().data.item()
return accuracy, mean_ent
def cal_acc_(loader, netF, netC):
start_test = True
with torch.no_grad():
iter_test = iter(loader)
for i in range(len(loader)):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
output_f = netF.forward(inputs) # a^t
outputs=netC(output_f)
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
_, predict = torch.max(all_output, 1)
accuracy = torch.sum(
torch.squeeze(predict).float() == all_label).item() / float(
all_label.size()[0])
mean_ent = torch.mean(Entropy(
nn.Softmax(dim=1)(all_output))).cpu().data.item()
return accuracy, mean_ent
def cal_acc_proto(loader, netF, netC,proto):
start_test = True
with torch.no_grad():
iter_test = iter(loader)
for i in range(len(loader)):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
outputs = netF.forward(inputs) # a^t
#outputs=F.normalize(outputs,dim=-1,p=2)
#outputs = netC(output_f)
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
all_output_np=np.array(all_output)
center=proto
center = center.float().detach().cpu().numpy()
dist=torch.from_numpy(cdist(all_output_np,center))
_, predict = torch.min(dist, 1)
accuracy = torch.sum(
torch.squeeze(predict).float() == all_label).item() / float(
all_label.size()[0])
return accuracy, accuracy
def cal_acc_sda(loader, netF,netC,t=0):
start_test = True
with torch.no_grad():
iter_test = iter(loader)
for i in range(len(loader)):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
outputs,_ = netF.forward(
inputs,t=t) # a^t
outputs = netC(outputs)
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
_, predict = torch.max(all_output, 1)
accuracy = torch.sum(
torch.squeeze(predict).float() == all_label).item() / float(
all_label.size()[0])
mean_ent = torch.mean(Entropy(
nn.Softmax(dim=1)(all_output))).cpu().data.item()
return accuracy, mean_ent
def image_train(resize_size=256, crop_size=224):
return transforms.Compose([
transforms.Resize((resize_size, resize_size)),
transforms.RandomCrop(crop_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
def image_target(resize_size=256, crop_size=224):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
return transforms.Compose([
transforms.Resize((resize_size, resize_size)),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(), normalize
])
def image_shift(resize_size=256, crop_size=224):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
return transforms.Compose([
transforms.Resize((resize_size, resize_size)),
transforms.ColorJitter(0.2, 0.2, 0.2, 0.1),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(), normalize
])
def image_test(resize_size=256, crop_size=224):
return transforms.Compose([
transforms.Resize((resize_size, resize_size)),
transforms.CenterCrop(crop_size),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
def make_dataset(image_list, labels):
if labels:
len_ = len(image_list)
images = [(image_list[i].strip(), labels[i, :]) for i in range(len_)]
else:
if len(image_list[0].split()) > 2:
images = [(val.split()[0],
np.array([int(la) for la in val.split()[1:]]))
for val in image_list]
else:
images = [(val.split()[0], int(val.split()[1]))
for val in image_list]
return images
def rgb_loader(path):
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
def l_loader(path):
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('L')
class ImageList(Dataset):
def __init__(self,
image_list,
labels=None,
transform=None,
target_transform=None,
mode='RGB'):
imgs = make_dataset(image_list, labels)
self.imgs = imgs
self.transform = transform
self.target_transform = target_transform
if mode == 'RGB':
self.loader = rgb_loader
elif mode == 'L':
self.loader = l_loader
def __getitem__(self, index):
path, target = self.imgs[index]
# for visda
img = self.loader(path)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.imgs)
def office_load(args):
train_bs = args.batch_size
if args.home == True:
ss = args.dset.split('2')[0]
tt = args.dset.split('2')[1]
if ss == 'a':
s = 'Art'
elif ss == 'c':
s = 'Clipart'
elif ss == 'p':
s = 'Product'
elif ss == 'r':
s = 'Real_World'
if tt == 'a':
t = 'Art'
elif tt == 'c':
t = 'Clipart'
elif tt == 'p':
t = 'Product'
elif tt == 'r':
t = 'Real_World'
s_tr, s_ts = './data/office-home/{}.txt'.format(
s), './data/office-home/{}.txt'.format(s)
txt_src = open(s_tr).readlines()
dsize = len(txt_src)
tv_size = int(0.8*dsize)
print(dsize, tv_size, dsize - tv_size)
s_tr, s_ts = torch.utils.data.random_split(txt_src, [tv_size, dsize - tv_size])
t_tr, t_ts = './data/office-home/{}.txt'.format(
t), './data/office-home/{}.txt'.format(t)
prep_dict = {}
prep_dict['source'] = image_train()
prep_dict['target'] = image_target()
prep_dict['test'] = image_test()
train_source = ImageList(s_tr,
transform=prep_dict['source'])
test_source = ImageList(s_ts,
transform=prep_dict['source'])
train_target = ImageList(open(t_tr).readlines(),
transform=prep_dict['target'])
test_target = ImageList(open(t_ts).readlines(),
transform=prep_dict['test'])
dset_loaders = {}
dset_loaders["source_tr"] = DataLoader(train_source,
batch_size=train_bs,
shuffle=True,
num_workers=args.worker,
drop_last=False)
dset_loaders["source_te"] = DataLoader(test_source,
batch_size=train_bs * 2,#2
shuffle=True,
num_workers=args.worker,
drop_last=False)
dset_loaders["target"] = DataLoader(train_target,
batch_size=train_bs,
shuffle=True,
num_workers=args.worker,
drop_last=False)
dset_loaders["test"] = DataLoader(test_target,
batch_size=train_bs * 3,#3
shuffle=True,
num_workers=args.worker,
drop_last=False)
return dset_loaders