| 14 | |
| 15 | |
| 16 | class DataPreprocessor: |
| 17 | def __init__(self, clip_lmdb_dir, out_lmdb_dir, n_poses, subdivision_stride, |
| 18 | pose_resampling_fps, mean_pose, mean_dir_vec, disable_filtering=False): |
| 19 | self.n_poses = n_poses |
| 20 | self.subdivision_stride = subdivision_stride |
| 21 | self.skeleton_resampling_fps = pose_resampling_fps |
| 22 | self.mean_pose = mean_pose |
| 23 | self.mean_dir_vec = mean_dir_vec |
| 24 | self.disable_filtering = disable_filtering |
| 25 | |
| 26 | self.src_lmdb_env = lmdb.open(clip_lmdb_dir, readonly=True, lock=False) |
| 27 | with self.src_lmdb_env.begin() as txn: |
| 28 | self.n_videos = txn.stat()['entries'] |
| 29 | |
| 30 | self.spectrogram_sample_length = utils.data_utils.calc_spectrogram_length_from_motion_length(self.n_poses, self.skeleton_resampling_fps) |
| 31 | self.audio_sample_length = int(self.n_poses / self.skeleton_resampling_fps * 16000) |
| 32 | |
| 33 | # create db for samples |
| 34 | map_size = 1024 * 50 # in MB |
| 35 | map_size <<= 20 # in B |
| 36 | self.dst_lmdb_env = lmdb.open(out_lmdb_dir, map_size=map_size) |
| 37 | self.n_out_samples = 0 |
| 38 | |
| 39 | def run(self): |
| 40 | n_filtered_out = defaultdict(int) |
| 41 | src_txn = self.src_lmdb_env.begin(write=False) |
| 42 | |
| 43 | # sampling and normalization |
| 44 | cursor = src_txn.cursor() |
| 45 | for key, value in cursor: |
| 46 | video = pyarrow.deserialize(value) |
| 47 | vid = video['vid'] |
| 48 | clips = video['clips'] |
| 49 | for clip_idx, clip in enumerate(clips): |
| 50 | filtered_result = self._sample_from_clip(vid, clip) |
| 51 | for type in filtered_result.keys(): |
| 52 | n_filtered_out[type] += filtered_result[type] |
| 53 | |
| 54 | # print stats |
| 55 | with self.dst_lmdb_env.begin() as txn: |
| 56 | print('no. of samples: ', txn.stat()['entries']) |
| 57 | n_total_filtered = 0 |
| 58 | for type, n_filtered in n_filtered_out.items(): |
| 59 | print('{}: {}'.format(type, n_filtered)) |
| 60 | n_total_filtered += n_filtered |
| 61 | print('no. of excluded samples: {} ({:.1f}%)'.format( |
| 62 | n_total_filtered, 100 * n_total_filtered / (txn.stat()['entries'] + n_total_filtered))) |
| 63 | |
| 64 | # close db |
| 65 | self.src_lmdb_env.close() |
| 66 | self.dst_lmdb_env.sync() |
| 67 | self.dst_lmdb_env.close() |
| 68 | |
| 69 | def _sample_from_clip(self, vid, clip): |
| 70 | clip_skeleton = clip['skeletons_3d'] |
| 71 | clip_audio = clip['audio_feat'] |
| 72 | clip_audio_raw = clip['audio_raw'] |
| 73 | clip_word_list = clip['words'] |