chatgpt lock tf in

This commit is contained in:
2026-01-10 23:38:17 -06:00
parent 1922898517
commit ab907280dd

View File

@@ -10,34 +10,31 @@ import torch.optim as optim
from torch.utils.data import Dataset, DataLoader from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler from sklearn.preprocessing import LabelEncoder
from multiprocessing import Pool, cpu_count from multiprocessing import Pool, cpu_count
from functools import partial from functools import partial
from tqdm import tqdm from tqdm import tqdm
from collections import Counter from collections import Counter
# ===============================
# DATA LOADING
# ===============================
def load_kaggle_asl_data(base_path): def load_kaggle_asl_data(base_path):
train_df = pd.read_csv(os.path.join(base_path, "train.csv")) train_df = pd.read_csv(os.path.join(base_path, "train.csv"))
with open(os.path.join(base_path, "sign_to_prediction_index_map.json")) as f: with open(os.path.join(base_path, "sign_to_prediction_index_map.json")) as f:
sign_to_idx = json.load(f) sign_to_idx = json.load(f)
return train_df, sign_to_idx return train_df, sign_to_idx
def extract_hand_landmarks_from_parquet(path): def extract_hand_landmarks_from_parquet(path):
try: try:
df = pd.read_parquet(path) df = pd.read_parquet(path)
left = df[df["type"] == "left_hand"] left = df[df["type"] == "left_hand"]
right = df[df["type"] == "right_hand"] right = df[df["type"] == "right_hand"]
hand = left if len(left) >= len(right) else right hand = left if len(left) >= len(right) else right
if len(hand) == 0: if len(hand) == 0:
return None return None
frames = sorted(hand['frame'].unique()) frames = sorted(hand['frame'].unique())
landmarks_seq = [] landmarks_seq = []
for frame in frames: for frame in frames:
lm_frame = hand[hand['frame'] == frame] lm_frame = hand[hand['frame'] == frame]
lm_list = [] lm_list = []
@@ -52,12 +49,10 @@ def extract_hand_landmarks_from_parquet(path):
float(lm['z'].iloc[0]) float(lm['z'].iloc[0])
]) ])
landmarks_seq.append(lm_list) landmarks_seq.append(lm_list)
return np.array(landmarks_seq, dtype=np.float32) return np.array(landmarks_seq, dtype=np.float32)
except: except:
return None return None
def get_features_sequence(landmarks_seq, max_frames=100): def get_features_sequence(landmarks_seq, max_frames=100):
if landmarks_seq is None or len(landmarks_seq) == 0: if landmarks_seq is None or len(landmarks_seq) == 0:
return None return None
@@ -65,45 +60,51 @@ def get_features_sequence(landmarks_seq, max_frames=100):
# Center on wrist # Center on wrist
landmarks_seq -= landmarks_seq[:, 0:1, :] landmarks_seq -= landmarks_seq[:, 0:1, :]
# Scale using index → middle finger tip distance (more stable than single point) # Robust scale: wrist → middle finger MCP
scale = np.linalg.norm(landmarks_seq[:, 8] - landmarks_seq[:, 12], axis=1, keepdims=True) scale = np.linalg.norm(landmarks_seq[:,0] - landmarks_seq[:,9], axis=1, keepdims=True)
scale = np.maximum(scale, 1e-6) scale = np.maximum(scale, 1e-6)
landmarks_seq /= scale landmarks_seq /= scale[:, np.newaxis, :]
# Flatten # Flatten
seq = landmarks_seq.reshape(landmarks_seq.shape[0], -1) seq = landmarks_seq.reshape(landmarks_seq.shape[0], -1)
# Pad / truncate # Pad / truncate
if len(seq) < max_frames: T = seq.shape[0]
pad = np.zeros((max_frames - len(seq), seq.shape[1]), dtype=np.float32) if T < max_frames:
pad = np.zeros((max_frames - T, seq.shape[1]), dtype=np.float32)
seq = np.concatenate([seq, pad]) seq = np.concatenate([seq, pad])
else: else:
seq = seq[:max_frames] seq = seq[:max_frames]
return seq # Mask for valid frames
valid_mask = np.zeros(max_frames, dtype=bool)
valid_mask[:min(T, max_frames)] = True
return seq, valid_mask
def process_row(row, base_path, max_frames=100): def process_row(row, base_path, max_frames=100):
path = os.path.join(base_path, row["path"]) path = os.path.join(base_path, row["path"])
if not os.path.exists(path): if not os.path.exists(path):
return None, None return None, None, None
try: try:
lm = extract_hand_landmarks_from_parquet(path) lm = extract_hand_landmarks_from_parquet(path)
if lm is None: if lm is None:
return None, None return None, None, None
feat = get_features_sequence(lm, max_frames) feat, mask = get_features_sequence(lm, max_frames)
if feat is None: if feat is None:
return None, None return None, None, None
return feat, row["sign"] return feat, mask, row["sign"]
except: except:
return None, None return None, None, None
# ===============================
# TRANSFORMER MODEL
# ===============================
class PositionalEncoding(nn.Module): class PositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=128): def __init__(self, d_model, max_len=128):
super().__init__() super().__init__()
pe = torch.zeros(max_len, d_model) pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) position = torch.arange(0, max_len, dtype=torch.float32).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)) div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term) pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term) pe[:, 1::2] = torch.cos(position * div_term)
@@ -112,28 +113,25 @@ class PositionalEncoding(nn.Module):
def forward(self, x): def forward(self, x):
return x + self.pe[:, :x.size(1)] return x + self.pe[:, :x.size(1)]
class TransformerASL(nn.Module): class TransformerASL(nn.Module):
def __init__(self, input_dim=63, num_classes=250, d_model=192, nhead=6, num_layers=4): def __init__(self, input_dim=63, num_classes=250, d_model=128, nhead=4, num_layers=2):
super().__init__() super().__init__()
self.proj = nn.Linear(input_dim, d_model) self.proj = nn.Linear(input_dim, d_model)
self.norm_in = nn.LayerNorm(d_model) self.norm_in = nn.LayerNorm(d_model)
self.pos = PositionalEncoding(d_model) self.pos = PositionalEncoding(d_model)
enc_layer = nn.TransformerEncoderLayer( enc_layer = nn.TransformerEncoderLayer(
d_model=d_model, d_model=d_model,
nhead=nhead, nhead=nhead,
dim_feedforward=d_model * 4, dim_feedforward=d_model*4,
dropout=0.15, dropout=0.1,
activation='gelu', activation='gelu',
batch_first=True, batch_first=True,
norm_first=True norm_first=True
) )
self.encoder = nn.TransformerEncoder(enc_layer, num_layers=num_layers) self.encoder = nn.TransformerEncoder(enc_layer, num_layers=num_layers)
self.head = nn.Sequential( self.head = nn.Sequential(
nn.LayerNorm(d_model), nn.LayerNorm(d_model),
nn.Dropout(0.25), nn.Dropout(0.2),
nn.Linear(d_model, num_classes) nn.Linear(d_model, num_classes)
) )
@@ -142,209 +140,144 @@ class TransformerASL(nn.Module):
x = self.norm_in(x) x = self.norm_in(x)
x = self.pos(x) x = self.pos(x)
x = self.encoder(x, src_key_padding_mask=key_padding_mask) x = self.encoder(x, src_key_padding_mask=key_padding_mask)
x = x.mean(dim=1) # global average pooling x = x.mean(dim=1)
return self.head(x) return self.head(x)
def create_padding_mask(valid_masks):
# valid_masks: (B,T) bool, True for valid
return ~valid_masks # True in mask = positions to ignore
def create_padding_mask(lengths, max_len): # ===============================
return torch.arange(max_len, device=lengths.device)[None, :] >= lengths[:, None] # MAIN
# ===============================
def main(): def main():
# ===============================
# DEVICE
# ===============================
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}") print(f"Using device: {device}")
if device.type == "cuda": if device.type == "cuda":
print("GPU:", torch.cuda.get_device_name(0)) print("GPU:", torch.cuda.get_device_name(0))
# =============================== base_path = "asl_kaggle"
# CONFIG
# ===============================
base_path = "asl_kaggle" # ← CHANGE THIS TO YOUR ACTUAL PATH
max_frames = 100 max_frames = 100
MIN_SAMPLES_PER_CLASS = 6 # ← important! prevents stratified split crash MIN_SAMPLES_PER_CLASS = 6
# =============================== # --- LOAD DATA ---
# DATA LOADING & PROCESSING
# ===============================
print("Loading metadata...")
train_df, sign_to_idx = load_kaggle_asl_data(base_path) train_df, sign_to_idx = load_kaggle_asl_data(base_path)
print(f"Processing {len(train_df)} videos...")
rows = [row for _, row in train_df.iterrows()] rows = [row for _, row in train_df.iterrows()]
with Pool(cpu_count()) as pool: with Pool(cpu_count()) as pool:
results = list(tqdm( results = list(tqdm(
pool.imap( pool.imap(partial(process_row, base_path=base_path, max_frames=max_frames), rows),
partial(process_row, base_path=base_path, max_frames=max_frames),
rows
),
total=len(rows), total=len(rows),
desc="Extracting landmarks" desc="Processing"
)) ))
X_list, y_list = [], [] X_list, mask_list, y_list = [], [], []
for feat, sign in results: for feat, mask, sign in results:
if feat is not None: if feat is not None:
X_list.append(feat) X_list.append(feat)
mask_list.append(mask)
y_list.append(sign) y_list.append(sign)
if not X_list: if not X_list:
print("No valid sequences found. Check parquet files / paths.") print("No valid sequences found.")
return return
X = np.stack(X_list) X = np.stack(X_list)
print(f"Loaded {len(X)} valid sequences | shape: {X.shape}") masks = np.stack(mask_list)
print(f"Loaded {len(X)} sequences | shape: {X.shape}")
# Global normalization (very important for stability) # --- NORMALIZE only valid frames ---
print("Before global norm → mean:", X.mean(), "std:", X.std()) for i in range(X.shape[0]):
X = np.clip(X, -5.0, 5.0) valid_idx = masks[i]
mean = X.mean(axis=(0, 1), keepdims=True) X[i, valid_idx] = (X[i, valid_idx] - X[i, valid_idx].mean(0)) / (X[i, valid_idx].std(0) + 1e-8)
std = X.std(axis=(0, 1), keepdims=True) + 1e-8
X = (X - mean) / std
print("After global norm → mean:", X.mean(), "std:", X.std())
# =============================== # --- LABELS ---
# LABELS
# ===============================
le = LabelEncoder() le = LabelEncoder()
y = le.fit_transform(y_list) y = le.fit_transform(y_list)
# Remove classes with too few samples (prevents stratify error) # Remove rare classes
counts = Counter(y) counts = Counter(y)
valid_classes = [cls for cls, cnt in counts.items() if cnt >= MIN_SAMPLES_PER_CLASS] valid_classes = [cls for cls, cnt in counts.items() if cnt >= MIN_SAMPLES_PER_CLASS]
mask_keep = np.isin(y, valid_classes)
mask = np.isin(y, valid_classes) X, masks, y = X[mask_keep], masks[mask_keep], y[mask_keep]
X = X[mask]
y = y[mask]
# Re-encode labels consecutively (0,1,2,... no gaps)
le = LabelEncoder() le = LabelEncoder()
y = le.fit_transform(y) y = le.fit_transform(y)
print(f"{len(X)} samples remain | {len(le.classes_)} classes")
print(f"After filtering: {len(X)} samples remain | {len(le.classes_)} classes") # --- SPLIT ---
X_train, X_test, masks_train, masks_test, y_train, y_test = train_test_split(
# =============================== X, masks, y, test_size=0.15, stratify=y, random_state=42
# SPLIT
# ===============================
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.15,
stratify=y, # should be safe now
random_state=42
) )
# =============================== # --- DATASETS ---
# DATASET & LOADERS
# ===============================
class ASLSequenceDataset(Dataset): class ASLSequenceDataset(Dataset):
def __init__(self, X, y): def __init__(self, X, masks, y):
self.X = torch.from_numpy(X).float() self.X = torch.from_numpy(X).float()
self.masks = torch.from_numpy(masks)
self.y = torch.from_numpy(y).long() self.y = torch.from_numpy(y).long()
def __len__(self): def __len__(self):
return len(self.X) return len(self.X)
def __getitem__(self, idx): def __getitem__(self, idx):
return self.X[idx], self.y[idx] return self.X[idx], self.masks[idx], self.y[idx]
train_loader = DataLoader( train_loader = DataLoader(ASLSequenceDataset(X_train, masks_train, y_train),
ASLSequenceDataset(X_train, y_train), batch_size=64, shuffle=True, num_workers=4, pin_memory=True)
batch_size=64, test_loader = DataLoader(ASLSequenceDataset(X_test, masks_test, y_test),
shuffle=True, batch_size=96, shuffle=False, num_workers=4, pin_memory=True)
num_workers=4,
pin_memory=True
)
test_loader = DataLoader( # --- MODEL ---
ASLSequenceDataset(X_test, y_test), model = TransformerASL(input_dim=X.shape[2], num_classes=len(le.classes_)).to(device)
batch_size=96, print(f"Model params: {sum(p.numel() for p in model.parameters()):,}")
shuffle=False,
num_workers=4,
pin_memory=True
)
# ===============================
# MODEL
# ===============================
model = TransformerASL(
input_dim=63,
num_classes=len(le.classes_),
d_model=192,
nhead=6,
num_layers=4
).to(device)
print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}")
# ===============================
# TRAINING SETUP
# ===============================
criterion = nn.CrossEntropyLoss(label_smoothing=0.05) criterion = nn.CrossEntropyLoss(label_smoothing=0.05)
optimizer = optim.AdamW(model.parameters(), lr=5e-4, weight_decay=1e-4) optimizer = optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-4)
scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=10) scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=10)
# =============================== # --- TRAINING ---
# TRAIN / EVAL best_acc = 0.0
# =============================== patience = 15
wait = 0
epochs = 70
def train_epoch(): def train_epoch():
model.train() model.train()
total_loss = 0 total_loss = 0
correct = total = 0 correct = total = 0
for x, m, yb in tqdm(train_loader, desc="Train"):
for x, y in tqdm(train_loader, desc="Train"): x, m, yb = x.to(device), m.to(device), yb.to(device)
x, y = x.to(device), y.to(device) mask = create_padding_mask(m)
lengths = (x.abs().sum(dim=2) > 1e-5).sum(dim=1)
mask = create_padding_mask(lengths, x.size(1))
optimizer.zero_grad(set_to_none=True) optimizer.zero_grad(set_to_none=True)
logits = model(x, key_padding_mask=mask) logits = model(x, key_padding_mask=mask)
loss = criterion(logits, yb)
loss = criterion(logits, y)
loss.backward() loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=0.8)
grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=0.8)
optimizer.step() optimizer.step()
total_loss += loss.item() total_loss += loss.item()
correct += (logits.argmax(-1) == y).sum().item() correct += (logits.argmax(-1) == yb).sum().item()
total += y.size(0) total += yb.size(0)
return total_loss / len(train_loader), correct / total * 100 return total_loss / len(train_loader), correct / total * 100
@torch.no_grad() @torch.no_grad()
def evaluate(): def evaluate():
model.eval() model.eval()
correct = total = 0 correct = total = 0
for x, y in test_loader: for x, m, yb in test_loader:
x, y = x.to(device), y.to(device) x, m, yb = x.to(device), m.to(device), yb.to(device)
lengths = (x.abs().sum(dim=2) > 1e-5).sum(dim=1) mask = create_padding_mask(m)
mask = create_padding_mask(lengths, x.size(1))
logits = model(x, key_padding_mask=mask) logits = model(x, key_padding_mask=mask)
correct += (logits.argmax(-1) == y).sum().item() correct += (logits.argmax(-1) == yb).sum().item()
total += y.size(0) total += yb.size(0)
return correct / total * 100 if total > 0 else 0.0 return correct / total * 100 if total > 0 else 0.0
# ===============================
# TRAINING LOOP
# ===============================
best_acc = 0.0
patience = 15
wait = 0
epochs = 70
for epoch in range(epochs): for epoch in range(epochs):
loss, train_acc = train_epoch() loss, train_acc = train_epoch()
test_acc = evaluate() test_acc = evaluate()
print(f"[{epoch+1}/{epochs}] loss: {loss:.4f} | train: {train_acc:.2f}% | test: {test_acc:.2f}%")
print(f"[{epoch + 1:2d}/{epochs}] loss: {loss:.4f} | train: {train_acc:.2f}% | test: {test_acc:.2f}%")
scheduler.step() scheduler.step()
if test_acc > best_acc: if test_acc > best_acc:
best_acc = test_acc best_acc = test_acc
wait = 0 wait = 0
@@ -362,8 +295,7 @@ def main():
print("Early stopping") print("Early stopping")
break break
print(f"\nBest test accuracy reached: {best_acc:.2f}%") print(f"\nBest test accuracy: {best_acc:.2f}%")
if __name__ == '__main__': if __name__ == '__main__':
main() main()