Initial commit: handshapes multiclass project
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
137
doc/train_seq.py
Normal file
137
doc/train_seq.py
Normal file
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env python3
|
||||
# Train BiGRU on (T, F) sequences; reads input_dim from meta.json
|
||||
|
||||
import os, json, argparse # stdlib
|
||||
import numpy as np # arrays
|
||||
import torch, torch.nn as nn # model/ops
|
||||
from torch.utils.data import Dataset, DataLoader # data pipeline
|
||||
|
||||
def get_device():
|
||||
"""
|
||||
Prefer Apple Silicon's MPS if available; fallback to CPU/GPU accordingly.
|
||||
"""
|
||||
return torch.device("mps") if torch.backends.mps.is_available() else torch.device("cpu")
|
||||
|
||||
class SeqDataset(Dataset):
|
||||
"""
|
||||
Simple dataset wrapper with optional light augmentation.
|
||||
"""
|
||||
def __init__(self, X, y, augment=False):
|
||||
self.X = X.astype(np.float32) # ensure float32 features
|
||||
self.y = y.astype(np.int64) # class indices as int64
|
||||
self.augment = augment
|
||||
def __len__(self): return len(self.y) # number of samples
|
||||
def _augment(self, seq):
|
||||
# Add tiny Gaussian noise; helpful regularizer for high-D continuous features.
|
||||
return seq + np.random.normal(0, 0.01, size=seq.shape).astype(np.float32)
|
||||
def __getitem__(self, i):
|
||||
xi = self.X[i] # (T, F)
|
||||
if self.augment: xi = self._augment(xi) # optional noise
|
||||
return torch.from_numpy(xi).float(), int(self.y[i]) # return (tensor, label)
|
||||
|
||||
class SeqGRU(nn.Module):
|
||||
"""
|
||||
BiGRU → MLP head classifier.
|
||||
Uses last time step of GRU outputs (many-to-one).
|
||||
"""
|
||||
def __init__(self, input_dim, hidden=128, num_classes=26):
|
||||
super().__init__()
|
||||
self.gru = nn.GRU(input_dim, hidden, batch_first=True, bidirectional=True)
|
||||
self.head = nn.Sequential(
|
||||
nn.Linear(hidden*2, 128), nn.ReLU(), nn.Dropout(0.2),
|
||||
nn.Linear(128, num_classes),
|
||||
)
|
||||
def forward(self, x):
|
||||
h,_ = self.gru(x) # (B,T,2H)
|
||||
return self.head(h[:, -1, :]) # logits (B,C)
|
||||
|
||||
def main():
|
||||
"""
|
||||
Train loop:
|
||||
- Load prepared dataset
|
||||
- Compute global mean/std on train and normalize train/val
|
||||
- Train BiGRU with AdamW + cosine schedule
|
||||
- Save best checkpoint by val accuracy (includes mean/std)
|
||||
"""
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--landmarks", default="landmarks_seq32") # dataset folder
|
||||
ap.add_argument("--epochs", type=int, default=40)
|
||||
ap.add_argument("--batch", type=int, default=64)
|
||||
ap.add_argument("--lr", type=float, default=1e-3)
|
||||
ap.add_argument("--out", default="asl_seq32_gru.pt") # model save path
|
||||
args = ap.parse_args()
|
||||
|
||||
trX = np.load(os.path.join(args.landmarks,"train_X.npy")) # (Ntr, T, F)
|
||||
trY = np.load(os.path.join(args.landmarks,"train_y.npy")) # (Ntr,)
|
||||
vaX = np.load(os.path.join(args.landmarks,"val_X.npy")) # (Nva, T, F)
|
||||
vaY = np.load(os.path.join(args.landmarks,"val_y.npy")) # (Nva,)
|
||||
classes = json.load(open(os.path.join(args.landmarks,"class_names.json")))
|
||||
meta = json.load(open(os.path.join(args.landmarks,"meta.json")))
|
||||
T = int(meta["frames"]) # #frames per clip
|
||||
input_dim = int(meta.get("input_dim", trX.shape[-1])) # feature dim (safety)
|
||||
|
||||
print(f"Loaded: train {trX.shape} val {vaX.shape} classes={classes} input_dim={input_dim}")
|
||||
|
||||
# Global normalization (feature-wise) computed on TRAIN ONLY
|
||||
X_mean = trX.reshape(-1, trX.shape[-1]).mean(axis=0, keepdims=True).astype(np.float32) # (1,F)
|
||||
X_std = trX.reshape(-1, trX.shape[-1]).std(axis=0, keepdims=True).astype(np.float32) + 1e-6
|
||||
trXn = (trX - X_mean) / X_std # normalize train
|
||||
vaXn = (vaX - X_mean) / X_std # normalize val using train stats
|
||||
|
||||
tr_ds = SeqDataset(trXn, trY, augment=True) # datasets
|
||||
va_ds = SeqDataset(vaXn, vaY, augment=False)
|
||||
tr_dl = DataLoader(tr_ds, batch_size=args.batch, shuffle=True) # loaders
|
||||
va_dl = DataLoader(va_ds, batch_size=args.batch, shuffle=False)
|
||||
|
||||
device = get_device() # target device
|
||||
model = SeqGRU(input_dim=input_dim, hidden=128, num_classes=len(classes)).to(device)
|
||||
crit = nn.CrossEntropyLoss() # standard multi-class loss
|
||||
opt = torch.optim.AdamW(model.parameters(), lr=args.lr, weight_decay=1e-4) # AdamW helps generalization
|
||||
sch = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=args.epochs) # smooth LR decay
|
||||
|
||||
best_acc, best_state = 0.0, None # track best val acc
|
||||
for epoch in range(1, args.epochs+1):
|
||||
model.train()
|
||||
tot, correct, loss_sum = 0, 0, 0.0
|
||||
for xb, yb in tr_dl:
|
||||
xb, yb = xb.to(device), yb.to(device) # move to device
|
||||
opt.zero_grad(set_to_none=True) # reset grads
|
||||
logits = model(xb) # forward
|
||||
loss = crit(logits, yb) # compute loss
|
||||
loss.backward() # backprop
|
||||
opt.step() # update weights
|
||||
loss_sum += loss.item() * yb.size(0) # accumulate loss
|
||||
correct += (logits.argmax(1)==yb).sum().item() # count train correct
|
||||
tot += yb.size(0) # sample counter
|
||||
tr_loss = loss_sum / max(1, tot)
|
||||
tr_acc = correct / max(1, tot)
|
||||
|
||||
model.eval()
|
||||
vtot, vcorrect = 0, 0
|
||||
with torch.no_grad():
|
||||
for xb, yb in va_dl:
|
||||
xb, yb = xb.to(device), yb.to(device)
|
||||
logits = model(xb)
|
||||
vcorrect += (logits.argmax(1)==yb).sum().item()
|
||||
vtot += yb.size(0)
|
||||
va_acc = vcorrect / max(1, vtot) # validation accuracy
|
||||
sch.step() # update LR schedule
|
||||
|
||||
print(f"Epoch {epoch:02d}: train_loss={tr_loss:.4f} train_acc={tr_acc:.3f} val_acc={va_acc:.3f}")
|
||||
|
||||
if va_acc > best_acc: # save best checkpoint
|
||||
best_acc = va_acc
|
||||
best_state = {
|
||||
"model": model.state_dict(),
|
||||
"classes": classes,
|
||||
"frames": T,
|
||||
"X_mean": torch.from_numpy(X_mean),
|
||||
"X_std": torch.from_numpy(X_std),
|
||||
}
|
||||
torch.save(best_state, args.out)
|
||||
print(f" ✅ Saved best → {args.out} (val_acc={best_acc:.3f})")
|
||||
|
||||
print("Done. Best val_acc:", best_acc)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user