-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
101 lines (91 loc) · 3.62 KB
/
model.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
import argparse
from torch.utils.data import Dataset
from models.transformer import Transformer
from models.action_recognizer import ActionRecognizer
from data_mgmt.dataloaders.transformer import DataLoader as TransformerDataLoader
from data_mgmt.dataloaders.gcn_transformer import DataLoader as GCNTransformerDataLoader
from typing import Dict, Tuple
def get_transformer_model(
config: Dict,
args: argparse.Namespace,
dataset: Tuple[Dataset, Dataset, Dataset],
) -> Tuple[
Transformer, Tuple[TransformerDataLoader, TransformerDataLoader, TransformerDataLoader]
]:
"""
Returns the model and the dataloader
Parameters
----------
config : Dict
Configuration for the model
args : argparse.Namespace
Arguments passed to the program
dataset : Tuple[Dataset, Dataset, Dataset]
Dataset to use for training, validation and testing
Returns
-------
Tuple[Transformer, Tuple[DataLoader, DataLoader, DataLoader]]
Model and the dataloaders
"""
train_dataset, val_dataset, test_dataset = dataset
train_loader = TransformerDataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True
)
val_loader = TransformerDataLoader(val_dataset, batch_size=args.batch_size, shuffle=True)
test_loader = TransformerDataLoader(
test_dataset, batch_size=args.batch_size, shuffle=True
)
return Transformer(
d_model=config["transformer_d_model"],
nhead=config["transformer_nhead"],
num_layers=config["transformer_num_layers"],
num_features=config["transformer_num_features"],
dropout=config["transformer_dropout"],
dim_ff=config["transformer_dim_feedforward"],
num_classes=config["transformer_num_classes"],
dataset=args.dataset_type,
), (train_loader, val_loader, test_loader)
def get_gcn_transformer_model(
config: Dict,
args: argparse.Namespace,
dataset: Tuple[Dataset, Dataset, Dataset],
) -> Tuple[
ActionRecognizer, Tuple[GCNTransformerDataLoader, GCNTransformerDataLoader, GCNTransformerDataLoader]
]:
"""
Returns the model and the dataloader
Parameters
----------
config : Dict
Configuration for the model
args : argparse.Namespace
Arguments passed to the program
dataset : Tuple[Dataset, Dataset, Dataset]
Dataset to use for training, validation and testing
Returns
-------
Tuple[ActionRecognizer, Tuple[DataLoader, DataLoader, DataLoader]]
Model and the dataloaders
"""
train_dataset, val_dataset, test_dataset = dataset
train_loader = GCNTransformerDataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True
)
val_loader = GCNTransformerDataLoader(val_dataset, batch_size=args.batch_size, shuffle=True)
test_loader = GCNTransformerDataLoader(
test_dataset, batch_size=args.batch_size, shuffle=True
)
return ActionRecognizer(
gcn_num_features=config["gcn_num_features"],
gcn_hidden_dim1=config["gcn_hidden_dim1"],
gcn_hidden_dim2=config["gcn_hidden_dim2"],
gcn_output_dim=config["gcn_output_dim"],
transformer_d_model=config["transformer_d_model"],
transformer_nhead=config["transformer_nhead"],
transformer_num_layers=config["transformer_num_layers"],
transformer_num_features=config["transformer_num_features"],
transformer_dropout=config["transformer_dropout"],
transformer_dim_feedforward=config["transformer_dim_feedforward"],
transformer_num_classes=config["transformer_num_classes"],
dataset=args.dataset_type,
), (train_loader, val_loader, test_loader)