forked from amathislab/DeepDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_all_classification_task.py
159 lines (137 loc) · 6.65 KB
/
train_all_classification_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
import os
import random
import argparse
import time
import pandas
import pickle
import sys
sys.path.append('../code')
# sys.path.append('./code')
from nn_models import ConvModel, RecurrentModel, ConvModel_new, RecurrentModel_new
from nn_train_utils import *
from path_utils import PATH_TO_DATA, PATH_TO_OLD
def load_df(model_type, arch_type):
""" 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 + '/classification/ALL_' + arch_type + '_newhyp_seed.p', 'rb'))
elif model_type == 'conv':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/classification/ALL_' + arch_type + '_seed.p', 'rb'))
elif model_type == 'rec':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/classification/ALL_' + arch_type + '_new_seed_mod1.p', 'rb'))
elif model_type == 'rec_new':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/classification/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):
""" Function to save the updated dataframe.
"""
if model_type == 'conv_new':
best_models_arch.to_pickle(PATH_TO_OLD + '/classification/ALL_' + arch_type + '_newhyp_seed.p')
elif model_type == 'conv':
best_models_arch.to_pickle(PATH_TO_OLD + '/classification/ALL_' + arch_type + '_seed.p')
elif model_type == 'rec':
best_models_arch.to_pickle(PATH_TO_OLD + '/classification/ALL_' + arch_type + '_new_seed_mod1.p')
elif model_type == 'rec_new':
best_models_arch.to_pickle(PATH_TO_OLD + '/classification/ALL_' + arch_type + '_newhyp_seed.p')
return
def main(args):
# Load dataset
exp_id = args.exp_id
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 = Dataset(train_data_path, val_data_path, 'train', key='spindle_info')
test_data_path = os.path.join(PATH_TO_DATA, 'dataset_test_snap_scaled_fin1_10all.hdf5')
test_data = Dataset(test_data_path, dataset_type='test', key='spindle_info')
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)
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 = ConvModel(
experiment_id=exp_id, #i
nclasses=20,
arch_type=arch_type,
nlayers=latents['nlayers'],
n_skernels=latents['n_skernels'],
n_tkernels=latents['n_tkernels'],
s_kernelsize=latents['s_kernelsize'],
t_kernelsize=latents['t_kernelsize'],
s_stride=latents['s_stride'],
t_stride=latents['t_stride']) #
elif model_type == 'conv_new':
# Create model
mymodel = ConvModel_new(
experiment_id=exp_id, #i
nclasses=20,
arch_type=arch_type,
nlayers=latents['nlayers'],
n_skernels=latents['n_skernels'],
n_tkernels=latents['n_tkernels'],
s_kernelsize=latents['s_kernelsize'],
t_kernelsize=latents['t_kernelsize'],
s_stride=latents['s_stride'],
t_stride=latents['t_stride']) #,
elif model_type == 'rec':
mymodel = RecurrentModel(
experiment_id=args.exp_id,
nclasses=20,
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'])
elif model_type == 'rec_new':
mymodel = RecurrentModel_new(
experiment_id=args.exp_id,
nclasses=20,
rec_blocktype=arch_type,
n_reclayers=latents['n_reclayers'],
n_recunits=latents['n_recunits'],
npplayers=latents['npplayers'],
nppfilters=latents['nppfilters'],
s_kernelsize=latents['s_kernelsize'],
s_stride=latents['s_stride'],
seed=latents['seed'])
print(mymodel.__dict__)
intime = time.time()
# Create trainer and train!
mytrainer = Trainer(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)
else:
mytrainer.train(num_epochs=50, batch_size = 512, verbose=True, save_rand=True)
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)
best_models_arch.at[i,key_trained] = True
save_df(best_models_arch, model_type, arch_type)
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('--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=7003)
parser.add_argument('--start_id', type=int, help='Id of net to start',default=0)
parser.add_argument('--end_id', type=int, help='Id of net to end',default=5)
main(parser.parse_args())