-
Notifications
You must be signed in to change notification settings - Fork 42
/
CSL_Skeleton_GCN.py
80 lines (71 loc) · 3.28 KB
/
CSL_Skeleton_GCN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
77
78
79
80
import os
import sys
from datetime import datetime
import logging
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, random_split
from torch.utils.tensorboard import SummaryWriter
from models.GCN import GCN
from dataset import CSL_Skeleton
from train import train_epoch
from validation import val_epoch
# Path setting
data_path = "/home/haodong/Data/CSL_Isolated_1/xf500_body_depth_txt"
label_path = "/home/haodong/Data/CSL_Isolated_1/dictionary.txt"
model_path = "/home/haodong/Data/gcn_models"
log_path = "log/gcn_{:%Y-%m-%d_%H-%M-%S}.log".format(datetime.now())
sum_path = "runs/slr_gcn_{:%Y-%m-%d_%H-%M-%S}".format(datetime.now())
# Log to file & tensorboard writer
logging.basicConfig(level=logging.INFO, format='%(message)s', handlers=[logging.FileHandler(log_path), logging.StreamHandler()])
logger = logging.getLogger('SLR')
logger.info('Logging to file...')
writer = SummaryWriter(sum_path)
# Use specific gpus
os.environ["CUDA_VISIBLE_DEVICES"]="1"
# Device setting
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Hyperparams
epochs = 200
batch_size = 32
learning_rate = 1e-5
log_interval = 100
num_classes = 500
in_channels = 2
sample_duration = 16
selected_joints = None
split_to_channels = True
# Train with GCN
if __name__ == '__main__':
# Load data
transform = None # TODO
train_set = CSL_Skeleton(data_path=data_path, label_path=label_path, frames=sample_duration, num_classes=num_classes,
selected_joints=selected_joints, split_to_channels=split_to_channels, train=True, transform=transform)
val_set = CSL_Skeleton(data_path=data_path, label_path=label_path, frames=sample_duration, num_classes=num_classes,
selected_joints=selected_joints, split_to_channels=split_to_channels, train=False, transform=transform)
logger.info("Dataset samples: {}".format(len(train_set)+len(val_set)))
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
# Create model
model = GCN(in_channels=in_channels, num_class=num_classes, graph_args={'layout': 'ntu-rgb+d'},
edge_importance_weighting=True).to(device)
# Run the model parallelly
if torch.cuda.device_count() > 1:
logger.info("Using {} GPUs".format(torch.cuda.device_count()))
model = nn.DataParallel(model)
# Create loss criterion & optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Start training
logger.info("Training Started".center(60, '#'))
for epoch in range(epochs):
# Train the model
train_epoch(model, criterion, optimizer, train_loader, device, epoch, logger, log_interval, writer)
# Validate the model
val_epoch(model, criterion, val_loader, device, epoch, logger, writer)
# Save model
torch.save(model.state_dict(), os.path.join(model_path, "slr_gcn_epoch{:03d}.pth".format(epoch+1)))
logger.info("Epoch {} Model Saved".format(epoch+1).center(60, '#'))
logger.info("Training Finished".center(60, '#'))