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