-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (145 loc) · 5.43 KB
/
main.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
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
126
127
128
129
130
131
132
133
134
135
136
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
import torch
import numpy as np
import argparse
from collections import Counter
from trainer import Trainer
from model import get_transformer_model, get_gcn_transformer_model
from utils.logger import Logger
from utils.model_config import ModelConfig
from data_mgmt.datasets.ur_dataset import URDataset
from data_mgmt.datasets.ntu_dataset import NTUDataset
from typing import Tuple
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Train the model")
parser.add_argument("--lr", type=float, default=1e-4, help="Learning rate")
parser.add_argument(
"--dataset",
type=str,
default="./data",
help="Path to the dataset folder",
)
parser.add_argument(
"--model",
type=str,
default="transformer",
help="Model to use for training, transformer or gcn_transformer",
)
parser.add_argument(
"--dataset_type",
type=str,
default="ur",
help="Type of dataset to use, ntu or ur",
)
parser.add_argument(
"--skip",
type=int,
default=11,
help="Number of frames to skip",
)
parser.add_argument("--epochs", type=int, default=50, help="Number of epochs")
parser.add_argument("--batch_size", type=int, default=32, help="Batch size")
parser.add_argument(
"--shuffle", type=bool, default=True, help="Shuffle the dataset"
)
parser.add_argument(
"--output_folder",
type=str,
default="./output/",
help="Path to the output folder",
)
parser.add_argument(
"--logger_config",
type=str,
default="./config/logger.ini",
help="Path to the logger config file",
)
parser.add_argument(
"--model_config",
type=str,
default="./config/model.json",
help="Path to the model config file",
)
parser.add_argument(
"--occlude",
action="store_true",
help="Whether to occlude the input or not",
)
args = parser.parse_args()
if args.dataset_type not in ["ntu", "ur"]:
raise ValueError("Dataset type should be either ntu or ur")
if args.model not in ["transformer", "gcn_transformer"]:
raise ValueError("Model should be either transformer or gcn_transformer")
if args.dataset_type == "ur":
if args.skip % 2 == 0:
raise ValueError("Skip frames should be odd")
if args.skip > 11:
raise ValueError("Skip frames should be less than 11")
return args
def load_dataset(args : argparse.Namespace, logger : Logger) -> Tuple[torch.utils.data.Dataset, torch.utils.data.Dataset, torch.utils.data.Dataset]:
np.random.seed(42)
if args.dataset_type == "ntu":
dataset = NTUDataset(args.dataset, occlude=args.occlude)
elif args.dataset_type == "ur":
dataset = URDataset(args.dataset, skip=args.skip)
if len(dataset) > 0:
logger.info("Dataset loaded successfully.")
logger.info(f"Dataset size: {len(dataset)}")
else:
logger.error("Dataset loading failed.")
logger.info("Check if the dataset folder is correct.")
exit()
train_size = int(0.60 * len(dataset))
val_size = len(dataset) - train_size
generator = torch.Generator().manual_seed(42)
train_dataset, val_dataset = torch.utils.data.random_split(
dataset, [train_size, val_size], generator=generator
)
test_size = int(0.3 * len(val_dataset))
val_dataset, test_dataset = torch.utils.data.random_split(
val_dataset, [len(val_dataset) - test_size, test_size], generator=generator
)
label_counts = Counter(dataset.labels)
unique_labels = len(list(set(dataset.labels)))
logger.info(f"Number of unique labels: {unique_labels}")
for label, count in label_counts.items():
logger.info(f"Label: {label}, Count: {count}")
return train_dataset, val_dataset, test_dataset
def main():
args = parse_args()
logger = Logger(args.logger_config).get_logger()
logger.info("\n")
logger.info("Loading the dataset...")
train_dataset, val_dataset, test_dataset = load_dataset(
args, logger
)
logger.info(f"Training dataset size: {len(train_dataset)}")
logger.info(f"Validation dataset size: {len(val_dataset)}")
logger.info(f"Testing dataset size: {len(test_dataset)}")
model_config = ModelConfig(args.model_config).get_config()
if args.model == "transformer":
model, (train_dataloader, val_dataloader, test_dataloader) = get_transformer_model(
model_config, args, (train_dataset, val_dataset, test_dataset)
)
elif args.model == "gcn_transformer":
model, (train_dataloader, val_dataloader, test_dataloader) = get_gcn_transformer_model(
model_config, args, (train_dataset, val_dataset, test_dataset)
)
trainer = Trainer(model, lr=args.lr, logger=logger, model_type=args.model)
logger.info(f"Batch size: {args.batch_size}")
logger.info(f"Number of epochs: {args.epochs}")
logger.info(f"Learning rate: {args.lr}")
logger.info("Training the model. Please wait...")
trainer.train(
train_dataloader,
val_dataloader,
epochs=args.epochs,
output_path=args.output_folder,
save_model=True,
)
logger.info("")
logger.info("Testing model on the test dataset...")
trainer.test(
test_dataloader, output_path=args.output_folder
)
if __name__ == "__main__":
main()