-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_ascvae.py
173 lines (141 loc) · 6.52 KB
/
train_ascvae.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
#-------------------------------------
# ------------------------This files train the Action+score conditioned VAE---------------------------------------
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_data,load_class
from sklearn.model_selection import train_test_split
from model.scvae import SCVAE
from model.ascvae import ASCVAE
from torch.utils.data import DataLoader,Subset
import torch
from utils.normalize import normalize_skeletons,unnormalize_generated_skeletons
from utils.visualize import plot_skel
def get_args():
parser = argparse.ArgumentParser(
description="")
parser.add_argument(
'--generative-model',
help="Which generative model to use .",
type=str,
choices=['ASCVAE','SCVAE'],
default='ASCVAE',
)
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(
'--wrec',
help="Weight for the reconstruction loss.",
type=float,
default=0.999
)
parser.add_argument(
'--wkl',
help="Weight for the kl loss.",
type=float,
default=1e-3
)
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 generate from",
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_gen_models = output_directory_results + 'Generative_models/'
create_directory(output_directory_gen_models)
output_directory_dataset = output_directory_gen_models + 'Score+Action_conditioned/'
create_directory(output_directory_dataset)
output_directory_generator = output_directory_dataset + args.generative_model + '/'
create_directory(output_directory_generator)
output_directory_weights_losses = output_directory_generator + 'Wrec_' + str(args.wrec) + '_Wkl_' + str(args.wkl) + '/'
create_directory(output_directory_weights_losses)
dataset_dir = 'data/' + args.dataset + '/'
for _run in range(args.runs):
output_directory_run = output_directory_weights_losses + 'run_' + str(_run) + '/'
create_directory(output_directory_run)
output_directory_skeletons = output_directory_run + 'class_'+ str(args.class_index) + '/'
create_directory(output_directory_skeletons)
output_directory_skeletons_class = output_directory_skeletons + 'generated_samples/'
create_directory(output_directory_skeletons_class)
if args.generative_model == 'ASCVAE':
data,labels,scores = load_data(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)
generator = ASCVAE(output_directory=output_directory_run,
epochs=args.epochs,
device=args.device,
w_rec=args.wrec,
w_kl=args.wkl)
generator.train_function(dataloader=train_loader,device=args.device)
generator.visualize_latent_space(train_loader,device=args.device)
elif args.generative_model == 'SCVAE':
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)
generator = SCVAE(output_directory=output_directory_skeletons,
epochs=args.epochs,
device=args.device,
w_rec=args.wrec,
w_kl=args.wkl)
generator.train_function(train_loader,device=args.device)
generated_samples,scores = generator.generate_samples_from_prior(device=args.device,gif_directory=output_directory_skeletons_class,dataloader=test_loader)
for i in range(5):
unnormalized_sample= unnormalize_generated_skeletons(generated_samples[i:],min_X, max_X,min_Y,max_Y, min_Z,max_Z)
plot_skel(unnormalized_sample,output_directory=output_directory_skeletons_class,title=f'sample_score_{scores[i]}')
print('plotting done', i)