|
a |
|
b/train.py |
|
|
1 |
""" |
|
|
2 |
Separated training for OmiEmbed |
|
|
3 |
""" |
|
|
4 |
import time |
|
|
5 |
import warnings |
|
|
6 |
from util import util |
|
|
7 |
from params.train_params import TrainParams |
|
|
8 |
from datasets import create_single_dataloader |
|
|
9 |
from models import create_model |
|
|
10 |
from util.visualizer import Visualizer |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
if __name__ == "__main__": |
|
|
14 |
warnings.filterwarnings('ignore') |
|
|
15 |
# Get parameters |
|
|
16 |
param = TrainParams().parse() |
|
|
17 |
if param.deterministic: |
|
|
18 |
util.setup_seed(param.seed) |
|
|
19 |
|
|
|
20 |
# Dataset related |
|
|
21 |
dataloader, sample_list = create_single_dataloader(param, enable_drop_last=True) |
|
|
22 |
print('The size of training set is {}'.format(len(dataloader))) |
|
|
23 |
# Get the dimension of input omics data |
|
|
24 |
param.omics_dims = dataloader.get_omics_dims() |
|
|
25 |
if param.downstream_task in ['classification', 'multitask', 'alltask']: |
|
|
26 |
# Get the number of classes for the classification task |
|
|
27 |
if param.class_num == 0: |
|
|
28 |
param.class_num = dataloader.get_class_num() |
|
|
29 |
if param.downstream_task != 'alltask': |
|
|
30 |
print('The number of classes: {}'.format(param.class_num)) |
|
|
31 |
if param.downstream_task in ['regression', 'multitask', 'alltask']: |
|
|
32 |
# Get the range of the target values |
|
|
33 |
values_min = dataloader.get_values_min() |
|
|
34 |
values_max = dataloader.get_values_max() |
|
|
35 |
if param.regression_scale == 1: |
|
|
36 |
param.regression_scale = values_max |
|
|
37 |
print('The range of the target values is [{}, {}]'.format(values_min, values_max)) |
|
|
38 |
if param.downstream_task in ['survival', 'multitask', 'alltask']: |
|
|
39 |
# Get the range of T |
|
|
40 |
survival_T_min = dataloader.get_survival_T_min() |
|
|
41 |
survival_T_max = dataloader.get_survival_T_max() |
|
|
42 |
if param.survival_T_max == -1: |
|
|
43 |
param.survival_T_max = survival_T_max |
|
|
44 |
print('The range of survival T is [{}, {}]'.format(survival_T_min, survival_T_max)) |
|
|
45 |
|
|
|
46 |
# Model related |
|
|
47 |
model = create_model(param) # Create a model given param.model and other parameters |
|
|
48 |
model.setup(param) # Regular setup for the model: load and print networks, create schedulers |
|
|
49 |
visualizer = Visualizer(param) # Create a visualizer to print results |
|
|
50 |
|
|
|
51 |
# Start the epoch loop |
|
|
52 |
visualizer.print_phase(model.phase) |
|
|
53 |
for epoch in range(param.epoch_count, param.epoch_num + 1): # outer loop for different epochs |
|
|
54 |
epoch_start_time = time.time() # Start time of this epoch |
|
|
55 |
model.epoch = epoch |
|
|
56 |
# TRAINING |
|
|
57 |
model.set_train() # Set train mode for training |
|
|
58 |
iter_load_start_time = time.time() # Start time of data loading for this iteration |
|
|
59 |
output_dict, losses_dict, metrics_dict = model.init_log_dict() # Initialize the log dictionaries |
|
|
60 |
if epoch == param.epoch_num_p1 + 1: |
|
|
61 |
model.phase = 'p2' # Change to supervised phase |
|
|
62 |
visualizer.print_phase(model.phase) |
|
|
63 |
if epoch == param.epoch_num_p1 + param.epoch_num_p2 + 1: |
|
|
64 |
model.phase = 'p3' # Change to supervised phase |
|
|
65 |
visualizer.print_phase(model.phase) |
|
|
66 |
if param.save_latent_space and epoch == param.epoch_num: |
|
|
67 |
latent_dict = model.init_latent_dict() |
|
|
68 |
|
|
|
69 |
# Start training loop |
|
|
70 |
for i, data in enumerate(dataloader): # Inner loop for different iteration within one epoch |
|
|
71 |
model.iter = i |
|
|
72 |
dataset_size = len(dataloader) |
|
|
73 |
actual_batch_size = len(data['index']) |
|
|
74 |
iter_start_time = time.time() # Timer for computation per iteration |
|
|
75 |
if i % param.print_freq == 0: |
|
|
76 |
load_time = iter_start_time - iter_load_start_time # Data loading time for this iteration |
|
|
77 |
model.set_input(data) # Unpack input data from the output dictionary of the dataloader |
|
|
78 |
model.update() # Calculate losses, gradients and update network parameters |
|
|
79 |
model.update_log_dict(output_dict, losses_dict, metrics_dict, actual_batch_size) # Update the log dictionaries |
|
|
80 |
if param.save_latent_space and epoch == param.epoch_num: |
|
|
81 |
latent_dict = model.update_latent_dict(latent_dict) # Update the latent space array |
|
|
82 |
if i % param.print_freq == 0: # Print training losses and save logging information to the disk |
|
|
83 |
comp_time = time.time() - iter_start_time # Computational time for this iteration |
|
|
84 |
visualizer.print_train_log(epoch, i, losses_dict, metrics_dict, load_time, comp_time, param.batch_size, dataset_size) |
|
|
85 |
iter_load_start_time = time.time() |
|
|
86 |
|
|
|
87 |
# Model saving |
|
|
88 |
if param.save_model: |
|
|
89 |
if param.save_epoch_freq == -1: # Only save networks during last epoch |
|
|
90 |
if epoch == param.epoch_num: |
|
|
91 |
print('Saving the model at the end of epoch {:d}'.format(epoch)) |
|
|
92 |
model.save_networks(str(epoch)) |
|
|
93 |
elif epoch % param.save_epoch_freq == 0: # Save both the generator and the discriminator every <save_epoch_freq> epochs |
|
|
94 |
print('Saving the model at the end of epoch {:d}'.format(epoch)) |
|
|
95 |
# model.save_networks('latest') |
|
|
96 |
model.save_networks(str(epoch)) |
|
|
97 |
|
|
|
98 |
train_time = time.time() - epoch_start_time |
|
|
99 |
current_lr = model.update_learning_rate() # update learning rates at the end of each epoch |
|
|
100 |
visualizer.print_train_summary(epoch, losses_dict, output_dict, train_time, current_lr) |
|
|
101 |
|
|
|
102 |
if param.save_latent_space and epoch == param.epoch_num: |
|
|
103 |
visualizer.save_latent_space(latent_dict, sample_list) |