forked from nomewang/M3DM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m3dm_runner.py
97 lines (83 loc) · 3.96 KB
/
m3dm_runner.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
import torch
from tqdm import tqdm
import os
from feature_extractors import multiple_features
from dataset import get_data_loader
class M3DM():
def __init__(self, args):
self.args = args
self.image_size = args.img_size
self.count = args.max_sample
if args.method_name == 'DINO':
self.methods = {
"DINO": multiple_features.RGBFeatures(args),
}
elif args.method_name == 'Point_MAE':
self.methods = {
"Point_MAE": multiple_features.PointFeatures(args),
}
elif args.method_name == 'Fusion':
self.methods = {
"Fusion": multiple_features.FusionFeatures(args),
}
elif args.method_name == 'DINO+Point_MAE':
self.methods = {
"DINO+Point_MAE": multiple_features.DoubleRGBPointFeatures(args),
}
elif args.method_name == 'DINO+Point_MAE+add':
self.methods = {
"DINO+Point_MAE": multiple_features.DoubleRGBPointFeatures_add(args),
}
elif args.method_name == 'DINO+Point_MAE+Fusion':
self.methods = {
"DINO+Point_MAE+Fusion": multiple_features.TripleFeatures(args),
}
def fit(self, class_name):
train_loader = get_data_loader("train", class_name=class_name, img_size=self.image_size, args=self.args)
flag = 0
for sample, _ in tqdm(train_loader, desc=f'Extracting train features for class {class_name}'):
for method in self.methods.values():
if self.args.save_feature:
method.add_sample_to_mem_bank(sample, class_name=class_name)
else:
method.add_sample_to_mem_bank(sample)
flag += 1
if flag > self.count:
flag = 0
break
for method_name, method in self.methods.items():
print(f'\n\nRunning coreset for {method_name} on class {class_name}...')
method.run_coreset()
if self.args.memory_bank == 'multiple':
flag = 0
for sample, _ in tqdm(train_loader, desc=f'Running late fusion for {method_name} on class {class_name}..'):
for method_name, method in self.methods.items():
method.add_sample_to_late_fusion_mem_bank(sample)
flag += 1
if flag > self.count:
flag = 0
break
for method_name, method in self.methods.items():
print(f'\n\nTraining Dicision Layer Fusion for {method_name} on class {class_name}...')
method.run_late_fusion()
def evaluate(self, class_name):
image_rocaucs = dict()
pixel_rocaucs = dict()
au_pros = dict()
test_loader = get_data_loader("test", class_name=class_name, img_size=self.image_size, args=self.args)
path_list = []
with torch.no_grad():
for sample, mask, label, rgb_path in tqdm(test_loader, desc=f'Extracting test features for class {class_name}'):
for method in self.methods.values():
method.predict(sample, mask, label)
path_list.append(rgb_path)
for method_name, method in self.methods.items():
method.calculate_metrics()
image_rocaucs[method_name] = round(method.image_rocauc, 3)
pixel_rocaucs[method_name] = round(method.pixel_rocauc, 3)
au_pros[method_name] = round(method.au_pro, 3)
print(
f'Class: {class_name}, {method_name} Image ROCAUC: {method.image_rocauc:.3f}, {method_name} Pixel ROCAUC: {method.pixel_rocauc:.3f}, {method_name} AU-PRO: {method.au_pro:.3f}')
if self.args.save_preds:
method.save_prediction_maps('./pred_maps', path_list)
return image_rocaucs, pixel_rocaucs, au_pros