-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkfold_validate.py
More file actions
129 lines (111 loc) · 3.59 KB
/
kfold_validate.py
File metadata and controls
129 lines (111 loc) · 3.59 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
'''
kfold_validate.py
File Purpose: Evaluate the best threshold value for fine-tuned
models using the existing validation dataset
@author: changspencer
'''
## PyTorch dependencies
import torch
## Python dependencies
import os
import matplotlib.pyplot as plt
## HSI-based dependencies - Remove spectral warnings
import spectral
spectral.settings.envi_support_nonlowercase_params = True
## Local external libraries
from src.Experiments.params_HyperPRI import ExpRedGreenBluePRI, ExpHyperspectralPRI
from src.PLTrainer import validate_net
#! Retain information on where the directory actually is relative to the calling file
rel_call_path = os.path.dirname(os.path.abspath(__file__))
## GLOBAL Parameters to set....
RANDOM_STATE = 1
USE_CUDA = True
LOAD_CKPT = False
TEST_AUG = False
n_seeds = 1
start_split = 0
num_splits = 5 # Assuming multiple splits
update_params = {
'model_name': [
'UNET',
'SpectralUNET',
'CubeNET',
],
'dataset': [
'RGB',
'HSI',
'HSI',
],
'spectral_bn_size': [
0,
1650,
0,
],
'cube_featmaps': [
0,
0,
64,
],
'criterion': [
torch.nn.BCEWithLogitsLoss(),
torch.nn.BCEWithLogitsLoss(),
torch.nn.BCEWithLogitsLoss(),
]
}
models = update_params['model_name']
datasets = update_params['dataset']
segmaps = [
False,
False,
False,
# True,
# True,
# True,
]
plt_colors = [
'tab:blue',
'tab:orange',
'tab:green',
'tab:red',
'tab:purple',
]
## File-specific parameters to set....
random_state = 1
use_cuda = True
print("\n ~~~~~~~~~~ 5-SPLIT CYCLES ~~~~~~~~~~\n")
plt.figure(dpi=150)
for run in range(start_split, num_splits):
print(f" ********** Split {run+1} **********")
for m_idx, (m, dset) in enumerate(zip(models, datasets)):
change_params = {}
for k_idx, k in enumerate(update_params):
change_params[k] = update_params[k][m_idx]
for seed_idx in range(n_seeds): # In case of running multiple random seeds on one split
split_no = seed_idx * 10 + run + 1
if dset.lower() == 'rgb':
exp_params = ExpRedGreenBluePRI(rel_call_path, split_no=run+1, augment=TEST_AUG)
# Switch to a different model (ie. change internal parameter strings)
exp_params.change_network_param(m, rel_call_path, run+1, model_params=change_params) # Num bins
else:
exp_params = ExpHyperspectralPRI(rel_call_path, split_no=run+1)
# Switch to a different model (ie. change internal parameter strings)
exp_params.change_network_param(m, rel_call_path, run+1, model_params=None) # Num bins
print(f" Model: {exp_params.model_param_str}")
print(f" Validation JSON: {exp_params.json_dir['val']}")
# pr_curve_info = validate_net(exp_params.get_val_data(),
pr_curve_info = validate_net(exp_params.get_val_data(),
exp_params,
save_segmaps=segmaps[m_idx])
# Will plot the last of all potential seeded runs
if run == start_split:
label_str = f"{exp_params.model_name}"
else:
label_str = None
plt.plot(pr_curve_info[1], pr_curve_info[0], alpha=0.7,
color=plt_colors[m_idx], label=label_str)
curve_str = "_".join(models)
plt.xlabel("Recall", fontsize=14)
plt.ylabel("Precision", fontsize=14)
plt.legend()
plt.savefig(f"{rel_call_path}/Saved_Models/{dset}/{curve_str}_pr.png")
plt.show()