-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient_experiments.py
175 lines (130 loc) · 6.18 KB
/
gradient_experiments.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
import os
import cv2
import yaml
import json
import torch # Sorry to the google supervisors
import shutil
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from torchviz import make_dot, make_dot_from_trace
# Imports from our files
from utils.arguments import arguments
from utils.modify_images import corrupt_rgbd
from slam.custom_slam import image_recover_slam
from loss.losses import knn_points_loss, color_points_loss
from utils.yaml_configs import load_yaml, save_yaml
from utils.training_utils import define_optim, define_schedular
# GradSLAM Imports
import gradslam as gs
from gradslam.datasets import ICL
from gradslam.slam import ICPSLAM
from gradslam.slam import PointFusion
from chamferdist import ChamferDistance
from gradslam import Pointclouds, RGBDImages
class Gradient_Flow:
def __init__(self, arguments):
self.args = arguments
self.device = torch.device("cuda" if self.args.SETTINGS.device == "cuda" else "cpu")
self.dataset_init()
self.model_init()
def dataset_init(self):
"""
Initialize the dataset in this function
Input:
None
Output:
None
"""
print("Loading Images of Size {} x {}".format(self.args.DATA.width,self.args.DATA.height))
self.dataset = ICL(basedir=self.args.DATA.data_path,
seqlen=self.args.DATA.sequence_length,
height=self.args.DATA.height,
width=self.args.DATA.width)
self.train_loader = DataLoader(dataset=self.dataset,
batch_size=self.args.OPTIMIZATION.batch_size,
shuffle=False,
num_workers=self.args.SETTINGS.num_workers,
pin_memory=True,
drop_last=True)
print("Data Loaded")
def model_init(self):
print("Initializing Models")
if self.args.MODEL.slam == "ICPSLAM":
self.slam = ICPSLAM(odom=self.args.MODEL.odom, device=self.device)
self.gt_slam = ICPSLAM(odom="gt", device=self.device)
elif self.args.MODEL.slam == "PointFusion":
self.slam = PointFusion(odom=self.args.MODEL.odom, device=self.device)
self.gt_slam = PointFusion(odom="gt", device=self.device)
print("Using the {} based model for SLAM".format(self.args.MODEL.slam))
def train(self):
self.recover_image()
return
def recover_image(self):
print("Recovering Image based on gradslam")
colors, depths, intrinsics, poses, transform_seq, _ = next(iter(self.train_loader))
colors /= 255
colors, depths, intrinsics, poses = colors.to(self.device), \
depths.to(self.device), \
intrinsics.to(self.device), \
poses.to(self.device)
rgbd = RGBDImages(colors, depths, intrinsics, poses)
# plot rgbd frames, check RGBDImages to see the plot function
#rgbd.plotly(0).show()
gt_reconstruction, _ = self.gt_slam(rgbd)
gt_reconstruction = gt_reconstruction.detach()
gt_pointcloud = gt_reconstruction.points_list[0].unsqueeze(0).contiguous()
gt_color_pointcloud = gt_reconstruction.colors_list[0].unsqueeze(0).contiguous()
#gt_reconstruction.plotly(0).show()
# Modify Colors and Depths
noisy_depths = depths.clone()
noisy_colors = colors.clone()
noisy_colors, noisy_depths = corrupt_rgbd(args = self.args,
device = self.device,
noisy_colors=noisy_colors,
noisy_depths=noisy_depths)
noisy_rgbd = RGBDImages(noisy_colors, noisy_depths, intrinsics, poses)
noisy_rgbd.plotly(0).show()
self.slam(noisy_rgbd)[0].plotly(0).show()
parameters = []
if self.args.DEPTH_RECOVER.optimize_depth:
parameters.append(noisy_rgbd.depth_image)
if self.args.DEPTH_RECOVER.optimize_color:
parameters.append(noisy_rgbd.rgb_image)
self.optimizer = define_optim(args=self.args,
parameters=parameters)
self.schedular = define_schedular(args=self.args,
optimizer=self.optimizer)
for i in tqdm(range(self.args.OPTIMIZATION.epochs)):
loss = 0
noisy_reconstruction = image_recover_slam(noisy_rgbd=noisy_rgbd,
slam=self.slam,
device=self.device)
noisy_pointcloud = noisy_reconstruction.points_list[0].unsqueeze(0).contiguous()
noisy_color_pointcloud = noisy_reconstruction.colors_list[0].unsqueeze(0).contiguous()
knn_loss, indexes = knn_points_loss(gt_pointcloud=gt_pointcloud,
noisy_pointcloud=noisy_pointcloud)
if self.args.DEPTH_RECOVER.optimize_depth:
loss += knn_loss
print("knn_loss: ", round(knn_loss.item(),6))
if self.args.DEPTH_RECOVER.optimize_color:
color_loss = color_points_loss(gt_pointcloud_color=gt_color_pointcloud,
noisy_pointcloud_color=noisy_color_pointcloud,
indexes=indexes)
loss += color_loss
print("color_loss: ", round(color_loss.item(),6))
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.schedular.step()
print("Optimization Finished")
noisy_rgbd.plotly(0).show()
noisy_reconstruction.plotly(0).show()
if __name__ == "__main__":
args = arguments()
config_path = args['config_path']
config_dict = load_yaml(config_path)
config_dict.SETTINGS.name = args['name']
SLAM = Gradient_Flow(config_dict)
SLAM.train()