forked from taewookim0812/C-3PO_Motion_Retargeting_Module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
372 lines (281 loc) · 12.4 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from distributions import get_distribution
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
nn.init.orthogonal_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0)
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
class Policy(nn.Module):
def __init__(self):
super(Policy, self).__init__()
def forward(self, inputs, states, masks):
raise NotImplementedError
def act(self, inputs, states, masks, deterministic=False):
hidden_critic, hidden_actor, states = self(inputs, states, masks)
action, action_mean, action_std = self.dist.sample(hidden_actor[-1], deterministic=deterministic) # action을 mean과 std의 형태로 sampling해서 구함
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(hidden_actor[-1], action)
value = self.critic_linear(hidden_critic[-1])
return value, action, action_log_probs, states, hidden_actor, hidden_critic
# actor -> and then critic for Q-function estimation
def actQ(self, inputs, states, masks, deterministic=False):
hidden_actor = self(inputs, states, masks, target='actor')
action, action_mean, action_std = self.dist.sample(hidden_actor[-1], deterministic=deterministic) # action을 mean과 std의 형태로 sampling해서 구함
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(hidden_actor[-1], action)
states.flatten()[-7:] = action.flatten()
states = states.reshape(1, -1)
hidden_critic = self(inputs, states, masks, target='critic')
value = self.critic_linear(hidden_critic[-1])
return value, action, action_log_probs, states, hidden_actor, hidden_critic
def get_value(self, inputs, states, masks):
hidden_critic, _, states = self(inputs, states, masks)
value = self.critic_linear(hidden_critic[-1])
return value
def get_valueQ(self, inputs, states, masks, deterministic=False):
hidden_actor = self(inputs, states, masks, target='actor')
action, action_mean, action_std = self.dist.sample(hidden_actor[-1], deterministic=deterministic)
states.flatten()[-7:] = action.flatten()
states = states.reshape(1, -1)
hidden_critic = self(inputs, states, masks, target='critic')
value = self.critic_linear(hidden_critic[-1])
return value, states
def evaluate_actions(self, inputs, states, masks, actions):
hidden_critic, hidden_actor, states = self(inputs, states, masks)
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(hidden_actor[-1], actions)
value = self.critic_linear(hidden_critic[-1])
return value, action_log_probs, dist_entropy, states
def evaluate_actionsQ(self, inputs, states, masks, actions): # input: observation, [num_step x 11]
hidden_actor = self(inputs, states, masks, target='actor')
hidden_critic = self(inputs, states, masks, target='critic')
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(hidden_actor[-1], actions)
value = self.critic_linear(hidden_critic[-1])
return value, action_log_probs, dist_entropy, states
class CONVPolicy(Policy):
def __init__(self, num_inputs, action_space):
super(CONVPolicy, self).__init__()
# image size: (180, 180)
self.main = nn.Sequential(
nn.Conv2d(num_inputs, 32, 8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, 5, stride=2),
nn.ReLU(),
nn.Conv2d(64, 32, 4, stride=2),
nn.ReLU(),
nn.Conv2d(32, 16, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(16 * 7 * 7, 512),
nn.ReLU()
)
self.critic_linear = nn.Linear(512, 1)
self.dist = get_distribution(512, action_space)
self.train()
self.reset_parameters()
def reset_parameters(self):
self.apply(weights_init)
def mult_gain(m):
relu_gain = nn.init.calculate_gain('relu')
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
m.weight.data.mul_(relu_gain)
self.main.apply(mult_gain)
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs, states, masks):
x = self.main(inputs)
return x, x, states # x: cricit, actor, states
class CNNPolicy(Policy):
def __init__(self, num_inputs, action_space, use_gru):
super(CNNPolicy, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(num_inputs, 32, 8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, 4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 32, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(32 * 7 * 7, 512),
nn.ReLU()
)
if use_gru:
self.gru = nn.GRUCell(512, 512)
self.critic_linear = nn.Linear(512, 1)
self.dist = get_distribution(512, action_space)
self.train()
self.reset_parameters()
@property
def state_size(self):
if hasattr(self, 'gru'):
return 512
else:
return 1
def reset_parameters(self):
self.apply(weights_init)
def mult_gain(m):
relu_gain = nn.init.calculate_gain('relu')
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
m.weight.data.mul_(relu_gain)
self.main.apply(mult_gain)
if hasattr(self, 'gru'):
nn.init.orthogonal_(self.gru.weight_ih.data)
nn.init.orthogonal_(self.gru.weight_hh.data)
self.gru.bias_ih.data.fill_(0)
self.gru.bias_hh.data.fill_(0)
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs, states, masks):
x = self.main(inputs / 255.0)
if hasattr(self, 'gru'):
if inputs.size(0) == states.size(0):
x = states = self.gru(x, states * masks)
else:
x = x.view(-1, states.size(0), x.size(1))
masks = masks.view(-1, states.size(0), 1)
outputs = []
for i in range(x.size(0)):
hx = states = self.gru(x[i], states * masks[i])
outputs.append(hx)
x = torch.cat(outputs, 0)
return x, x, states
def weights_init_mlp(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
m.weight.data.normal_(0, 1)
m.weight.data *= 1 / torch.sqrt(m.weight.data.pow(2).sum(1, keepdim=True))
if m.bias is not None:
m.bias.data.fill_(0)
class RNNPolicy(Policy):
def __init__(self, num_actor_inputs, num_critic_inputs, action_space, use_gru=False, cuda_use=False):
super(RNNPolicy, self).__init__()
self.action_space = action_space
self.nNode = 64
self.hidden_dim = 64
self.cuda_use = cuda_use
if use_gru == True:
self.gru = 0
self.actor = nn.Sequential(
nn.Linear(num_actor_inputs, self.nNode),
nn.Tanh()
# nn.ReLU()
)
self.actor_lstm = nn.LSTM(self.nNode, self.hidden_dim, num_layers=1)
self.a_lstm_hidden = self.init_hidden()
self.critic = nn.Sequential(
nn.Linear(num_critic_inputs, self.nNode),
nn.Tanh()
# nn.ReLU()
)
self.critic_lstm = nn.LSTM(self.nNode, self.hidden_dim, num_layers=1)
self.c_lstm_hidden = self.init_hidden()
self.critic_linear = nn.Linear(self.hidden_dim, 1)
self.dist = get_distribution(self.hidden_dim, action_space)
self.train()
self.reset_parameters()
def init_hidden(self):
# (num_layers, batch, hidden_size)
if self.cuda_use:
h = torch.autograd.Variable(torch.zeros(1, 1, self.hidden_dim)).cuda()
c = torch.autograd.Variable(torch.zeros(1, 1, self.hidden_dim)).cuda()
else:
h = torch.autograd.Variable(torch.zeros(1, 1, self.hidden_dim))
c = torch.autograd.Variable(torch.zeros(1, 1, self.hidden_dim))
self.c_lstm_hidden = (h, c)
self.a_lstm_hidden = (h, c)
@property
def state_size(self):
return 1
def reset_parameters(self):
self.apply(weights_init_mlp)
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs, states, masks): # input은 observation값
rst_a = self.actor(inputs)
self.actor_lstm.flatten_parameters()
a_lstm_out, self.a_lstm_hidden = self.actor_lstm(rst_a.view(len(inputs), 1, -1), self.a_lstm_hidden)
rst_c = self.critic(inputs)
self.critic_lstm.flatten_parameters()
c_lstm_out, self.c_lstm_hidden = self.critic_lstm(rst_c.view(len(inputs), 1, -1), self.c_lstm_hidden)
# hidden_actor_list = [self.actor_linear_final(a_lstm_out.view(len(inputs), -1))]
# hidden_critic_list = [self.critic_linear_final(c_lstm_out.view(len(inputs), -1))]
hidden_actor_list = [a_lstm_out.view(len(inputs), -1)]
hidden_critic_list = [c_lstm_out.view(len(inputs), -1)]
return hidden_critic_list, hidden_actor_list, states
class MLPPolicy(Policy):
def __init__(self, num_actor_inputs, num_critic_inputs, action_space, symm_policy=True, use_seq=False, cuda_use=False):
super(MLPPolicy, self).__init__()
self.action_space = action_space
self.nNode = 512 # 64, 128
self.hidden_dim = 512 # 64, 128
self.cuda_use = cuda_use
self.symm_policy = symm_policy
if use_seq == True:
self.seq = 0
# as input, (N, Cin, L), N: batch size, Cin: input size, L: length of signal seq
self.actor = nn.Sequential(
nn.Linear(num_actor_inputs, self.nNode),
# nn.Tanh(),
nn.ReLU(),
nn.Linear(self.nNode, self.hidden_dim),
# nn.Tanh(),
nn.ReLU(),
nn.Linear(self.nNode, self.hidden_dim),
# nn.Tanh(),
nn.ReLU(),
)
self.critic = nn.Sequential(
nn.Linear(num_critic_inputs, self.nNode),
# nn.Tanh(),
nn.ReLU(),
nn.Linear(self.nNode, self.hidden_dim),
# nn.Tanh(),
nn.ReLU(),
nn.Linear(self.nNode, self.hidden_dim),
# nn.Tanh(),
nn.ReLU(),
)
self.critic_linear = nn.Linear(self.hidden_dim, 1) # self.hidden_dim
self.dist = get_distribution(self.hidden_dim, action_space) # self.hidden_dim
self.train()
self.reset_parameters()
def init_hidden(self):
# do nothing in MLP class.
pass
@property
def state_size(self):
return 1
def reset_parameters(self):
self.apply(weights_init_mlp)
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs, states, masks):
# a_inputs = inputs[:, :, :-1].view(inputs.size(0), -1)
a_inputs = inputs.view(inputs.size(0), -1)
if self.symm_policy:
c_inputs = inputs.view(inputs.size(0), -1)
else:
c_inputs = states.view(states.size(0), -1)
hidden_actor_list = [self.actor(a_inputs)]
hidden_critic_list = [self.critic(c_inputs)]
return hidden_critic_list, hidden_actor_list, states
# def forward(self, inputs, states, masks, target):
# if target == 'actor':
# a_inputs = inputs.view(inputs.size(0), -1)
# hidden_actor_list = [self.actor(a_inputs)]
# return hidden_actor_list
# elif target == 'critic':
# if self.symm_policy:
# c_inputs = inputs.view(inputs.size(0), -1)
# else:
# c_inputs = states.view(states.size(0), -1)
# hidden_critic_list = [self.critic(c_inputs)]
# return hidden_critic_list
# else:
# raise ValueError