From 622bc19d811709ff0715581abc292e8dc36527ad Mon Sep 17 00:00:00 2001 From: Francesco Torta <62566275+Fra98@users.noreply.github.com> Date: Sun, 13 Feb 2022 22:41:27 +0100 Subject: [PATCH] Cleanup --- main_centralized.py | 2 +- main_colab_fedAVG.py | 67 --------------------------------------- main_colab_fedOPT.py | 75 -------------------------------------------- 3 files changed, 1 insertion(+), 143 deletions(-) delete mode 100644 main_colab_fedAVG.py delete mode 100644 main_colab_fedOPT.py diff --git a/main_centralized.py b/main_centralized.py index a323c3b..641fc6f 100644 --- a/main_centralized.py +++ b/main_centralized.py @@ -19,7 +19,7 @@ STEP_SIZE = [5, 10, 15] # How many epochs before decreasing learning rate (if using a step-down policy) GAMMA = 0.1 # Multiplicative factor for learning rate step-down -NET = LeNet5_MOD_LIGHT_BN +NET = LeNet5_MOD_BN def run_training(net, criterion, optimizer, scheduler=None): diff --git a/main_colab_fedAVG.py b/main_colab_fedAVG.py deleted file mode 100644 index 640e6f2..0000000 --- a/main_colab_fedAVG.py +++ /dev/null @@ -1,67 +0,0 @@ -import torch -import yaml -import os - -from src.fedAVG.server import Server -from src.logger_manager import LoggerHandler - -DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") - -CONFIG = 'config_light' - -BASE_PATH = f'.log/run/{CONFIG}/' -# BASE_PATH = f'/content/gdrive/MyDrive/run/{CONFIG}/' -NET_PATH = BASE_PATH + 'server.pth' -COUNTER_PATH = BASE_PATH + 'counter.txt' -LOG_NAME = f'{CONFIG}.log' -TRAIN_ACC_LOG = 'train_acc.txt' -TEST_ACC_LOG = 'test_acc.txt' - -if __name__ == "__main__": - print(DEVICE) - - loggerManager = LoggerHandler(BASE_PATH) - loggerManager.create_logger(LOG_NAME, LOG_NAME) - loggerManager.set_default_logger(LOG_NAME) - loggerManager.create_logger(TRAIN_ACC_LOG, TRAIN_ACC_LOG) - loggerManager.create_logger(TEST_ACC_LOG, TEST_ACC_LOG) - - with open(f'./configs/fedAVG/{CONFIG}.yaml') as f: - configs = list(yaml.load_all(f, Loader=yaml.FullLoader)) - - data_config = configs[0]["data_config"] - model_config = configs[1]["model_config"] - optim_config = configs[2]["optim_config"] - fed_config = configs[3]["fed_config"] - - server = Server(DEVICE, data_config=data_config, model_config=model_config, optim_config=optim_config, - fed_config=fed_config, logger=loggerManager) - - server.init_clients() - - # Loading current model - state_dict = None - round_num = 0 - if os.path.isfile(NET_PATH): - state_dict = torch.load(NET_PATH) - with open(COUNTER_PATH, 'r') as f: - round_num = int(f.readline()) - - # TRAINING - server.run_training(print_acc=False, state_dict=state_dict, round_num=round_num) - - round_num += server.num_rounds - - # EVALUATE TRAINSET - accuracy, loss = server.run_testing(train=True) - loggerManager.get_logger(TRAIN_ACC_LOG).info(f'{round_num:03} {accuracy:.3f} {loss:.3f}') - - # EVALUATE TESTSET - accuracy, loss = server.run_testing(train=False) - loggerManager.get_logger(TEST_ACC_LOG).info(f'{round_num:03} {accuracy:.3f} {loss:.3f}') - - # Saving current model - torch.save(server.global_net.state_dict(), NET_PATH) - torch.save(server.global_net.state_dict(), f'{BASE_PATH}/server_{round_num:03}.pth') - with open(COUNTER_PATH, 'w') as f: - f.write(str(round_num)) diff --git a/main_colab_fedOPT.py b/main_colab_fedOPT.py deleted file mode 100644 index 28475ef..0000000 --- a/main_colab_fedOPT.py +++ /dev/null @@ -1,75 +0,0 @@ -import torch -import yaml -import os - -from src.fedOPT.server import Server -from src.logger_manager import LoggerHandler - -DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") - -CONFIG = 'config_Adam' - -BASE_PATH = f'.log/run/{CONFIG}/' -# BASE_PATH = f'/content/gdrive/MyDrive/run/{CONFIG}/' -NET_PATH = BASE_PATH + 'server.pth' -OPT_PATH = BASE_PATH + 'optimizer.pth' -COUNTER_PATH = BASE_PATH + 'counter.txt' -LOG_NAME = f'{CONFIG}.log' -TRAIN_ACC_LOG = 'train_acc.txt' -TEST_ACC_LOG = 'test_acc.txt' - -if __name__ == "__main__": - print(DEVICE) - - loggerManager = LoggerHandler(BASE_PATH) - loggerManager.create_logger(LOG_NAME, LOG_NAME) - loggerManager.set_default_logger(LOG_NAME) - loggerManager.create_logger(TRAIN_ACC_LOG, TRAIN_ACC_LOG) - loggerManager.create_logger(TEST_ACC_LOG, TEST_ACC_LOG) - - with open(f'./configs/fedOPT/{CONFIG}.yaml') as f: - configs = list(yaml.load_all(f, Loader=yaml.FullLoader)) - - data_config = configs[0]["data_config"] - model_config = configs[1]["model_config"] - client_optimizer = configs[2]["client_optimizer"] - server_optimizer = configs[3]["server_optimizer"] - fed_config = configs[4]["fed_config"] - - server = Server(DEVICE, data_config=data_config, model_config=model_config, - client_optimizer=client_optimizer, server_optimizer=server_optimizer, - fed_config=fed_config, logger=loggerManager) - - server.init_clients() - - # Loading current model - state_dict = None - round_num = 0 - if os.path.isfile(NET_PATH): - state_dict = torch.load(NET_PATH) - with open(COUNTER_PATH, 'r') as f: - round_num = int(f.readline()) - - state_dict_opt = None - if os.path.isfile(OPT_PATH): - state_dict_opt = torch.load(OPT_PATH) - - - # TRAINING - server.run_training(print_acc=False, state_dict=state_dict, state_dict_opt=state_dict_opt, round_num=round_num) - - round_num += server.num_rounds - - # EVALUATE TRAINSET - accuracy, loss = server.run_testing(train=True) - loggerManager.get_logger(TRAIN_ACC_LOG).info(f'{round_num:03} {accuracy:.3f} {loss:.3f}') - - # EVALUATE TESTSET - accuracy, loss = server.run_testing(train=False) - loggerManager.get_logger(TEST_ACC_LOG).info(f'{round_num:03} {accuracy:.3f} {loss:.3f}') - - # Saving current model - torch.save(server.global_net.state_dict(), NET_PATH) - torch.save(server.global_net.state_dict(), f'{BASE_PATH}/server_{round_num:03}.pth') - with open(COUNTER_PATH, 'w') as f: - f.write(str(round_num))