File size: 7,907 Bytes
ec9a6bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
from einops import rearrange


class Calibrator():
    def __init__(self, dataset, bfm, camera, recorder):
        self.dataset = dataset
        self.bfm = bfm
        self.camera = camera
        self.recorder = recorder
        self.device = torch.device('cuda:0')

        self.optimizer1 = torch.optim.Adam([{'params' : self.bfm.parameters(), 'lr' : 1e-2}])  
        self.optimizer2 = torch.optim.Adam([{'params' : self.bfm.parameters(), 'lr' : 1e-3}])  
    
    def calibrate(self):
        landmarks_gt, extrinsics0, intrinsics0, eids = self.dataset.get_item()
        landmarks_gt = torch.from_numpy(landmarks_gt).float().to(self.device)
        extrinsics0 = torch.from_numpy(extrinsics0).float().to(self.device)
        intrinsics0 = torch.from_numpy(intrinsics0).float().to(self.device)
        extrinsics = rearrange(extrinsics0, 'b v x y -> (b v) x y')
        intrinsics = rearrange(intrinsics0, 'b v x y -> (b v) x y')
        
        pprev_loss = 1e8
        prev_loss = 1e8

        for i in range(100000000):
            self.optimizer1.zero_grad()

            _, landmarks_3d = self.bfm()
            landmarks_3d = landmarks_3d.unsqueeze(1).repeat(1, landmarks_gt.shape[1], 1, 1)
            landmarks_3d = rearrange(landmarks_3d, 'b v x y -> (b v) x y')

            landmarks_2d = self.project(landmarks_3d, intrinsics, extrinsics)
            landmarks_2d = rearrange(landmarks_2d, '(b v) x y -> b v x y', b=landmarks_gt.shape[0])

            pro_loss = (((landmarks_2d / self.camera.image_size - landmarks_gt[:, :, :, 0:2] / self.camera.image_size) * landmarks_gt[:, :, :, 2:3]) ** 2).sum(-1).sum(-2).mean()
            reg_loss = self.bfm.reg_loss(5e-6, 1e-6)
            loss = pro_loss + reg_loss
            loss.backward()
            self.optimizer1.step()

            if abs(loss.item() - prev_loss) < 1e-8 and abs(loss.item() - pprev_loss) < 1e-7:
                if i % 100 == 0:
                    print(pro_loss.item(), reg_loss.item())
                break
            else:
                pprev_loss = prev_loss
                prev_loss = loss.item()
                if i % 100 == 0:
                    print(pro_loss.item(), reg_loss.item())


        for i in range(100000000):
            self.optimizer2.zero_grad()
            

            _, landmarks_3d = self.bfm()
            landmarks_3d = landmarks_3d.unsqueeze(1).repeat(1, landmarks_gt.shape[1], 1, 1)
            landmarks_3d = rearrange(landmarks_3d, 'b v x y -> (b v) x y')

            landmarks_2d = self.project(landmarks_3d, intrinsics, extrinsics)
            landmarks_2d = rearrange(landmarks_2d, '(b v) x y -> b v x y', b=landmarks_gt.shape[0])

            pro_loss = (((landmarks_2d / self.camera.image_size - landmarks_gt[:, :, :, 0:2] / self.camera.image_size) * landmarks_gt[:, :, :, 2:3]) ** 2).sum(-1).sum(-2).mean()
            reg_loss = self.bfm.reg_loss(5e-6, 1e-6)
            loss = pro_loss + reg_loss
            loss.backward()
            self.optimizer2.step()

            if abs(loss.item() - prev_loss) < 1e-11 and abs(loss.item() - pprev_loss) < 1e-10:
                if i % 100 == 0:
                    print(pro_loss.item(), reg_loss.item())
                break
            else:
                pprev_loss = prev_loss
                prev_loss = loss.item()
                if i % 100 == 0:
                    print(pro_loss.item(), reg_loss.item())

        log = {
            'eids': eids,
            'landmarks_gt': landmarks_gt,
            'landmarks_2d': landmarks_2d.detach(),
            'bfm': self.bfm,
            'intrinsics': intrinsics0,
            'extrinsics': extrinsics0
        }
        self.recorder.log(log)


    def project(self, points_3d, intrinsic, extrinsic):
        points_3d = points_3d.permute(0,2,1)
        calibrations = torch.bmm(intrinsic, extrinsic)
        points_2d = self.camera.perspective(points_3d, calibrations)
        points_2d = points_2d.permute(0,2,1)
        return points_2d


