run.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from train import Training
  2. from test import Testing
  3. from model import ModelCT
  4. import json
  5. import os
  6. if __name__ == '__main__':
  7. main_path_to_data = "/data/PSUF_naloge/5-naloga/processed" # mapa v kateri se nahajajo vsi procesirani podatki
  8. path_to_model = "trained_models/testrun123" # mapa v katero se bo shranilo: utezi naucenega modela, auc in loss med ucenjem
  9. # Nalozimo sezname za ucno, validacijsko in testno mnozico
  10. with open(os.path.join(main_path_to_data, "train_info.json")) as f:
  11. train_info = json.load(f)
  12. with open(os.path.join(main_path_to_data, "val_info.json")) as f:
  13. valid_info = json.load(f)
  14. with open(os.path.join(main_path_to_data, "test_info.json")) as f:
  15. test_info = json.load(f)
  16. # Izberemo model
  17. my_model = ModelCT()
  18. # Nastavimo hiperparametre v slovarju
  19. hyperparameters = {}
  20. hyperparameters['learning_rate'] = 0.2e-3 # learning rate
  21. hyperparameters['weight_decay'] = 0.0001 # weight decay
  22. hyperparameters['total_epoch'] = 10 # total number of epochs
  23. hyperparameters['multiplicator'] = 0.95 # each epoch learning rate is decreased on LR*multiplicator
  24. # Ustvarimo ucni in testni razred
  25. TrainClass = Training(main_path_to_data)
  26. TestClass = Testing(main_path_to_data)
  27. # Naucimo model za izbrane hiperparametre
  28. aucs, losses = TrainClass.train(train_info, valid_info, my_model, hyperparameters, path_to_model)
  29. # Utezi naucenega modela
  30. path_to_model_weights = os.path.join(path_to_model, "trained_model_weights.pth")
  31. # Testiramo nas model na testni mnozici
  32. auc, fpr, tpr, thresholds, trues, predictions = TestClass.test(test_info, my_model, path_to_model_weights)
  33. print("Test set AUC result: ", auc)