-
Notifications
You must be signed in to change notification settings - Fork 2
/
03_train_NN.py
253 lines (195 loc) · 9.94 KB
/
03_train_NN.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'''
This script trains a vanila feedforward neural network on the generated KUKA-surf dataset.
The NN inputs are the 7 joint positions, velocities and torques, while outputs are the 7 joint accelerations.
'''
import os, sys, importlib
sys.path.append( os.path.join( os.path.dirname(__file__), os.path.pardir ) )
import dill
import matplotlib.pyplot as plt
import numpy as np
import torch
import sgp.sgp as sgp
from utils.evalGPplots import *
from utils.Tee import Tee
from scipy.spatial.transform import Rotation as R
from MBD_simulator_torch.classes.RigidBody import *
from MBD_simulator_torch.classes.BodyOnSurfaceBilateralConstraint import *
from MBD_simulator_torch.classes.MultiRigidBody import *
from MBD_simulator_torch.classes.RotationalJoint import *
# setup print options
np.set_printoptions(precision=4,threshold=1000,linewidth=500,suppress=True)
torch.set_printoptions(precision=4,threshold=1000,linewidth=500)
# clean GPU cache
sgp.cleanGPUcache()
# ! VERY IMPORTANT: change torch to double precision
torch.set_default_tensor_type(torch.DoubleTensor)
''' ------------------------------------------------------------------------
Open config file
------------------------------------------------------------------------ '''
# load configuration module either from standard file or from file argument
if len(sys.argv) >=3:
cfg_dataset = importlib.import_module(sys.argv[1])
cfg_model = importlib.import_module(sys.argv[2])
else:
cfg_dataset = importlib.import_module('results.KUKA-surf-dataset.config_KUKA')
cfg_model = importlib.import_module('results.KUKA-surf-dataset.exp_comp_gp-sgp-nn-mbd.config_ML')
with Tee(cfg_model.nn.addFolderAndPrefix('TrainingResults-log')):
''' ------------------------------------------------------------------------
Load training data
------------------------------------------------------------------------ '''
# load sate logs
with open(cfg_dataset.log.resultsFileName, 'rb') as f:
data = dill.load(f)
# select choosen device if available
device = torch.device("cuda") if torch.cuda.is_available() and cfg_model.nn.useGPU else torch.device("cpu")
print(f'\nUsing device: {device}')
# convert dataset to torch and move to right device
dataset_train = data.dataset_train.to(device, dtype=torch.DoubleTensor)
dataset_test = data.dataset_test_list[0].to(device, dtype=torch.DoubleTensor)
''' ------------------------------------------------------------------------
Create new NN model object and load parameters if they exist
------------------------------------------------------------------------ '''
from torch import nn
class NN(nn.Module):
def __init__(self, nbrInputs:int=21, nbrHidenNeurons=[30,20], nbrOutputs:int=7, dataset:StructTorchArray=None):
super().__init__()
self.layers = nn.ModuleList()
self.layers.append( nn.Linear(in_features=nbrInputs, out_features=nbrHidenNeurons[0]) )
for i in range(len(nbrHidenNeurons)-1):
self.layers.append( nn.Sigmoid() )
self.layers.append( nn.Linear(in_features=nbrHidenNeurons[i], out_features=nbrHidenNeurons[i+1]) )
self.layers.append( nn.Sigmoid() )
self.layers.append( nn.Linear(in_features=nbrHidenNeurons[-1], out_features=nbrOutputs) )
if dataset is not None:
feat = torch.cat([dataset.q,dataset.dq,dataset.tau], axis=-1)
out = dataset.ddq
self.in_mean = feat.mean(axis=0, keepdim=True)
self.in_std = feat.std(axis=0, keepdim=True)
self.out_mean = out.mean(axis=0, keepdim=True)
self.out_std = out.std(axis=0, keepdim=True)
def forward(self, dataset:StructTorchArray):
feat = torch.cat([dataset.q,dataset.dq,dataset.tau], axis=-1)
x = (feat - self.in_mean) / self.in_std
for l in self.layers:
x = l(x)
ddq = (x * self.out_std) + self.out_mean
return ddq
model = NN(nbrInputs=21, nbrHidenNeurons=[80], nbrOutputs=7, dataset=dataset_train).to(device)
# load hyperparameters if provided file exists
if os.path.isfile(cfg_model.nn.fileName) and not cfg_model.nn.trainFromScratch:
state_dict = torch.load(cfg_model.nn.fileName, map_location=device)
model.load_state_dict(state_dict)
print('\nFound existing trained model! Loading parameters from this model!')
elif cfg_model.nn.train:
print('\nTraining from scratch!')
else:
raise Exception(f'No trained NN found at {cfg_model.nn.fileName}')
if cfg_model.nn.train:
''' ------------------------------------------------------------------------
Train
------------------------------------------------------------------------ '''
sgp.cleanGPUcache()
# set GP to training mode (prediction outputs prior)
model.train()
# Use the adam optimizer
optimizer = torch.optim.Adam([{'params': model.parameters()}], lr=cfg_model.nn.lr)
for i in range(cfg_model.nn.training_iterations):
print(f'Iter {i+1:3d}/{cfg_model.nn.training_iterations} {" ":5s}', end='')
# select batch
idx_batch = np.random.choice(len(dataset_train), cfg_model.nn.batchsize)
dataset_train_batch = dataset_train[idx_batch]
ddq_pred = model(dataset_train_batch)
loss = nn.functional.mse_loss( input=dataset_train_batch.ddq, target=ddq_pred, reduction='mean' )
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f'-mse: {loss.item():10.2f}')
# restart optimizer every iterRestartOptimizer iterations
if not i % cfg_model.nn.iterRestartOptimizer and i>0:
optimizer = torch.optim.Adam([{'params': model.parameters()}], lr=cfg_model.nn.lr)
''' ------------------------------------------------------------------------
Test model
------------------------------------------------------------------------ '''
if not i % cfg_model.nn.display_every_x_iter and i>0:
with torch.no_grad():
model.eval()
if data.contact:
ddq_pred = model(dataset_test)
MAE = torch.mean(torch.abs(ddq_pred - dataset_test.ddq), dim=0).cpu()
# MAE = nn.functional.l1_loss( input=dataset_test.ddq, target=ddq_pred, reduction='mean' ).cpu()
print(f'\nMAE_ddq_test = {MAE}\n')
model.train()
for name, param in model.named_parameters():
if param.requires_grad:
print(f'{name}, {param.data}\n')
# sgp.printParameterList(model)
sgp.cleanGPUcache()
''' ------------------------------------------------------------------------
Save model
------------------------------------------------------------------------ '''
if cfg_model.nn.saveModel:
torch.save(model.state_dict(), cfg_model.nn.fileName)
print(f'\nNN SAVED to {cfg_model.nn.fileName}\n')
''' ------------------------------------------------------------------------
Eval Prediction
------------------------------------------------------------------------ '''
if cfg_model.nn.eval:
print('Evaluating model...')
model.eval()
with torch.no_grad():
timeRange=[10,13]
dataset = dataset_test
ddq_pred = model(dataset)
# t = dataset.t.cpu() - dataset.t[0].item()
t = dataset.t.cpu() - dataset.t[0].item() if timeRange is None else dataset.t.cpu() - timeRange[0]
''' Plot prediction'''
fig1, axs = plt.subplots(1, data.nq, figsize=(20,3), sharex=True)
for j in range(data.nq):
axs[j].grid(True)
axs[j].set_title(f'$\ddot q_{{ {j} }}$')
axs[j].set_xlabel(f'time [s]')
axs[j].plot(
t,
dataset.ddq[:,j].cpu(),
marker='', ls='--', lw=1, color='k', mfc=None, zorder=3
)
axs[j].plot(
t,
ddq_pred[:,j].cpu(),
marker='', ls='-', lw=1, color='purple', mfc=None, zorder=2
)
# axs[j].set_xlim([min(t).item(),max(t).item()])
axs[j].set_xlim([0,timeRange[1]-timeRange[0]])
plt.tight_layout()
# plt.show()
if cfg_model.log.saveImages:
fig1.savefig( cfg_model.nn.addFolderAndPrefix('evalPrediction.pdf'), dpi=cfg_model.log.dpi)
if cfg_model.log.showImages: plt.show()
''' Plot prediction error'''
fig1, axs = plt.subplots(1, data.nq, figsize=(20,3), sharex=True)
for j in range(data.nq):
axs[j].grid(True)
axs[j].set_title(f'$\ddot q_{{ {j},pred }} - \ddot q_{{ {j},true }}$')
axs[j].set_xlabel(f'time [s]')
axs[j].plot(
t,
t * 0,
marker='', ls='--', lw=1, color='k', mfc=None, zorder=3
)
axs[j].plot(
t,
ddq_pred[:,j].cpu() - dataset.ddq[:,j].cpu(),
marker='', ls='-', lw=1, color='purple', mfc=None, zorder=2
)
# axs[j].set_xlim([min(t).item(),max(t).item()])
axs[j].set_xlim([0,timeRange[1]-timeRange[0]])
# axs[j].set_ylim([-1,1])
plt.tight_layout()
# plt.show()
if cfg_model.log.saveImages:
fig1.savefig( cfg_model.nn.addFolderAndPrefix('evalPredictionError.pdf'), dpi=cfg_model.log.dpi)
if cfg_model.log.showImages: plt.show()
# fig3 = evalConstraintSatisfaction( model, dataset_train, dataset_test )
# if cfg_model.log.saveImages: fig3.savefig( cfg_model.nn.addFolderAndPrefix('ConstraintError.pdf'), dpi=cfg_model.log.dpi)
# if cfg_model.log.showImages: plt.show()
# print('...finished')