-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathode_sfm.py
206 lines (169 loc) · 6.82 KB
/
ode_sfm.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
import os
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as Data
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import random
parser = argparse.ArgumentParser('ODE demo')
parser.add_argument('--method', type=str, choices=['dopri5', 'adams'], default='dopri5')
parser.add_argument('--batch_time', type=int, default=10)
parser.add_argument('--test_freq', type=int, default=10)
parser.add_argument('--lr', type=float, default=1e-2)
parser.add_argument('--viz', type=int, default=1)
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--adjoint', action='store_true')
parser.add_argument("--mode", default='client')
args = parser.parse_args()
if args.adjoint:
from torchdiffeq import odeint_adjoint as odeint
else:
from torchdiffeq import odeint
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
left = -5.0
right = 5.0
up = 5.0
down = -5.0
width = 1.0
door = np.array([right, 0])
wall = np.array([[right, up, left, up], # 上
[left, up, left, down], # 左
[left, down, right, down], # 下
[right, down, door[0], -width],
[door[0], width, right, up]])
SEED = 10
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
random.seed(SEED)
Np = 5
Nknn = 4
Nsamples = 200
saveDir = "result/sfm"
class Dataset(Dataset):
def __init__(self, x, y):
self.x = x
self.y = y
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.y[idx]
def makedirs(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
class ODEFunc(nn.Module):
def __init__(self):
super(ODEFunc, self).__init__()
self.nfe = 0
self.net1 = nn.Sequential(
nn.Linear(4, 2),
)
self.net2 = nn.Sequential(
nn.Linear(1, 1),
)
self.net3 = nn.Sequential(
nn.Linear(1, 16),
nn.ReLU(),
nn.Linear(16, 8),
nn.ReLU(),
nn.Linear(8, 1),
)
for m in self.net1.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=0.1)
nn.init.constant_(m.bias, val=0)
for m in self.net2.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=0.1)
nn.init.constant_(m.bias, val=0)
for m in self.net3.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=0.1)
nn.init.constant_(m.bias, val=0)
def forward(self, t, y):
self.nfe += 1
y = y.float()
theX = y[:, 0:2]
theV = y[:, 2:4]
ux = y[:, 4:2*Np+2]
dxdt = theV
unit_e = 1.0*(torch.tensor([right, 0]).to(device) - theX) * (1.0 / torch.sqrt(torch.sum(torch.square(
theX - torch.tensor([right, 0]).to(device)), dim=1)).reshape(-1, 1))
f_1 = self.net1(torch.cat([unit_e, theV], 1))
rightdist = right - theX[:, 0].reshape(-1, 1)
todoor = np.where((theX[:, 1].cpu() < width) & (theX[:, 1].cpu() > -width))[0]
rightdist[todoor] = 100
f_2 = torch.exp(self.net2(theX[:, 0].reshape(-1, 1) - left)) * torch.tensor([[1, 0]]).to(device) \
+ torch.exp(self.net2(theX[:, 1].reshape(-1, 1) - down)) * torch.tensor([[0, 1]]).to(device) \
+ torch.exp(self.net2(up - theX[:, 1].reshape(-1, 1))) * torch.tensor([[0, -1]]).to(device) \
+ torch.exp(self.net2(rightdist)) * torch.tensor([[-1, 0]]).to(device)
f_3 = torch.zeros((len(y), 2)).to(device)
for j in range(Nknn):
d_ij = torch.sqrt(torch.sum(torch.square(ux[:, 2 * j:2 * j + 2]), dim=1))
f_3 += self.net3(d_ij.reshape(-1, 1))*(-ux[:, 2*j:2*j+2]/d_ij.reshape(-1, 1))
dvdt = (f_1 + f_2 + f_3) / 80.0
dudt = torch.zeros((len(y), 2*Nknn)).to(device)
result = torch.cat([dxdt, dvdt], 1)
result = torch.cat([result, dudt], 1)
return result
if __name__ == "__main__":
saveDir = "result/sfm"
makedirs(saveDir)
train_x = torch.load(saveDir + '/train_x.pt')
train_y = torch.load(saveDir + '/train_y.pt')
test_x = torch.load(saveDir + '/test_x.pt')
test_y = torch.load(saveDir + '/test_y.pt')
train_dataset = Dataset(train_x, train_y)
test_dataset = Dataset(test_x, test_y)
minLoss = 100.0
ii = 0
func = ODEFunc().to(device)
optimizer = optim.RMSprop(func.parameters(), lr=args.lr)
batch_t = torch.tensor([0, 0.01]).to(device)
lambda_regularizer = 0.01
with open(saveDir + '/modelInfo.txt', mode='a') as filename:
filename.write('the networks approximate force\n')
filename.write('net1: 4-2\n')
filename.write('net2: 1-1\n')
filename.write('net3: 1-16-8-1\n')
# train the network
for epoch in range(1000):
train_set = DataLoader(train_dataset, batch_size=600, shuffle=True)
test_set = DataLoader(test_dataset, batch_size=300, shuffle=True)
train_loss = 0.0
for i, data in enumerate(train_set):
optimizer.zero_grad()
batch_y0, batch_y = data
batch_y0 = batch_y0.to(device)
batch_y = batch_y.to(device)
pred_y = odeint(func, batch_y0, batch_t).to(device)
loss = torch.mean(torch.abs(pred_y[1][:, 0:4] - batch_y))
train_loss += loss.item()
loss.backward()
optimizer.step()
train_loss = train_loss / (i+1)
with torch.no_grad():
test_loss = 0.0
for j, data in enumerate(test_set):
batch_y0, batch_y = data
batch_y0 = batch_y0.to(device)
batch_y = batch_y.to(device)
pred_y = odeint(func, batch_y0, batch_t).to(device)
loss = torch.mean(torch.abs(pred_y[1][:, 0:4] - batch_y))
test_loss += loss.item()
test_loss = test_loss / (j+1)
with open(saveDir + '/loss.txt', mode='a') as filename:
filename.write('Epoch {:03d} | TrainLoss {:.6f} | TestLoss {:.6f}'.format(epoch, train_loss, test_loss))
filename.write('\n')
totalLoss = train_loss + test_loss
if totalLoss < minLoss:
minLoss = totalLoss
minIndex = epoch
torch.save(func.net1.state_dict(), saveDir + '/net1_parameter.pkl')
torch.save(func.net2.state_dict(), saveDir + '/net2_parameter.pkl')
torch.save(func.net3.state_dict(), saveDir + '/net3_parameter.pkl')
with open(saveDir + '/loss.txt', mode='a') as filename:
filename.write('minIndex {:03d} | minLoss {:.6f}'.format(minIndex, minLoss))
filename.write('\n')