-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_regressor.py
160 lines (135 loc) · 5.68 KB
/
train_regressor.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
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
import pandas as pd
import numpy as np
import argparse
from utils.visualize import create_directory
from dataset.dataset import Kimore, load_class
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader,Subset
from model.stgcn import STGCN
from model.regressor import REG
import torch
from utils.normalize import normalize_skeletons
def get_args():
parser = argparse.ArgumentParser(
description="training regression model to predict generated data score")
parser.add_argument(
'--regression_model',
help = 'Choose a regression model',
type=str,
choices = ['STGCN','REG'],
default='STGCN',
)
parser.add_argument(
'--dataset',
help="Which dataset to use.",
type=str,
default='Kimore'
)
parser.add_argument(
'--output-directory',
type=str,
default='results/'
)
parser.add_argument(
'--runs',
help="Number of experiments to do.",
type=int,
default=5
)
parser.add_argument(
'--epochs',
help="Number of epochs to train the model.",
type=int,
default=2000
)
parser.add_argument(
'--device',
help="Device to run the training on.",
type=str,
choices=['cpu', 'cuda', 'mps'],
default='cuda' if torch.cuda.is_available() else ('mps' if torch.backends.mps.is_available() else 'cpu')
)
parser.add_argument(
'--data_split',
help="choose wether split the data or use it all",
type=str,
choices=['all', 'split'],
default='split'
)
parser.add_argument(
'--class_index',
help="which class to use",
type=int,
default=0
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = get_args()
output_directory_results = args.output_directory
create_directory(output_directory_results)
output_directory_reg_models = output_directory_results + 'regression_models/'
create_directory(output_directory_reg_models)
output_directory_regressor = output_directory_reg_models + args.regression_model + '/'
create_directory(output_directory_regressor)
dataset_dir = 'data/' + args.dataset + '/'
data,labels,scores = load_class(args.class_index,root_dir=dataset_dir)
xtrain,xtest,ytrain,ytest,strain,stest= train_test_split(data,labels,scores,test_size=0.2,random_state=42)
xtrain,min_X, max_X,min_Y,max_Y, min_Z,max_Z = normalize_skeletons(xtrain)
train_set = Kimore(xtrain,ytrain,strain)
train_loader = DataLoader(train_set,batch_size=16,shuffle =True)
xtest,_,_,_,_,_,_= normalize_skeletons(xtest,min_X, max_X,min_Y,max_Y, min_Z,max_Z)
test_set = Kimore(xtest,ytest,stest)
test_loader = DataLoader(test_set,batch_size=16,shuffle=False)
if args.data_split == 'all':
for _run in range(args.runs):
output_directory_run = output_directory_regressor + 'run_' + str(_run) + '/'
create_directory(output_directory_run)
output_directory_class = output_directory_run + 'class_' + str(args.class_index) + '/'
create_directory(output_directory_class)
if args.regression_model == 'STGCN':
model = STGCN(
output_directory=output_directory_class,
epochs=args.epochs,
device=args.device,
edge_importance_weighting=True)
# model.train_stgcn(device=args.device,train_loader=train_loader,test_loader=test_loader)
model.predict_scores(test_loader,args.device)
model.plot_train_scores(device= args.device,train_loader=train_loader)
elif args.regression_model == 'REG':
model = REG(
output_directory=output_directory_class,
epochs=args.epochs,
device=args.device,
)
# model.train_fun(device=args.device,train_loader=train_loader,test_loader=test_loader)
model.predict_scores(test_loader,args.device)
model.plot_train_scores(device= args.device,train_loader=train_loader)
elif args.data_split == 'split':
for _run in range(args.runs):
output_directory_run = output_directory_regressor + 'run_' + str(4) + '/'
create_directory(output_directory_run)
output_directory_class = output_directory_run + 'class_' + str(args.class_index) + '/'
create_directory(output_directory_class)
if args.regression_model == 'STGCN':
model = STGCN(
output_directory=output_directory_class,
epochs=args.epochs,
device=args.device,
edge_importance_weighting=True)
# model.train_stgcn(device=args.device,train_loader=train_loader,test_loader=test_loader)
model.predict_scores(test_loader,args.device)
model.plot_train_scores(device= args.device,train_loader=train_loader)
elif args.regression_model == 'REG':
model = REG(
output_directory=output_directory_class,
epochs=args.epochs,
device=args.device,
)
# model.train_fun(device=args.device,train_loader=train_loader,test_loader=test_loader)
model.predict_scores(test_loader,args.device)
model.plot_train_scores(device= args.device,train_loader=train_loader)