-
Notifications
You must be signed in to change notification settings - Fork 1
/
plots_cm16.py
167 lines (128 loc) · 5.45 KB
/
plots_cm16.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
import os
import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from common.meter import Meter
from common.utils import compute_accuracy, compute_accuracy_bce, load_model, setup_run, by
from models.dataloaders.data_utils import dataset_builder
from models.mil_ss import FRMIL
# plotting tools
# density plot
import seaborn as sns, numpy as np
import matplotlib, cv2
import matplotlib.pyplot as plt
import pandas as pd
sns.set(rc={"figure.figsize": (6, 4)}); np.random.seed(0)
from matplotlib import rc
rc('font', **{'family': 'DejaVu Sans', 'serif':['Computer Modern'], 'weight': 'bold', 'size': 16})
rc('text', usetex=True)
def evaluate(epoch, model, loader, args=None, set='val', show=False, thrs=0.5, use_model=False):
if use_model:
model.eval()
tqdm_gen = tqdm.tqdm(loader)
acc_meter = Meter()
data_ext = 'simclr' if '_simclr_' in args.data_dir else 'imgnet'
print(f'Using Model || --: {use_model} | {data_ext}')
if use_model:
if args.model_name == 'frmil':
model.module.mode = 1
l_key = {0: 'normal', 1: 'tumor'}
bags = {0: [], 1: []}
bags_probs = {0: [], 1: []}
bag_ids = []
# From Scratch
# CM16 (simclr) : mean-max
# threshold = 8.48
# prob_threshold = 0.91
# prob_var(std) = 0.538
# CM16 (simclr) : mean-nomax
# threshold = 18.6
# prob_threshold = 1.
# prob_var(std) = 0.10
mag_thrs = 8.48
p_th = 0.91
p_var = 0.538
fm_type = 'mean-max' #
with torch.no_grad():
for idx, (data, labels,_) in enumerate(tqdm_gen, 1):
data = data.cuda()
gt = labels.float()
label = labels[0].data.cpu().numpy()[0]
#################################################
# if using model (perform self-att first)
if use_model:
data = model(data)
#################################################
if fm_type == 'mean-max':
data_max, c = torch.max(data, dim=1,keepdim=True)
data = data - data_max
# feature magnitude
fm = torch.norm(data,dim=-1)
# mean feature magnitude
mean_mag = np.mean(fm.data.cpu().numpy()[0])
bags[label].append(mean_mag)
mean_mag = mean_mag - p_var
prob = np.min([mag_thrs,mean_mag])/mag_thrs
bags_probs[label].append(prob)
prob = torch.tensor(np.array([prob]))
prob[prob < 0] = 0.0
acc = compute_accuracy_bce(prob, gt, p_th)
acc_meter.update(acc)
acc_meter.update_gt(gt.data.cpu().numpy()[0],prob.data.cpu().numpy()[0])
if show:
tqdm_gen.set_description(f'[{set:^5}] avg.acc:{by(acc_meter.avg())}')
if set == 'train':
N_prob = np.mean(bags_probs[0])
N_std = np.std(bags_probs[0])
T_prob = np.mean(bags_probs[1])
T_std = np.std(bags_probs[1])
print(f'neg (prob) : {N_prob:.3f}+-{N_std:.3f} | pos (prob) : {T_prob:.3f}+-{T_std:.3f} ')
N_bag = np.mean(bags[0])
T_bag = np.mean(bags[1])
N_std = np.mean(bags[0])
T_std = np.mean(bags[1])
print(f'neg (bag) : {N_bag:.2f}+-{N_std:.3f} | pos (bag) : {T_bag:.2f}+/-{T_std:.3f}')
test_acc, test_auc, op_thrs = acc_meter.acc_auc(p_th)
print(f'[{set:^5}][final] |--> acc: {by(test_acc)} | auc: {by(test_auc)} | op_thres: {by(op_thrs/100.)}|')
X = np.array(bags[0])
Y = np.array(bags[1])
plt.figure()
if 'cm16' == args.data_name:
sns.distplot(X, label="Normal",rug=False, hist=False)
sns.distplot(Y, label="Tumor", rug=False, hist=False)
else:
sns.distplot(X, label="Negative",rug=False, hist=False)
sns.distplot(Y, label="Positive", rug=False, hist=False)
plt.ylabel('Density')
plt.xlabel('Feature Magnitude')
plt.legend(loc='best')
if use_model:
plt.savefig(os.path.join(args.save_path.split(args.extra_dir)[0],f'model_{args.data_name}_{data_ext}_fmean_{fm_type}_{set}.png'),
dpi=400,bbox_inches='tight')
else:
plt.savefig(os.path.join(args.save_path.split(args.extra_dir)[0],f'{args.data_name}_{data_ext}_fmean_{fm_type}_{set}.png'),
dpi=400,bbox_inches='tight')
print(f'Check Folder [{args.save_path.split(args.extra_dir)[0]}]')
print('Done!')
def test_main(model, args, thrs=0.5):
use_m = False # use model
split = 'train' # split
Dataset = dataset_builder(args)
lib_root = args.data_dir
testset = Dataset(root=lib_root, mode=split)
loader = DataLoader(dataset=testset, batch_size=1, shuffle=False, num_workers=4, pin_memory=False)
model = None
if use_m:
model = load_model(model, os.path.join(args.save_path, f'max_acc.pth'))
evaluate("best", model, loader, args, set=split, show=True, thrs=thrs, use_model=use_m)
if __name__ == '__main__':
args = setup_run(arg_mode='test')
''' define model '''
if args.model_name == 'frmil':
model = FRMIL(args).cuda()
else:
raise ValueError('Model not found')
model = nn.DataParallel(model, device_ids=args.device_ids)
test_main(model, args, thrs=args.thres)