Files
audio2motion/scripts/Obama/run-31-train-vaild-test-norm.py
2022-01-21 17:42:41 +08:00

136 lines
5.8 KiB
Python

import json
import numpy as np
from pathlib2 import Path
def slice_data(data, window_size, overlap):
nframes = data.shape[0]
overlap_frames = (int)(overlap*window_size)
n_sequences = (nframes-overlap_frames)//(window_size-overlap_frames)
sliced = np.zeros((n_sequences, window_size, data.shape[1])).astype(np.float32)
if n_sequences>0:
# extract sequences from the data
for i in range(0, n_sequences):
frameIdx = (window_size-overlap_frames) * i
sliced[i,:,:] = data[frameIdx:frameIdx+window_size,:].copy()
else:
print("WARNING: data too small for window")
return sliced
if __name__ == "__main__":
path_data = Path('/yrfs/lsen/DATA/Obama/processing') # '../data/trinity/source'
path_save = Path('data/obama-landmark-3-5-05') # processing
path_save.mkdir(parents=True, exist_ok=True)
v_dir_videos = sorted(path_data.glob("*/audio/feat_ldmk_trim_norm.npy"))
print(f"Have {len(v_dir_videos)} dir files")
v_dir_videos = [ipath.parent.parent for ipath in v_dir_videos]
vids = [ipath.stem for ipath in v_dir_videos]
nv_total = len(v_dir_videos)
nv_valid, nv_test = 2, 2
nv_train = nv_total - (nv_valid + nv_test)
vid_test = vids[:nv_test]
vid_valid = vids[nv_test:nv_test+nv_valid]
vid_train = vids[nv_test+nv_valid:]
print("vid_test = ", vid_test)
# set windows
nwin_train_secs = 3 # 6
nwin_test_secs = 5 # 20
window_overlap = 0.5 # 0.8
fps = 30
nwin_train = nwin_train_secs * fps # 6 * 20 = 120
nwin_test = nwin_test_secs * fps # 20 * 20 = 400
# for each video
motion_train, motion_valid, motion_test = [], [], []
speech_train, speech_valid, speech_test = [], [], []
for i, ipath_data in enumerate(v_dir_videos):
vid = ipath_data.stem
if i % 50 == 0:
print(f"\n processing the {i}-th video {vid} ... ")
ipath_info = ipath_data.joinpath("video/info.json")
ipath_feat_motion = ipath_data.joinpath("audio/feat_ldmk_trim_norm.npy") #
ipath_feat_speech = ipath_data.joinpath("audio/feat_mel_trim.npy")
# load video info
with open(str(ipath_info), "r") as f:
info = json.load(f)
width = info["width"][0] # 1280 or 1920
height = info["height"][0] # 720 or 1080
print("width=",width, "height=",height, "fps=",info["fps"], "-->", ipath_data)
data_motion = np.load(ipath_feat_motion).reshape(-1, 136) # (nframe, 68, 2)
data_speech = np.load(ipath_feat_speech) # (nframe, 27)
# data_motion = data_motion / np.array([[[width, height]]])
# data_motion = data_motion.reshape(-1, 136)
print(f"motion data = {data_motion.shape}, speech data = {data_speech.shape}")
assert data_motion.shape[0] == data_speech.shape[0]
# nfeat_motion = data_motion.shape[1]
if vid in vid_train:
motion_sliced = slice_data(data_motion, window_size=nwin_train, overlap=window_overlap)
speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=window_overlap)
motion_train.append(motion_sliced)
speech_train.append(speech_sliced)
elif vid in vid_valid:
motion_sliced = slice_data(data_motion, window_size=nwin_train, overlap=window_overlap)
speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=window_overlap)
motion_valid.append(motion_sliced)
speech_valid.append(speech_sliced)
else:
motion_sliced = slice_data(data_motion, window_size=nwin_test, overlap=0)
speech_sliced = slice_data(data_speech, window_size=nwin_test, overlap=0)
motion_test.append(motion_sliced)
speech_test.append(speech_sliced)
print(f"sliced motion data = {motion_sliced.shape}, speech data = {speech_sliced.shape}, minmax=({np.min(data_speech)},{np.max(data_speech)})")
# save
motion_train = np.concatenate(motion_train, axis=0) # (10612, 180, 136)
speech_train = np.concatenate(speech_train, axis=0) # (10612, 180, 27)
# _, motion_scaler = fit_and_standardize(motion_train)
# _, speech_scaler = fit_and_standardize(speech_train)
# print(motion_scaler.mean_ )
np.savez(path_save.joinpath("motion_train.npz"), data=motion_train)
np.savez(path_save.joinpath("speech_train.npz"), data=speech_train)
# jl.dump(motion_scaler, str(path_save.joinpath(f'motion_scaler.sav')))
# jl.dump(speech_scaler, str(path_save.joinpath(f'speech_scaler.sav')))
print(f"save train data motion({motion_train.shape}), speech({speech_train.shape}) -> {path_save}")
motion_valid = np.concatenate(motion_valid, axis=0) # (350, 180, 136)
speech_valid = np.concatenate(speech_valid, axis=0) # (350, 180, 27)
# motion_valid = standardize(motion_valid, motion_scaler)
# speech_valid = standardize(speech_valid, speech_scaler)
np.savez(path_save.joinpath("motion_valid.npz"), data=motion_valid)
np.savez(path_save.joinpath("speech_valid.npz"), data=speech_valid)
print(f"save valid data motion({motion_valid.shape}), speech({speech_valid.shape}) -> {path_save}")
motion_test = np.concatenate(motion_test, axis=0) # (41, 600, 136)
speech_test = np.concatenate(speech_test, axis=0) # (41, 600, 27)
# motion_test = standardize(motion_test, motion_scaler)
# speech_test = standardize(speech_test, speech_scaler)
np.savez(path_save.joinpath("motion_test.npz"), data=motion_test)
np.savez(path_save.joinpath("speech_test.npz"), data=speech_test)
print(f"save test data motion({motion_test.shape}), speech({speech_test.shape}) -> {path_save}")