-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_nabirds.py
1266 lines (1008 loc) · 45.2 KB
/
eval_nabirds.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
with open('config.json', 'r') as file:
config_info = json.load(file)
#DATA_init: not used for this code
FOLDER_init = config_info["FOLDER_init"]
#FOLDER_init = '/FOLDER_init/' #Location where this repogistory "ProxyDR" is located.
import argparse
parser = argparse.ArgumentParser(description= 'Evaluate models for NABirds data')
parser.add_argument('--GPU' , default=2, type=int, help='GPU number')
parser.add_argument('--method' , default='DR', help='DR/normface/softmax')
parser.add_argument('--distance' , default='euc', help='euc/arccos')
parser.add_argument('--backbone' , default='resnet50', help='backbone: inception / resnet50')
parser.add_argument('--rand_backbone' , action='store_true', help='randomly initialized backbone (instead of pretrained backbone)')
parser.add_argument('--seed' , default=1, type=int, help='seed for training (, validation) and test data split')
#parser.add_argument('--batch_size' , default=32, type=int, help='batch for training (, validation) and test')
parser.add_argument('--small' , action='store_true', help='use small training dataset (5000 training images)')
parser.add_argument('--use_val' , action='store_true', help='use validation set to find best model')
parser.add_argument('--last' , action='store_true', help='use the last (epoch) model for evaluattion')
parser.add_argument('--aug' , action='store_true', help='use augmentation')
parser.add_argument('--resize' , default=32, type=int, help='resize image size. Default=32 (no change). 224: Resnet50 default size. 299: Inception-v3 default size.')
parser.add_argument('--clspri' , action='store_true', help='use class prior probability')
parser.add_argument('--ema' , action='store_true', help='use exponential moving average (EMA) for proxy')
parser.add_argument('--alpha' , default=0.01, type=float, help='alpha for EMA')
parser.add_argument('--mds_W' , action='store_true', help='use MDS (multi-dimensional scaling) for proxy')
parser.add_argument('--beta' , default=1.0, type=float, help='parameter beta for distance transformation T (only used in MDS). T(d)=pi*d/(beta+2*d) or T(d)=d/(beta+d)')
parser.add_argument('--dynamic' , action='store_true', help='update scale factor for AdaCos/AdaDR')
parser.add_argument('--CORR' , action='store_true', help='CORR loss proposed by Barz and Denzler (2019). It requires --mds_W==True to be turned on. Otherwise, it will be ignored. --dynamic==True has no effect')
params= parser.parse_args()
params.dataset='nabirds'
params.batch_size=32
#print(params.method, params.backbone, params.ema)
import torch
import numpy as np
import math
import argparse
from torch.autograd import Variable
from torch.utils.data import DataLoader
import pandas as pd
import sys
import os, time
os.environ["CUDA_VISIBLE_DEVICES"]=str(params.GPU)
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
#from sklearn.metrics import top_k_accuracy_score #Requires python version 3.7 or newer
params.dataset='nabirds'
if params.dataset=='nabirds':
DATA_TAG = 'nabird'
else:
print('Unknown dataset!')
raise
if params.small:
DATA_TAG = 'nabirdS'
gray = False
data_info = pd.read_csv(FOLDER_init+'ProxyDR/'+params.dataset+'_info.csv')
data_cls = pd.read_csv(FOLDER_init+'ProxyDR/nabirds_cls2.csv',delimiter='|')
N= len(data_info) #Number of images: 48562
if params.use_val:
N_train = int(0.7*N) #70%
N_val = int(0.1*N) #10%
N_test = N-N_train-N_val #20%
else:
N_train = int(0.9*N) #90%
N_test = N-N_train #10%
n_classes = len(data_cls) #Number of clasees
if params.small:
N_train = 5000
if params.use_val:
N_val= 30000
N_test=N-N_train-N_val #13562
else:
N_test=N-N_train
n_classes = len(data_cls) #Number of clasees
data_cls2=data_cls.copy()
pd.options.mode.chained_assignment = None
max_len=1
for i in range(len(data_cls2)):
tmp_Path = data_cls2['Hierarchy'][i] #.replace(DATA_PATH,'')
data_cls2['Hierarchy'][i] = tmp_Path.split('//')
if len(data_cls2['Hierarchy'][i])>max_len:
max_len=len(data_cls2['Hierarchy'][i])
def calc_dist(l1, l2, mode='diff'):
dist=0
len1, len2 = len(l1), len(l2)
len_min = np.minimum(len1, len2)
chk_sm=-1
for i in range(len_min):
tmp_chk_sm=(l1[i]==l2[i])
if tmp_chk_sm:
chk_sm=i
else:
break
if mode=='diff': #Only difference in hierarchy
return (len1-chk_sm-1)+(len2-chk_sm-1)
elif mode=='whole': #Assume each class has depth 10 in hierarchy
return (max_len-chk_sm-1)+(max_len-chk_sm-1)
else:
raise
tree_dist = np.ones([len(data_cls2),len(data_cls2)])*np.nan
for i in range(len(data_cls2)):
for j in range(len(data_cls2)):
tree_dist[i,j] = calc_dist(data_cls2['Hierarchy'][i], data_cls2['Hierarchy'][j], mode='diff')
chk_depth=0
def higher_cls(inds, level=0, return_list=False, verbose=0):
global chk_depth
h_cls_list=[]
for i in range(len(data_cls2)):
depth = len(data_cls2['Hierarchy'][i])
#print(depth)
if depth>level:
h_cls_list.append(data_cls2['Hierarchy'][i][level])
else:
if chk_depth==0:
print('Some label will be used instead of higher level class (due to \'depth<=level\')!\n depth:',depth, 'level:',level)
h_cls_list.append(data_cls2['Hierarchy'][i][-1])
chk_depth=1
unq_inds=np.unique(h_cls_list, return_index=True)[1]
h_cls_list_unq = [h_cls_list[ind_] for ind_ in sorted(unq_inds)]
#h_cls_list_unq = sorted(set(h_cls_list), key=h_cls_list.index) #Without repeats
h_cls_list_unq = np.array(h_cls_list_unq)
h_cls_list = np.array(h_cls_list)
cls2ind = {cls: ind for ind, cls in enumerate(h_cls_list_unq)}
if verbose==1:
print('Number of higher classes:',len(h_cls_list_unq))
elif verbose==2:
print('Number of higher classes:',len(h_cls_list_unq), '\ncls2ind:',cls2ind) #h_cls_list_unq)
else:
pass
h_cls = h_cls_list[inds]
h_cls_ind= list(map(lambda x: cls2ind[x], h_cls))
if return_list:
return h_cls, h_cls_ind, h_cls_list_unq
else:
return h_cls, h_cls_ind
from torch.utils.data.dataset import Dataset
from torchvision import transforms
from PIL import Image
import torchvision.transforms.functional as TF
class NabirdsDataset(Dataset):
def __init__(self, info_path, cls_path, transform=None):
self.data_info = pd.read_csv(info_path)
self.data_cls = pd.read_csv(cls_path)
self.transform = transform
def __getitem__(self, index):
img_path = self.data_info.iloc[index,0]
img = Image.open(img_path).convert('RGB')
cls = self.data_info.iloc[index,1]
label = torch.tensor(int(self.data_cls.loc[self.data_cls['Class']==cls,'Number']))
if self.transform is not None:
img = self.transform(img)
if params.resize!=32:
img = TF.resize(img, size=[params.resize,params.resize]) #size=[299,299]) #
return (img, label)
def __len__(self):
return len(self.data_info)
nabirds_dataset = NabirdsDataset(info_path=FOLDER_init+'ProxyDR/'+params.dataset+'_info.csv',
cls_path=FOLDER_init+'ProxyDR/'+params.dataset+'_cls.csv',transform=None)
if params.use_val:
train_set, val_set, test_set = torch.utils.data.random_split(nabirds_dataset, lengths=[N_train, N_val, N_test], generator=torch.Generator().manual_seed(params.seed))
else:
train_set, test_set = torch.utils.data.random_split(nabirds_dataset, lengths=[N_train, N_test], generator=torch.Generator().manual_seed(params.seed))
class MapDataset(torch.utils.data.Dataset):
def __init__(self, dataset, map_fn):
self.dataset = dataset
self.map = map_fn
def __getitem__(self, index):
img, label= self.dataset[index]
img = self.map(img)
return (img, label)
def __len__(self):
return len(self.dataset)
AUG=params.aug
if AUG:
#In eval, we don't use augmentation
aug_eval=transforms.ToTensor()
train_set_tf = MapDataset(train_set, aug_eval)
else:
train_set_tf = MapDataset(train_set, transforms.ToTensor())
if params.use_val:
val_set_tf = MapDataset(val_set, transforms.ToTensor())
test_set_tf = MapDataset(test_set, transforms.ToTensor())
trainloader = DataLoader(train_set_tf, batch_size=params.batch_size, shuffle=True)
if AUG:
trainloader2 = DataLoader(MapDataset(train_set, aug_eval), batch_size=50, shuffle=True) #To analyze training
else:
trainloader2 = DataLoader(MapDataset(train_set, transforms.ToTensor()), batch_size=50, shuffle=True) #To analyze training
if params.use_val:
valloader = DataLoader(val_set_tf, batch_size=params.batch_size)
if AUG:
valloader2 = DataLoader(MapDataset(val_set, aug_eval), batch_size=200, shuffle=True) #To analyze training
else:
valloader2 = DataLoader(MapDataset(val_set, transforms.ToTensor()), batch_size=200, shuffle=True)
testloader = DataLoader(test_set_tf, batch_size=params.batch_size)
testloader2 = DataLoader(test_set, batch_size=200, shuffle=True)
'''Analyze training set'''
use_cuda = torch.cuda.is_available()
labels_np=np.ones(0)*np.nan
t1=time.time()
if params.clspri:
for i, data in enumerate(trainloader2):
# get the inputs; data is a list of [inputs, labels]
if i%100==0:
t2=time.time()
print(i)
print('Spend time:', t2-t1)
inputs, labels = data
if use_cuda:
inputs, labels = inputs.cuda(), labels.cuda()
labels_np=np.concatenate([labels_np, labels.cpu().numpy()],axis=0)
cls_unq, cls_cnt= np.unique(labels_np,return_counts=True)
log_cls_prior = np.log(cls_cnt/np.sum(cls_cnt)) #np.log(cls_cnt)-np.mean(np.log(cls_cnt))
'''Define backbones'''
from typing import Any
import torch.nn.functional as F
from torch import nn, Tensor
class Net(nn.Module):
def __init__(self, distance='arccos'):
super().__init__()
self.W = F.normalize(torch.randn([n_classes,128]))
self.W = torch.nn.Parameter(self.W)
self.distance= distance
def forward(self):
self.WN = self.W/(torch.norm(self.W, p=2, dim =1,keepdim=True)+ 1e-6)
cos_sim_W = torch.mm(self.WN, self.WN.t())
euc_W = (torch.clip(2-2*cos_sim_W,0,4)+1e-12)**0.5
arc_cos_W = torch.acos(torch.clip(cos_sim_W,-1,1)*(1-1e-6)) #torch.acos(torch.clip(cos_sim_W,-1,1))
if self.distance=='arccos':
return arc_cos_W
elif self.distance=='euc':
return euc_W
else:
raise
if params.mds_W:
net = Net(distance= params.distance)
net = net.cuda()
W_optim = torch.optim.Adam(net.parameters(),lr=1e-3, weight_decay=0.)
if params.distance=='arccos':
tree_dist2 = (np.pi/2)*tree_dist/(params.beta+tree_dist)
elif params.distance=='euc':
tree_dist2 = (2**0.5)*tree_dist/(params.beta+tree_dist)
else:
raise
stresses_ = []
for it in range(1000):
net.train()
dist = torch.tensor(tree_dist2,dtype=torch.float)
dist = dist.to('cuda')
W_optim.zero_grad()
dist_W = net()
stress = torch.norm(dist-dist_W)/torch.norm(dist)
#stress_sq = torch.norm(dist-arc_cos_W)**2/torch.norm(dist)**2
stress.backward()
#stress_sq.backward()
W_optim.step()
stresses_.append(stress.detach().cpu().numpy())
net.eval()
net.WN = F.normalize(net.W).detach()
class BasicConv2d(nn.Module):
def __init__(self, in_channels: int, out_channels: int, **kwargs: Any) -> None:
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)
self.bn = nn.BatchNorm2d(out_channels, eps=0.001)
def forward(self, x: Tensor) -> Tensor:
x = self.conv(x)
x = self.bn(x)
return F.relu(x, inplace=True)
def Inception3(method):
model = torch.hub.load('pytorch/vision:v0.10.0', 'inception_v3', pretrained=not params.rand_backbone)
model.Conv2d_1a_3x3 = BasicConv2d(3, 32, kernel_size=3, stride=2)
model.fc=torch.nn.Linear(in_features=2048, out_features=128, bias=True)
model.final_feat_dim = 128
model.aux_logits=None
model.AuxLogits=None
model.transform_input=False
if method in ['DR','normface']:
model.W = F.normalize(torch.randn([n_classes,128]))
model.W = torch.nn.Parameter(model.W)
#model._forward(x), _ = model._forward(x)
model.scale = 10
elif method=='softmax':
model.W = torch.randn([n_classes,128])
model.W = torch.nn.Parameter(model.W)
model.b = torch.zeros([1, n_classes]) #bias term
model.b = torch.nn.Parameter(model.b)
else:
raise
return model
def ResNet50(method):
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=not params.rand_backbone)
model.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
model.fc = nn.Linear(in_features=512 * 4, out_features=128, bias=True) #in_features=512 * block.expansion
#model.final_feat_dim = 128
#model.aux_logits=None
#model.AuxLogits=None
#model.transform_input=False
if method in ['DR','normface']:
model.W = F.normalize(torch.randn([n_classes,128]))
model.W = torch.nn.Parameter(model.W)
#model._forward(x), _ = model._forward(x)
model.scale = 10
elif method=='softmax':
model.W = torch.randn([n_classes,128])
model.W = torch.nn.Parameter(model.W)
model.b = torch.zeros([1, n_classes]) #bias term
model.b = torch.nn.Parameter(model.b)
else:
raise
return model
def get_discriminality(model, loader):
global use_cuda
feats_np = np.ones([0,128])*np.nan
labels_np = np.ones([0])*np.nan
model.eval()
for i, data in enumerate(loader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
if use_cuda:
inputs, labels = inputs.cuda(), labels.cuda()
feats = model(inputs.detach()) #torch.Size([32, 128])
feats_n = F.normalize(feats).detach()
feats_np = np.concatenate([feats_np, feats_n.cpu().numpy()],axis=0)
labels_np = np.concatenate([labels_np, labels.cpu().numpy()],axis=0)
indexes = [np.where(labels_np==i)[0] for i in range(len(data_cls))]
proto_ = [np.mean(feats_np[indexes[i]],axis=0,keepdims=True) for i in range(len(data_cls))]
mu = np.concatenate(proto_, axis=0) #[48, 128] #Before normalization
proto_ = [proto_[i]/(np.linalg.norm(proto_[i],axis=-1,keepdims=True)+1e-12) for i in range(len(data_cls))]
proto = np.concatenate(proto_, axis=0) #[48, 128]
np.random.seed(params.seed)
for i in range(len(data_cls)):
if len(indexes[i])==0:
feats_rand = np.random.normal(size=128)
proto[i,:] = feats_rand/(np.linalg.norm(feats_rand,keepdims=True)+1e-12)
model.proto = torch.tensor(proto,dtype=torch.float,device = model.W.device) #Without NaNs
WN = F.normalize(model.W.detach())
W_np = WN.cpu().numpy()
mu_dot = np.matmul(mu, mu.transpose()) #[48,48]
mu_sq = np.sum(mu**2,axis=1,keepdims=True) #[48,1]
if np.sum(np.isnan(mu_sq))>=1:
nonnan_ind = np.logical_not(np.isnan(mu_sq)[:,0])
mu_dot_ = mu_dot[nonnan_ind,:] #[?, 48]
sum_inter = 2*mu_sq[nonnan_ind]-2*mu_dot_[:,nonnan_ind] #[?, ?]
len_mu = np.sum(nonnan_ind)
else:
sum_inter = 2*mu_sq-2*mu_dot #[48, 48]
len_mu = len(mu) #np.sum(nonnan_ind)
var_inter = np.sum(sum_inter)/(len_mu*(len_mu-1))
feat_sqs = [np.sum(feats_np[indexes[i]]**2,axis=1,keepdims=True) for i in range(len(data_cls))] #Each element: [N_i,1]
feat_dots = [np.matmul(feats_np[indexes[i]], mu[i, None].transpose()) for i in range(len(data_cls))] #Each element: [N_i,1]
sum_intra = [np.sum(feat_sqs[i]+mu_sq[i,None]-2*feat_dots[i])/len(indexes[i]) for i in range(len(data_cls)) if len(indexes[i])>=1] #Each element: [N_i,1]->1
var_intra = np.sum(sum_intra)/len_mu
cos_sim_ = np.matmul(proto, W_np.transpose())
arccos_ = np.arccos(np.clip(cos_sim_,-1,1))
arccos_proto_W = arccos_[np.arange(len(proto)),np.arange(len(proto))]
return var_inter, var_intra, arccos_proto_W
def top_k_correct(label, score, k=5):
#label: [n], score: [n,n_classes]
y_preds=np.argsort(-score,axis=-1)[:,:k] #[n,k]
correct_np=np.zeros_like(label,dtype=bool)
for i in range(k):
correct_np=np.logical_or(correct_np,y_preds[:,i]==label)
return np.sum(correct_np)
def top_k_AHD(label, score, k=5):
#label: [n], score: [n,n_classes]
#global tree_dist
y_preds=np.argsort(-score,axis=-1)[:,:k] #[n,k]
AHD_sum=0.0
for i in range(k):
AHD_sum+=np.sum(tree_dist[y_preds[:,i],label])
return AHD_sum/k
def top_k_HPK(label, score, k=5):
#label: [n], score: [n,n_classes]
#global tree_dist
y_preds=np.argsort(-score,axis=-1)[:,:k] #[n,k]
HPK_sum=0.0
hdist = np.sort(tree_dist[label,:],axis=-1)[:,1+k] #[n]?
hCorrectlist=[np.where(tree_dist[label[j],:]<=hdist[j])[0] for j in range(len(label))]
for j in range(len(label)):
tmp_intersect=np.intersect1d(y_preds[j,:],hCorrectlist[j])
HPK_sum+= len(tmp_intersect)#np.sum(tree_dist[y_preds[:,i],label])
return HPK_sum/k
def top_k_HSR(feats_n, labels, k=250):
#feats_n: [n_test, dim], labels: [n_test]
#global tree_dist
if params.distance=='arccos':
tree_dist2 = (np.pi/2)*tree_dist/(params.beta+tree_dist) #[n_classes, n_classes], range: [0, np.pi/2]
tree_sim = np.cos(tree_dist2)
elif params.distance=='euc':
tree_dist2 = (2**0.5)*tree_dist/(params.beta+tree_dist) #[n_classes, n_classes], range: [0, 2**0.5]
tree_sim = 1-tree_dist2**2/2
else:
raise
#Similarity based on features
cos_feats = np.matmul(feats_n, feats_n.transpose()) #[n_test, n_test]
knn_inds=np.argsort(-cos_feats,axis=-1)[:,1:k+1] #[n_test, k]
labels = labels.astype(int)
#Similarity based on labels (classes)
sim_cls = tree_sim[labels,:][:,labels] #[n_test, n_test]
max_sim_inds= np.argsort(-sim_cls,axis=-1)[:,1:k+1] #[n_test, k]
sim_sum=np.zeros(shape=[len(feats_n),k]) #[n_test, k]
sim_max=np.zeros(shape=[len(feats_n),k])
for i in range(k):
sim_sum[:,i] = tree_sim[labels, labels[knn_inds[:,i]]]
sim_max[:,i] = tree_sim[labels, labels[max_sim_inds[:,i]]]
sim_sum = np.cumsum(sim_sum,axis=-1) #[n_test, k]
sim_max = np.cumsum(sim_max,axis=-1)
HS_np = sim_sum/sim_max #[n_test, k]
return HS_np #[n_test, k] #np.nanmean(HS_np)
'''Evaluate model'''
method = params.method
dist = params.distance
backbone = params.backbone
rand_backbone=params.rand_backbone
seed = params.seed
#batch_size= params.batch_size
small = params.small
use_val = params.use_val
resize = params.resize
last=params.last
clspri = params.clspri #Whether to use class prior probability or not
ema = params.ema
if ema:
alpha=params.alpha
mds_W = params.mds_W
if mds_W:
beta = params.beta
dynamic = params.dynamic
CORR = params.CORR
if backbone == 'inception':
model = Inception3(method=method)
elif backbone == 'resnet50':
model = ResNet50(method=method)
else:
raise
if clspri:
model.log_cls_prior = torch.tensor(log_cls_prior)
model.log_cls_prior = torch.unsqueeze(model.log_cls_prior,dim=0) #[1, n_classes]
if ema:
model.W.requires_grad=False
#Use EMA_marker to use feature positions as initial W (initialize W using feature positions)
model.EMA_marker = torch.ones(len(model.W)) #, dtype=torch.bool
model.EMA_marker.requires_grad=False
if dynamic:
from scipy.optimize import fsolve
if method == 'DR':
if dist=='euc':
sc_func = lambda x : ((n_classes-1.0)/2**(x/2))*(x+1)*(2-2**0.5)**(x/2)-x+1
elif dist=='arccos':
sc_func = lambda x : ((n_classes-1.0)/(np.pi/2)**x)*(x+1)*(np.pi/4)**x-x+1
else:
raise
elif method == 'normface':
if dist=='euc':
sc_func = lambda x : np.cos(np.pi/4)*(n_classes-1+np.exp(x*np.cos(np.pi/4)))+x*np.sin(np.pi/4)**2*(np.exp(x*np.cos(np.pi/4))-n_classes+1)
elif dist=='arccos':
sc_func = lambda x : (n_classes-1)*np.exp(-x*(np.pi/2)**2)*np.exp(2*x*(np.pi/4)**2)*(2*x*(np.pi/4)**2-1)-np.exp(x*(np.pi/4)**2)*(2*x*(np.pi/4)**2+1)
else:
raise
else:
raise
model.scale = fsolve(sc_func, model.scale)[0]
model.log_scale = torch.nn.Parameter(torch.tensor([np.log(model.scale)],dtype=torch.float))
model.log_scale.requires_grad = True
model = model.cuda()
if clspri:
model.log_cls_prior = model.log_cls_prior.to(model.W.device)
if backbone == 'inception':
optimizer = torch.optim.Adam(model.parameters(),lr=1e-4, weight_decay=0.) #lr=1e-4: Inception3
if dynamic:
optimizer0 = torch.optim.Adam([model.log_scale],lr=1e-4, weight_decay=0.)
elif backbone == 'resnet50':
optimizer = torch.optim.Adam(model.parameters(),lr=1e-4, weight_decay=0.)
if dynamic:
optimizer0 = torch.optim.Adam([model.log_scale],lr=1e-4, weight_decay=0.)
else:
raise
criterion = nn.CrossEntropyLoss()
use_cuda = torch.cuda.is_available()
losses_ = []
model_PATH_ = 'models_'+DATA_TAG+'/'
if use_val:
model_PATH_ += backbone+'_'+method+'_vsd'+str(seed)
else:
model_PATH_ += backbone+'_'+method+'_sd'+str(seed)
if rand_backbone:
model_PATH_ = model_PATH_.replace(backbone,'rd'+backbone)
if (method=='DR')&(dist=='euc'):
model_PATH_ = model_PATH_.replace(method, method+'_euc')
if (method=='normface')&(dist=='arccos'):
model_PATH_ = model_PATH_.replace(method, method+'_arccos')
if clspri:
model_PATH_ += '_clspri'
if ema:
model_PATH_ += '_ema'
if mds_W:
model_PATH_ += '_mds'
if dynamic:
model_PATH_ += '_dnm'
if CORR:
model_PATH_ += '_CORR'
if AUG:
model_PATH_ += '_aug'
if resize!=32:
model_PATH_ += '_rsz'+str(resize)
if last:
model_PATH_ += '_last'
old_stdout = sys.stdout
log_file = open(model_PATH_.replace('models_'+DATA_TAG+'/','record/'+DATA_TAG+'_')+'_eval.txt','w')
sys.stdout = log_file
if last:
model_PATH_ = model_PATH_.replace('_last','')
print('Method:',method)
if method in ['DR','normface']:
print('Distance:',dist)
print('Backbone:',backbone)
print('Seed:',params.seed)
print('Small nabirds:',small)
print('Use validation:',use_val)
print('Augmentation:',AUG)
if method in ['DR','normface']:
print('Consider class prior:',clspri)
print('EMA:',ema)
if ema:
print(' alpha:',alpha)
print('MDS_W:',mds_W)
if mds_W:
print(' beta:',beta)
#print(' Stress (normalized):',stresses_[0],stresses_[-1])
if ema:
print(' (EMA is ignored when \'mds_W==True\'.)')
print(' CORR:',CORR)
print('Dynamic scale:',dynamic)
if dynamic:
print('Scale (initial):',model.scale)
else:
print('Scale:',model.scale)
print('\n\n')
sys.stdout.flush()
if use_val&(not last):
model.load_state_dict(torch.load(model_PATH_+'_best.pth'))
else:
model.load_state_dict(torch.load(model_PATH_+'.pth'))
model = model.cuda()
model.eval()
var_inter, var_intra, arccos_proto_W = get_discriminality(model, trainloader)
softmax = nn.Softmax(dim=1)
'''We use shortest path distance for hierarchical distance, and not lowest common ancestor (LCA)'''
#k-acc (Top k-error): correct if the tree class is among the top k classes with the highest confidence.
#HDM (The hierarchical distance of a mistake): shortest path distance between true and predicted classes
#HDM is eqivalent with AHC when we use shortest path distance.
#AHD (The average hierarchical distance of top-k): average shortest path distance between true and top-k predicted classes
#HP@k (from "DEVISE" Frome et al. paper): hierarchical precision at k
#HS@R (from Barz & Denzler): hierarchical similarity at R (the authors name it hierarchical precision at R, but it is not actual precision and the above HP@k has very similar name. We name differently to avoid confusion)
#AHS@250 ()
corrects2 = 0
corrects2_proto = 0
top5_corrects2=0
top5_corrects2_proto=0
HC_sum = 0 #Hierarchical cost (summation)
HC_sum_proto = 0 #Hierarchical cost (summation)
#HDM_sum=0
AHD_sum=0
AHD_sum_proto=0
HPK_sum=0
HPK_sum_proto=0
#HSR_sum=0
#HSR_sum_proto=0
h_corrects2 = 4*[0]
h_corrects2_proto = 4*[0]
cnt2=0
conf_np = np.ones([0])*np.nan
pred_np = np.ones([0])*np.nan
labels_np = np.ones([0])*np.nan
feats_n_np = np.ones([0, 128])*np.nan
for i, data in enumerate(testloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
if use_cuda:
inputs, labels = inputs.cuda(), labels.cuda()
feats = model(inputs).detach() #torch.Size([32, 128])
if method in ['DR','normface']:
feats_n = feats/torch.norm(feats, p=2, dim =1,keepdim=True) #feats_n = F.normalize(feats)
'''Based on model.W'''
if ema:
cos_sim = torch.mm(feats_n, F.normalize(model.W).t())
else:
cos_sim = torch.mm(feats_n, (model.W/torch.norm(model.W, p=2, dim =1,keepdim=True)).t())
if dist=='arccos':
arccos = torch.acos(0.999999*torch.clip(cos_sim,-1,1))
if method=='DR':
pre_score = -torch.log(arccos+1e-12)
else:
pre_score = -arccos**2
else: #dist=='euc'
euc_sq = 2-2*cos_sim
if method=='DR':
pre_score = -torch.log(euc_sq+1e-12)/2
else:
pre_score = cos_sim #-euc_sq
if dynamic:
arccos = torch.acos(0.999999*torch.clip(cos_sim,-1,1))
if dynamic:
score = torch.exp(model.log_scale.detach())*pre_score
else:
score = model.scale*pre_score
'''Based on model.proto'''
if ema:
cos_sim_proto = torch.mm(feats_n, F.normalize(model.proto).t())
else:
cos_sim_proto = torch.mm(feats_n, (model.proto/torch.norm(model.proto, p=2, dim =1,keepdim=True)).t())
if dist=='arccos':
arccos_proto = torch.acos(0.999999*torch.clip(cos_sim_proto,-1,1))
if method=='DR':
pre_score_proto = -torch.log(arccos_proto+1e-12)
else:
pre_score_proto = -arccos_proto**2
else: #dist=='euc'
euc_sq_proto = 2-2*cos_sim_proto
if method=='DR':
pre_score_proto = -torch.log(euc_sq_proto+1e-12)/2
else:
pre_score_proto = cos_sim_proto #-euc_sq
if dynamic:
score_proto = torch.exp(model.log_scale.detach())*pre_score_proto
else:
score_proto = model.scale*pre_score_proto
elif method=='softmax':
score = torch.mm(feats, model.W.t())+model.b #[32, 48]: [batch_size, n_classes]
feats_n = F.normalize(feats)
else:
raise
if clspri:
score +=model.log_cls_prior
score_proto +=model.log_cls_prior
corrects2+=torch.sum(torch.argmax(score,dim=-1)==labels)
if method in ['DR','normface']:
corrects2_proto+=torch.sum(torch.argmax(score_proto,dim=-1)==labels)
labels_np_ = labels.detach().cpu().numpy()
pred_np_ = torch.argmax(score,dim=-1).detach().cpu().numpy()
top5_corrects2+= top_k_correct(labels_np_,score.detach().cpu().numpy(), k=5)
if method in ['DR','normface']:
pred_proto_np = torch.argmax(score_proto,dim=-1).detach().cpu().numpy()
top5_corrects2_proto+= top_k_correct(labels_np_,score_proto.detach().cpu().numpy(), k=5)
HC_sum+=np.sum(tree_dist[pred_np_,labels_np_])
AHD_sum+=top_k_AHD(labels_np_,score.detach().cpu().numpy(), k=5)
if method in ['DR','normface']:
HC_sum_proto+=np.sum(tree_dist[pred_proto_np,labels_np_])
AHD_sum_proto+=top_k_AHD(labels_np_,score_proto.detach().cpu().numpy(), k=5)
HPK_sum+=top_k_HPK(labels_np_,score.detach().cpu().numpy(), k=5)
#HSR_sum+=top_k_HSR(labels_np_,score.detach().cpu().numpy(), k=5)
if method in ['DR','normface']:
HPK_sum_proto+=top_k_HPK(labels_np_,score_proto.detach().cpu().numpy(), k=5)
#HSR_sum_proto+=top_k_HSR(labels_np_,score_proto.detach().cpu().numpy(), k=5)
for j in range(len(h_corrects2)):
_, h_labels_np = higher_cls(labels_np_,level=j)
_, h_pred_np = higher_cls(pred_np_,level=j)
if method in ['DR','normface']:
_, h_pred_proto_np = higher_cls(pred_proto_np,level=j)
#print(h_labels_np, h_pred_np, np.equal(h_labels_np,h_pred_np))
h_corrects2[j]+= np.sum(np.equal(h_labels_np,h_pred_np))
if method in ['DR','normface']:
h_corrects2_proto[j]+= np.sum(np.equal(h_labels_np,h_pred_proto_np))
cnt2+=len(labels.detach().cpu().numpy())
conf, _ = torch.max(softmax(score),dim=-1)
conf_np = np.concatenate([conf_np, conf.detach().cpu().numpy()],axis=0)
pred_np = np.concatenate([pred_np, torch.argmax(score,dim=-1).detach().cpu().numpy()],axis=0)
labels_np = np.concatenate([labels_np, labels.cpu().numpy()],axis=0)
feats_n_np = np.concatenate([feats_n_np, feats_n.cpu().numpy()],axis=0)
print('Accuracy (test):',(corrects2/cnt2).detach().cpu().numpy())
if method in ['DR','normface']:
print('Accuracy (test, proto):',(corrects2_proto/cnt2).detach().cpu().numpy())
print('Top-5 accuracy (test):',top5_corrects2/cnt2)
if method in ['DR','normface']:
print('Top-5 accuracy (test, proto):',top5_corrects2_proto/cnt2)
print()
print('AHC (test):',HC_sum/cnt2)
if method in ['DR','normface']:
print('AHC (test, proto):',HC_sum_proto/cnt2)
print('AHD (k=5, test):',AHD_sum/cnt2)
if method in ['DR','normface']:
print('AHD (k=5, test, proto):',AHD_sum_proto/cnt2)
print()
print('HP@5 (test):',HPK_sum/cnt2)
if method in ['DR','normface']:
print('HP@5 (test, proto):',HPK_sum_proto/cnt2)
print()
sys.stdout.flush()
HS_np=top_k_HSR(feats_n_np, labels_np, k=250) #HS_np: [n_test, k] #score.detach().cpu().numpy(),
HSR_vals = np.nanmean(HS_np,axis=0) #[k]
#HSR = HSR_vals[-1] #np.nanmean(HS_np[:,-1])
AHSR = np.trapz(HSR_vals,dx=1/(250-1))
for k in [1, 50, 100, 150, 200, 250]:
print('HS@{0} (test): {1}'.format(k,HSR_vals[k-1]))
print('AHS@250 (test):', AHSR)
'''if method in ['DR','normface']:
HSR_proto=top_k_HSR(feats_n_np, labels_np, k=250, proto=True)
print('HS@k (k=250, test, proto):', HSR_proto)'''
print()
for j in range(len(h_corrects2)-1,-1,-1):
_,_, h_cls_list_unq = higher_cls(np.array([0]),j,True)
print('level: {0}, number of classes: {1}'.format(j,len(h_cls_list_unq)))
print(' Accuracy (test): {0}'.format(h_corrects2[j]/cnt2)) #' Accuracy (test, level={0}): {1}'.format(j,h_corrects2[j]/cnt2))
if method in ['DR','normface']:
print(' Accuracy (test, proto): {0}'.format(h_corrects2_proto[j]/cnt2)) #' Accuracy (test, level={0}, proto): {1}'.format(j,h_corrects2_proto[j]/cnt2))
print()
sys.stdout.flush()
'''Confidence plot'''
from scipy.stats import spearmanr
def plot_conf_acc(conf, pred, labels, bins=10+ 1,mode='equal'):
global method
if mode=='sort':
sort_ind = np.argsort(conf)
split_ind = np.array_split(sort_ind,bins,axis=0)
mns = [np.mean(conf[split_ind[i]]) for i in range(bins) if len(split_ind[i])>=1]
elif mode=='equal':
conf_ranges = np.linspace(0,1,bins+1)
split_ind = [np.where((conf_ranges[i]<=conf)&(conf<conf_ranges[i+1]))[0] for i in range(bins)]
mns = [(conf_ranges[i]+conf_ranges[i+1])/2 for i in range(bins) if len(split_ind[i])>=1]
else:
raise
conf_avg = [np.mean(conf[split_ind[i]]) for i in range(bins) if len(split_ind[i])>=1]
freqs = [len(split_ind[i])/len(conf) for i in range(bins) if len(split_ind[i])>=1]
accs = [np.nanmean(pred[split_ind[i]]==labels[split_ind[i]]) for i in range(bins) if len(split_ind[i])>=1]
conf_avg = np.array(conf_avg)
freqs = np.array(freqs)
accs = np.array(accs)
print('ECE:',sum(freqs*np.abs(accs-conf_avg)))
print('MCE:',np.max(np.abs(accs-conf_avg)))
print(spearmanr(mns,accs))
plot_conf_acc(conf_np, pred_np, labels_np, bins=15, mode='equal') #bins=15+1
print('\n\n')
sys.stdout.flush()
'''Accuracies'''
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import confusion_matrix
import pandas as pd
classes = list(data_cls['Class name'])
classes = [cls.replace(' ','_') for cls in classes]
metric_results = precision_recall_fscore_support(labels_np, pred_np, labels=np.arange(len(classes)))
confusion_matrix_result = confusion_matrix(labels_np,pred_np, labels=np.arange(len(classes)))
diff_labels= np.setdiff1d(np.arange(len(classes)),np.unique(labels_np))
diff_pred= np.setdiff1d(np.arange(len(classes)),np.unique(pred_np))
confusion_matrix_prob = confusion_matrix_result/(np.sum(confusion_matrix_result,1)[:,None])
confusion_matrix_df=pd.DataFrame(confusion_matrix_result.transpose(),index=classes,columns=classes)
confusion_matrix_prob_df=pd.DataFrame(confusion_matrix_prob.transpose(),index=classes,columns=classes)
if len(diff_labels)>=1:
metric_results[1][diff_labels]=np.nan
metric_results[2][diff_labels]=np.nan
if len(diff_pred)>=1:
metric_results[0][diff_pred]=np.nan
metric_results[2][diff_pred]=np.nan
np.set_printoptions(precision=4,threshold=np.inf)
print('Precision:',metric_results[0],'\n')
print('Recall:',metric_results[1],'\n')
print('F-score:',metric_results[2],'\n')
pd.set_option('display.max_columns',100)
pd.set_option('display.width',120)
# Plot the normalized confusion matrix
'''fig=plt.figure(figsize=(10, 10), dpi= 100, facecolor='w', edgecolor='k')
plt.imshow(confusion_matrix_prob_df)
plt.colorbar()
plt.title('Normalized confusion matrix')
plt.xlabel('True label')
plt.xticks(np.arange(n_classes),labels=list(confusion_matrix_prob_df.columns.values),fontsize=8, rotation=90)
plt.ylabel('Predicted label')
plt.yticks(np.arange(n_classes),labels=list(confusion_matrix_prob_df.index),fontsize=8)
#plt.savefig('confusion_maxtirx_'+method,dpi=200)
plt.show()'''
print('\n')
sys.stdout.flush()
labels_np = labels_np.astype(int)
pred_np = pred_np.astype(int)
for j in range(len(h_corrects2)-1,0,-1):
_,_, h_cls_list_unq = higher_cls(np.array([0]),j,True)
_, h_labels_np = higher_cls(labels_np,level=j)
_, h_pred_np = higher_cls(pred_np,level=j)
#_, h_pred_proto_np = higher_cls(pred_proto_np,level=j)
metric_results = precision_recall_fscore_support(h_labels_np, h_pred_np, labels=np.arange(len(h_cls_list_unq)))
confusion_matrix_result = confusion_matrix(h_labels_np, h_pred_np, labels=np.arange(len(h_cls_list_unq)))
diff_labels= np.setdiff1d(np.arange(len(h_cls_list_unq)),np.unique(h_labels_np))
diff_pred= np.setdiff1d(np.arange(len(h_cls_list_unq)),np.unique(h_pred_np))
del confusion_matrix_df, confusion_matrix_prob_df
confusion_matrix_prob = confusion_matrix_result/(np.sum(confusion_matrix_result,1)[:,None])
confusion_matrix_df=pd.DataFrame(confusion_matrix_result.transpose(),index=h_cls_list_unq,columns=h_cls_list_unq)
confusion_matrix_prob_df=pd.DataFrame(confusion_matrix_prob.transpose(),index=h_cls_list_unq,columns=h_cls_list_unq)
if len(diff_labels)>=1:
metric_results[1][diff_labels]=np.nan
metric_results[2][diff_labels]=np.nan
if len(diff_pred)>=1:
metric_results[0][diff_pred]=np.nan
metric_results[2][diff_pred]=np.nan
#np.set_printoptions(precision=4,threshold=np.inf)
print('level: {0}, number of classes: {1}'.format(j,len(h_cls_list_unq)))
print(' Precision:',metric_results[0],'\n')
print(' Recall:',metric_results[1],'\n')