-
Notifications
You must be signed in to change notification settings - Fork 11
/
model.py
executable file
·291 lines (217 loc) · 9.28 KB
/
model.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from __future__ import division
import torch
import numpy as np
import torch.nn as nn
from gym import spaces
import torch.nn.functional as F
from torch.autograd import Variable
from utils import norm_col_init, weights_init
from perception import NoisyLinear, BiRNN, AttentionLayer
def build_model(obs_space, action_space, args, device):
name = args.model
if 'single' in name:
model = A3C_Single(obs_space, action_space, args, device)
elif 'multi' in name:
model = A3C_Multi(obs_space, action_space, args, device)
model.train()
return model
def wrap_action(self, action):
action = np.squeeze(action)
out = action * (self.action_high - self.action_low) / 2 + (self.action_high + self.action_low) / 2.0
return out
def sample_action(mu_multi, sigma_multi, device, test=False):
# discrete
logit = mu_multi
prob = F.softmax(logit, dim=-1)
log_prob = F.log_softmax(logit, dim=-1)
entropy = -(log_prob * prob).sum(-1, keepdim=True)
if test:
action = prob.max(-1)[1].data
action_env = action.cpu().numpy() # np.squeeze(action.cpu().numpy(), axis=0)
else:
action = prob.multinomial(1).data
log_prob = log_prob.gather(1, Variable(action)) # [num_agent, 1] # comment for sl slave
action_env = action.squeeze(0)
return action_env, entropy, log_prob
class ValueNet(nn.Module):
def __init__(self, input_dim, head_name, num=1):
super(ValueNet, self).__init__()
if 'ns' in head_name:
self.noise = True
self.critic_linear = NoisyLinear(input_dim, num, sigma_init=0.017)
else:
self.noise = False
self.critic_linear = nn.Linear(input_dim, num)
self.critic_linear.weight.data = norm_col_init(self.critic_linear.weight.data, 0.1)
self.critic_linear.bias.data.fill_(0)
def forward(self, x):
value = self.critic_linear(x)
return value
def sample_noise(self):
if self.noise:
self.critic_linear.sample_noise()
def remove_noise(self):
if self.noise:
self.critic_linear.sample_noise()
class AMCValueNet(nn.Module):
def __init__(self, input_dim, head_name, num=1, device=torch.device('cpu')):
super(AMCValueNet, self).__init__()
self.head_name = head_name
self.device = device
if 'ns' in head_name:
self.noise = True
self.critic_linear = NoisyLinear(input_dim, num, sigma_init=0.017)
if 'onlyJ' in head_name:
self.noise = False
self.critic_linear = nn.Linear(input_dim, num)
self.critic_linear.weight.data = norm_col_init(self.critic_linear.weight.data, 0.1)
self.critic_linear.bias.data.fill_(0)
else:
self.noise = False
self.critic_linear = nn.Linear(2 * input_dim, num)
self.critic_linear.weight.data = norm_col_init(self.critic_linear.weight.data, 0.1)
self.critic_linear.bias.data.fill_(0)
self.attention = AttentionLayer(input_dim, input_dim, device)
self.feature_dim = input_dim
def forward(self, x, goal):
_, feature_dim = x.shape
value = []
coalition = x.view(-1, feature_dim)
n = coalition.shape[0]
feature = torch.zeros([self.feature_dim]).to(self.device)
value.append(self.critic_linear(torch.cat([feature, coalition[0]])))
for j in range(1, n):
_, feature = self.attention(coalition[:j].unsqueeze(0))
value.append(self.critic_linear(torch.cat([feature.squeeze(), coalition[j]]))) # delta f = f[:j]-f[:j-1]
# mean and sum
value = torch.cat(value).sum()
return value.unsqueeze(0)
def sample_noise(self):
if self.noise:
self.critic_linear.sample_noise()
def remove_noise(self):
if self.noise:
self.critic_linear.sample_noise()
class PolicyNet(nn.Module):
def __init__(self, input_dim, action_space, head_name, device):
super(PolicyNet, self).__init__()
self.head_name = head_name
self.device = device
num_outputs = action_space.n
if 'ns' in head_name:
self.noise = True
self.actor_linear = NoisyLinear(input_dim, num_outputs, sigma_init=0.017)
else:
self.noise = False
self.actor_linear = nn.Linear(input_dim, num_outputs)
# init layers
self.actor_linear.weight.data = norm_col_init(self.actor_linear.weight.data, 0.1)
self.actor_linear.bias.data.fill_(0)
def forward(self, x, test=False):
mu = F.relu(self.actor_linear(x))
sigma = torch.ones_like(mu)
action, entropy, log_prob = sample_action(mu, sigma, self.device, test)
return action, entropy, log_prob
def sample_noise(self):
if self.noise:
self.actor_linear.sample_noise()
self.actor_linear2.sample_noise()
def remove_noise(self):
if self.noise:
self.actor_linear.sample_noise()
self.actor_linear2.sample_noise()
class EncodeBiRNN(torch.nn.Module):
def __init__(self, dim_in, lstm_out=128, head_name='birnn_lstm', device=None):
super(EncodeBiRNN, self).__init__()
self.head_name = head_name
self.encoder = BiRNN(dim_in, int(lstm_out / 2), 1, device, 'gru')
self.feature_dim = self.encoder.feature_dim
self.global_feature_dim = self.encoder.feature_dim
self.apply(weights_init)
self.train()
def forward(self, inputs):
x = inputs
cn, hn = self.encoder(x)
feature = cn # shape: [bs, num_camera, lstm_dim]
global_feature = hn.permute(1, 0, 2).reshape(-1)
return feature, global_feature
class EncodeLinear(torch.nn.Module):
def __init__(self, dim_in, dim_out=32, head_name='lstm', device=None):
super(EncodeLinear, self).__init__()
self.features = nn.Sequential(
nn.Linear(dim_in, dim_out),
nn.ReLU(inplace=True),
nn.Linear(dim_out, dim_out),
nn.ReLU(inplace=True)
)
self.head_name = head_name
self.feature_dim = dim_out
self.train()
def forward(self, inputs):
x = inputs
feature = self.features(x)
return feature
class A3C_Single(torch.nn.Module): # single vision Tracking
def __init__(self, obs_space, action_spaces, args, device=torch.device('cpu')):
super(A3C_Single, self).__init__()
self.n = len(obs_space)
obs_dim = obs_space[0].shape[1]
lstm_out = args.lstm_out
head_name = args.model
self.head_name = head_name
self.encoder = AttentionLayer(obs_dim, lstm_out, device)
self.critic = ValueNet(lstm_out, head_name, 1)
self.actor = PolicyNet(lstm_out, action_spaces[0], head_name, device)
self.train()
self.device = device
def forward(self, inputs, test=False):
data = Variable(inputs, requires_grad=True)
_, feature = self.encoder(data)
actions, entropies, log_probs = self.actor(feature, test)
values = self.critic(feature)
return values, actions, entropies, log_probs
def sample_noise(self):
self.actor.sample_noise()
self.actor.sample_noise()
def remove_noise(self):
self.actor.remove_noise()
self.actor.remove_noise()
class A3C_Multi(torch.nn.Module):
def __init__(self, obs_space, action_spaces, args, device=torch.device('cpu')):
super(A3C_Multi, self).__init__()
self.num_agents, self.num_targets, self.pose_dim = obs_space.shape
lstm_out = args.lstm_out
head_name = args.model
self.head_name = head_name
self.encoder = EncodeLinear(self.pose_dim, lstm_out, head_name, device)
feature_dim = self.encoder.feature_dim
self.attention = AttentionLayer(feature_dim, lstm_out, device)
feature_dim = self.attention.feature_dim
# create actor & critic
self.actor = PolicyNet(feature_dim, spaces.Discrete(2), head_name, device)
if 'shap' in head_name:
self.ShapleyVcritic = AMCValueNet(feature_dim, head_name, 1, device)
else:
self.critic = ValueNet(feature_dim, head_name, 1)
self.train()
self.device = device
def forward(self, inputs, test=False):
pos_obs = inputs
feature_target = Variable(pos_obs, requires_grad=True)
feature_target = self.encoder(feature_target) # num_agent, num_target, feature_dim
feature_target = feature_target.reshape(-1, self.encoder.feature_dim).unsqueeze(0) # [1, agent*target, feature_dim]
feature, global_feature = self.attention(feature_target) # num_agents, feature_dim
feature = feature.squeeze()
actions, entropies, log_probs = self.actor(feature, test)
actions = actions.reshape(self.num_agents, self.num_targets, -1)
if 'shap' not in self.head_name:
values = self.critic(global_feature)
else:
values = self.ShapleyVcritic(feature, actions) # shape [1,1]
return values, actions, entropies, log_probs
def sample_noise(self):
self.actor.sample_noise()
self.actor.sample_noise()
def remove_noise(self):
self.actor.remove_noise()
self.actor.remove_noise()