class CalibratorSingleView():
    def __init__(self, dataset, bfm, camera, recorder):
        self.dataset = dataset
        self.bfm = bfm
        self.camera = camera
        self.recorder = recorder
        self.device = torch.device('cuda:0')

        self.optimizer1 = torch.optim.Adam([{'params' : self.bfm.parameters(), 'lr' : 1e-2}])  
        self.optimizer2 = torch.optim.Adam([{'params' : self.bfm.parameters(), 'lr' : 1e-3}])  
    
    def calibrate(self):
        landmarks_gt, extrinsics0, intrinsics0, eids = self.dataset.get_item()
        landmarks_gt = torch.from_numpy(landmarks_gt).float().to(self.device)
        extrinsics0 = torch.from_numpy(extrinsics0).float().to(self.device)
        intrinsics0 = torch.from_numpy(intrinsics0).float().to(self.device)
        extrinsics = rearrange(extrinsics0, 'b v x y -> (b v) x y')
        intrinsics = rearrange(intrinsics0, 'b v x y -> (b v) x y')
        
        pprev_loss = 1e8
        prev_loss = 1e8

        for i in range(3000):
            self.optimizer1.zero_grad()

            _, landmarks_3d = self.bfm()
            landmarks_3d = landmarks_3d.unsqueeze(1).repeat(1, landmarks_gt.shape[1], 1, 1)
            landmarks_3d = rearrange(landmarks_3d, 'b v x y -> (b v) x y')

            landmarks_2d = self.project(landmarks_3d, intrinsics, extrinsics)
            landmarks_2d = rearrange(landmarks_2d, '(b v) x y -> b v x y', b=landmarks_gt.shape[0])

            pro_loss = (((landmarks_2d / self.camera.image_size - landmarks_gt[:, :, :, 0:2] / self.camera.image_size) * landmarks_gt[:, :, :, 2:3]) ** 2).sum(-1).sum(-2).mean()
            reg_loss = self.bfm.reg_loss(5e-6, 1e-6)
            loss = pro_loss + reg_loss
            loss.backward()
            self.optimizer1.step()

            if False: #abs(loss.item() - prev_loss) < 1e-8 and abs(loss.item() - pprev_loss) < 1e-7:
                print(prev_loss, 'optimization ends')
                break
            else:
                pprev_loss = prev_loss
                prev_loss = loss.item()
                if i % 100 == 0:
                    print(i, prev_loss)


        for i in range(1000):
            self.optimizer2.zero_grad()
            

            _, landmarks_3d = self.bfm()
            landmarks_3d = landmarks_3d.unsqueeze(1).repeat(1, landmarks_gt.shape[1], 1, 1)
            landmarks_3d = rearrange(landmarks_3d, 'b v x y -> (b v) x y')

            landmarks_2d = self.project(landmarks_3d, intrinsics, extrinsics)
            landmarks_2d = rearrange(landmarks_2d, '(b v) x y -> b v x y', b=landmarks_gt.shape[0])

            pro_loss = (((landmarks_2d / self.camera.image_size - landmarks_gt[:, :, :, 0:2] / self.camera.image_size) * landmarks_gt[:, :, :, 2:3]) ** 2).sum(-1).sum(-2).mean()
            reg_loss = self.bfm.reg_loss(5e-6, 1e-6) + self.bfm.temporal_smooth_loss(3e-5)
            loss = pro_loss + reg_loss
            loss.backward()
            self.optimizer2.step()

            if False: #abs(loss.item() - prev_loss) < 1e-11 and abs(loss.item() - pprev_loss) < 1e-10:
                print(prev_loss, 'optimization ends')
                break
            else:
                pprev_loss = prev_loss
                prev_loss = loss.item()
                if i % 100 == 0:
                    print(i, prev_loss)

        log = {
            'eids': eids,
            'landmarks_gt': landmarks_gt,
            'landmarks_2d': landmarks_2d.detach(),
            'bfm': self.bfm,
            'intrinsics': intrinsics0,
            'extrinsics': extrinsics0
        }
        self.recorder.log(log)


    def project(self, points_3d, intrinsic, extrinsic):
        points_3d = points_3d.permute(0,2,1)
        calibrations = torch.bmm(intrinsic, extrinsic)
        points_2d = self.camera.perspective(points_3d, calibrations)
        points_2d = points_2d.permute(0,2,1)
        return points_2d