-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsplit.py
309 lines (257 loc) · 13.2 KB
/
split.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
# coding=utf-8
# Copyright 2023 Ling-Hao CHEN (https://lhchen.top) from Tsinghua University.
#
# For all the datasets, be sure to read and follow their license agreements,
# and cite them accordingly.
# If the unifier is used in your research, please consider to cite as:
#
# @article{chen2023unimocap,
# title={UniMocap: Unifier for BABEL, HumanML3D, and KIT},
# author={Chen, Ling-Hao and UniMocap, Contributors},
# journal={https://github.com/LinghaoChan/UniMoCap},
# year={2023}
# }
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. We provide a license to use the code,
# please read the specific details carefully.
import argparse
import os
import random
import subprocess
import pandas as pd
import tqdm
def split_list_randomly(input_list, split_ratios):
"""
Split a List Randomly Based on Ratios
This function takes a list and splits it into multiple sublists randomly based on the provided split ratios.
Args:
input_list (list): The input list to be split.
split_ratios (list): List of ratios for splitting the input list.
Returns:
list: A list of sublists obtained by splitting the input list.
"""
total_length = len(input_list)
# Calculate the length of each sublist based on the ratios
split_lengths = [int(ratio * total_length) for ratio in split_ratios]
# Add an additional sublist to handle rounding errors
split_lengths.append(total_length - sum(split_lengths))
# Shuffle the input list randomly
random.shuffle(input_list)
# Generate sublists using list slicing
result = []
_start = 0
for length in split_lengths:
result.append(input_list[_start:_start+length])
_start += length
return result
def truncate_list_elements(lst):
"""
Truncate the Last 4 Characters of Elements in a List
This function takes a list and truncates the last 4 characters of each element.
Args:
lst (list): The input list with elements to be truncated.
Returns:
list: A list with elements having the last 4 characters removed.
"""
return [item[:-4] for item in lst]
def write_list_to_txt(input_list, file_path):
"""
Write a List to a Text File
This function writes the elements of a list to a text file, one element per line.
Args:
input_list (list): The input list to be written to the file.
file_path (str): The path to the output text file.
Returns:
None
"""
with open(file_path, 'w') as file:
for item in input_list:
file.write(item + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# It allows choosing between two options: "body-only-unimocap" and "whole-body-motion"
parser.add_argument('--motion_type', choices=["body-only-unimocap", "whole-body-motion"],
type=str, default="body-only-unimocap", help='Choice of motion type.')
args = parser.parse_args()
motion_type = args.motion_type
alldata = []
dataset = ['h3d_h3dformat.csv',
'babel_h3dformat.csv', 'kitml_h3dformat.csv']
for d in dataset:
df = pd.read_csv(d)
column_list = df['source_path'].tolist()
alldata.extend(column_list)
alldata = list(set(alldata))
split_ratios = [0.80, 0.05, 0.15]
# Separate the result into training, validation, and test sets
result = split_list_randomly(alldata, split_ratios)
train, val, test = result[0], result[1], result[2]
# Create a dictionary to store split information for each dataset
split = {}
for sub in dataset:
df = pd.read_csv(sub)
tmp_train, tmp_val, tmp_test = [], [], []
for index, row in df.iterrows():
if row['source_path'] in train:
tmp_train.append(row['new_name'])
elif row['source_path'] in test:
tmp_test.append(row['new_name'])
elif row['source_path'] in val:
tmp_val.append(row['new_name'])
else:
assert "not found"
s = sub[:-4]
split[s] = [tmp_train, tmp_test, tmp_val]
# Set save_dir based on the chosen motion type
if motion_type == "body-only-unimocap":
os.system(f"mkdir ./{motion_type}/UniMocap")
os.system(f"mkdir ./{motion_type}/UniMocap/texts")
os.system(f"mkdir ./{motion_type}/UniMocap/new_joints")
os.system(f"mkdir ./{motion_type}/UniMocap/new_joint_vecs")
else:
os.system(f"mkdir ./{motion_type}/UniMocap")
os.system(f"mkdir ./{motion_type}/UniMocap/texts")
os.system(f"mkdir ./{motion_type}/UniMocap/smplx_322")
save_dir = f"./{motion_type}/UniMocap"
os.makedirs(save_dir, exist_ok=True)
count = 0
trainlist, vallist, testlist = [], [], []
for key in split.keys():
print(key)
# Define the mapping from csv name to save directory
def f(key): return {'h3d_h3dformat': "H3D",
'babel_h3dformat': "BABEL", 'kitml_h3dformat': "KIT"}[key]
new_key = f(key)
value = split[key]
sub_train, sub_test, sub_val = value[0], value[1], value[2]
subdir = f'./{motion_type}/' + new_key
# Determine the appropriate jointname based on the motion type
if motion_type == "body-only-unimocap":
jointname = "new_joints"
else:
jointname = "joints"
# Copy and organize data files for training set
for filename in tqdm.tqdm(sub_train):
if not os.path.exists(f"{subdir}/{jointname}/{filename}"):
print(f"{subdir}/{jointname}/{filename}")
continue
else:
if motion_type == "body-only-unimocap":
new_id = str(count).zfill(8)
# Define copy commands for various data files and subprocess.Popen to execute them
c1 = f"cp {subdir}/new_joints/{filename} {save_dir}/new_joints/{new_id}.npy"
c2 = f"cp {subdir}/new_joint_vecs/{filename} {save_dir}/new_joint_vecs/{new_id}.npy"
c3 = f"cp {subdir}/texts/{filename[:-4]}.txt {save_dir}/texts/{new_id}.txt"
c4 = f"cp {subdir}/new_joints/M{filename} {save_dir}/new_joints/M{new_id}.npy"
c5 = f"cp {subdir}/new_joint_vecs/M{filename} {save_dir}/new_joint_vecs/M{new_id}.npy"
c6 = f"cp {subdir}/texts/M{filename[:-4]}.txt {save_dir}/texts/M{new_id}.txt"
# Execute the copy commands using subprocess.Popen
subprocess.Popen(c1, shell=True)
subprocess.Popen(c2, shell=True)
subprocess.Popen(c3, shell=True)
subprocess.Popen(c4, shell=True)
subprocess.Popen(c5, shell=True)
subprocess.Popen(c6, shell=True)
else:
new_id = str(count).zfill(8)
# Define copy commands for joints and texts, and execute them using subprocess.Popen
c1 = f"cp {subdir}/joints/{filename} {save_dir}/smplx_322/{new_id}.npy"
c2 = f"cp {subdir}/texts/{filename[:-4]}.txt {save_dir}/texts/{new_id}.txt"
c3 = f"cp {subdir}/joints/M{filename} {save_dir}/smplx_322/M{new_id}.npy"
c4 = f"cp {subdir}/texts/M{filename[:-4]}.txt {save_dir}/texts/M{new_id}.txt"
# Execute the copy commands using subprocess.Popen
subprocess.Popen(c1, shell=True)
subprocess.Popen(c2, shell=True)
subprocess.Popen(c3, shell=True)
subprocess.Popen(c4, shell=True)
count += 1
trainlist.append(new_id)
trainlist.append("M"+new_id)
# Copy and organize data files for test set (similar to the training set)
for filename in tqdm.tqdm(sub_test):
if not os.path.exists(f"{subdir}/{jointname}/{filename}"):
print(f"{subdir}/{jointname}/{filename}")
continue
else:
if motion_type == "body-only-unimocap":
new_id = str(count).zfill(8)
# Define copy commands for various data files and subprocess.Popen to execute them
c1 = f"cp {subdir}/new_joints/{filename} {save_dir}/new_joints/{new_id}.npy"
c2 = f"cp {subdir}/new_joint_vecs/{filename} {save_dir}/new_joint_vecs/{new_id}.npy"
c3 = f"cp {subdir}/texts/{filename[:-4]}.txt {save_dir}/texts/{new_id}.txt"
c4 = f"cp {subdir}/new_joints/M{filename} {save_dir}/new_joints/M{new_id}.npy"
c5 = f"cp {subdir}/new_joint_vecs/M{filename} {save_dir}/new_joint_vecs/M{new_id}.npy"
c6 = f"cp {subdir}/texts/M{filename[:-4]}.txt {save_dir}/texts/M{new_id}.txt"
# Execute the copy commands using subprocess.Popen
subprocess.Popen(c1, shell=True)
subprocess.Popen(c2, shell=True)
subprocess.Popen(c3, shell=True)
subprocess.Popen(c4, shell=True)
subprocess.Popen(c5, shell=True)
subprocess.Popen(c6, shell=True)
else:
new_id = str(count).zfill(8)
# Define copy commands for joints and texts, and execute them using subprocess.Popen
c1 = f"cp {subdir}/joints/{filename} {save_dir}/smplx_322/{new_id}.npy"
c2 = f"cp {subdir}/texts/{filename[:-4]}.txt {save_dir}/texts/{new_id}.txt"
c3 = f"cp {subdir}/joints/M{filename} {save_dir}/smplx_322/M{new_id}.npy"
c4 = f"cp {subdir}/texts/M{filename[:-4]}.txt {save_dir}/texts/M{new_id}.txt"
# Execute the copy commands using subprocess.Popen
subprocess.Popen(c1, shell=True)
subprocess.Popen(c2, shell=True)
subprocess.Popen(c3, shell=True)
subprocess.Popen(c4, shell=True)
count += 1
testlist.append(new_id)
testlist.append("M"+new_id)
# Copy and organize data files for the validation set (similar to the training set)
for filename in tqdm.tqdm(sub_val):
if not os.path.exists(f"{subdir}/{jointname}/{filename}"):
print(f"{subdir}/{jointname}/{filename}")
continue
else:
if motion_type == "body-only-unimocap":
new_id = str(count).zfill(8)
# Define copy commands for various data files and subprocess.Popen to execute them
c1 = f"cp {subdir}/new_joints/{filename} {save_dir}/new_joints/{new_id}.npy"
c2 = f"cp {subdir}/new_joint_vecs/{filename} {save_dir}/new_joint_vecs/{new_id}.npy"
c3 = f"cp {subdir}/texts/{filename[:-4]}.txt {save_dir}/texts/{new_id}.txt"
c4 = f"cp {subdir}/new_joints/M{filename} {save_dir}/new_joints/M{new_id}.npy"
c5 = f"cp {subdir}/new_joint_vecs/M{filename} {save_dir}/new_joint_vecs/M{new_id}.npy"
c6 = f"cp {subdir}/texts/M{filename[:-4]}.txt {save_dir}/texts/M{new_id}.txt"
# Execute the copy commands using subprocess.Popen
subprocess.Popen(c1, shell=True)
subprocess.Popen(c2, shell=True)
subprocess.Popen(c3, shell=True)
subprocess.Popen(c4, shell=True)
subprocess.Popen(c5, shell=True)
subprocess.Popen(c6, shell=True)
else:
new_id = str(count).zfill(8)
# Define copy commands for joints and texts, and execute them using subprocess.Popen
c1 = f"cp {subdir}/joints/{filename} {save_dir}/smplx_322/{new_id}.npy"
c2 = f"cp {subdir}/texts/{filename[:-4]}.txt {save_dir}/texts/{new_id}.txt"
c3 = f"cp {subdir}/joints/M{filename} {save_dir}/smplx_322/M{new_id}.npy"
c4 = f"cp {subdir}/texts/M{filename[:-4]}.txt {save_dir}/texts/M{new_id}.txt"
# Execute the copy commands using subprocess.Popen
subprocess.Popen(c1, shell=True)
subprocess.Popen(c2, shell=True)
subprocess.Popen(c3, shell=True)
subprocess.Popen(c4, shell=True)
count += 1
vallist.append(new_id)
vallist.append("M"+new_id)
# Write lists of training, validation, and test IDs to text files
write_list_to_txt(trainlist, f"./{motion_type}/UniMocap/train.txt")
write_list_to_txt(vallist, f"./{motion_type}/UniMocap/val.txt")
write_list_to_txt(testlist, f"./{motion_type}/UniMocap/test.txt")