-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
116 lines (83 loc) · 3.15 KB
/
utils.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
import sys
import pygame
def signal_handler(sig, frame):
print('Procedure terminated!')
pygame.display.quit()
pygame.quit()
sys.exit(0)
from scipy.interpolate import interp1d
## This function provides a prospective lateral-coordinate generator w.r.t possible longitudinal coordinates
## for the ego vehicle in Scenario 0, which can be taken as a demonstration
def get_path():
waypoint_x_mark = np.array([200,212.5,225,237.5,250,300])
waypoint_y_mark = np.array([335,336.5,338,337.5,335,334])
pathgenerator = interp1d(waypoint_x_mark, waypoint_y_mark,kind='cubic')
return pathgenerator
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
def weights_init_normal(m):
'''Takes in a module and initializes all linear layers with weight
values taken from a normal distribution.'''
classname = m.__class__.__name__
# for every Linear layer in a model
if classname.find('Linear') != -1:
y = m.in_features
# m.weight.data shoud be taken from a normal distribution
m.weight.data.normal_(0.0,1/np.sqrt(y))
# m.bias.data should be 0
m.bias.data.fill_(0)
class NET(nn.Module):
def __init__(self):
super(NET, self).__init__()
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(1,6,6)
self.conv2 = nn.Conv2d(6,16,6)
self.fc = nn.Linear(16*7*16, 128)
def forward(self, x):
x = x.unsqueeze(1)
x = F.max_pool2d( self.conv1(x),2)
x = F.max_pool2d( self.conv2(x),2)
x = x.view(x.size(0),16*16*7)
x = self.fc(x)
x = self.relu(x)
return x
class RND(nn.Module):
def __init__(self, use_cuda = True):
super(RND, self).__init__()
self.use_cuda = use_cuda
self.fix = NET()
self.estimator = NET()
self.fix.apply(weights_init_normal)
self.estimator.apply(weights_init_normal)
self.criterion = nn.MSELoss()
self.optim = optim.Adam(self.estimator.parameters(),0.0001)
if self.use_cuda:
self.fix.cuda()
self.estimator.cuda()
def forward(self, state):
state = torch.tensor(state, dtype=torch.float).reshape(1,45,80)
if self.use_cuda:
state = state.cuda()
target = self.fix.forward(state)
estimate = self.estimator.forward(state)
loss = self.criterion(estimate, target)
self.optim.zero_grad()
loss.backward()
self.optim.step()
error = loss.item()
mu = torch.mean(target)
std = torch.std(target)
return error, mu.detach().cpu().numpy(), std.detach().cpu().numpy()
def get_reward_i(self, state):
error, mu, std = self.forward(state)
alpha = 1+(error-mu)/std
x = min( max(alpha, 1), 2)
return x