diff --git a/dataset/utils/HAR_utils.py b/dataset/utils/HAR_utils.py index d7859abc..4bf77269 100644 --- a/dataset/utils/HAR_utils.py +++ b/dataset/utils/HAR_utils.py @@ -17,7 +17,6 @@ # https://github.com/jindongwang/Deep-learning-activity-recognition/blob/master/pytorch/data_preprocess.py import numpy as np -import os import ujson import numpy as np import gc diff --git a/system/flcore/clients/clientala.py b/system/flcore/clients/clientala.py index 834cfbf8..6d059283 100644 --- a/system/flcore/clients/clientala.py +++ b/system/flcore/clients/clientala.py @@ -15,8 +15,6 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientamp.py b/system/flcore/clients/clientamp.py index f6d0941a..621e9c5d 100644 --- a/system/flcore/clients/clientamp.py +++ b/system/flcore/clients/clientamp.py @@ -16,7 +16,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import torch.nn as nn import numpy as np import time import copy diff --git a/system/flcore/clients/clientapfl.py b/system/flcore/clients/clientapfl.py index 685fee92..6cec5433 100644 --- a/system/flcore/clients/clientapfl.py +++ b/system/flcore/clients/clientapfl.py @@ -17,7 +17,6 @@ import copy import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientapple.py b/system/flcore/clients/clientapple.py index ea957d7c..464d5da8 100644 --- a/system/flcore/clients/clientapple.py +++ b/system/flcore/clients/clientapple.py @@ -18,7 +18,6 @@ import copy import math import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientavg.py b/system/flcore/clients/clientavg.py index a3890e34..2a39a3e0 100644 --- a/system/flcore/clients/clientavg.py +++ b/system/flcore/clients/clientavg.py @@ -17,7 +17,6 @@ import copy import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientbabu.py b/system/flcore/clients/clientbabu.py index 73dd1734..e7ec917f 100644 --- a/system/flcore/clients/clientbabu.py +++ b/system/flcore/clients/clientbabu.py @@ -15,9 +15,6 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import copy -import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientbase.py b/system/flcore/clients/clientbase.py index 646f1c3d..4bf5b5a1 100644 --- a/system/flcore/clients/clientbase.py +++ b/system/flcore/clients/clientbase.py @@ -20,7 +20,6 @@ import torch.nn as nn import numpy as np import os -import torch.nn.functional as F from torch.utils.data import DataLoader from sklearn.preprocessing import label_binarize from sklearn import metrics diff --git a/system/flcore/clients/clientbn.py b/system/flcore/clients/clientbn.py index d9ac75e1..093f714d 100644 --- a/system/flcore/clients/clientbn.py +++ b/system/flcore/clients/clientbn.py @@ -15,8 +15,6 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientditto.py b/system/flcore/clients/clientditto.py index ba10455c..d6099f30 100644 --- a/system/flcore/clients/clientditto.py +++ b/system/flcore/clients/clientditto.py @@ -19,7 +19,6 @@ import numpy as np import time import copy -import torch.nn as nn from flcore.optimizers.fedoptimizer import PerturbedGradientDescent from flcore.clients.clientbase import Client import torch.nn.functional as F diff --git a/system/flcore/clients/clientdyn.py b/system/flcore/clients/clientdyn.py index 00c0d476..3b5d0317 100644 --- a/system/flcore/clients/clientdyn.py +++ b/system/flcore/clients/clientdyn.py @@ -17,7 +17,6 @@ import copy import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientfomo.py b/system/flcore/clients/clientfomo.py index a31fe9ef..91d5f5ec 100644 --- a/system/flcore/clients/clientfomo.py +++ b/system/flcore/clients/clientfomo.py @@ -16,7 +16,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import torch.nn as nn import numpy as np import time import copy diff --git a/system/flcore/clients/clientgen.py b/system/flcore/clients/clientgen.py index c1ce9916..69d1fce3 100644 --- a/system/flcore/clients/clientgen.py +++ b/system/flcore/clients/clientgen.py @@ -16,7 +16,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientlg.py b/system/flcore/clients/clientlg.py index d159fecb..6951048e 100644 --- a/system/flcore/clients/clientlg.py +++ b/system/flcore/clients/clientlg.py @@ -15,8 +15,6 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientmoon.py b/system/flcore/clients/clientmoon.py index 3f78b5ff..7e9eb51e 100644 --- a/system/flcore/clients/clientmoon.py +++ b/system/flcore/clients/clientmoon.py @@ -17,7 +17,6 @@ import copy import torch -import torch.nn as nn import numpy as np import time import torch.nn.functional as F diff --git a/system/flcore/clients/clientmtl.py b/system/flcore/clients/clientmtl.py index bc0cb8be..2e458225 100644 --- a/system/flcore/clients/clientmtl.py +++ b/system/flcore/clients/clientmtl.py @@ -16,7 +16,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import torch.nn as nn import numpy as np import time import math diff --git a/system/flcore/clients/clientpFedMe.py b/system/flcore/clients/clientpFedMe.py index 33a2e5f6..48e16f99 100644 --- a/system/flcore/clients/clientpFedMe.py +++ b/system/flcore/clients/clientpFedMe.py @@ -19,7 +19,6 @@ import time import copy import torch -import torch.nn as nn from flcore.optimizers.fedoptimizer import pFedMeOptimizer from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientpcl.py b/system/flcore/clients/clientpcl.py index 4590d334..d5b48be4 100644 --- a/system/flcore/clients/clientpcl.py +++ b/system/flcore/clients/clientpcl.py @@ -15,10 +15,8 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import torch.nn.functional as F -import copy import torch -import torch.nn as nn +import torch.nn.functional as F import numpy as np import time from collections import defaultdict diff --git a/system/flcore/clients/clientper.py b/system/flcore/clients/clientper.py index bb9ac4d7..a75056c9 100644 --- a/system/flcore/clients/clientper.py +++ b/system/flcore/clients/clientper.py @@ -15,9 +15,6 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import copy -import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientperavg.py b/system/flcore/clients/clientperavg.py index eadc2dec..fb589391 100644 --- a/system/flcore/clients/clientperavg.py +++ b/system/flcore/clients/clientperavg.py @@ -19,7 +19,6 @@ import torch import time import copy -import torch.nn as nn from flcore.optimizers.fedoptimizer import PerAvgOptimizer from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientphp.py b/system/flcore/clients/clientphp.py index a9c9601a..cc0bd76c 100644 --- a/system/flcore/clients/clientphp.py +++ b/system/flcore/clients/clientphp.py @@ -17,7 +17,6 @@ import copy import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientrep.py b/system/flcore/clients/clientrep.py index dcd7b013..97b84b87 100644 --- a/system/flcore/clients/clientrep.py +++ b/system/flcore/clients/clientrep.py @@ -16,7 +16,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import torch.nn as nn import numpy as np import time from flcore.clients.clientbase import Client diff --git a/system/flcore/clients/clientrod.py b/system/flcore/clients/clientrod.py index 19ed2a48..9f33c856 100644 --- a/system/flcore/clients/clientrod.py +++ b/system/flcore/clients/clientrod.py @@ -17,7 +17,6 @@ import copy import torch -import torch.nn as nn import numpy as np import time import torch.nn.functional as F diff --git a/system/flcore/clients/clientscaffold.py b/system/flcore/clients/clientscaffold.py index b2cd595e..3edc51e7 100644 --- a/system/flcore/clients/clientscaffold.py +++ b/system/flcore/clients/clientscaffold.py @@ -16,10 +16,8 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import torch.nn as nn import numpy as np import time -import copy from flcore.clients.clientbase import Client from flcore.optimizers.fedoptimizer import SCAFFOLDOptimizer diff --git a/system/flcore/servers/serveramp.py b/system/flcore/servers/serveramp.py index 13a58da0..f8df1010 100644 --- a/system/flcore/servers/serveramp.py +++ b/system/flcore/servers/serveramp.py @@ -39,9 +39,12 @@ def __init__(self, args, times): print(f"\nJoin ratio / total clients: {self.join_ratio} / {self.num_clients}") print("Finished creating server and clients.") + self.Budget = [] + def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -66,6 +69,9 @@ def train(self): self.call_dlg(i) self.receive_models() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -73,6 +79,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverapfl.py b/system/flcore/servers/serverapfl.py index 4f95201e..da7cf64b 100644 --- a/system/flcore/servers/serverapfl.py +++ b/system/flcore/servers/serverapfl.py @@ -15,6 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientapfl import clientAPFL from flcore.servers.serverbase import Server from threading import Thread @@ -32,10 +33,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -57,6 +60,9 @@ def train(self): self.call_dlg(i) self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -64,6 +70,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverapple.py b/system/flcore/servers/serverapple.py index ab1ca194..d5757974 100644 --- a/system/flcore/servers/serverapple.py +++ b/system/flcore/servers/serverapple.py @@ -16,8 +16,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import torch -import copy -import random import time from flcore.clients.clientapple import clientAPPLE from flcore.servers.serverbase import Server diff --git a/system/flcore/servers/serverbabu.py b/system/flcore/servers/serverbabu.py index a534e23b..8488f5d9 100644 --- a/system/flcore/servers/serverbabu.py +++ b/system/flcore/servers/serverbabu.py @@ -15,6 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time import random from flcore.clients.clientbabu import clientBABU from flcore.servers.serverbase import Server @@ -33,10 +34,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -58,6 +61,9 @@ def train(self): self.call_dlg(i) self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -65,6 +71,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) for client in self.clients: client.fine_tune() diff --git a/system/flcore/servers/serverbase.py b/system/flcore/servers/serverbase.py index cec3106e..09df0974 100644 --- a/system/flcore/servers/serverbase.py +++ b/system/flcore/servers/serverbase.py @@ -22,7 +22,6 @@ import copy import time import random - from utils.data_utils import read_client_data from utils.dlg import DLG diff --git a/system/flcore/servers/serverbn.py b/system/flcore/servers/serverbn.py index a7f755ec..463cbd82 100644 --- a/system/flcore/servers/serverbn.py +++ b/system/flcore/servers/serverbn.py @@ -15,11 +15,11 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientbn import clientBN from flcore.servers.serverbase import Server from utils.data_utils import read_client_data from threading import Thread -import time class FedBN(Server): @@ -34,10 +34,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -57,6 +59,9 @@ def train(self): self.receive_models() self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -64,6 +69,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/servercp.py b/system/flcore/servers/servercp.py index cb97ce8c..d58f003a 100644 --- a/system/flcore/servers/servercp.py +++ b/system/flcore/servers/servercp.py @@ -114,6 +114,7 @@ def train(self): print("\nBest global accuracy.") print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") print(sum(self.Budget[1:])/len(self.Budget[1:])) diff --git a/system/flcore/servers/serverdistill.py b/system/flcore/servers/serverdistill.py index 540161a2..3073417f 100644 --- a/system/flcore/servers/serverdistill.py +++ b/system/flcore/servers/serverdistill.py @@ -15,11 +15,10 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientdistill import clientDistill from flcore.servers.serverbase import Server from threading import Thread -import time -import numpy as np from collections import defaultdict diff --git a/system/flcore/servers/serverditto.py b/system/flcore/servers/serverditto.py index ed505d61..00448265 100644 --- a/system/flcore/servers/serverditto.py +++ b/system/flcore/servers/serverditto.py @@ -15,7 +15,6 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import copy import numpy as np import time from flcore.clients.clientditto import clientDitto diff --git a/system/flcore/servers/serverdyn.py b/system/flcore/servers/serverdyn.py index d4c072d9..29d15397 100644 --- a/system/flcore/servers/serverdyn.py +++ b/system/flcore/servers/serverdyn.py @@ -16,11 +16,11 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import copy +import time import torch from flcore.clients.clientdyn import clientDyn from flcore.servers.serverbase import Server from threading import Thread -import time class FedDyn(Server): diff --git a/system/flcore/servers/serverfomo.py b/system/flcore/servers/serverfomo.py index 311534b6..d0aaae19 100644 --- a/system/flcore/servers/serverfomo.py +++ b/system/flcore/servers/serverfomo.py @@ -41,9 +41,12 @@ def __init__(self, args, times): print(f"\nJoin ratio / total clients: {self.join_ratio} / {self.num_clients}") print("Finished creating server and clients.") + self.Budget = [] + def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -64,6 +67,9 @@ def train(self): self.call_dlg(i) self.receive_models() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -71,6 +77,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverkd.py b/system/flcore/servers/serverkd.py index 5168c983..fa94a737 100644 --- a/system/flcore/servers/serverkd.py +++ b/system/flcore/servers/serverkd.py @@ -18,7 +18,6 @@ import copy import random import time - import numpy as np from flcore.clients.clientkd import clientKD from flcore.servers.serverbase import Server diff --git a/system/flcore/servers/serverlocal.py b/system/flcore/servers/serverlocal.py index c051f418..2e75e056 100644 --- a/system/flcore/servers/serverlocal.py +++ b/system/flcore/servers/serverlocal.py @@ -15,6 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientavg import clientAVG from flcore.servers.serverbase import Server from threading import Thread @@ -32,10 +33,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() if i%self.eval_gap == 0: @@ -52,6 +55,9 @@ def train(self): # [t.start() for t in threads] # [t.join() for t in threads] + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -60,6 +66,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() self.save_global_model() diff --git a/system/flcore/servers/servermoon.py b/system/flcore/servers/servermoon.py index 4769db6b..ea0dc20d 100644 --- a/system/flcore/servers/servermoon.py +++ b/system/flcore/servers/servermoon.py @@ -15,11 +15,11 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientmoon import clientMOON from flcore.servers.serverbase import Server from utils.data_utils import read_client_data from threading import Thread -import time class MOON(Server): diff --git a/system/flcore/servers/servermtl.py b/system/flcore/servers/servermtl.py index 887c7285..97e5e30b 100644 --- a/system/flcore/servers/servermtl.py +++ b/system/flcore/servers/servermtl.py @@ -41,10 +41,12 @@ def __init__(self, args, times): print(f"\nJoin clients / total clients: {self.num_join_clients} / {self.num_clients}") print("Finished creating server and clients.") + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.aggregate_parameters() @@ -68,6 +70,9 @@ def train(self): # [t.start() for t in threads] # [t.join() for t in threads] + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -76,6 +81,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverpFedMe.py b/system/flcore/servers/serverpFedMe.py index 940f2a66..45928de5 100644 --- a/system/flcore/servers/serverpFedMe.py +++ b/system/flcore/servers/serverpFedMe.py @@ -16,6 +16,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os +import time import copy import h5py from flcore.clients.clientpFedMe import clientpFedMe @@ -38,9 +39,11 @@ def __init__(self, args, times): print(f"\nJoin ratio / total clients: {self.join_ratio} / {self.num_clients}") print("Finished creating server and clients.") + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -69,6 +72,9 @@ def train(self): self.aggregate_parameters() self.beta_aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc_per], top_cnt=self.top_cnt): break @@ -81,6 +87,8 @@ def train(self): # self.print_(max(self.rs_test_acc_per), max( # self.rs_train_acc_per), min(self.rs_train_loss_per)) print(max(self.rs_test_acc_per)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverpac.py b/system/flcore/servers/serverpac.py index b6bfbbe7..c66e0566 100644 --- a/system/flcore/servers/serverpac.py +++ b/system/flcore/servers/serverpac.py @@ -15,17 +15,16 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from flcore.clients.clientpac import clientPAC -from flcore.servers.serverbase import Server -from utils.data_utils import read_client_data -from threading import Thread -from collections import defaultdict import time import numpy as np import random import torch import cvxpy as cvx import copy +from flcore.clients.clientpac import clientPAC +from flcore.servers.serverbase import Server +from threading import Thread +from collections import defaultdict class FedPAC(Server): diff --git a/system/flcore/servers/serverpcl.py b/system/flcore/servers/serverpcl.py index 3d6472bb..061b0ac8 100644 --- a/system/flcore/servers/serverpcl.py +++ b/system/flcore/servers/serverpcl.py @@ -15,12 +15,11 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time +import numpy as np from flcore.clients.clientpcl import clientPCL from flcore.servers.serverbase import Server -from utils.data_utils import read_client_data from threading import Thread -import time -import numpy as np from collections import defaultdict diff --git a/system/flcore/servers/serverper.py b/system/flcore/servers/serverper.py index 4adcb085..dc3ae19d 100644 --- a/system/flcore/servers/serverper.py +++ b/system/flcore/servers/serverper.py @@ -16,6 +16,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import random +import time from flcore.clients.clientper import clientPer from flcore.servers.serverbase import Server from threading import Thread @@ -33,10 +34,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -58,6 +61,9 @@ def train(self): self.call_dlg(i) self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -65,6 +71,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverperavg.py b/system/flcore/servers/serverperavg.py index ff145482..12e8d480 100644 --- a/system/flcore/servers/serverperavg.py +++ b/system/flcore/servers/serverperavg.py @@ -16,7 +16,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import copy -import torch +import time import numpy as np from flcore.clients.clientperavg import clientPerAvg from flcore.servers.serverbase import Server @@ -33,9 +33,11 @@ def __init__(self, args, times): print(f"\nJoin ratio / total clients: {self.join_ratio} / {self.num_clients}") print("Finished creating server and clients.") + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() # send all parameter for clients self.send_models() @@ -60,6 +62,9 @@ def train(self): self.call_dlg(i) self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -67,6 +72,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/servers/serverphp.py b/system/flcore/servers/serverphp.py index b930f265..1cbd7d8c 100644 --- a/system/flcore/servers/serverphp.py +++ b/system/flcore/servers/serverphp.py @@ -15,12 +15,11 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time +import torch from flcore.clients.clientphp import clientPHP from flcore.servers.serverbase import Server from threading import Thread -import time -import copy -import torch class FedPHP(Server): diff --git a/system/flcore/servers/serverproto.py b/system/flcore/servers/serverproto.py index 0a2fdd44..b26c670c 100644 --- a/system/flcore/servers/serverproto.py +++ b/system/flcore/servers/serverproto.py @@ -15,12 +15,11 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time +import numpy as np from flcore.clients.clientproto import clientProto from flcore.servers.serverbase import Server -from utils.data_utils import read_client_data from threading import Thread -import time -import numpy as np from collections import defaultdict diff --git a/system/flcore/servers/serverprox.py b/system/flcore/servers/serverprox.py index f46af265..43891933 100644 --- a/system/flcore/servers/serverprox.py +++ b/system/flcore/servers/serverprox.py @@ -15,6 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientprox import clientProx from flcore.servers.serverbase import Server from threading import Thread @@ -33,10 +34,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -58,6 +61,9 @@ def train(self): self.call_dlg(i) self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -65,6 +71,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() self.save_global_model() diff --git a/system/flcore/servers/serverrod.py b/system/flcore/servers/serverrod.py index 3a38df4f..ba6cc72e 100644 --- a/system/flcore/servers/serverrod.py +++ b/system/flcore/servers/serverrod.py @@ -15,11 +15,10 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import time from flcore.clients.clientrod import clientROD from flcore.servers.serverbase import Server -from utils.data_utils import read_client_data from threading import Thread -import time class FedROD(Server): @@ -34,10 +33,12 @@ def __init__(self, args, times): print("Finished creating server and clients.") # self.load_model() + self.Budget = [] def train(self): for i in range(self.global_rounds+1): + s_t = time.time() self.selected_clients = self.select_clients() self.send_models() @@ -59,6 +60,9 @@ def train(self): self.call_dlg(i) self.aggregate_parameters() + self.Budget.append(time.time() - s_t) + print('-'*25, 'time cost', '-'*25, self.Budget[-1]) + if self.auto_break and self.check_done(acc_lss=[self.rs_test_acc], top_cnt=self.top_cnt): break @@ -66,6 +70,8 @@ def train(self): # self.print_(max(self.rs_test_acc), max( # self.rs_train_acc), min(self.rs_train_loss)) print(max(self.rs_test_acc)) + print("\nAverage time cost per round.") + print(sum(self.Budget[1:])/len(self.Budget[1:])) self.save_results() diff --git a/system/flcore/trainmodel/models.py b/system/flcore/trainmodel/models.py index b412e3b8..3c50f626 100644 --- a/system/flcore/trainmodel/models.py +++ b/system/flcore/trainmodel/models.py @@ -15,10 +15,9 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import math import torch import torch.nn.functional as F -from torch import nn, Tensor +from torch import nn batch_size = 10 diff --git a/system/flcore/trainmodel/resnet.py b/system/flcore/trainmodel/resnet.py index 75f1bc72..35013df8 100644 --- a/system/flcore/trainmodel/resnet.py +++ b/system/flcore/trainmodel/resnet.py @@ -15,10 +15,9 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import torch -from torch import Tensor import torch.nn as nn -from typing import Type, Any, Callable, Union, List, Optional +from torch import Tensor +from typing import Any, Callable, List, Optional def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d: diff --git a/system/flcore/trainmodel/transformer.py b/system/flcore/trainmodel/transformer.py index 993656f6..497b63f5 100644 --- a/system/flcore/trainmodel/transformer.py +++ b/system/flcore/trainmodel/transformer.py @@ -17,7 +17,6 @@ import math import torch -import torch.nn.functional as F from torch.nn import TransformerEncoder, TransformerEncoderLayer from torch import nn, Tensor diff --git a/system/main.py b/system/main.py index 9a349ad1..a4d65fad 100644 --- a/system/main.py +++ b/system/main.py @@ -396,7 +396,7 @@ def run(args): help="Ratio of clients per round") parser.add_argument('-rjr', "--random_join_ratio", type=bool, default=False, help="Random ratio of clients per round") - parser.add_argument('-nc', "--num_clients", type=int, default=2, + parser.add_argument('-nc', "--num_clients", type=int, default=20, help="Total number of clients") parser.add_argument('-pv', "--prev", type=int, default=0, help="Previous Running times")