Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
sigal-raab committed Jul 24, 2022
1 parent b565dea commit 047f0d8
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 40 deletions.
15 changes: 2 additions & 13 deletions Motion/Animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ def __getitem__(self, k):

try:
# since joints are re-indexed, we need to take spacial care on parents indices
reind = reindex(self.parents, k[1])

order_inversed = {num: i for i, num in enumerate(k[1])}
order_inversed[-1] = -1
reindexed_parents = np.array([order_inversed[self.parents[i]] for i in k[1]])
if not all(reind==reindexed_parents): # todo: delete prev 3 lines
print('ABORT ABORT ABORT')
reindexed_parents = reindex(self.parents, k[1])
except: # we will reach the 'except' if k[1] is Nones, i.e., if the sub-selection is over frames rather then over joints
reindexed_parents = self.parents

Expand Down Expand Up @@ -687,12 +681,7 @@ def animation_from_offsets(offsets, parents, shape=None):
offsets = offsets[sorted_order]

# reorder parents
p = reindex(parents, sorted_order)

sorted_order_inversed = {num: i for i, num in enumerate(sorted_order)}
sorted_order_inversed[-1] = -1
parents = np.array([sorted_order_inversed[parents[i]] for i in sorted_order])
assert all(p==parents) # todo: delete prev 3 lines if assert works well
parents = reindex(parents, sorted_order)

