-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi_solution_trainer.py
80 lines (68 loc) · 2.79 KB
/
multi_solution_trainer.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
import logging
from .base_trainer import BaseTrainer
import torch
import numpy as np
from tqdm import tqdm
from src.utils.moo import circle_points
class MultiSolutionTrainer(BaseTrainer):
def __init__(self, alpha, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.alpha = alpha
self.num_tasks = self.benchmark.num_tasks
self.sampling_distribution = torch.distributions.Dirichlet(torch.ones(self.num_tasks) * self.alpha)
self.logging_freq = 10
def on_before_training_step(self, *args, **kwargs):
self.ray = self.sampling_distribution.sample().cuda()
self.model.ray = self.ray
self.method.ray = self.ray
return super().on_before_training_step(*args, **kwargs)
def predict(self, test_loader, leave_tqdm=False):
n = 11 if self.num_tasks == 2 else 66
rays = circle_points(n, dim=self.num_tasks)
results = {}
for i, ray in enumerate(tqdm(rays)):
ray = torch.from_numpy(ray.astype(np.float32)).cuda()
ray /= ray.sum()
self.ray = ray
self.model.ray = self.ray
self.method.ray = self.ray
results[i] = super().predict(test_loader, leave_tqdm=False)
self.present_results(results)
for k, v in results.items():
for kk, vv in v.items():
name = "test/{}/ray-{}".format(kk, k)
self.log(name, vv)
logging.info(results)
logging.info(self.present_results(results))
return results
@staticmethod
def present_results(results):
for k, v in results.items():
a = ""
for kk, vv in v.items():
if "acc" in kk:
a = "{}\t{}".format(a, round(vv, 5))
if "huber" in kk or "depth" in kk or "iou" in kk:
a = "{}\t{}".format(a, round(vv, 5))
print(a)
def _val_loop(self, leave_tqdm=False):
if self.epoch % self.logging_freq == 0:
n = 11 if self.num_tasks == 2 else 66
rays = circle_points(n, dim=self.num_tasks)
results = {}
for i, ray in enumerate(tqdm(rays)):
ray = torch.from_numpy(ray.astype(np.float32)).cuda()
ray /= ray.sum()
self.ray = ray
self.model.ray = self.ray
self.method.ray = self.ray
results[i] = super()._val_loop(leave_tqdm)
logging.info(results)
self.present_results(results)
for k, v in results.items():
for kk, vv in v.items():
name = "val/{}/ray-{}".format(kk, k)
self.log(name, vv)
logging.info(results)
logging.info(self.present_results(results))
return results