Update data.py
Browse files
data.py
CHANGED
|
@@ -1,227 +1,226 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from torch.utils.data import Dataset, DataLoader, random_split
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import numpy as np
|
| 6 |
-
from transformers import BertTokenizer
|
| 7 |
-
import os
|
| 8 |
-
from pose_format import Pose
|
| 9 |
-
import matplotlib.pyplot as plt
|
| 10 |
-
from matplotlib import animation
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
0.
|
| 30 |
-
0.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
joint_weights
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
current_frames
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
#
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
np.save(
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
self.
|
| 135 |
-
self.
|
| 136 |
-
self.
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
current_frames
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
input_ids.
|
| 212 |
-
|
| 213 |
-
torch.tensor(
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
torch.stack(
|
| 223 |
-
torch.stack(
|
| 224 |
-
torch.stack(
|
| 225 |
-
|
| 226 |
-
list(words)
|
| 227 |
)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torch.utils.data import Dataset, DataLoader, random_split
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from transformers import BertTokenizer
|
| 7 |
+
import os
|
| 8 |
+
from pose_format import Pose
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from matplotlib import animation
|
| 11 |
+
from fastdtw import fastdtw # Keep this import
|
| 12 |
+
from scipy.spatial.distance import cosine
|
| 13 |
+
from config import MAX_TEXT_LEN, TARGET_NUM_FRAMES, BATCH_SIZE, TEACHER_FORCING_RATIO, SMOOTHING_ENABLED
|
| 14 |
+
|
| 15 |
+
# ===== KEYPOINT SELECTION =====
|
| 16 |
+
selected_keypoint_indices = list(np.r_[0:25, 501:522, 522:543])
|
| 17 |
+
NUM_KEYPOINTS = len(selected_keypoint_indices)
|
| 18 |
+
POSE_DIM = NUM_KEYPOINTS * 3
|
| 19 |
+
|
| 20 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
|
| 22 |
+
# ===== SMOOTHING =====
|
| 23 |
+
def selective_smoothing(preds):
|
| 24 |
+
smoothed = preds.clone()
|
| 25 |
+
body_indices = slice(0, 25 * 3)
|
| 26 |
+
for t in range(1, preds.shape[1] - 1):
|
| 27 |
+
smoothed[:, t, body_indices] = (
|
| 28 |
+
0.25 * preds[:, t - 1, body_indices] +
|
| 29 |
+
0.5 * preds[:, t, body_indices] +
|
| 30 |
+
0.25 * preds[:, t + 1, body_indices]
|
| 31 |
+
)
|
| 32 |
+
return smoothed
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# ===== HAND INDEX SETUP =====
|
| 36 |
+
hand_indices = list(range(15 * 3, POSE_DIM)) # hand joints = after body joints in flattened 3D vector
|
| 37 |
+
joint_weights = torch.ones(POSE_DIM).to(device)
|
| 38 |
+
joint_weights[hand_indices] *= 3.0
|
| 39 |
+
|
| 40 |
+
# ===== GLOBAL NORMALIZATION =====
|
| 41 |
+
def compute_global_mean_std(pose_folder, csv_file):
|
| 42 |
+
data = pd.read_csv(csv_file)
|
| 43 |
+
all_poses = []
|
| 44 |
+
# Store valid masks separately to ensure correct normalization
|
| 45 |
+
all_masks = []
|
| 46 |
+
|
| 47 |
+
for filename in data["filename"]:
|
| 48 |
+
pose_path = os.path.join(pose_folder, filename)
|
| 49 |
+
with open(pose_path, "rb") as f:
|
| 50 |
+
pose = Pose.read(f.read())
|
| 51 |
+
|
| 52 |
+
keypoints = np.array(selected_keypoint_indices)
|
| 53 |
+
# (T, 1, K, 3) -> (T, K, 3)
|
| 54 |
+
pose_data = np.squeeze(pose.body.data, axis=1)[:, keypoints, :]
|
| 55 |
+
# (T, 1, K) -> (T, K)
|
| 56 |
+
confidence = np.squeeze(pose.body.confidence, axis=1)[:, keypoints]
|
| 57 |
+
|
| 58 |
+
# Reshape to (T, K*3)
|
| 59 |
+
pose_data_flat = pose_data.reshape(pose_data.shape[0], -1)
|
| 60 |
+
# Reshape confidence to (T, K*3) - repeat confidence for each coordinate
|
| 61 |
+
confidence_flat = np.repeat(confidence, 3, axis=1)
|
| 62 |
+
|
| 63 |
+
# Create a mask based on confidence for the flattened data
|
| 64 |
+
mask_flat = (confidence_flat > 0.5).astype(np.float32)
|
| 65 |
+
|
| 66 |
+
# Append the full pose data and mask for interpolation later
|
| 67 |
+
all_poses.append(pose_data_flat)
|
| 68 |
+
all_masks.append(mask_flat)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# Pad or interpolate all poses and masks to a fixed length (TARGET_NUM_FRAMES)
|
| 72 |
+
padded_poses = []
|
| 73 |
+
padded_masks = []
|
| 74 |
+
for pose_data_flat, mask_flat in zip(all_poses, all_masks):
|
| 75 |
+
current_frames = pose_data_flat.shape[0]
|
| 76 |
+
if current_frames < TARGET_NUM_FRAMES:
|
| 77 |
+
pad_len = TARGET_NUM_FRAMES - current_frames
|
| 78 |
+
pose_pad = np.zeros((pad_len, POSE_DIM))
|
| 79 |
+
mask_pad = np.zeros((pad_len, POSE_DIM)) # Pad mask with zeros
|
| 80 |
+
padded_pose = np.concatenate([pose_data_flat, pose_pad], axis=0)
|
| 81 |
+
padded_mask = np.concatenate([mask_flat, mask_pad], axis=0)
|
| 82 |
+
else:
|
| 83 |
+
indices = np.linspace(0, current_frames - 1, TARGET_NUM_FRAMES).astype(int)
|
| 84 |
+
padded_pose = pose_data_flat[indices]
|
| 85 |
+
padded_mask = mask_flat[indices]
|
| 86 |
+
|
| 87 |
+
padded_poses.append(padded_pose)
|
| 88 |
+
padded_masks.append(padded_mask)
|
| 89 |
+
|
| 90 |
+
# Stack all padded poses and masks: (Total_Samples * TARGET_NUM_FRAMES, POSE_DIM)
|
| 91 |
+
stacked_poses = np.vstack(padded_poses)
|
| 92 |
+
stacked_masks = np.vstack(padded_masks)
|
| 93 |
+
|
| 94 |
+
# Compute mean and std using the masks to only include valid points
|
| 95 |
+
# Weighted average using mask as weights
|
| 96 |
+
mean = np.sum(stacked_poses * stacked_masks, axis=0) / (np.sum(stacked_masks, axis=0) + 1e-8) # Add epsilon for stability
|
| 97 |
+
# Compute variance, then sqrt for std
|
| 98 |
+
variance = np.sum(stacked_masks * (stacked_poses - mean)**2, axis=0) / (np.sum(stacked_masks, axis=0) + 1e-8)
|
| 99 |
+
std = np.sqrt(variance)
|
| 100 |
+
|
| 101 |
+
std[std == 0] = 1e-8 # Avoid division by zero
|
| 102 |
+
return mean, std
|
| 103 |
+
|
| 104 |
+
#POSE_FOLDER = "/content/drive/MyDrive/pose/words/ase"
|
| 105 |
+
CSV_FILE = "annotated.csv"
|
| 106 |
+
mean_path = "global_mean.npy"
|
| 107 |
+
std_path = "global_std.npy"
|
| 108 |
+
|
| 109 |
+
if os.path.exists(mean_path) and os.path.exists(std_path):
|
| 110 |
+
print("Loading global mean and std from file.")
|
| 111 |
+
GLOBAL_MEAN = np.load(mean_path)
|
| 112 |
+
GLOBAL_STD = np.load(std_path)
|
| 113 |
+
else:
|
| 114 |
+
print("Computing global mean and std from dataset.")
|
| 115 |
+
GLOBAL_MEAN, GLOBAL_STD = compute_global_mean_std(POSE_FOLDER, CSV_FILE)
|
| 116 |
+
# Save the computed mean and std
|
| 117 |
+
# Ensure they are not MaskedArrays if the computation somehow produced them
|
| 118 |
+
# If compute_global_mean_std is modified to return standard arrays, this is redundant but safe
|
| 119 |
+
if isinstance(GLOBAL_MEAN, np.ma.MaskedArray):
|
| 120 |
+
GLOBAL_MEAN = GLOBAL_MEAN.data
|
| 121 |
+
if isinstance(GLOBAL_STD, np.ma.MaskedArray):
|
| 122 |
+
GLOBAL_STD = GLOBAL_STD.data
|
| 123 |
+
|
| 124 |
+
np.save(mean_path, GLOBAL_MEAN)
|
| 125 |
+
np.save(std_path, GLOBAL_STD)
|
| 126 |
+
|
| 127 |
+
GLOBAL_MEAN_T = torch.tensor(GLOBAL_MEAN).float().to(device)
|
| 128 |
+
GLOBAL_STD_T = torch.tensor(GLOBAL_STD).float().to(device)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class TextToPoseDataset(Dataset):
|
| 132 |
+
def __init__(self, csv_file, pose_folder, tokenizer, is_train=True):
|
| 133 |
+
self.data = pd.read_csv(csv_file)
|
| 134 |
+
self.pose_folder = pose_folder
|
| 135 |
+
self.tokenizer = tokenizer
|
| 136 |
+
self.is_train = is_train # enable augment only during training
|
| 137 |
+
|
| 138 |
+
def __len__(self):
|
| 139 |
+
return len(self.data)
|
| 140 |
+
|
| 141 |
+
def load_pose_data_and_mask(self, filename):
|
| 142 |
+
pose_path = os.path.join(self.pose_folder, filename)
|
| 143 |
+
with open(pose_path, "rb") as f:
|
| 144 |
+
pose = Pose.read(f.read())
|
| 145 |
+
|
| 146 |
+
keypoints = np.array(selected_keypoint_indices)
|
| 147 |
+
pose_data = np.squeeze(pose.body.data, axis=1)[:, keypoints, :]
|
| 148 |
+
confidence = np.squeeze(pose.body.confidence, axis=1)[:, keypoints]
|
| 149 |
+
|
| 150 |
+
return pose_data, confidence
|
| 151 |
+
|
| 152 |
+
def apply_augmentations(self, pose_data, confidence):
|
| 153 |
+
T = pose_data.shape[0]
|
| 154 |
+
|
| 155 |
+
# Temporal warp (resample frame indices with small noise)
|
| 156 |
+
if T > TARGET_NUM_FRAMES and np.random.rand() < 0.5:
|
| 157 |
+
indices = np.linspace(0, T - 1, TARGET_NUM_FRAMES)
|
| 158 |
+
jitter = np.random.uniform(-0.5, 0.5, size=indices.shape)
|
| 159 |
+
indices = np.clip(indices + jitter, 0, T - 1).astype(int)
|
| 160 |
+
pose_data = pose_data[indices]
|
| 161 |
+
confidence = confidence[indices]
|
| 162 |
+
|
| 163 |
+
# Mirror (flip X-axis)
|
| 164 |
+
if np.random.rand() < 0.3:
|
| 165 |
+
pose_data[..., 0] *= -1
|
| 166 |
+
|
| 167 |
+
# Jitter (small Gaussian noise)
|
| 168 |
+
if np.random.rand() < 0.3:
|
| 169 |
+
pose_data += np.random.normal(0, 0.02, pose_data.shape)
|
| 170 |
+
|
| 171 |
+
return pose_data, confidence
|
| 172 |
+
|
| 173 |
+
def __getitem__(self, idx):
|
| 174 |
+
row = self.data.iloc[idx]
|
| 175 |
+
filename = row["filename"]
|
| 176 |
+
text = row["text"]
|
| 177 |
+
|
| 178 |
+
input_ids = self.tokenizer(
|
| 179 |
+
text, padding="max_length", truncation=True,
|
| 180 |
+
max_length=MAX_TEXT_LEN, return_tensors="pt"
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
pose_data, confidence = self.load_pose_data_and_mask(filename)
|
| 184 |
+
|
| 185 |
+
if self.is_train:
|
| 186 |
+
pose_data, confidence = self.apply_augmentations(pose_data, confidence)
|
| 187 |
+
|
| 188 |
+
# OLD Flatten
|
| 189 |
+
pose_data_flat = pose_data.reshape(pose_data.shape[0], -1)
|
| 190 |
+
confidence_flat = np.repeat(confidence, 3, axis=1)
|
| 191 |
+
mask_flat = (confidence_flat > 0.5).astype(np.float32)
|
| 192 |
+
|
| 193 |
+
# Pad or warp to fixed length
|
| 194 |
+
current_frames = pose_data_flat.shape[0]
|
| 195 |
+
if current_frames < TARGET_NUM_FRAMES:
|
| 196 |
+
pad_len = TARGET_NUM_FRAMES - current_frames
|
| 197 |
+
pose_pad = np.zeros((pad_len, POSE_DIM))
|
| 198 |
+
mask_pad = np.zeros((pad_len, POSE_DIM))
|
| 199 |
+
padded_pose = np.concatenate([pose_data_flat, pose_pad], axis=0)
|
| 200 |
+
padded_mask = np.concatenate([mask_flat, mask_pad], axis=0)
|
| 201 |
+
else:
|
| 202 |
+
indices = np.linspace(0, current_frames - 1, TARGET_NUM_FRAMES).astype(int)
|
| 203 |
+
padded_pose = pose_data_flat[indices]
|
| 204 |
+
padded_mask = mask_flat[indices]
|
| 205 |
+
|
| 206 |
+
# Normalize
|
| 207 |
+
normalized_pose = (padded_pose - GLOBAL_MEAN) / GLOBAL_STD
|
| 208 |
+
|
| 209 |
+
return (
|
| 210 |
+
input_ids.input_ids.squeeze(0),
|
| 211 |
+
input_ids.attention_mask.squeeze(0),
|
| 212 |
+
torch.tensor(normalized_pose).float(),
|
| 213 |
+
torch.tensor(padded_mask).float(),
|
| 214 |
+
text
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def collate_fn(batch):
|
| 219 |
+
input_ids, attn_masks, poses, masks, words = zip(*batch)
|
| 220 |
+
return (
|
| 221 |
+
torch.stack(input_ids),
|
| 222 |
+
torch.stack(attn_masks),
|
| 223 |
+
torch.stack(poses),
|
| 224 |
+
torch.stack(masks),
|
| 225 |
+
list(words)
|
|
|
|
| 226 |
)
|