forked from papagina/Auto_Conditioned_RNN_motion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytorch_train_aclstm.py
More file actions
executable file
·285 lines (202 loc) · 10.3 KB
/
Copy pathpytorch_train_aclstm.py
File metadata and controls
executable file
·285 lines (202 loc) · 10.3 KB
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
import os
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
import random
import read_bvh
Hip_index = read_bvh.joint_index['hip']
Seq_len=100
Hidden_size = 1024
Joints_num = 57
Condition_num=5
Groundtruth_num=5
In_frame_size = Joints_num*3
class acLSTM(nn.Module):
def __init__(self, in_frame_size=171, hidden_size=1024, out_frame_size=171):
super(acLSTM, self).__init__()
self.in_frame_size=in_frame_size
self.hidden_size=hidden_size
self.out_frame_size=out_frame_size
##lstm#########################################################
self.lstm1 = nn.LSTMCell(self.in_frame_size, self.hidden_size)#param+ID
self.lstm2 = nn.LSTMCell(self.hidden_size, self.hidden_size)
self.lstm3 = nn.LSTMCell(self.hidden_size, self.hidden_size)
self.decoder = nn.Linear(self.hidden_size, self.out_frame_size)
#output: [batch*1024, batch*1024, batch*1024], [batch*1024, batch*1024, batch*1024]
def init_hidden(self, batch):
#c batch*(3*1024)
c0 = torch.autograd.Variable(torch.FloatTensor(np.zeros((batch, self.hidden_size)) ).cuda())
c1= torch.autograd.Variable(torch.FloatTensor(np.zeros((batch, self.hidden_size)) ).cuda())
c2 = torch.autograd.Variable(torch.FloatTensor(np.zeros((batch, self.hidden_size)) ).cuda())
h0 = torch.autograd.Variable(torch.FloatTensor(np.zeros((batch, self.hidden_size)) ).cuda())
h1= torch.autograd.Variable(torch.FloatTensor(np.zeros((batch, self.hidden_size)) ).cuda())
h2= torch.autograd.Variable(torch.FloatTensor(np.zeros((batch, self.hidden_size)) ).cuda())
return ([h0,h1,h2], [c0,c1,c2])
#in_frame b*In_frame_size
#vec_h [b*1024,b*1024,b*1024] vec_c [b*1024,b*1024,b*1024]
#out_frame b*In_frame_size
#vec_h_new [b*1024,b*1024,b*1024] vec_c_new [b*1024,b*1024,b*1024]
def forward_lstm(self, in_frame, vec_h, vec_c):
vec_h0,vec_c0=self.lstm1(in_frame, (vec_h[0],vec_c[0]))
vec_h1,vec_c1=self.lstm2(vec_h[0], (vec_h[1],vec_c[1]))
vec_h2,vec_c2=self.lstm3(vec_h[1], (vec_h[2],vec_c[2]))
out_frame = self.decoder(vec_h2) #out b*150
vec_h_new=[vec_h0, vec_h1, vec_h2]
vec_c_new=[vec_c0, vec_c1, vec_c2]
return (out_frame, vec_h_new, vec_c_new)
#output numpy condition list in the form of [groundtruth_num of 1, condition_num of 0, groundtruth_num of 1, condition_num of 0,.....]
def get_condition_lst(self,condition_num, groundtruth_num, seq_len ):
gt_lst=np.ones((100,groundtruth_num))
con_lst=np.zeros((100,condition_num))
lst=np.concatenate((gt_lst, con_lst),1).reshape(-1)
return lst[0:seq_len]
#in cuda tensor real_seq: b*seq_len*frame_size
#out cuda tensor out_seq b* (seq_len*frame_size)
def forward(self, real_seq, condition_num=5, groundtruth_num=5):
batch=real_seq.size()[0]
seq_len=real_seq.size()[1]
condition_lst=self.get_condition_lst(condition_num, groundtruth_num, seq_len)
#initialize vec_h vec_m #set as 0
(vec_h, vec_c) = self.init_hidden(batch)
out_seq = torch.autograd.Variable(torch.FloatTensor( np.zeros((batch,1)) ).cuda())
out_frame=torch.autograd.Variable(torch.FloatTensor( np.zeros((batch,self.out_frame_size)) ).cuda())
for i in range(seq_len):
if(condition_lst[i]==1):##input groundtruth frame
in_frame=real_seq[:,i]
else:
in_frame=out_frame
(out_frame, vec_h,vec_c) = self.forward_lstm(in_frame, vec_h, vec_c)
out_seq = torch.cat((out_seq, out_frame),1)
return out_seq[:, 1: out_seq.size()[1]]
#cuda tensor out_seq batch*(seq_len*frame_size)
#cuda tensor groundtruth_seq batch*(seq_len*frame_size)
def calculate_loss(self, out_seq, groundtruth_seq):
loss_function = nn.MSELoss()
loss = loss_function(out_seq, groundtruth_seq)
return loss
#numpy array real_seq_np: batch*seq_len*frame_size
def train_one_iteraton(real_seq_np, model, optimizer, iteration, save_dance_folder, print_loss=False, save_bvh_motion=True):
#set hip_x and hip_z as the difference from the future frame to current frame
dif = real_seq_np[:, 1:real_seq_np.shape[1]] - real_seq_np[:, 0: real_seq_np.shape[1]-1]
real_seq_dif_hip_x_z_np = real_seq_np[:, 0:real_seq_np.shape[1]-1].copy()
real_seq_dif_hip_x_z_np[:,:,Hip_index*3]=dif[:,:,Hip_index*3]
real_seq_dif_hip_x_z_np[:,:,Hip_index*3+2]=dif[:,:,Hip_index*3+2]
real_seq = torch.autograd.Variable(torch.FloatTensor(real_seq_dif_hip_x_z_np.tolist()).cuda() )
seq_len=real_seq.size()[1]-1
in_real_seq=real_seq[:, 0:seq_len]
predict_groundtruth_seq= torch.autograd.Variable(torch.FloatTensor(real_seq_dif_hip_x_z_np[:,1:seq_len+1].tolist())).cuda().view(real_seq_np.shape[0],-1)
predict_seq = model.forward(in_real_seq, Condition_num, Groundtruth_num)
optimizer.zero_grad()
loss=model.calculate_loss(predict_seq, predict_groundtruth_seq)
loss.backward()
optimizer.step()
if(print_loss==True):
print ("###########"+"iter %07d"%iteration +"######################")
print ("loss: "+str(loss.data.tolist()[0]))
if(save_bvh_motion==True):
##save the first motion sequence int the batch.
gt_seq=np.array(predict_groundtruth_seq[0].data.tolist()).reshape(-1,In_frame_size)
last_x=0.0
last_z=0.0
for frame in range(gt_seq.shape[0]):
gt_seq[frame,Hip_index*3]=gt_seq[frame,Hip_index*3]+last_x
last_x=gt_seq[frame,Hip_index*3]
gt_seq[frame,Hip_index*3+2]=gt_seq[frame,Hip_index*3+2]+last_z
last_z=gt_seq[frame,Hip_index*3+2]
out_seq=np.array(predict_seq[0].data.tolist()).reshape(-1,In_frame_size)
last_x=0.0
last_z=0.0
for frame in range(out_seq.shape[0]):
out_seq[frame,Hip_index*3]=out_seq[frame,Hip_index*3]+last_x
last_x=out_seq[frame,Hip_index*3]
out_seq[frame,Hip_index*3+2]=out_seq[frame,Hip_index*3+2]+last_z
last_z=out_seq[frame,Hip_index*3+2]
read_bvh.write_traindata_to_bvh(save_dance_folder+"%07d"%iteration+"_gt.bvh", gt_seq)
read_bvh.write_traindata_to_bvh(save_dance_folder+"%07d"%iteration+"_out.bvh", out_seq)
#input a list of dances [dance1, dance2, dance3]
#return a list of dance index, the occurence number of a dance's index is proportional to the length of the dance
def get_dance_len_lst(dances):
len_lst=[]
for dance in dances:
#length=len(dance)/100
length = 10
if(length<1):
length=1
len_lst=len_lst+[length]
index_lst=[]
index=0
for length in len_lst:
for i in range(length):
index_lst=index_lst+[index]
index=index+1
return index_lst
#input dance_folder name
#output a list of dances.
def load_dances(dance_folder):
dance_files=os.listdir(dance_folder)
dances=[]
for dance_file in dance_files:
print ("load "+dance_file)
dance=np.load(dance_folder+dance_file)
print ("frame number: "+ str(dance.shape[0]))
dances=dances+[dance]
return dances
# dances: [dance1, dance2, dance3,....]
def train(dances, frame_rate, batch, seq_len, read_weight_path, write_weight_folder, write_bvh_motion_folder, total_iter=500000):
seq_len=seq_len+2
torch.cuda.set_device(0)
model = acLSTM()
if(read_weight_path!=""):
model.load_state_dict(torch.load(read_weight_path))
model.cuda()
#model=torch.nn.DataParallel(model, device_ids=[0,1])
current_lr=0.0001
optimizer = torch.optim.Adam(model.parameters(), lr=current_lr)
model.train()
#dance_len_lst contains the index of the dance, the occurance number of a dance's index is proportional to the length of the dance
dance_len_lst=get_dance_len_lst(dances)
random_range=len(dance_len_lst)
speed=frame_rate/30 # we train the network with frame rate of 30
for iteration in range(total_iter):
#get a batch of dances
dance_batch=[]
for b in range(batch):
#randomly pick up one dance. the longer the dance is the more likely the dance is picked up
dance_id = dance_len_lst[np.random.randint(0,random_range)]
dance=dances[dance_id].copy()
dance_len = dance.shape[0]
start_id=random.randint(10, dance_len-seq_len*speed-10)#the first and last several frames are sometimes noisy.
sample_seq=[]
for i in range(seq_len):
sample_seq=sample_seq+[dance[int(i*speed+start_id)]]
#augment the direction and position of the dance
T=[0.1*(random.random()-0.5),0.0, 0.1*(random.random()-0.5)]
R=[0,1,0,(random.random()-0.5)*np.pi*2]
sample_seq_augmented=read_bvh.augment_train_data(sample_seq, T, R)
dance_batch=dance_batch+[sample_seq_augmented]
dance_batch_np=np.array(dance_batch)
print_loss=False
save_bvh_motion=False
if(iteration % 1==0):
print_loss=True
if(iteration % 1000==0):
save_bvh_motion=True
train_one_iteraton(dance_batch_np, model, optimizer, iteration, write_bvh_motion_folder, print_loss, save_bvh_motion)
#end=time.time()
#print end-start
if(iteration%1000 == 0):
path = write_weight_folder + "%07d"%iteration +".weight"
torch.save(model.state_dict(), path)
read_weight_path=""
write_weight_folder="../train_weight_aclstm_indian/"
write_bvh_motion_folder="../train_tmp_bvh_aclstm_indian/"
dances_folder = "../train_data_xyz/indian/"
dance_frame_rate=60
batch=32
if not os.path.exists(write_weight_folder):
os.makedirs(write_weight_folder)
if not os.path.exists(write_bvh_motion_folder):
os.makedirs(write_bvh_motion_folder)
dances= load_dances(dances_folder)
train(dances, dance_frame_rate, batch, 100, read_weight_path, write_weight_folder, write_bvh_motion_folder, 200000)