-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_observer_conceiver.py
197 lines (163 loc) · 6.29 KB
/
eval_observer_conceiver.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
import torch
from tqdm import tqdm
import metrics
import datasets
import albumentations as A
from torch.utils.data import DataLoader
import numpy as np
import argparse
import yaml
from misc import Logger
import os
import random
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from torchsampler import ImbalancedDatasetSampler
import math
import models
import torch.nn as nn
import matplotlib.pyplot as plt
from misc import fit_line
import timm
import yaml
from pathlib import PurePath
from losses import ConceiverLoss
#%%
## load configs
parser = argparse.ArgumentParser(description="Conceiver")
parser.add_argument('--config', type=str, default="./configs/exp.yaml", metavar='-c')
parser.add_argument('--conceiver_path', type=str, default='logs/conceiver3/model.t7')
parser.add_argument('--observer_path', type=str, default='logs/nnn3/model.t7')
config_args = parser.parse_args()
with open(config_args.config, 'r') as f:
args = yaml.load(f, Loader=yaml.FullLoader)
data_cfg = args['DATA']
train_cfg = args['TRAINING']
device = "cuda" if torch.cuda.is_available() else "cpu"
if not train_cfg['UseCUDA']:
device = "cpu"
epochs = train_cfg['Epochs']
batch_size = train_cfg['BatchSize']
lr = train_cfg['LearningRate']
weight_decay = train_cfg['WeightDecay']
seg_channel = data_cfg['SegChannel']
in_channels = data_cfg['ImageChannel']
#%%
# # fix random seed
seed = train_cfg['Seed']
torch.manual_seed(seed)
# np.random.seed(seed)
# random.seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
reg_index = data_cfg['RegIndex']
cat_index = data_cfg['CatIndex']
global_index = data_cfg['GlobalIndex']
local_index = data_cfg['LocalIndex']
relationship = data_cfg['Relationship']
# set up model and dataset here
if data_cfg['DataSet'] == 'FetalTrim3':
# fetal dataset
## build datasets
tfs = []
tfs.append(A.Resize(*train_cfg['EvalSize']))
augs = train_cfg['EvalAugmentations']
for a in augs.keys():
aug = eval("A.%s(**%s)"%(a, augs[a]))
tfs.append(aug)
eval_transforms = A.Compose(
tfs
)
dataset_cfg = data_cfg['Configs']
testset = datasets.FetalSeg(eval_transforms, split='test', **dataset_cfg)
## build the model
model = models.FetalConceiver(
in_channels=in_channels,
seg_concept_num=seg_channel+7, # 7 for caliper concepts
global_index=global_index,
local_index=local_index,
relationship=relationship
)
seg_model = models.DTUNet(in_channels, seg_channel)
else:
raise NotImplementedError() # add your dataset here
model = model.to(device)
seg_model = seg_model.to(device)
model.load_state_dict(torch.load(config_args.conceiver_path))
seg_model.load_state_dict(torch.load(config_args.observer_path))
exp_name = PurePath(config_args.config).parts[-2] + PurePath(config_args.config).parts[-1].split('.')[0] + '_eval'
exp_folder = os.path.join("./logs", exp_name)
model_path = os.path.join(exp_folder, "model.t7")
if not os.path.exists(exp_folder):
os.system(f"mkdir {exp_folder}")
os.system(f"cp {config_args.config} {os.path.join(exp_folder, 'config.yaml')}")
ckp_folder = os.path.join(exp_folder, 'checkpoints')
if not os.path.exists(ckp_folder):
os.system(f"mkdir {ckp_folder}")
# initialize logger
logger = Logger(os.path.join(exp_folder, 'logs.log'), 'a')
logger.fprint(f"Start experiment {exp_name}")
logger.fprint(f'Fix random seed at {seed}')
logger.fprint("Model")
#%%
testloader = DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=np.min([batch_size, 32]))
def validate_one_epoch(loader):
model.eval()
seg_model.eval()
loss_meter = metrics.AverageMeter()
cls_meter = metrics.ClassMeter()
concept_preds = []
concept_gts = []
for x in tqdm(loader):
image, concept_gt = x['gray_image'], x['concept']
image, concept_gt = image.to(device), concept_gt.to(device)
with torch.no_grad():
assign_mtx = seg_model({'image':image})['logit']
# seg_mask = torch.argmax(assign_mtx, dim=1)
# seg_mask = x['mask'].to(device)
# assign_mtx = torch.nn.functional.one_hot(seg_mask, num_classes=seg_channel).permute(0,3,1,2)
x['image'] = image
x['assign_mtx'] = assign_mtx
x['concept_gt'] = concept_gt
with torch.no_grad():
x = model(x)
batch_size = image.size(0)
concept_preds.append(x['concept_pred'].detach().cpu().numpy())
concept_gts.append(concept_gt.detach().cpu().numpy())
epoch_metrics = {}
concept_preds = np.concatenate(concept_preds, axis=0)
concept_gts = np.concatenate(concept_gts, axis=0)
cat_pred = concept_preds[:, np.array(cat_index)]
cat_gt = concept_gts[:, np.array(cat_index)]
for i in range(len(cat_index)):
print(cat_index[i]+1, accuracy_score(cat_gt[:,i], cat_pred[:,i]))
reg_pred = concept_preds[:, np.array(reg_index)]
reg_gt = concept_gts[:, np.array(reg_index)]
loss = []
for i in range(10):
if np.sum(reg_gt==i):
loss.append(math.sqrt(np.mean((reg_pred[reg_gt==i] - reg_gt[reg_gt==i])**2)))
# for i in range(len(reg_index)):
# pred = reg_pred[:, i]
# gt = reg_gt[:, i]
# loss = []
# for j in range(11):
# if np.sum(gt==j):
# loss.append(math.sqrt(np.mean((pred[gt==j] - gt[gt==j])**2)))
# print(reg_index[i]+1, np.mean(loss))
epoch_metrics['avg_mse'] = np.mean(loss)
epoch_metrics['mse nonzero'] = math.sqrt(np.mean((reg_pred[reg_gt!=0] - reg_gt[reg_gt!=0])**2))
epoch_metrics['mse'] = math.sqrt(np.mean((reg_pred - reg_gt)**2))
epoch_metrics['acc'] = accuracy_score(cat_gt.flatten(), cat_pred.flatten())
epoch_metrics['avg_acc'] = balanced_accuracy_score(cat_gt.flatten(), cat_pred.flatten())
return epoch_metrics
def test():
val_metrics = validate_one_epoch(testloader)
log_info = f"Test on Testset"
for k, v in val_metrics.items():
if k == 'confusion_matrix' or k == "classification_report":
log_info += f", eval_{k}: {v}"
else:
log_info += f", eval_{k}: {v: .4f}"
logger.fprint(log_info)
if __name__ == "__main__":
test()