forked from amathislab/DeepDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_all_barlow_task.py
More file actions
193 lines (167 loc) · 8.79 KB
/
Copy pathtrain_all_barlow_task.py
File metadata and controls
193 lines (167 loc) · 8.79 KB
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
185
186
187
188
189
190
191
192
193
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, BarlowTwinsModel, BarlowTwinsModel_new, BarlowTwinsModel_rec, BarlowTwinsModel_rec_new
from nn_train_utils_barlow_new 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 == 'barlow_conv_new':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_newhyp_seed.p', 'rb'))
elif model_type == 'barlow_conv':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_seed.p', 'rb'))
elif model_type == 'barlow_rec':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_seed.p', 'rb'))
elif model_type == 'barlow_rec_new':
all_conv_models = pickle.load(open(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_newhyp_seed.p', 'rb'))
if (model_type == 'barlow_conv_new') or (model_type == 'barlow_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 == 'barlow_conv_new':
best_models_arch.to_pickle(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_newhyp_seed.p')
elif model_type == 'barlow_conv':
best_models_arch.to_pickle(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_seed.p')
elif model_type == 'barlow_rec':
best_models_arch.to_pickle(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_seed.p')
elif model_type == 'barlow_rec_new':
best_models_arch.to_pickle(PATH_TO_OLD + '/barlow/ALL_' + arch_type + '_BT_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
exp_id = args.exp_id
augmentations = [str(aug) for aug in args.augmentation]
all_startpoint_index = os.path.join(PATH_TO_DATA + '/all_startpoint_index.hdf5')
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, path_to_ind = all_startpoint_index, augm_type =augmentations, dataset_type='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, path_to_ind = all_startpoint_index, augm_type =augmentations, 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)
old_exp_id = sel_exp_id(model_type)
for i in range(start_id, end_id):
print('---------------------------------')
print('Training model: ', i)
print('---------------------------------')
if not best_models_arch.iloc[i][key_trained]:
latents = best_models_arch.iloc[i]
# Sample model hyperparameters
if model_type == 'barlow_conv':
mymodel = BarlowTwinsModel(
experiment_id=exp_id,
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'],
nlayers_fc=3,
nunits= [args.proj_size for _ in range(3)], #latents_barlow['units_fc'],
with_projector=True)
elif model_type == 'barlow_conv_new':
mymodel = BarlowTwinsModel_new(
experiment_id=exp_id,
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'],
nlayers_fc=3,
nunits= [args.proj_size for _ in range(3)], #latents_barlow['units_fc'],
with_projector=True)
elif model_type == 'barlow_rec':
mymodel = BarlowTwinsModel_rec(
experiment_id=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'],
nlayers_fc=3,
nunits=[args.proj_size for _ in range(3)],
with_projector=True)
elif model_type == 'barlow_rec_new':
mymodel = BarlowTwinsModel_rec_new(
experiment_id=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'],
nlayers_fc=3,
nunits=[args.proj_size for _ in range(3)],
with_projector=True)
intime = time.time()
print(mymodel.__dict__)
# Create trainer and train!
mytrainer = Trainer(mymodel, train_data, test_data)
if (model_type == 'barlow_rec') or (model_type == 'barlow_rec_new'):
mytrainer.train(num_epochs=40, learning_rate=1e-3, batch_size=256,
early_stopping_epochs=1, verbose=True, save_rand=True, retrain_same_init=True, old_exp_dir = old_exp_id)
elif (model_type == 'barlow_conv') or (model_type == 'barlow_conv_new'):
mytrainer.train(num_epochs=25, learning_rate=0.005, batch_size = 256, 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)
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='barlow')
parser.add_argument('--arch_type', type=str, help='Architecture of specific model',default='spatial_temporal')
parser.add_argument('--augmentation', nargs='+', help='Augmentation to apply (time, muscle, noise, close, far)',default=['time'])
parser.add_argument('--proj_size', type=int, help='Size of the projector',default=256)
# parser.add_argument('--lr', type=float, help='Learning rate of the network',default=0.005)
parser.add_argument('--exp_id', type=int, help='Experiment ID',default=7040)
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=1)
main(parser.parse_args())