-
Notifications
You must be signed in to change notification settings - Fork 7
/
hybrid_network_train.py
153 lines (122 loc) · 4.85 KB
/
hybrid_network_train.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
import torch
import pyrtklib as prl
import rtk_util as util
import json
import sys
import numpy as np
import pandas as pd
import pymap3d as p3d
from model import HybridShareNet
from torch.nn import HuberLoss,MSELoss
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
DEVICE = 'cuda'
try:
config = sys.argv[1]
except:
config = "config/hybrid_share/p40_klt3_train.json"
with open(config) as f:
conf = json.load(f)
mode = conf['mode']
if mode not in ['train','predict']:
raise RuntimeError("%s is not a valid option"%mode)
os.makedirs(conf['model'],exist_ok=True)
result = config.split("/")[-1].split(".json")[0]
result_path = "result/hybrid_share/"+result
os.makedirs(result_path,exist_ok=True)
obs,nav,sta = util.read_obs(conf['obs'],conf['eph'])
prl.sortobs(obs)
obss = util.split_obs(obs)
tmp = []
if conf.get("gt",None):
gt = pd.read_csv(conf['gt'],skiprows = 30, header = None,sep =' +', skipfooter = 4, error_bad_lines=False, engine='python')
gt[0] = gt[0]+18 # leap seconds
gts = []
# filter and normalize
gather_data = []
for o in obss:
t = o.data[0].time
t = t.time+t.sec
if t > conf['start_time'] and (conf['end_time'] == -1 and 1 or t < conf['end_time']):
tmp.append(o)
if conf.get("gt",None):
gt_row = gt.loc[(gt[0]-t).abs().argmin()]
gts.append([gt_row[3]+gt_row[4]/60+gt_row[5]/3600,gt_row[6]+gt_row[7]/60+gt_row[8]/3600,gt_row[9]])
ret = util.get_ls_pnt_pos(o,nav)
if not ret['status']:
continue
rs = ret['data']['eph']
dts = ret['data']['dts']
sats = ret['data']['sats']
exclude = ret['data']['exclude']
prs = ret['data']['prs']
resd = np.array(ret['data']['residual'])
SNR = np.array(ret['data']['SNR'])
azel = np.delete(np.array(ret['data']['azel']).reshape((-1,2)),exclude,axis=0)
gather_data.append(np.hstack([SNR.reshape(-1,1),azel[:,1:],resd]))
norm_data = np.vstack(gather_data)
imean = norm_data.mean(axis=0)
istd = norm_data.std(axis=0)
print(f"preprocess done, mean:{imean}, std:{istd}")
net = HybridShareNet(torch.tensor(imean,dtype=torch.float32),torch.tensor(istd,dtype=torch.float32))
net.double()
net = net.to(DEVICE)
obss = tmp
pos_errs = []
opt = torch.optim.Adam(net.parameters(),lr = 0.01)
epoch = conf.get('epoch',500)
batch = conf.get('batch',128)
lossFn = MSELoss(reduction='sum')
vis_loss = []
for k in range(epoch):
loss = 0
with tqdm(range(len(obss)),desc=f"Epoch {k+1}") as t:
for i in t:
o = obss[i]
if conf.get("gt",None):
gt_row = gts[i]
ret = util.get_ls_pnt_pos(o,nav)
if not ret['status']:
continue
pos_err_src = p3d.ecef2enu(*ret['pos'][:3],gt_row[0],gt_row[1],gt_row[2])
rs = ret['data']['eph']
dts = ret['data']['dts']
sats = ret['data']['sats']
exclude = ret['data']['exclude']
prs = ret['data']['prs']
resd = np.array(ret['data']['residual'])
SNR = np.array(ret['data']['SNR'])
azel = np.delete(np.array(ret['data']['azel']).reshape((-1,2)),exclude,axis=0)
in_data = torch.tensor(np.hstack([SNR.reshape(-1,1),azel[:,1:],resd]),dtype=torch.float32).to(DEVICE)
predict= net(in_data)
weight = predict[0]
bias = predict[1]
#print(predict_weight)
select_sats = list(np.delete(np.array(sats),exclude))
ret = util.get_ls_pnt_pos_torch(o,nav,torch.diag(weight),bias.reshape(-1,1),p_init=ret['pos'])
gt_ecef = p3d.geodetic2ecef(*gt_row)
enu = p3d.ecef2enu(*ret['pos'][:3],gt_row[0],gt_row[1],gt_row[2])
epoch_loss = torch.norm(torch.hstack(enu[:3]))
#epoch_loss = lossFn(ret['pos'][:3],torch.tensor(gt_ecef).to(DEVICE))
loss += epoch_loss
t.set_postfix({'epoch loss':epoch_loss.item()})
#torch.norm(ret['pos'][:3]-torch.tensor(gt_ecef).to(DEVICE))
# pos_err_pre = p3d.ecef2enu(*ret['pos'][:3],gt_row[0],gt_row[1],gt_row[2])
# pos_errs.append([np.linalg.norm(pos_err_src[:2]),np.linalg.norm(pos_err_pre[:2])])
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item()/len(obss))
vis_loss.append(loss.item())
if k == 100:
torch.save(net.state_dict(),conf['model']+"/hybrid_share_100.pth")
vis_loss_300 = np.array(vis_loss)
plt.plot(vis_loss)
plt.savefig(result_path+"/loss_100.png")
np.savetxt(result_path+"/loss_100.csv",vis_loss_300.reshape(-1,1))
torch.save(net.state_dict(),conf['model']+f"/hybrid_share_3d.pth")
vis_loss = np.array(vis_loss)
plt.plot(vis_loss)
plt.savefig(result_path+f"/loss_{epoch}.png")
np.savetxt(result_path+f"/loss_{epoch}.csv",vis_loss.reshape(-1,1))