add audio2mesh_ar

This commit is contained in:
Sen Liang committed 2022-03-14 18:13:13 +08:00
1 parent ded9b27f71
commit e0f24982d1
46 files changed
+3917 -325

No files matched your search

+192 -24
View File
@@ -1,11 +1,14 @@
import imp
import numpy as np
import torch
import time
import random
from torch.utils.data import Dataset
from pathlib import Path
from scipy.signal import savgol_filter
from common.debug import debug
# common
from common.resampling import resample_by_nsteps
class MotionLiveDataset(Dataset):
"""
@@ -18,15 +21,13 @@ class MotionLiveDataset(Dataset):
"""
super(MotionLiveDataset, self).__init__()
self.opt = opt
self.phase = phase
path_data= Path(opt.DATASET.root).joinpath(f"data_{phase}.npz")
# for test
# if phase == "valid":
# path_data= Path(opt.DATASET.root).joinpath(f"data_train.npz")
# if phase == "test":
# path_data= Path(opt.DATASET.root).joinpath(f"data_train.npz")
@@ -49,7 +50,6 @@ class MotionLiveDataset(Dataset):
f'''nsteps_frame={self.nsteps_frame}\t'''
f'''nsteps_segmt={self.nsteps_segmt} ''')
# path_motion = self.path_data.joinpath(f"motion_{phase}.npz")
# path_speech = self.path_data.joinpath(f"speech_{phase}.npz")
# print(f"load data ... --> {path_motion} | {path_speech}")
@@ -65,9 +65,9 @@ class MotionLiveDataset(Dataset):
def __len__(self):
if self.phase == "train":
return 20000 #40000
return 30000 #40000
elif self.phase == "valid":
return 2000 #5000
return 3000 #5000
else:
return 100
@@ -91,38 +91,206 @@ class MotionLiveDataset(Dataset):
data = self.annodb[vid]
data_motion = data["data_motion"] # (nframes, nc_motion)
data_speech = data["data_speech"] # (nsegemts, )
# data_shape = data["data_shape"] # (nframes, nc_shape)
data_motion = data["data_motion"] # (nframes, nc_motion)
data_speech = data["data_speech"] # (nsegemts, )
# data_shape = data["data_shape"] # (nframes, nc_shape)
nframe = data_motion.shape[0]
nsegmt = data_speech.shape[0]
# random select beg:end frame
beg_frame = np.random.randint(nframe - self.nsteps_frame - 10) + 5 # select from (5:-5)
end_frame = beg_frame + self.nsteps_frame
if self.phase == "train" and self.opt.DATASET.shifting_speed and random.random() < 0.3:
speed = random.sample({0.5, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.5},1)[0]
beg_segmt = int((1.0 * beg_frame / nframe) * nsegmt)
end_segmt = beg_segmt + self.nsteps_segmt
# print(f"nframe-{self.nsteps_frame:04d} =[{beg_frame:04d},{end_frame:04d})\t nsegmet-{self.nsteps_segmt:04d}=[{beg_segmt:04d},{end_segmt:04d}), vid={vid}")
beg_frame = np.random.randint(nframe - int(self.nsteps_frame * speed) - 10) + 5 # select from (5:-5)
end_frame = beg_frame + int(self.nsteps_frame * speed)
data_motion = data_motion[beg_frame:end_frame, :] # (nteps, 53)
data_speech = data_speech[beg_segmt:end_segmt] # (nsegmt_steps, )
# data_shape = data_shape[beg_frame:end_frame, :] # (nsteps, 100)
beg_segmt = int((1.0 * beg_frame / nframe) * nsegmt)
end_segmt = beg_segmt + int(self.nsteps_segmt * speed)
data_motion = data_motion[beg_frame:end_frame] # (nteps, 53)
data_speech = data_speech[beg_segmt:end_segmt] # (nsegmt_steps, )
assert data_speech.shape[0] == int(self.nsteps_segmt * speed), f"data_speech shape={data_speech.shape}, vid={vid}"
assert data_motion.shape[0] == int(self.nsteps_frame * speed), f"data_motion shape={data_motion.shape}, vid={vid}"
# print(f"speed={speed}, data_speech shape={data_speech.shape} data_motion shape={data_motion.shape}")
# resample to same nsteps
data_motion = resample_by_nsteps(data_motion, nsteps_tgt=self.nsteps_frame)
data_speech = resample_by_nsteps(data_speech, nsteps_tgt=self.nsteps_segmt)
# print(f"resampling > data_speech shape={data_speech.shape} data_motion shape={data_motion.shape} \n")
else:
# random select beg:end frame
beg_frame = np.random.randint(nframe - self.nsteps_frame - 10) + 5 # select from (5:-5)
end_frame = beg_frame + self.nsteps_frame
beg_segmt = int((1.0 * beg_frame / nframe) * nsegmt)
end_segmt = beg_segmt + self.nsteps_segmt
# print(f"nframe-{self.nsteps_frame:04d} =[{beg_frame:04d},{end_frame:04d})\t nsegmet-{self.nsteps_segmt:04d}=[{beg_segmt:04d},{end_segmt:04d}), vid={vid}")
data_motion = data_motion[beg_frame:end_frame] # (nteps, 53)
data_speech = data_speech[beg_segmt:end_segmt] # (nsegmt_steps, )
assert data_speech.shape[0] == self.nsteps_segmt, f"data_speech shape={data_speech.shape}, vid={vid}"
assert data_motion.shape[0] == self.nsteps_frame, f"data_motion shape={data_motion.shape}, vid={vid}"
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
data_motion = savgol_filter(data_motion, window_length=5, polyorder=3, axis=0, mode='nearest')
# data_shape = savgol_filter(data_shape, window_length=5, polyorder=3, axis=0, mode='nearest')
# random augments: reverse
if self.phase == "train" and self.opt.DATASET.flip:
data_motion, data_speech = self.make_auguments_flip(data_motion, data_speech, p=0.3)
# print(f"flip: data_motion-{data_motion.shape} data_speech={data_speech.shape}")
# random augments: shuffle speed
result = {
"data": data_motion, # (nsteps_frame, dim_motion)
"cond": data_speech, # (nsteps_frame, dim_speech)
"vid": vid,
"nframe": nframe,
"frame_interval": [beg_frame, end_frame],
}
return result
def make_auguments_flip(self, data_motion, data_speech, p=0.5):
'''
parameters:
data_motion: (nsteps_motion, 53)
data_speech: (nsteps_speech, ), where nsteps_speech!= nsteps_motion
'''
if random.random() < p:
data_motion = data_motion[::-1].copy()
data_speech = data_speech[::-1].copy()
return data_motion, data_speech
def make_auguments_speed(self, data_motion, data_speech, p=0.5):
'''
parameters:
data_motion: (nsteps_motion, 53)
data_speech: (nsteps_speech, ), where nsteps_speech!= nsteps_motion
'''
if random.random() > p:
return data_motion, data_speech
nsteps_motion = data_motion.shape[0]
nsteps_speech = data_speech.shape[0]
speed = random.sample({0.5, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.5},1)[0]
if speed < 0:
data_motion = data_motion[:nsteps_motion*speed]
data_speech = data_speech[:nsteps_speech*speed]
return data_motion, data_speech
class MotionLiveEvalDataset(Dataset):
"""
Motion dataset.
Prepares conditioning information (previous poses + control signal) and the corresponding next poses"""
def __init__(self, opt, phase="train"):
"""
phase = "train | valid | test "
"""
super(MotionLiveEvalDataset, self).__init__()
self.opt = opt
self.phase = phase
path_data= Path(opt.DATASET.root).joinpath(f"data_{phase}.npz")
print(f"\n [{self.phase}] load data ... --> {path_data}")
# dict vid: {"data_motion", # [nframe, 53]
# "data_speech", # [duration * sample_rate, ] raw deepseep
# "nframe", "fps", "sr"}
self.annodb = np.load(path_data, allow_pickle=True)['data'].item()
self.num_pid = len(self.annodb)
self.pids = list(self.annodb.keys())
self.annodb_accums = self.count_annotations()
self.overlap = 0.5
self.fps = opt.DATASET.fps # 50
self.sr = opt.DATASET.sr # 16000
self.nwind = eval(f"opt.DATASET.nwind_second_{phase}")
self.nsteps_frame = self.nwind * self.fps
self.nsteps_segmt = self.nwind * self.sr
print(f'''len(annodb)={len(self.annodb)}\t'''
f'''nsteps_frame={self.nsteps_frame}\t'''
f'''nsteps_segmt={self.nsteps_segmt} ''')
def __len__(self):
return self.annodb_accums[-1]
def count_annotations(self):
acc = 0
annodb_accums = np.zeros(self.num_pid + 1, dtype=int) # the first element set 0
for i, anno_dict in enumerate(self.annodb.values()):
acc += anno_dict["data_motion"].shape[0] // (self.nsteps_frame * self.overlap) - 1 # ncount_windows
annodb_accums[i+1] = acc # 2
return annodb_accums
def __getitem__(self, idx):
"""
Returns poses and conditioning.
If data-dropout sould be applied, a random selection of the previous poses is masked.
The control is not masked
return:
motion: (nsteps, nc_motion), current frame's pose (15 joint, xyz position)
speech: (nsteps, nc_speech), conditional data = previous motion data + full windows contrl data
"""
for i, iacc in enumerate(self.annodb_accums):
if idx > iacc -1 and idx < self.annodb_accums[i+1]:
pid, new_idx = i, idx - iacc
break
print(f"DEBUG: idx={idx}, pid = {pid}, iacc ={self.annodb_accums[i]}, new_idx = {new_idx}")
# return item
vid = self.pids[pid]
data = self.annodb[vid]
data_motion = data["data_motion"] # (nframes, nc_motion)
data_speech = data["data_speech"] # (nsegemts, )
nframe = data_motion.shape[0]
nsegmt = data_speech.shape[0]
# random select beg:end frame
beg_frame = new_idx
end_frame = beg_frame + self.nsteps_frame
beg_segmt = int((1.0 * beg_frame / nframe) * nsegmt)
end_segmt = beg_segmt + self.nsteps_segmt
print(f"nframe-{self.nsteps_frame:04d} =[{beg_frame:04d},{end_frame:04d})\nnsegmet-{self.nsteps_segmt:04d}=[{beg_segmt:04d},{end_segmt:04d}), vid={vid}")
data_motion = data_motion[beg_frame:end_frame] # (nteps, 53)
data_speech = data_speech[beg_segmt:end_segmt] # (nsegmt_steps, )
print(f"data_motion-{data_motion.shape} data_speech={data_speech.shape}")
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
data_motion = savgol_filter(data_motion, window_length=5, polyorder=3, axis=0, mode='nearest')
result = {
"data": data_motion, # (nsteps_frame, dim_motion)
"cond": data_speech, # (nsteps_frame, dim_speech)
# "shape": data_shape,
"vid": vid,
"nframe": nframe,
"frame_interval": [beg_frame, end_frame],
@@ -131,4 +299,4 @@ class MotionLiveDataset(Dataset):
assert data_speech.shape[0] == self.nsteps_segmt, f"data_speech shape={data_speech.shape}, vid={vid}"
return result
return result
+46 -3
View File
@@ -1,3 +1,4 @@
from random import random
import numpy as np
import torch
import time
@@ -10,7 +11,9 @@ from common.debug import debug
class MotionLive2Dataset(Dataset):
"""
Motion dataset.
Prepares conditioning information (previous poses + control signal) and the corresponding next poses"""
Prepares conditioning information (previous poses + control signal) and the corresponding next poses
+ shape
"""
def __init__(self, opt, phase="train"):
"""
@@ -110,7 +113,6 @@ class MotionLive2Dataset(Dataset):
data_shape = data_shape[beg_frame:end_frame, :] # (nsteps, 100)
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
data_motion = savgol_filter(data_motion, window_length=5, polyorder=3, axis=0, mode='nearest')
@@ -129,4 +131,45 @@ class MotionLive2Dataset(Dataset):
assert data_speech.shape[0] == self.nsteps_segmt, f"data_speech shape={data_speech.shape}, vid={vid}"
return result
return result
def make_auguments_reverse(self, data_motion, data_speech, p=0.5):
'''
parameters:
data_motion: (nsteps_motion, 53)
data_speech: (nsteps_speech, ), where nsteps_speech!= nsteps_motion
'''
if random.random() < p:
data_motion = data_motion[::-1]
data_speech = data_speech[::-1]
return data_motion, data_speech
def make_auguments_speed(self, data_motion, data_speech, p=0.5):
'''
parameters:
data_motion: (nsteps_motion, 53)
data_speech: (nsteps_speech, ), where nsteps_speech!= nsteps_motion
'''
if random.random() > p:
return data_motion, data_speech
nsteps_motion = data_motion.shape[0]
nsteps_speech = data_speech.shape[0]
speed = random.sample({0.5, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.5},1)[0]
if speed < 0:
data_motion = data_motion[:nsteps_motion*speed]
data_speech = data_speech[:nsteps_speech*speed]
return data_motion, data_speech
+160
View File
@@ -0,0 +1,160 @@
import numpy as np
import random
import time
from torch.utils.data import Dataset
from pathlib import Path
from scipy.signal import savgol_filter
from common.debug import debug
class MotionLive3Dataset(Dataset):
"""
Motion dataset.
Prepares conditioning information (previous poses + control signal) and the corresponding next poses
+ shape
+ selected idx
"""
def __init__(self, opt, phase="train"):
"""
phase = "train | valid | test "
"""
super(MotionLive3Dataset, self).__init__()
self.opt = opt
self.phase = phase
path_data= Path(opt.DATASET.root).joinpath(f"data_{phase}.npz")
# for test
# if phase == "valid":
# path_data= Path(opt.DATASET.root).joinpath(f"data_train.npz")
# if phase == "test":
# path_data= Path(opt.DATASET.root).joinpath(f"data_train.npz")
print(f"\n [{self.phase}] load data ... --> {path_data}")
# dict vid: {"data_motion", # [nframe, 53]
# "data_speech", # [duration * sample_rate, ] raw deepseep
# "nframe", "fps", "sr"}
self.annodb = np.load(path_data, allow_pickle=True)['data'].item()
self.num_pid = len(self.annodb)
self.pids = list(self.annodb.keys())
self.fps = opt.DATASET.fps # 50
self.sr = opt.DATASET.sr # 16000
self.nwind = eval(f"opt.DATASET.nwind_second_{phase}")
self.nsteps_frame = self.nwind * self.fps
self.nsteps_segmt = self.nwind * self.sr
print(f'''len(annodb)={len(self.annodb)}\t'''
f'''nsteps_frame={self.nsteps_frame}\t'''
f'''nsteps_segmt={self.nsteps_segmt} ''')
# path_motion = self.path_data.joinpath(f"motion_{phase}.npz")
# path_speech = self.path_data.joinpath(f"speech_{phase}.npz")
# print(f"load data ... --> {path_motion} | {path_speech}")
# self.data_motion = np.load(path_motion)['data'].astype(np.float32) # (N, nsteps, dim_motion)
# self.data_speech = np.load(path_speech)['data'].astype(np.float32) # (N, nsteps, dim_speech)
# print(f"motion data ={self.data_motion.shape}, output={self.data_speech.shape}")
# self.nc_motion = self.data_motion.shape[-1]
# self.nc_speech = self.data_speech.shape[-1]
# debug(opt, f"nc_motion={self.nc_motion}, nc_speech ={self.nc_speech }")
def __len__(self):
if self.phase == "train":
return 20000 #40000
elif self.phase == "valid":
return 2000 #5000
else:
return 100
def __getitem__(self, idx):
"""
Returns poses and conditioning.
If data-dropout sould be applied, a random selection of the previous poses is masked.
The control is not masked
return:
motion: (nsteps, nc_motion), current frame's pose (15 joint, xyz position)
speech: (nsteps, nc_speech), conditional data = previous motion data + full windows contrl data
"""
np.random.seed(int(idx + time.time()))
vid = self.pids[idx % self.num_pid]
# # filter "2021-P29"
# while vid == "2021-P29":
# vid = self.pids[idx % self.num_pid]
# random select idx
data = self.annodb[vid]
data_motion = data["data_motion"] # (nframes, nc_motion)
data_speech = data["data_speech"] # (nsegemts, )
# data_shape = data["data_shape"] # (nframes, nc_shape)\
set_idx = data["idx_sampling"] # (nframes_sampling)
nframe = data_motion.shape[0]
nsegmt = data_speech.shape[0]
# random select beg:end frame
if self.phase == "train":
beg_frame = random.sample(set_idx,1)[0]
if beg_frame + self.nsteps_frame > nframe: # resampling when last
beg_frame = nframe - self.nsteps_frame - 5
else:
beg_frame = np.random.randint(nframe - self.nsteps_frame - 10) + 5 # select from (5:-5)
end_frame = beg_frame + self.nsteps_frame
beg_segmt = int((1.0 * beg_frame / nframe) * nsegmt)
end_segmt = beg_segmt + self.nsteps_segmt
# print(f"nframe-{self.nsteps_frame:04d} =[{beg_frame:04d},{end_frame:04d})\t nsegmet-{self.nsteps_segmt:04d}=[{beg_segmt:04d},{end_segmt:04d}), vid={vid}")
data_motion = data_motion[beg_frame:end_frame, :] # (nteps, 53)
data_speech = data_speech[beg_segmt:end_segmt] # (nsegmt_steps, )
# data_shape = data_shape[beg_frame:end_frame, :] # (nsteps, 100)
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
data_motion = savgol_filter(data_motion, window_length=5, polyorder=3, axis=0, mode='nearest')
# data_shape = savgol_filter(data_shape, window_length=5, polyorder=3, axis=0, mode='nearest')
# random augments: reverse
if self.phase == "train" and self.opt.DATASET.flip:
data_motion, data_speech = self.make_auguments_flip(data_motion, data_speech, p=0.3)
# random augments: shuffle speed
result = {
"data": data_motion, # (nsteps_frame, dim_motion)
"cond": data_speech, # (nsteps_frame, dim_speech)
# "shape": data_shape,
"vid": vid,
"nframe": nframe,
"frame_interval": [beg_frame, end_frame],
# "duration_interval": [beg_frame/nframe, end_frame/nframe]
}
assert data_speech.shape[0] == self.nsteps_segmt, f"data_speech shape={data_speech.shape}, vid={vid}"
return result
def make_auguments_flip(self, data_motion, data_speech, p=0.5):
'''
parameters:
data_motion: (nsteps_motion, 53)
data_speech: (nsteps_speech, ), where nsteps_speech!= nsteps_motion
'''
if random.random() < p:
data_motion = data_motion[::-1].copy()
data_speech = data_speech[::-1].copy()
return data_motion, data_speech