if shape is None:
shape=(1, offsets.shape[0])
Expand Down
4 changes: 2 additions & 2 deletions Motion/AnimationStructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def children_list(parents):
"""

def joint_children(i):
return [j for j, p in enumerate(parents) if not isinstance(p, tuple) and p == i] # todo: 'isinstance' is a hack. change later
return [j for j, p in enumerate(parents) if not isinstance(p, tuple) and p == i]

return list(map(lambda j: np.array(joint_children(j)), joints(parents)))

Expand Down Expand Up @@ -477,4 +477,4 @@ def get_sorted_order(parents):
sorted_order, _ = get_sorted_order_internal(sorted_order, 0, root_idx, children)
return sorted_order



6 changes: 3 additions & 3 deletions blender/import_multiple_bvh.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def location(horiz_loc, vert_loc, entity):

scale_factor = 3

base_path = '/Users/sigalraab/momo/train_outputs/stylegan2_motion_skeleton' # example: '/home/username/train_outputs'
cur_path = 'Jasper_all_5K_norm_mixing_0p9_pool_edgerot_globpos_footvelo_tmpMoDi_rmnoise_shaeb57_deep_2.3508de29d4184dcb8eb60451a7dee025/models/079999_files/22_06_29_08_55_sample_10' # example: 'experiment_name.362db4a171934333bea96e9c10712d95/models/079999_files/22_06_27_16_27_interp')'
base_path = '<type the path to your models>' # example: '/home/username/train_outputs'
cur_path = '<type the rest of the path>' # example: 'experiment_name.362db4a171934333bea96e9c10712d95/models/079999_files/22_06_16_15_10_sample_10')'
path = osp.join(base_path, cur_path)
files = glob.glob(osp.join(path, 'generated_*.bvh'))
files = [osp.basename(file) for file in files if 'fixed' not in file] # use only file name without path
Expand All @@ -35,7 +35,7 @@ def location(horiz_loc, vert_loc, entity):
print('***********************')
print('args.csv was not found.')
print('***********************')
exit()
args = {'type': 'sample'}

if 'entity' in args:
entity = args['entity']
Expand Down
2 changes: 1 addition & 1 deletion evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def generate(args, g_ema, device, mean_joints, std_joints, entity):
anim_dicts, frame_mults, is_sub_motion = edge_rot_dict_from_edge_motion_data(motion_data, type='sample', edge_rot_dict_general = edge_rot_dict_general)

for j, (anim_dict, frame_mult) in enumerate(zip(anim_dicts, frame_mults)):
anim, names = anim_from_edge_rot_dict(anim_dict, root_name='Hips') # todo: fix the hard coding
anim, names = anim_from_edge_rot_dict(anim_dict, root_name='Hips')
# compute global positions using anim
positions = Animation.positions_global(anim)

Expand Down
2 changes: 0 additions & 2 deletions models/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ def __init__(self, pooling_list, output_joints_num):

for i, affecting_joints in self.pooling_list.items():
for j in affecting_joints:
# todo: consider learning a weight for each unpooled vertex
# assign a fixed weight for each unpooled vertex which is 1/count
self.weight[j, i] = 1

# if an output joint is affected by more than one input joint, it takes the average of all contributors
Expand Down
2 changes: 1 addition & 1 deletion train.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
get_world_size,
)
try:
from clear import Task
from clearml import Task
except ImportError:
Task = None

Expand Down
18 changes: 7 additions & 11 deletions utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from utils.foot import get_foot_contact


foot_names = ['LeftFoot', 'RightFoot'] # todo: convert to generic code
foot_names = ['LeftFoot', 'RightFoot']

class openpose_joints(object):
def __init__(self):
Expand Down Expand Up @@ -66,8 +66,7 @@ def entity2index(obj, ordered_entities):
return res


class Joint(): # todo: move to a more appropriate file
# todo: deduce automatically from input structure
class Joint():
n_channels = 3

@staticmethod
Expand All @@ -84,7 +83,6 @@ def str():
{0:[0], 1:[3]},
{0:[0], 1:[2], 2:[4], 3:[5], 4:[7], 5:[9]},
{0:[1], 1:[3], 2:[4], 3:[6], 4:[7], 5:[8], 6:[10], 7:[11], 8:[13], 9:[14]}]
# todo: deduce automatically from skeletal_pooling_dist_1
oj = openpose_joints()
parents_list = [[-1],
[-1,0],
Expand All @@ -94,7 +92,7 @@ def str():
parents_list = list(map(np.array, parents_list)) # convert list items to np


class Edge(): # todo: instead of edge, relate motion to end joint (unlike start joint in bvh). then the toppology structure will be same as for Joint
class Edge():
n_channels = 4

@staticmethod
Expand Down Expand Up @@ -169,9 +167,7 @@ def str():
assert all([list(parents.keys()) == edges for (parents, edges) in zip(parents_list_edges, edges_list)]) # same order as edges_list
assert all([list(parents_list.values())[0]==-1 for parents_list in parents_list_edges]) # 1st item is root

# todo: this is a temporary solution until we have a topology class
feet_list_edges = [[], [(0, 1)], [(3, 5), (3, 4)], [(8, 9), (6, 7)], [(13, 14), (10, 11)]] # should be ordered [LeftFoot,RightFoot]
# n_feet = len(feet_list_edges[-1])

n_hierarchical_stages = len(parents_list_edges)
assert len(edges_list) == n_hierarchical_stages and len(parents_list_edges) == n_hierarchical_stages and \
Expand Down Expand Up @@ -289,7 +285,7 @@ def calc_bone_lengths(motion_data, parents=None, names=None):
if motion_data.ndim == 3: # no batch dim
motion_data = motion_data[np.newaxis]

# hack to support 16 joints skeleton
# support 16 joints skeleton
if motion_data.shape[1] == 16:
motion_data = motion_data[:,:15]

Expand Down Expand Up @@ -636,7 +632,7 @@ def edge_rot_dict_from_edge_motion_data(motion_data, type='sample', edge_rot_dic
parents = Edge.parents_list[hierarchy_level]
nearest_edge_idx_w_root_edge = np.array(
(list(Edge.skeletal_pooling_dist_0[hierarchy_level].values()))).flatten()
assert parents[0] == -1 and Edge.parents_list[hierarchy_level+1][0] == -1 # hack to make next line count on root location at index 0
assert parents[0] == -1 and Edge.parents_list[hierarchy_level+1][0] == -1 # make next line count on root location at index 0
nearest_edge_idx = nearest_edge_idx_w_root_edge[1:] - 1 # root is first, hence we look from index 1 and reduce one because root is first on uppler level too.
if feet:
# remove foot contact label
Expand Down Expand Up @@ -754,7 +750,7 @@ def motion_from_raw(args, motion_data_raw):
edge_rot_dict_general['mean'] = mean_joints.transpose(0, 2, 1, 3)
edge_rot_dict_general['std'] = std_joints.transpose(0, 2, 1, 3)

if args.foot: # todo: depart np/torch functions
if args.foot:
motion_data_torch = torch.from_numpy(motion_data).transpose(1, 2)
motion_data_torch = append_foot_contact(motion_data_torch, args.glob_pos, args.axis_up, edge_rot_dict_general)
motion_data = motion_data_torch.transpose(1, 2).numpy()
Expand Down Expand Up @@ -791,4 +787,4 @@ def append_foot_contact(motion_data, glob_pos, axis_up, edge_rot_dict_general):


if __name__ == "__main__":
pass
pass
6 changes: 3 additions & 3 deletions utils/preprocess_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ def compute_mean_bone_length(animation_names, char_dir):
anim, _, _ = BVH.load(anim_path)

global_location = positions_global(anim)[:, :]
parents_hack = copy.deepcopy(anim.parents) # this hack is good for Jasper topology only
parents_hack[3] = 0
anim_offsets = (global_location[:, 1:] - global_location[:, parents_hack[1:]])
parents = copy.deepcopy(anim.parents)
parents[3] = 0
anim_offsets = (global_location[:, 1:] - global_location[:, parents[1:]])
anim_offsets = np.concatenate([global_location[:, :1], anim_offsets],
axis=1) # for root joint (index 0), put the offset from the global zero
if one_char_offsets is None:
Expand Down
1 change: 0 additions & 1 deletion utils/traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ def fixed_dim_pad(self, kernel_size):
return (self.smaller_n_joints - 1,) + super().fixed_dim_pad(kernel_size)

def weight_internal(self, in_channel, out_channel, kernel_size):
# todo: change weight initialization
return torch.randn(out_channel, in_channel, self.smaller_n_joints, self.larger_n_joints, kernel_size)

def mask_affectors(self, mask, out_channel, joint_idx, affectors_this_joint):
Expand Down
5 changes: 2 additions & 3 deletions utils/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ def motion2fig(data, H=512, W=512, n_sampled_motions=5, n_sampled_frames=5, enti

assert not isinstance(data, list) and not isinstance(data[0], dict)

# todo: not debugged for normalization!!!
data = data * edge_rot_dict_general['std'].transpose(0, 2, 1, 3) + edge_rot_dict_general['mean'].transpose(0, 2, 1, 3)

sampled_data = data[sampled_motions][:, :, :, sampled_frames]
Expand All @@ -164,7 +163,7 @@ def motion2fig(data, H=512, W=512, n_sampled_motions=5, n_sampled_frames=5, enti
anim, _ = anim_from_edge_rot_dict(one_edge_rot_dict)
joints[idx] = Animation.positions_global(anim)

anim_joints_1_to_open_pose = [7, 6, 15, 16, 17, 10, 11, 12, 0, 23, 24, 25, 19, 20, 21] # todo: change to non hard coded
anim_joints_1_to_open_pose = [7, 6, 15, 16, 17, 10, 11, 12, 0, 23, 24, 25, 19, 20, 21]
data = joints[:, :, anim_joints_1_to_open_pose, :]
data = data.transpose(0, 2, 3, 1) # samples x frames x joints x features ==> samples x joints x features x frames

Expand Down Expand Up @@ -218,7 +217,7 @@ def motion2bvh_rot(motion_data, bvh_file_path, type, edge_rot_dict_general=None)

# from this point input is a list of edge_rot_dicts
for i, (edge_rot_dict, frame_mult) in enumerate(zip(edge_rot_dicts, frame_mults)):
anim, names = anim_from_edge_rot_dict(edge_rot_dict, root_name='Hips') # todo: fix the hard coding
anim, names = anim_from_edge_rot_dict(edge_rot_dict, root_name='Hips')
if is_sub_motion:
suffix = '_{}x{}'.format(edge_rot_dict['rot_edge_no_root'].shape[1], int(edge_rot_dict['rot_edge_no_root'].shape[0]/frame_mult))
elif type == 'edit':
Expand Down

0 comments on commit 047f0d8

Please sign in to comment.