Fix best-checkpoint saving bug + evaluation fallback + cleanup
Browse files- train_and_test.py +10 -0
- trainer.py +9 -9
train_and_test.py
CHANGED
|
@@ -6,6 +6,7 @@ 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():
|
|
@@ -235,6 +236,15 @@ Model Evaluation at the best epoch
|
|
| 235 |
epoch = "best" # int/"best"/"last"
|
| 236 |
eval_name = opt_exp.checkpoints_dir
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
if model_type == 'mamba':
|
| 239 |
# Mamba: reload single model
|
| 240 |
mamba_model.load_networks(epoch, load_dir=eval_name)
|
|
|
|
| 6 |
read the README in `params_storage` folder.
|
| 7 |
'''
|
| 8 |
import sys
|
| 9 |
+
import os
|
| 10 |
import torch
|
| 11 |
import warnings
|
| 12 |
with warnings.catch_warnings():
|
|
|
|
| 236 |
epoch = "best" # int/"best"/"last"
|
| 237 |
eval_name = opt_exp.checkpoints_dir
|
| 238 |
|
| 239 |
+
# Check if "best" checkpoint exists; fall back to "latest" if not
|
| 240 |
+
if model_type == 'mamba':
|
| 241 |
+
best_path = os.path.join(eval_name, 'mamba_dloc', f'best_net_mamba_dloc.pth')
|
| 242 |
+
else:
|
| 243 |
+
best_path = os.path.join(eval_name, 'encoder', f'best_net_encoder.pth')
|
| 244 |
+
if not os.path.exists(best_path):
|
| 245 |
+
print(f'WARNING: "best" checkpoint not found at {best_path}, falling back to "latest"')
|
| 246 |
+
epoch = "latest"
|
| 247 |
+
|
| 248 |
if model_type == 'mamba':
|
| 249 |
# Mamba: reload single model
|
| 250 |
mamba_model.load_networks(epoch, load_dir=eval_name)
|
trainer.py
CHANGED
|
@@ -32,7 +32,7 @@ def train(model, train_loader, test_loader):
|
|
| 32 |
# initialization
|
| 33 |
total_steps = 0
|
| 34 |
print('Training called')
|
| 35 |
-
|
| 36 |
|
| 37 |
for epoch in range(model.opt.starting_epoch_count+1, model.opt.n_epochs+1): # opt.niter + opt.niter_decay + 1):
|
| 38 |
epoch_start_time = time.time()
|
|
@@ -79,21 +79,21 @@ def train(model, train_loader, test_loader):
|
|
| 79 |
write_log([str(nighty_percentile_error_tr)], model.decoder.model_name, log_dir=model.decoder.opt.log_dir, log_type='train_90_error')
|
| 80 |
if (epoch == model.opt.starting_epoch_count + 1):
|
| 81 |
min_eval_loss, median_error = test(model, test_loader, save_output=False)
|
|
|
|
|
|
|
|
|
|
| 82 |
else:
|
| 83 |
new_eval_loss, new_med_error = test(model, test_loader, save_output=False)
|
| 84 |
-
if
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
# generated_outputs = temp_generator_outputs
|
| 89 |
if epoch % model.encoder.opt.save_epoch_freq == 0:
|
| 90 |
print('saving the model at the end of epoch %d, iters %d' %(epoch, total_steps))
|
| 91 |
model.save_networks('latest')
|
| 92 |
model.save_networks(epoch)
|
| 93 |
-
if (stopping_count==2):
|
| 94 |
-
print('Saving best model at %d epoch' %(epoch))
|
| 95 |
-
model.save_networks('best')
|
| 96 |
-
stopping_count=0
|
| 97 |
|
| 98 |
print('End of epoch %d / %d \t Time Taken: %d sec' %
|
| 99 |
(epoch, model.decoder.opt.niter + model.decoder.opt.niter_decay, time.time() - epoch_start_time))
|
|
|
|
| 32 |
# initialization
|
| 33 |
total_steps = 0
|
| 34 |
print('Training called')
|
| 35 |
+
best_median = float('inf')
|
| 36 |
|
| 37 |
for epoch in range(model.opt.starting_epoch_count+1, model.opt.n_epochs+1): # opt.niter + opt.niter_decay + 1):
|
| 38 |
epoch_start_time = time.time()
|
|
|
|
| 79 |
write_log([str(nighty_percentile_error_tr)], model.decoder.model_name, log_dir=model.decoder.opt.log_dir, log_type='train_90_error')
|
| 80 |
if (epoch == model.opt.starting_epoch_count + 1):
|
| 81 |
min_eval_loss, median_error = test(model, test_loader, save_output=False)
|
| 82 |
+
best_median = median_error
|
| 83 |
+
print('Saving initial best model at epoch %d (median=%.4f)' % (epoch, best_median))
|
| 84 |
+
model.save_networks('best')
|
| 85 |
else:
|
| 86 |
new_eval_loss, new_med_error = test(model, test_loader, save_output=False)
|
| 87 |
+
if new_med_error < best_median:
|
| 88 |
+
best_median = new_med_error
|
| 89 |
+
print('Saving new best model at epoch %d (median=%.4f)' % (epoch, best_median))
|
| 90 |
+
model.save_networks('best')
|
| 91 |
+
median_error = new_med_error
|
| 92 |
|
|
|
|
| 93 |
if epoch % model.encoder.opt.save_epoch_freq == 0:
|
| 94 |
print('saving the model at the end of epoch %d, iters %d' %(epoch, total_steps))
|
| 95 |
model.save_networks('latest')
|
| 96 |
model.save_networks(epoch)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
print('End of epoch %d / %d \t Time Taken: %d sec' %
|
| 99 |
(epoch, model.decoder.opt.niter + model.decoder.opt.niter_decay, time.time() - epoch_start_time))
|