|
a |
|
b/test.py |
|
|
1 |
""" |
|
|
2 |
Separated testing for OmiEmbed |
|
|
3 |
""" |
|
|
4 |
import time |
|
|
5 |
from util import util |
|
|
6 |
from params.test_params import TestParams |
|
|
7 |
from datasets import create_single_dataloader |
|
|
8 |
from models import create_model |
|
|
9 |
from util.visualizer import Visualizer |
|
|
10 |
|
|
|
11 |
if __name__ == '__main__': |
|
|
12 |
# Get testing parameter |
|
|
13 |
param = TestParams().parse() |
|
|
14 |
if param.deterministic: |
|
|
15 |
util.setup_seed(param.seed) |
|
|
16 |
|
|
|
17 |
# Dataset related |
|
|
18 |
dataloader, sample_list = create_single_dataloader(param, shuffle=False) # No shuffle for testing |
|
|
19 |
print('The size of testing set is {}'.format(len(dataloader))) |
|
|
20 |
# Get sample list for the dataset |
|
|
21 |
param.sample_list = dataloader.get_sample_list() |
|
|
22 |
# Get the dimension of input omics data |
|
|
23 |
param.omics_dims = dataloader.get_omics_dims() |
|
|
24 |
if param.downstream_task == 'classification' or param.downstream_task == 'multitask': |
|
|
25 |
# Get the number of classes for the classification task |
|
|
26 |
if param.class_num == 0: |
|
|
27 |
param.class_num = dataloader.get_class_num() |
|
|
28 |
print('The number of classes: {}'.format(param.class_num)) |
|
|
29 |
if param.downstream_task == 'regression' or param.downstream_task == 'multitask': |
|
|
30 |
# Get the range of the target values |
|
|
31 |
values_min = dataloader.get_values_min() |
|
|
32 |
values_max = dataloader.get_values_max() |
|
|
33 |
if param.regression_scale == 1: |
|
|
34 |
param.regression_scale = values_max |
|
|
35 |
print('The range of the target values is [{}, {}]'.format(values_min, values_max)) |
|
|
36 |
if param.downstream_task == 'survival' or param.downstream_task == 'multitask': |
|
|
37 |
# Get the range of T |
|
|
38 |
survival_T_min = dataloader.get_survival_T_min() |
|
|
39 |
survival_T_max = dataloader.get_survival_T_max() |
|
|
40 |
if param.survival_T_max == -1: |
|
|
41 |
param.survival_T_max = survival_T_max |
|
|
42 |
print('The range of survival T is [{}, {}]'.format(survival_T_min, survival_T_max)) |
|
|
43 |
|
|
|
44 |
# Model related |
|
|
45 |
model = create_model(param) # Create a model given param.model and other parameters |
|
|
46 |
model.setup(param) # Regular setup for the model: load and print networks, create schedulers |
|
|
47 |
visualizer = Visualizer(param) # Create a visualizer to print results |
|
|
48 |
|
|
|
49 |
# TESTING |
|
|
50 |
model.set_eval() |
|
|
51 |
test_start_time = time.time() # Start time of testing |
|
|
52 |
output_dict, losses_dict, metrics_dict = model.init_log_dict() # Initialize the log dictionaries |
|
|
53 |
if param.save_latent_space: |
|
|
54 |
latent_dict = model.init_latent_dict() |
|
|
55 |
|
|
|
56 |
# Start testing loop |
|
|
57 |
for i, data in enumerate(dataloader): |
|
|
58 |
dataset_size = len(dataloader) |
|
|
59 |
actual_batch_size = len(data['index']) |
|
|
60 |
model.set_input(data) # Unpack input data from the output dictionary of the dataloader |
|
|
61 |
model.test() # Run forward to get the output tensors |
|
|
62 |
model.update_log_dict(output_dict, losses_dict, metrics_dict, actual_batch_size) # Update the log dictionaries |
|
|
63 |
if param.save_latent_space: |
|
|
64 |
latent_dict = model.update_latent_dict(latent_dict) # Update the latent space array |
|
|
65 |
if i % param.print_freq == 0: # Print testing log |
|
|
66 |
visualizer.print_test_log(param.epoch_to_load, i, losses_dict, metrics_dict, param.batch_size, dataset_size) |
|
|
67 |
|
|
|
68 |
test_time = time.time() - test_start_time |
|
|
69 |
visualizer.print_test_summary(param.epoch_to_load, losses_dict, output_dict, test_time) |
|
|
70 |
visualizer.save_output_dict(output_dict) |
|
|
71 |
if param.save_latent_space: |
|
|
72 |
visualizer.save_latent_space(latent_dict, sample_list) |