initial commit

This commit is contained in:
Sen Liang committed 2022-01-21 17:42:41 +08:00
commit c7175dd7ff
127 files changed
+13949

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
import importlib
def create_dataset(opt, phase="train", logger=None):
dataset = find_dataset_by_name(opt.DATASET.name)
# instance
dataset = dataset(opt, phase)
print(f"dataset {type(dataset).__name__} was created!")
return dataset
def find_dataset_by_name(dataset_name):
"""
Given the option --model [dataset_name], the file "src/dataset/dataset_name.py" will be imported
"""
path_dataset = f"src.datasets.{dataset_name}"
# path_dataset = f"src.datasets.{dataset_name.lower()}"
modellib = importlib.import_module(path_dataset)
# in the file, the calss called DATASET() will be instantiated.
# it has to be a subclass of Dataset, and it is case-insensitive
dataset = None
target_name = dataset_name.replace('_', '') + 'Dataset'
# target_name = dataset_name.upper() + "Dataset"
for name, cls in modellib.__dict__.items():
if name.lower() == target_name.lower():
dataset = cls
if dataset is None:
print(f"In {dataset_name}.py, there should be a class with name {target_name} in lowercase")
exit(0)
return dataset
+67
View File
@@ -0,0 +1,67 @@
import numpy as np
import torch
from torch.utils.data import Dataset
from pathlib import Path
from scipy.signal import savgol_filter
from common.debug import debug
class MotionDataset(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(MotionDataset, self).__init__()
self.opt = opt
self.phase = phase
self.path_data = Path(opt.DATASET.root)
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}")
if self.opt.DEBUG:
self.data_motion = self.data_motion[:500, ...]
self.data_speech = self.data_speech[:500, ...]
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
self.data_motion = savgol_filter(self.data_motion, window_length=5, polyorder=3, axis=1, mode='nearest')
# self.data_speech = savgol_filter(self.data_speech, window_length=5, polyorder=3, axis=1, mode='nearest')
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 n_channels(self):
return self.nc_motion, self.nc_speech
def __len__(self):
return self.data_motion.shape[0]
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
"""
sample = {'data': self.data_motion[idx], # (nwind, nc_motion)
'cond': self.data_speech[idx] # (nwind, nc_speech)
}
return sample
+101
View File
@@ -0,0 +1,101 @@
import numpy as np
import torch
from torch.utils.data import Dataset
from pathlib import Path
from scipy.signal import savgol_filter
from common.debug import debug
class MotionRegressiveDataset(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(MotionRegressiveDataset, self).__init__()
self.opt = opt
self.phase = phase
self.path_data = Path(opt.DATASET.root)
self.nseqR = opt.MODEL.n_lookprevs # 5, previous sequence, number of autoregressive body poses
self.nseqL = opt.MODEL.n_lookahead # 20, future sequence
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}")
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[2]
self.nc_speech = self.data_speech.shape[2]
self.nc_condit = (self.nseqR + 1 + self.nseqL) * self.nc_speech
debug(opt, f"nc_motion={self.nc_motion}, nc_speech ={self.nc_speech } nc_condit={self.nc_condit}")
def n_channels(self):
return self.nc_motion, self.nc_condit
def __len__(self):
return self.data_motion.shape[0]
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:
data: ( 45, 95), current frame's pose (15 joint, xyz position)
cond: (927, 95), conditional data = previous motion data + full windows contrl data
"""
idata_motion = self.data_motion[idx, :, :] # (nwind, nc_motion)
idata_speech = self.data_speech[idx, :, :] # (nwind, nc_speech)
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
idata_motion = savgol_filter(idata_motion, window_length=5, polyorder=3, axis=0, mode='nearest')
idata_speech = savgol_filter(idata_speech, window_length=5, polyorder=3, axis=0, mode='nearest')
# convert to torch
idata_motion = torch.from_numpy(idata_motion) # (nwind, nc_motion)
idata_speech = torch.from_numpy(idata_speech) # (nwind, nc_speech)
# prepare data motion: nc_motion
# prepare data condition: nseqR * nc_motion + (nseqR + 1 + nseqL) * nc_speech
nwind = idata_motion.shape[0]
nwind_new = nwind - self.nseqR - self.nseqL
wsize_s = self.nseqR + 1 + self.nseqL
res_motion = idata_motion[self.nseqR:self.nseqR + nwind_new, :]
# unfold motion
res_condit_m = idata_motion.unfold(dimension=0, size=self.nseqR, step=1) # (nwind - self.nseqR +1, nc_motion, wsize)
res_condit_m = res_condit_m.swapaxes_(1,2) # (nwind_new, wsize, nc_speech)
res_condit_m = res_condit_m.reshape(nwind - self.nseqR +1, -1) # (nwind - self.nseqR +1, self.nseqR * self.nc_motion)
assert res_condit_m.shape[1] == self.nseqR * self.nc_motion
# unfold speech
res_condit_s = idata_speech.unfold(dimension=0, size=wsize_s, step=1) # (nwind_new, nc_speech, wsize)
res_condit_s = res_condit_s.swapaxes_(1,2) # (nwind_new, wsize, nc_speech)
res_condit_s = res_condit_s.reshape(nwind_new, -1) # (nwind_new, wsize * nc_speech)
assert res_condit_s.shape[1] == wsize_s * self.nc_speech
# concate
res_condit = torch.concat([res_condit_s, res_condit_m], dim=1)
assert res_condit.shape[1] == self.nc_condit
sample = {'data': res_motion, # (nwind, nc_motion)
'cond': res_condit # (nwind, nc_condit)
}
return sample
+86
View File
@@ -0,0 +1,86 @@
import numpy as np
import torch
from torch.utils.data import Dataset
from pathlib import Path
from scipy.signal import savgol_filter
from common.debug import debug
class MotionWindowDataset(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(MotionWindowDataset, self).__init__()
self.opt = opt
self.phase = phase
self.path_data = Path(opt.DATASET.root)
self.nseqR = opt.MODEL.n_lookprevs_speech # 5, previous sequence, number of autoregressive body poses
self.nseqL = opt.MODEL.n_lookahead_speech # 20, future sequence
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}")
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 shape ={self.data_motion.shape}, speech shape ={self.data_speech.shape}")
if self.opt.DEBUG:
self.data_motion = self.data_motion[:500, ...]
self.data_speech = self.data_speech[:500, ...]
# filter, explore doc: ./notebook/explore_pose.ipynb
if self.opt.DATASET.filter == "savgol":
self.data_motion = savgol_filter(self.data_motion, window_length=5, polyorder=3, axis=1, mode='nearest')
# self.data_speech = savgol_filter(self.data_speech, window_length=5, polyorder=3, axis=1, mode='nearest')
self.nc_motion = self.data_motion.shape[2]
self.nc_speech = self.data_speech.shape[2]
self.nc_condit = (self.nseqR + 1 + self.nseqL) * self.nc_speech
debug(opt, f"nc_motion={self.nc_motion}, nc_speech ={self.nc_speech } nc_condit={self.nc_condit}")
def n_channels(self):
return self.nc_motion, self.nc_condit
def __len__(self):
return self.data_motion.shape[0]
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:
data: ( 45, 95), current frame's pose (15 joint, xyz position)
cond: (927, 95), conditional data = previous motion data + full windows contrl data
"""
idata_motion = torch.from_numpy(self.data_motion[idx, :, :]) # (nstep, nc_motion)
idata_speech = torch.from_numpy(self.data_speech[idx, :, :]) # (nstep, nc_speech)
# prepare data motion: nc_motion
# prepare data condition: nc_condit = (nseqR + 1 + nseqL) * nc_speech
nstep = idata_motion.shape[0]
nstep_new = nstep - self.nseqR - self.nseqL
wsize = self.nseqR + 1 + self.nseqL
res_motion = idata_motion[self.nseqR:self.nseqR + nstep_new, :]
res_condit = idata_speech.unfold(dimension=0, size=wsize, step=1) # (nstep_new, nc_speech, wsize)
res_condit = res_condit.swapaxes_(1,2) # (nstep_new, wsize, nc_speech)
res_condit = res_condit.reshape(nstep_new, self.nc_condit)
sample = {'data': res_motion, # (nstep_new, nc_motion)
'cond': res_condit # (nstep_new, nc_condit)
}
return sample