forked from amathislab/DeepDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_all_autoencoder_task.py
184 lines (154 loc) · 7.72 KB
/
train_all_autoencoder_task.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
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import random
import argparse
import time
import pandas
import pickle
import sys
sys.path.append('../code')
sys.path.append('./code')
from nn_rmodels_w_outputs_muscles_lin import ConvRModel, RecurrentRModel, ConvRModel_new
from nn_train_rutils_muscles_auto_lindec import *
from path_utils import PATH_TO_DATA, PATH_TO_OLD
def load_df(model_type, arch_type, task):
""" Function to load dataframe based on model type and architecture type.
Outputs:
- all_conv_models: dataframe containing all the networks to train
- key_trained: key to set when train is finished
"""
if model_type == 'conv_new':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_newhyp_seed.p', 'rb'))
elif model_type == 'conv':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_seed.p', 'rb'))
elif model_type == 'rec':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_seed.p', 'rb'))
elif model_type == 'rec_new':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_newhyp_seed.p', 'rb'))
if (model_type == 'conv_new') or (model_type == 'conv'):
best_models_arch = all_conv_models[all_conv_models['arch_type'] == arch_type] #.nlargest(1, 'test_accuracy')
key_trained = 'is_trained'
else:
best_models_arch = all_conv_models[all_conv_models['rec_blocktype'] == arch_type]
key_trained = 'is_training'
return best_models_arch, key_trained
def save_df(best_models_arch, model_type, arch_type, task):
""" Function to save the updated dataframe.
"""
if model_type == 'conv_new':
best_models_arch.to_pickle(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_newhyp_seed.p')
elif model_type == 'conv':
best_models_arch.to_pickle(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_seed.p')
elif model_type == 'rec':
best_models_arch.to_pickle(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_seed.p')
elif model_type == 'rec_new':
best_models_arch.to_pickle(PATH_TO_OLD + '/' + task + '/ALL_' + arch_type + '_newhyp_seed.p')
return
def sel_exp_id(model_type):
"""Return the experiment ID for loading the same initialization
Args:
model_type (str): Define the model type
Returns:
int: Experiment ID of the corresponding untrained models
"""
if model_type == 'conv_new':
old_exp_id = 115
elif model_type == 'conv':
old_exp_id = 15
elif model_type == 'rec':
old_exp_id = 45
elif model_type == 'rec_new':
old_exp_id = 5045
return old_exp_id
def main(args):
# Load dataset
task = args.task
exp_id = args.exp_id
path_to_dataset = PATH_TO_DATA
train_data_path = os.path.join(PATH_TO_DATA, 'dataset_train_snap_scaled_fin1_10all.hdf5')
val_data_path = os.path.join(PATH_TO_DATA, 'dataset_val_snap_scaled_fin1_10all.hdf5')
train_data = RDataset(train_data_path, val_data_path, 'train', key='spindle_info',target_key='spindle_info')
test_data_path = os.path.join(PATH_TO_DATA, 'dataset_test_snap_scaled_fin1_10all.hdf5')
test_data = RDataset(test_data_path, dataset_type='test', key='spindle_info',target_key='spindle_info')
n_outputs = train_data.train_targets.shape[1] * train_data.train_targets.shape[3]
print(n_outputs)
start_id = args.start_id
end_id = args.end_id
model_type = args.type
arch_type = args.arch_type
best_models_arch, key_trained = load_df(model_type, arch_type, task)
# print(best_models_arch)
old_exp_id = sel_exp_id(model_type)
for i in range(start_id, end_id):
print('---------------------------------')
print('Training model: ', i)
print('---------------------------------')
## Decomment this
if not best_models_arch.iloc[i][key_trained]:
latents = best_models_arch.iloc[i]
if model_type == 'conv':
# Create model
mymodel = ConvRModel(
experiment_id=exp_id, #i
arch_type=arch_type,
nlayers=latents['nlayers'],
n_skernels=latents['n_skernels'],
n_tkernels=latents['n_tkernels'],
n_skernels_trans=latents['n_skernels_trans'],
n_tkernels_trans=latents['n_tkernels_trans'],
s_kernelsize=latents['s_kernelsize'],
t_kernelsize=latents['t_kernelsize'],
s_stride=latents['s_stride'],
t_stride=latents['t_stride'],
n_outputs=n_outputs)
elif model_type == 'conv_new':
# Create model
mymodel = ConvRModel_new(
experiment_id=exp_id, #i
arch_type=arch_type,
nlayers=latents['nlayers'],
n_skernels=latents['n_skernels'],
n_tkernels=latents['n_tkernels'],
n_skernels_trans=latents['n_skernels_trans'],
n_tkernels_trans=latents['n_tkernels_trans'],
s_kernelsize=latents['s_kernelsize'],
t_kernelsize=latents['t_kernelsize'],
s_stride=latents['s_stride'],
t_stride=latents['t_stride'],
s_stride_trans=latents['s_stride_trans'],
t_stride_trans=latents['t_stride_trans'],
n_outputs=n_outputs) #,
elif model_type == 'rec':
mymodel = RecurrentRModel(
experiment_id=args.exp_id,
rec_blocktype=arch_type,
n_recunits=latents['n_recunits'],
npplayers=latents['npplayers'],
nppfilters=latents['nppfilters'],
s_kernelsize=latents['s_kernelsize'],
s_stride=latents['s_stride'],
seed=latents['seed'],
n_outputs=n_outputs)
print(mymodel.__dict__)
intime = time.time()
# Create trainer and train!
mytrainer = RTrainer(mymodel, train_data, test_data)
if model_type == 'rec':
mytrainer.train(num_epochs=70, learning_rate=1e-3, batch_size=512,
early_stopping_epochs=1, verbose=True, save_rand=True, retrain_same_init=True, old_exp_dir = old_exp_id)
else:
mytrainer.train(num_epochs=50, batch_size = 512, verbose=True, save_rand=True, retrain_same_init=True, old_exp_dir = old_exp_id)
outt = time.time()
print(f'Successfully trained model {i+1} / {args.end_id - args.start_id} in {(outt-intime)/60} minutes.')
best_models_arch, key_trained = load_df(model_type, arch_type, task)
best_models_arch.at[i,key_trained] = True
save_df(best_models_arch, model_type, arch_type, task)
if __name__=='__main__':
parser = argparse.ArgumentParser(description='Training Convolutional Nets for PCR.')
# parser.add_argument('--old_models', type=str, help='Name of old conv models',default='ALL_spatial_temporal')
parser.add_argument('--task', type=str, help='Task for training',default='autoencoder')
parser.add_argument('--type', type=str, help='Type of model',default='conv')
parser.add_argument('--arch_type', type=str, help='Architecture of specific model',default='spatial_temporal')
parser.add_argument('--exp_id', type=int, help='Experiment ID',default=90000)
parser.add_argument('--start_id', type=int, help='Id of net to start',default=16)
parser.add_argument('--end_id', type=int, help='Id of net to end',default=17)
main(parser.parse_args())