-
Notifications
You must be signed in to change notification settings - Fork 45
/
deepmapping.py
138 lines (113 loc) · 5.24 KB
/
deepmapping.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
from copy import deepcopy
import numpy as np
import torch
import torch.nn as nn
from .networks import LocNetReg2D, LocNetRegAVD, MLP
from utils import transform_to_global_2D, transform_to_global_AVD
def get_M_net_inputs_labels(occupied_points, unoccupited_points):
"""
get global coord (occupied and unoccupied) and corresponding labels
"""
n_pos = occupied_points.shape[1]
inputs = torch.cat((occupied_points, unoccupited_points), 1)
bs, N, _ = inputs.shape
gt = torch.zeros([bs, N, 1], device=occupied_points.device)
gt.requires_grad_(False)
gt[:, :n_pos, :] = 1
return inputs, gt
def sample_unoccupied_point(local_point_cloud, n_samples, center):
"""
sample unoccupied points along rays in local point cloud
local_point_cloud: <BxLxk>
n_samples: number of samples on each ray
center: location of sensor <Bx1xk>
"""
bs, L, k = local_point_cloud.shape
center = center.expand(-1,L,-1) # <BxLxk>
unoccupied = torch.zeros(bs, L * n_samples, k,
device=local_point_cloud.device)
for idx in range(1, n_samples + 1):
fac = torch.rand(1).item()
unoccupied[:, (idx - 1) * L:idx * L, :] = center + (local_point_cloud-center) * fac
return unoccupied
class DeepMapping2D(nn.Module):
def __init__(self, loss_fn, n_obs=256, n_samples=19, dim=[2, 64, 512, 512, 256, 128, 1]):
super(DeepMapping2D, self).__init__()
self.n_obs = n_obs
self.n_samples = n_samples
self.loss_fn = loss_fn
self.loc_net = LocNetReg2D(n_points=n_obs, out_dims=3)
self.occup_net = MLP(dim)
def forward(self, obs_local,valid_points,sensor_pose):
# obs_local: <BxLx2>
# sensor_pose: init pose <Bx1x3>
self.obs_local = deepcopy(obs_local)
self.valid_points = valid_points
self.pose_est = self.loc_net(self.obs_local)
self.obs_global_est = transform_to_global_2D(
self.pose_est, self.obs_local)
if self.training:
sensor_center = sensor_pose[:,:,:2]
self.unoccupied_local = sample_unoccupied_point(
self.obs_local, self.n_samples,sensor_center)
self.unoccupied_global = transform_to_global_2D(
self.pose_est, self.unoccupied_local)
inputs, self.gt = get_M_net_inputs_labels(
self.obs_global_est, self.unoccupied_global)
self.occp_prob = self.occup_net(inputs)
loss = self.compute_loss()
return loss
def compute_loss(self):
valid_unoccupied_points = self.valid_points.repeat(1, self.n_samples)
bce_weight = torch.cat(
(self.valid_points, valid_unoccupied_points), 1).float()
# <Bx(n+1)Lx1> same as occp_prob and gt
bce_weight = bce_weight.unsqueeze(-1)
if self.loss_fn.__name__ == 'bce_ch':
loss = self.loss_fn(self.occp_prob, self.gt, self.obs_global_est,
self.valid_points, bce_weight, seq=4, gamma=0.1) # BCE_CH
elif self.loss_fn.__name__ == 'bce':
loss = self.loss_fn(self.occp_prob, self.gt, bce_weight) # BCE
return loss
class DeepMapping_AVD(nn.Module):
#def __init__(self, loss_fn, n_samples=35, dim=[3, 256, 256, 256, 256, 256, 256, 1]):
def __init__(self, loss_fn, n_samples=35, dim=[3, 64, 512, 512, 256, 128, 1]):
super(DeepMapping_AVD, self).__init__()
self.n_samples = n_samples
self.loss_fn = loss_fn
self.loc_net = LocNetRegAVD(out_dims=3) # <x,z,theta> y=0
self.occup_net = MLP(dim)
def forward(self, obs_local,valid_points,sensor_pose):
# obs_local: <BxHxWx3>
# valid_points: <BxHxW>
self.obs_local = deepcopy(obs_local)
self.valid_points = valid_points
self.pose_est = self.loc_net(self.obs_local)
bs = obs_local.shape[0]
self.obs_local = self.obs_local.view(bs,-1,3)
self.valid_points = self.valid_points.view(bs,-1)
self.obs_global_est = transform_to_global_AVD(
self.pose_est, self.obs_local)
if self.training:
sensor_center = sensor_pose[:,:,:2]
self.unoccupied_local = sample_unoccupied_point(
self.obs_local, self.n_samples,sensor_center)
self.unoccupied_global = transform_to_global_AVD(
self.pose_est, self.unoccupied_local)
inputs, self.gt = get_M_net_inputs_labels(
self.obs_global_est, self.unoccupied_global)
self.occp_prob = self.occup_net(inputs)
loss = self.compute_loss()
return loss
def compute_loss(self):
valid_unoccupied_points = self.valid_points.repeat(1, self.n_samples)
bce_weight = torch.cat(
(self.valid_points, valid_unoccupied_points), 1).float()
# <Bx(n+1)Lx1> same as occp_prob and gt
bce_weight = bce_weight.unsqueeze(-1)
if self.loss_fn.__name__ == 'bce_ch':
loss = self.loss_fn(self.occp_prob, self.gt, self.obs_global_est,
self.valid_points, bce_weight, seq=2, gamma=0.9) # BCE_CH
elif self.loss_fn.__name__ == 'bce':
loss = self.loss_fn(self.occp_prob, self.gt, bce_weight) # BCE
return loss