-
Notifications
You must be signed in to change notification settings - Fork 3
/
metamodel.py
71 lines (58 loc) · 2.39 KB
/
metamodel.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
import torch
from argus import Model
from argus.engine import State
from argus.utils import deep_detach, deep_to
from src.models.line.loss import EHMLoss
from src.models.line.model import HRNetHeatmap
from src.models.line.transforms import EHMPredictionTransform
class EHMMetaModel(Model):
nn_module = HRNetHeatmap
loss = EHMLoss
optimizer = torch.optim.Adam
prediction_transform = EHMPredictionTransform
def __init__(self, params):
super().__init__(params)
self.amp = (False if 'amp' not in self.params
else bool(self.params['amp']))
self.scaler = torch.cuda.amp.GradScaler() if self.amp else None
def train_step(self, batch, state: State) -> dict:
self.train()
self.optimizer.zero_grad()
batch = deep_to(batch, device=self.device, non_blocking=True)
with torch.cuda.amp.autocast(enabled=self.amp):
prediction = self.nn_module(batch['image'])
loss = self.loss(prediction, batch['keypoint_maps'])
if self.amp:
self.scaler.scale(loss).backward()
else:
loss.backward()
if self.amp:
self.scaler.step(self.optimizer)
self.scaler.update()
else:
self.optimizer.step()
prediction = deep_detach(prediction)
prediction = self.prediction_transform(prediction[-1])
return {
'prediction': prediction,
'target': deep_detach(batch['keypoint_maps']),
'keypoints': deep_detach(batch['keypoints']),
'line_para': deep_detach(batch['line_para']),
'loss': loss.item()
}
def val_step(self, batch, state: State) -> dict:
self.eval()
with torch.no_grad():
batch = deep_to(batch, device=self.device, non_blocking=True)
with torch.cuda.amp.autocast(enabled=self.amp):
prediction = self.nn_module(batch['image'])
loss = self.loss(prediction, batch['keypoint_maps'])
prediction = deep_detach(prediction)
prediction = self.prediction_transform(prediction[-1])
return {
'prediction': prediction,
'target': deep_detach(batch['keypoint_maps']),
'keypoints': deep_detach(batch['keypoints']),
'line_para': deep_detach(batch['line_para']),
'loss': loss.item()
}