import numpy as np from torch.utils.data import Dataset from pathlib import Path from scipy.signal import savgol_filter class MotionEvalDataset(Dataset): """ Motion dataset. Prepares conditioning information (previous poses + control signal) and the corresponding next poses""" def __init__(self, opt, phase="train"): super(MotionEvalDataset, self).__init__() self.opt = opt self.phase = phase path_data= Path(opt.DATASET.root).joinpath(f"data_{phase}.npz") print(f"[{self.phase}] load data ... --> {path_data}") self.annodb = np.load(path_data, allow_pickle=True)['data'].item() self.num_pid = len(self.annodb) self.pids = list(self.annodb.keys()) self.overlap = 0 if self.phase == "train" else 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 self.annodb_accums = self.count_annotations() print(f'''num_pid={self.num_pid}, niters={self.annodb_accums[-1]}, nsteps_frame={self.nsteps_frame}, 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, ikey in enumerate(self.annodb): acc += self.annodb[ikey]["data_motion"].shape[0] // (self.nsteps_frame * (1-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}") assert data_speech.shape[0] == self.nsteps_segmt, f"data_speech shape={data_speech.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') 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