Delete duplicate/old file: \/train_and_test.py
Browse files- ///train_and_test.py +0 -255
///train_and_test.py
DELETED
|
@@ -1,255 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/python
|
| 2 |
-
'''
|
| 3 |
-
Script for both training and evaluating the DLoc network
|
| 4 |
-
Automatically imports the parameters from params.py.
|
| 5 |
-
For further details onto which params file to load
|
| 6 |
-
read the README in `params_storage` folder.
|
| 7 |
-
'''
|
| 8 |
-
import sys
|
| 9 |
-
import torch
|
| 10 |
-
import warnings
|
| 11 |
-
with warnings.catch_warnings():
|
| 12 |
-
warnings.filterwarnings("ignore",category=FutureWarning)
|
| 13 |
-
from utils import *
|
| 14 |
-
from modelADT import ModelADT
|
| 15 |
-
from Generators import *
|
| 16 |
-
from data_loader import load_data, LazyMultiHDF5Dataset
|
| 17 |
-
from joint_model import Enc_2Dec_Network
|
| 18 |
-
from joint_model import Enc_Dec_Network
|
| 19 |
-
from joint_model import Mamba_Network
|
| 20 |
-
from params import *
|
| 21 |
-
import trainer
|
| 22 |
-
torch.manual_seed(0)
|
| 23 |
-
np.random.seed(0)
|
| 24 |
-
|
| 25 |
-
'''
|
| 26 |
-
Defining the paths from where to Load Data.
|
| 27 |
-
Assumes that the data is stored in a subfolder called data in the current data folder
|
| 28 |
-
'''
|
| 29 |
-
|
| 30 |
-
#####################################Final Simple Space Results################################################
|
| 31 |
-
if "data" in opt_exp and opt_exp.data == "rw_to_rw_atk":
|
| 32 |
-
# Training and testing data loaded for the Final results For Env-1 (The smaller space) in the paper (Figure 10a)
|
| 33 |
-
trainpath = ['./data/dataset_non_fov_train_July18.mat',
|
| 34 |
-
'./data/dataset_fov_train_July18.mat']
|
| 35 |
-
testpath = ['./data/dataset_non_fov_test_July18.mat',
|
| 36 |
-
'./data/dataset_fov_test_July18.mat']
|
| 37 |
-
print('Real World to Real World experiments started')
|
| 38 |
-
|
| 39 |
-
#####################################Final Complex Space Results################################################
|
| 40 |
-
elif "data" in opt_exp and opt_exp.data == "rw_to_rw":
|
| 41 |
-
# Training and testing data loaded for the Final results For Env-2 (The larger space) in the paper (Figure 10b)
|
| 42 |
-
trainpath = ['./data/dataset_jacobs_July28.mat',
|
| 43 |
-
'./data/dataset_non_fov_train_jacobs_July28_2.mat',
|
| 44 |
-
'./data/dataset_fov_train_jacobs_July28_2.mat']
|
| 45 |
-
testpath = ['./data/dataset_fov_test_jacobs_July28_2.mat',
|
| 46 |
-
'./data/dataset_non_fov_test_jacobs_July28_2.mat']
|
| 47 |
-
print('Real World to Real World experiments started')
|
| 48 |
-
|
| 49 |
-
#########################################Generalization across Scenarios###########################################
|
| 50 |
-
|
| 51 |
-
elif "data" in opt_exp and opt_exp.data == "rw_to_rw_env2":
|
| 52 |
-
# Training and testing data loaded for the Final results For Env-2
|
| 53 |
-
# for Generalization across scenarios (Table-1) train on 1/3/4 and test on 2
|
| 54 |
-
trainpath = ['./data/dataset_jacobs_July28.mat',
|
| 55 |
-
'./data/dataset_non_fov_train_jacobs_July28_2.mat',
|
| 56 |
-
'./data/dataset_fov_train_jacobs_July28_2.mat',
|
| 57 |
-
'./data/dataset_train_jacobs_Aug16_3.mat',
|
| 58 |
-
'./data/dataset_train_jacobs_Aug16_4_ref.mat']
|
| 59 |
-
testpath = ['./data/dataset_train_jacobs_Aug16_1.mat']
|
| 60 |
-
print('Real World to Real World experiments started')
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
elif "data" in opt_exp and opt_exp.data == "rw_to_rw_env3":
|
| 64 |
-
# Training and testing data loaded for the Final results For Env-2
|
| 65 |
-
# for Generalization across scenarios (Table-1) train on 1/2/4 and test on 3
|
| 66 |
-
trainpath = ['./data/dataset_jacobs_July28.mat',
|
| 67 |
-
'./data/dataset_non_fov_train_jacobs_July28_2.mat',
|
| 68 |
-
'./data/dataset_fov_train_jacobs_July28_2.mat',
|
| 69 |
-
'./data/dataset_train_jacobs_Aug16_1.mat',
|
| 70 |
-
'./data/dataset_train_jacobs_Aug16_4_ref.mat']
|
| 71 |
-
testpath = ['./data/dataset_train_jacobs_Aug16_3.mat']
|
| 72 |
-
print('Real World to Real World experiments started')
|
| 73 |
-
|
| 74 |
-
elif "data" in opt_exp and opt_exp.data == "rw_to_rw_env4":
|
| 75 |
-
# Training and testing data loaded for the Final results For Env-2
|
| 76 |
-
# for Generalization across scenarios (Table-1) train on 1/2/3 and test on 4
|
| 77 |
-
trainpath = ['./data/dataset_jacobs_July28.mat',
|
| 78 |
-
'./data/dataset_non_fov_train_jacobs_July28_2.mat',
|
| 79 |
-
'./data/dataset_fov_train_jacobs_July28_2.mat',
|
| 80 |
-
'./data/dataset_train_jacobs_Aug16_1.mat',
|
| 81 |
-
'./data/dataset_train_jacobs_Aug16_3.mat']
|
| 82 |
-
testpath = ['./data/dataset_train_jacobs_Aug16_4_ref.mat']
|
| 83 |
-
print('Real World to Real World experiments started')
|
| 84 |
-
|
| 85 |
-
######################################Generalization Across Bandwidth##########################################
|
| 86 |
-
|
| 87 |
-
elif "data" in opt_exp and opt_exp.data == "rw_to_rw_40":
|
| 88 |
-
# Training and testing data loaded for the Generalization results For Env-2 (The larger space) in the paper (Figure 13a) at 40MHz
|
| 89 |
-
trainpath = ['./data/dataset40_jacobs_July28.mat',
|
| 90 |
-
'./data/dataset40_non_fov_train_jacobs_July28_2.mat',
|
| 91 |
-
'./data/dataset40_fov_train_jacobs_July28_2.mat']
|
| 92 |
-
testpath = ['./data/dataset40_fov_test_jacobs_July28_2.mat',
|
| 93 |
-
'./data/dataset40_non_fov_test_jacobs_July28_2.mat']
|
| 94 |
-
print('Real World to Real World experiments started')
|
| 95 |
-
|
| 96 |
-
elif "data" in opt_exp and opt_exp.data == "rw_to_rw_20":
|
| 97 |
-
# Training and testing data loaded for the Generalization results For Env-2 (The larger space) in the paper (Figure 13a) at 20MHz
|
| 98 |
-
trainpath = ['./data/dataset20_jacobs_July28.mat',
|
| 99 |
-
'./data/dataset20_non_fov_train_jacobs_July28_2.mat',
|
| 100 |
-
'./data/dataset20_fov_train_jacobs_July28_2.mat']
|
| 101 |
-
testpath = ['./data/dataset20_fov_test_jacobs_July28_2.mat',
|
| 102 |
-
'./data/dataset20_non_fov_test_jacobs_July28_2.mat']
|
| 103 |
-
print('Real World to Real World experiments started')
|
| 104 |
-
|
| 105 |
-
######################################Generalization Across Space##########################################
|
| 106 |
-
|
| 107 |
-
elif "data" in opt_exp and opt_exp.data == "data_segment":
|
| 108 |
-
# Training and testing data loaded for the Final results For Env-2
|
| 109 |
-
# for Disjoint Training and Testing(The larger space) in the paper (Figure 13b)
|
| 110 |
-
trainpath = ['./data/dataset_train_jacobs_July28.mat',
|
| 111 |
-
'./data/dataset_train_jacobs_July28_2.mat']
|
| 112 |
-
testpath = ['./data/dataset_test_jacobs_July28.mat',
|
| 113 |
-
'./data/dataset_test_jacobs_July28_2.mat']
|
| 114 |
-
print('non-FOV to non-FOV experiments started')
|
| 115 |
-
|
| 116 |
-
######################################################################################################################
|
| 117 |
-
'''
|
| 118 |
-
Loading Training and Evaluation Data into RAM.
|
| 119 |
-
'''
|
| 120 |
-
print('Loading training files into RAM...')
|
| 121 |
-
train_wo, train_w, train_lbl = [], [], []
|
| 122 |
-
for fp in trainpath:
|
| 123 |
-
wo, w, lbl = load_data(fp)
|
| 124 |
-
train_wo.append(wo)
|
| 125 |
-
train_w.append(w)
|
| 126 |
-
train_lbl.append(lbl)
|
| 127 |
-
train_wo = torch.cat(train_wo, dim=0)
|
| 128 |
-
train_w = torch.cat(train_w, dim=0)
|
| 129 |
-
train_lbl = torch.cat(train_lbl, dim=0)
|
| 130 |
-
train_data = torch.utils.data.TensorDataset(train_wo, train_w, train_lbl)
|
| 131 |
-
train_loader = torch.utils.data.DataLoader(
|
| 132 |
-
train_data,
|
| 133 |
-
batch_size=opt_exp.batch_size,
|
| 134 |
-
shuffle=True,
|
| 135 |
-
num_workers=4,
|
| 136 |
-
pin_memory=True,
|
| 137 |
-
persistent_workers=True,
|
| 138 |
-
)
|
| 139 |
-
print(f'# training samples = {len(train_data)}')
|
| 140 |
-
print(f'# training mini batch = {len(train_loader)}')
|
| 141 |
-
|
| 142 |
-
print('Loading testing files into RAM...')
|
| 143 |
-
test_wo, test_w, test_lbl = [], [], []
|
| 144 |
-
for fp in testpath:
|
| 145 |
-
wo, w, lbl = load_data(fp)
|
| 146 |
-
test_wo.append(wo)
|
| 147 |
-
test_w.append(w)
|
| 148 |
-
test_lbl.append(lbl)
|
| 149 |
-
test_wo = torch.cat(test_wo, dim=0)
|
| 150 |
-
test_w = torch.cat(test_w, dim=0)
|
| 151 |
-
test_lbl = torch.cat(test_lbl, dim=0)
|
| 152 |
-
test_data = torch.utils.data.TensorDataset(test_wo, test_w, test_lbl)
|
| 153 |
-
test_loader = torch.utils.data.DataLoader(
|
| 154 |
-
test_data,
|
| 155 |
-
batch_size=opt_exp.batch_size,
|
| 156 |
-
shuffle=False,
|
| 157 |
-
num_workers=4,
|
| 158 |
-
pin_memory=True,
|
| 159 |
-
persistent_workers=True,
|
| 160 |
-
)
|
| 161 |
-
print(f'# testing samples = {len(test_data)}')
|
| 162 |
-
print(f'# testing mini batch = {len(test_loader)}')
|
| 163 |
-
print('Data Loaded (RAM)')
|
| 164 |
-
|
| 165 |
-
'''
|
| 166 |
-
Initiate the Network and build the graph
|
| 167 |
-
'''
|
| 168 |
-
|
| 169 |
-
model_type = getattr(opt_exp, 'model_type', 'dloc')
|
| 170 |
-
|
| 171 |
-
if model_type == 'mamba':
|
| 172 |
-
# Mamba: single end-to-end model wrapped in one ModelADT
|
| 173 |
-
from params import opt_mamba
|
| 174 |
-
mamba_model = ModelADT()
|
| 175 |
-
mamba_model.initialize(opt_mamba)
|
| 176 |
-
mamba_model.setup(opt_mamba)
|
| 177 |
-
|
| 178 |
-
print('Making the Mamba joint model')
|
| 179 |
-
joint_model = Mamba_Network()
|
| 180 |
-
joint_model.initialize(opt_exp, mamba_model, gpu_ids=opt_exp.gpu_ids)
|
| 181 |
-
|
| 182 |
-
else:
|
| 183 |
-
# Original DLoc pipeline
|
| 184 |
-
# init encoder
|
| 185 |
-
enc_model = ModelADT()
|
| 186 |
-
enc_model.initialize(opt_encoder)
|
| 187 |
-
enc_model.setup(opt_encoder)
|
| 188 |
-
|
| 189 |
-
# init decoder1
|
| 190 |
-
dec_model = ModelADT()
|
| 191 |
-
dec_model.initialize(opt_decoder)
|
| 192 |
-
dec_model.setup(opt_decoder)
|
| 193 |
-
|
| 194 |
-
if opt_exp.n_decoders == 2:
|
| 195 |
-
# init decoder2
|
| 196 |
-
offset_dec_model = ModelADT()
|
| 197 |
-
offset_dec_model.initialize(opt_offset_decoder)
|
| 198 |
-
offset_dec_model.setup(opt_offset_decoder)
|
| 199 |
-
|
| 200 |
-
# join all models
|
| 201 |
-
print('Making the joint_model')
|
| 202 |
-
joint_model = Enc_2Dec_Network()
|
| 203 |
-
joint_model.initialize(opt_exp, enc_model, dec_model, offset_dec_model, gpu_ids=opt_exp.gpu_ids)
|
| 204 |
-
|
| 205 |
-
elif opt_exp.n_decoders == 1:
|
| 206 |
-
# join all models
|
| 207 |
-
print('Making the joint_model')
|
| 208 |
-
joint_model = Enc_Dec_Network()
|
| 209 |
-
joint_model.initialize(opt_exp, enc_model, dec_model, gpu_ids=opt_exp.gpu_ids)
|
| 210 |
-
|
| 211 |
-
else:
|
| 212 |
-
print('Incorrect number of Decoders specified in the parameters')
|
| 213 |
-
sys.exit(-1)
|
| 214 |
-
|
| 215 |
-
if getattr(opt_exp, 'isFrozen', False):
|
| 216 |
-
enc_model.load_networks(opt_encoder.starting_epoch_count)
|
| 217 |
-
dec_model.load_networks(opt_decoder.starting_epoch_count)
|
| 218 |
-
if opt_exp.n_decoders == 2:
|
| 219 |
-
offset_dec_model.load_networks(opt_offset_decoder.starting_epoch_count)
|
| 220 |
-
|
| 221 |
-
# train the model
|
| 222 |
-
'''
|
| 223 |
-
Trainig the network
|
| 224 |
-
'''
|
| 225 |
-
trainer.train(joint_model, train_loader, test_loader)
|
| 226 |
-
|
| 227 |
-
'''
|
| 228 |
-
Model Evaluation at the best epoch
|
| 229 |
-
'''
|
| 230 |
-
|
| 231 |
-
epoch = "best" # int/"best"/"last"
|
| 232 |
-
eval_name = opt_exp.checkpoints_dir
|
| 233 |
-
|
| 234 |
-
if model_type == 'mamba':
|
| 235 |
-
# Mamba: reload single model
|
| 236 |
-
mamba_model.load_networks(epoch, load_dir=eval_name)
|
| 237 |
-
joint_model.initialize(opt_exp, mamba_model, gpu_ids=opt_exp.gpu_ids)
|
| 238 |
-
else:
|
| 239 |
-
# DLoc: reload encoder + decoder(s)
|
| 240 |
-
enc_model.load_networks(epoch, load_dir=eval_name)
|
| 241 |
-
dec_model.load_networks(epoch, load_dir=eval_name)
|
| 242 |
-
if opt_exp.n_decoders == 2:
|
| 243 |
-
offset_dec_model.load_networks(epoch, load_dir=eval_name)
|
| 244 |
-
joint_model.initialize(opt_exp, enc_model, dec_model, offset_dec_model, gpu_ids=opt_exp.gpu_ids)
|
| 245 |
-
elif opt_exp.n_decoders == 1:
|
| 246 |
-
joint_model.initialize(opt_exp, enc_model, dec_model, gpu_ids=opt_exp.gpu_ids)
|
| 247 |
-
|
| 248 |
-
# pass data through model
|
| 249 |
-
total_loss, median_error = trainer.test(joint_model,
|
| 250 |
-
test_loader,
|
| 251 |
-
save_output=True,
|
| 252 |
-
save_dir=eval_name,
|
| 253 |
-
save_name=f"decoder_test_result_epoch_{epoch}",
|
| 254 |
-
log=False)
|
| 255 |
-
print(f"total_loss: {total_loss}, median_error: {median_error}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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